summaryrefslogtreecommitdiff
path: root/tests/test-suite/events.js
blob: 49576efe3f3565fda05a5147462ce90834d58939 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"use strict";

const test     = require('tape');

global.WEB = false;

const lauxlib  = require("../../src/lauxlib.js");
const lua      = require('../../src/lua.js');


test("[test-suite] events: testing metatable", function (t) {
    let luaCode = `
        X = 20; B = 30

        _ENV = setmetatable({}, {__index=_G})

        collectgarbage()

        X = X+10
        assert(X == 30 and _G.X == 20)
        B = false
        assert(B == false)
        B = nil
        assert(B == 30)

        assert(getmetatable{} == nil)
        assert(getmetatable(4) == nil)
        assert(getmetatable(nil) == nil)
        a={name = "NAME"}; setmetatable(a, {__metatable = "xuxu",
                            __tostring=function(x) return x.name end})
        assert(getmetatable(a) == "xuxu")
        assert(tostring(a) == "NAME")
        -- cannot change a protected metatable
        assert(pcall(setmetatable, a, {}) == false)
        a.name = "gororoba"
        assert(tostring(a) == "gororoba")

        local a, t = {10,20,30; x="10", y="20"}, {}
        assert(setmetatable(a,t) == a)
        assert(getmetatable(a) == t)
        assert(setmetatable(a,nil) == a)
        assert(getmetatable(a) == nil)
        assert(setmetatable(a,t) == a)


        function f (t, i, e)
          assert(not e)
          local p = rawget(t, "parent")
          return (p and p[i]+3), "dummy return"
        end

        t.__index = f

        a.parent = {z=25, x=12, [4] = 24}
        assert(a[1] == 10 and a.z == 28 and a[4] == 27 and a.x == "10")

        collectgarbage()

        a = setmetatable({}, t)
        function f(t, i, v) rawset(t, i, v-3) end
        setmetatable(t, t)   -- causes a bug in 5.1 !
        t.__newindex = f
        a[1] = 30; a.x = "101"; a[5] = 200
        assert(a[1] == 27 and a.x == 98 and a[5] == 197)

        do    -- bug in Lua 5.3.2
          local mt = {}
          mt.__newindex = mt
          local t = setmetatable({}, mt)
          t[1] = 10     -- will segfault on some machines
          assert(mt[1] == 10)
        end

        local c = {}
        a = setmetatable({}, t)
        t.__newindex = c
        a[1] = 10; a[2] = 20; a[3] = 90
        assert(c[1] == 10 and c[2] == 20 and c[3] == 90)

        do
          local a;
          a = setmetatable({}, {__index = setmetatable({},
                             {__index = setmetatable({},
                             {__index = function (_,n) return a[n-3]+4, "lixo" end})})})
          a[0] = 20
          for i=0,10 do
            assert(a[i*3] == 20 + i*4)
          end
        end

        do  -- newindex
          local foi
          local a = {}
          for i=1,10 do a[i] = 0; a['a'..i] = 0; end
          setmetatable(a, {__newindex = function (t,k,v) foi=true; rawset(t,k,v) end})
          foi = false; a[1]=0; assert(not foi)
          foi = false; a['a1']=0; assert(not foi)
          foi = false; a['a11']=0; assert(foi)
          foi = false; a[11]=0; assert(foi)
          foi = false; a[1]=nil; assert(not foi)
          foi = false; a[1]=nil; assert(foi)
        end

        setmetatable(t, nil)
        function f (t, ...) return t, {...} end
        t.__call = f

        do
          local x,y = a(table.unpack{'a', 1})
          assert(x==a and y[1]=='a' and y[2]==1 and y[3]==nil)
          x,y = a()
          assert(x==a and y[1]==nil)
        end


        local b = setmetatable({}, t)
        setmetatable(b,t)

        function f(op)
          return function (...) cap = {[0] = op, ...} ; return (...) end
        end
        t.__add = f("add")
        t.__sub = f("sub")
        t.__mul = f("mul")
        t.__div = f("div")
        t.__idiv = f("idiv")
        t.__mod = f("mod")
        t.__unm = f("unm")
        t.__pow = f("pow")
        t.__len = f("len")
        t.__band = f("band")
        t.__bor = f("bor")
        t.__bxor = f("bxor")
        t.__shl = f("shl")
        t.__shr = f("shr")
        t.__bnot = f("bnot")

        assert(b+5 == b)
        assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==nil)
        assert(b+'5' == b)
        assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==nil)
        assert(5+b == 5)
        assert(cap[0] == "add" and cap[1] == 5 and cap[2] == b and cap[3]==nil)
        assert('5'+b == '5')
        assert(cap[0] == "add" and cap[1] == '5' and cap[2] == b and cap[3]==nil)
        b=b-3; assert(getmetatable(b) == t)
        assert(5-a == 5)
        assert(cap[0] == "sub" and cap[1] == 5 and cap[2] == a and cap[3]==nil)
        assert('5'-a == '5')
        assert(cap[0] == "sub" and cap[1] == '5' and cap[2] == a and cap[3]==nil)
        assert(a*a == a)
        assert(cap[0] == "mul" and cap[1] == a and cap[2] == a and cap[3]==nil)
        assert(a/0 == a)
        assert(cap[0] == "div" and cap[1] == a and cap[2] == 0 and cap[3]==nil)
        assert(a%2 == a)
        assert(cap[0] == "mod" and cap[1] == a and cap[2] == 2 and cap[3]==nil)
        assert(a // (1/0) == a)
        assert(cap[0] == "idiv" and cap[1] == a and cap[2] == 1/0 and cap[3]==nil)
        assert(a & "hi" == a)
        assert(cap[0] == "band" and cap[1] == a and cap[2] == "hi" and cap[3]==nil)
        assert(a | "hi" == a)
        assert(cap[0] == "bor" and cap[1] == a and cap[2] == "hi" and cap[3]==nil)
        assert("hi" ~ a == "hi")
        assert(cap[0] == "bxor" and cap[1] == "hi" and cap[2] == a and cap[3]==nil)
        assert(-a == a)
        assert(cap[0] == "unm" and cap[1] == a)
        assert(a^4 == a)
        assert(cap[0] == "pow" and cap[1] == a and cap[2] == 4 and cap[3]==nil)
        assert(a^'4' == a)
        assert(cap[0] == "pow" and cap[1] == a and cap[2] == '4' and cap[3]==nil)
        assert(4^a == 4)
        assert(cap[0] == "pow" and cap[1] == 4 and cap[2] == a and cap[3]==nil)
        assert('4'^a == '4')
        assert(cap[0] == "pow" and cap[1] == '4' and cap[2] == a and cap[3]==nil)
        assert(#a == a)
        assert(cap[0] == "len" and cap[1] == a)
        assert(~a == a)
        assert(cap[0] == "bnot" and cap[1] == a)
        assert(a << 3 == a)
        assert(cap[0] == "shl" and cap[1] == a and cap[2] == 3)
        assert(1.5 >> a == 1.5)
        assert(cap[0] == "shr" and cap[1] == 1.5 and cap[2] == a)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});