summary refs log tree commit diff stats
path: root/tests/objects/tobjcov.nim
blob: a12f747025879276a27350227f47e53303ef0771 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
discard """
action: compile
targets: "c"
"""

# Covariance is not type safe:
# Note: `nim cpp` makes it a compile error (after codegen), even with:
# `var f = cast[proc (x: var TA) {.nimcall.}](cast[pointer](bp))`, which
# currently removes all the `cast` in cgen'd code, hence the compile error.

type
  TA = object of RootObj
    a: int
  TB = object of TA
    b: array[0..5000_000, int]

proc ap(x: var TA) = x.a = -1
proc bp(x: var TB) = x.b[high(x.b)] = -1

# in Nim proc (x: TB) is compatible to proc (x: TA),
# but this is not type safe:
var f = cast[proc (x: var TA) {.nimcall.}](bp)
var a: TA
f(a) # bp expects a TB, but gets a TA
class="nb">NULL || size == 0) return EOZ; z->n = size - 1; z->p = buff; return char2int(*(z->p++)); } int luaZ_lookahead (ZIO *z) { if (z->n == 0) { if (luaZ_fill(z) == EOZ) return EOZ; else { z->n++; /* luaZ_fill removed first byte; put back it */ z->p--; } } return char2int(*z->p); } void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { z->L = L; z->reader = reader; z->data = data; z->n = 0; z->p = NULL; } /* --------------------------------------------------------------- read --- */ size_t luaZ_read (ZIO *z, void *b, size_t n) { while (n) { size_t m; if (luaZ_lookahead(z) == EOZ) return n; /* return number of missing bytes */ m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ memcpy(b, z->p, m); z->n -= m; z->p += m; b = (char *)b + m; n -= m; } return 0; } /* ------------------------------------------------------------------------ */ char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { if (n > buff->buffsize) { if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; luaZ_resizebuffer(L, buff, n); } return buff->buffer; } e>
8dcf085 ^
18f06a5 ^
36ef0c2 ^
18f06a5 ^
36ef0c2 ^
36ef0c2 ^



8dcf085 ^





36ef0c2 ^
8dcf085 ^



36ef0c2 ^
8dcf085 ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

                         












                                   
                         
 
                                 
 
                   






                          
                   


                            
                      



                                       
                     



                                       
 
                                             

                  
                                          
                                    
                                           



               
                                                            


                                                          
                                     
                                                  
                                      
                              
                                     

                   
                           
                                       
                                    
                                        

                   







                                     

   
                       

                                       


                                  
                                           
     
                  


   
                            



                               
                       
                                                                        
                                               
                                                               
                                             



                     





                             
 



                  
 


                 
curses = require "curses"

count = {}
function foo(event, line)
  local s = debug.getinfo(2)
--?   print(s.name)
  if s.name ~= nil then
    if count[s.name] == nil then
      count[s.name] = 0
    end
    count[s.name] = count[s.name]+1
  end
end
debug.sethook(foo, "c")

window = curses.initscr()

tower = {{6, 5, 4, 3, 2}, {}, {}}

function len(array)
  local result = 0
  for k in pairs(array) do
    result = result+1
  end
  return result
end

function pop(array)
  return table.remove(array)
end

function lines(window)
  local lines, cols = window:getmaxyx()
  return lines
end

function cols(window)
  local lines, cols = window:getmaxyx()
  return cols
end


function render_disk(window, line, col, size)
  col = col-size+1
  for i=1,size do
    window:attron(curses.color_pair(size))
    window:mvaddstr(line, col, "  ")
    window:attroff(curses.color_pair(size))
    col = col+2
  end
end

function render_tower(window, line, col, tower_index, tower)
  window:attron(curses.A_BOLD)
  window:mvaddch(line+2, col, string.char(96+tower_index))
  window:attroff(curses.A_BOLD)
  window:attron(curses.color_pair(7))
  window:mvaddstr(line+1, col-6, "              ")
  window:attroff(curses.color_pair(7))
  for i, n in ipairs(tower) do
    render_disk(window, line, col, n)
    line = line - 1
  end
  for i=1,5-len(tower)+1 do
    window:attron(curses.color_pair(7))
    window:mvaddstr(line, col, "  ")
    window:attroff(curses.color_pair(7))
    line = line - 1
  end

  window:mvaddstr(30, 0, "profile: ")
  for k,v in pairs(count) do
    window:addstr(k)
    window:addstr(": ")
    window:addstr(v)
    window:addstr("; ")
  end
end

function render(window)
  window:clear()
  local lines, cols = window:getmaxyx()
  local line = math.floor(lines/2)
  local col = math.floor(cols/4)
  for i,t in ipairs(tower) do
    render_tower(window, line, i*col, i, t)
  end
  curses.refresh()
end


function make_move(from, to)
  local disk = pop(tower[from])
  table.insert(tower[to], disk)
end

function update(window)
  window:mvaddstr(lines(window)-2, 5, "tower to remove top disk from? ")
  local from = string.byte(curses.getch()) - 96
  window:mvaddstr(lines(window)-1, 5, "tower to stack it on? ")
  local to = string.byte(curses.getch()) - 96
  make_move(from, to)
end


function main()
  curses.start_color()
  curses.use_default_colors()
  for i=1,7 do
    curses.init_pair(i, 0, i)
  end

  while true do
    render(window)
    update(window)
  end

  curses.endwin()
end
main()