summaryrefslogtreecommitdiff
path: root/tests/ltm.js
blob: 5644ae8cac5ed7fb1602444b264c3c56d5b1fe23 (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
/*jshint esversion: 6 */
"use strict";

const test     = require('tape');
const beautify = require('js-beautify').js_beautify;

const VM       = require("../src/lvm.js");
const OC       = require('../src/lopcodes.js');

const getState = require("./tests.js").getState;


test('__index, __newindex: with actual table', function (t) {
    let luaCode = `
        local t = {yo=1}
        return t.yo, t.lo
    `, L;
    
    t.plan(3);

    t.comment("Running following code: \n" + luaCode);

    t.doesNotThrow(function () {
        L = getState(luaCode);
        VM.luaV_execute(L);
    }, "Program executed without errors");

    t.strictEqual(
        L.stack[L.top - 1].value,
        null,
        "Program output is correct"
    );

    t.strictEqual(
        L.stack[L.top - 2].value,
        1,
        "Program output is correct"
    );
});


test('__index: with non table', function (t) {
    let luaCode = `
        local t = "a string"
        return t.yo
    `, L;
    
    t.plan(2);

    t.comment("Running following code: \n" + luaCode);

    t.doesNotThrow(function () {
        L = getState(luaCode);
    }, "Bytecode parsed without errors");

    t.throws(function () {
        VM.luaV_execute(L);
    }, "Program executed with expected error");
});


test('__newindex: with non table', function (t) {
    let luaCode = `
        local t = "a string"
        t.yo = "hello"
    `, L;
    
    t.plan(2);

    t.comment("Running following code: \n" + luaCode);

    t.doesNotThrow(function () {
        L = getState(luaCode);
    }, "Bytecode parsed without errors");

    t.throws(function () {
        VM.luaV_execute(L);
    }, "Program executed with expected error");
});


test('__index function in metatable', function (t) {
    let luaCode = `
        local mt = {
            __index = function (table, key)
                return "__index"
            end
        }

        local t = {}

        -- setmetatable(t, mt)

        return t.yo
    `, L;
    
    t.plan(8);

    t.comment("Running following code: \n" + luaCode);

    t.doesNotThrow(function () {
        L = getState(luaCode);
    }, "Bytecode parsed without errors");


    // main <hello.lua:0,0> (7 instructions at 0x7fe6b4403050)
    // 0+ params, 3 slots, 1 upvalue, 2 locals, 2 constants, 1 function
    //         1       [1]     NEWTABLE        0 0 1
    //         2       [4]     CLOSURE         1 0     ; 0x7fe6b4403290
    //         3       [4]     SETTABLE        0 -1 1  ; "__index" -
    //         4       [7]     NEWTABLE        1 0 0
    //         5       [9]     GETTABLE        2 1 -2  ; "yo"            <=== We stop here
    //         6       [9]     RETURN          2 2
    //         7       [9]     RETURN          0 1
    //
    // function <hello.lua:2,4> (3 instructions at 0x7fe6b4403290)
    // 2 params, 3 slots, 0 upvalues, 2 locals, 1 constant, 0 functions
    //         1       [3]     LOADK           2 -1    ; "__index"
    //         2       [3]     RETURN          2 2
    //         3       [4]     RETURN          0 1

    t.strictEqual(
        OC.OpCodes[L.stack[0].p.code[4].opcode],
        "OP_GETTABLE",
        "Correct opcode marked as breakpoint"
    );

    t.comment("We set a breakpoint just before 'return t.yo'")
    L.stack[0].p.code[4].breakpoint = true; // Stop just before 'return t.yo'

    t.doesNotThrow(function () {
        VM.luaV_execute(L);
    }, "First part of the program executed without errors");

    t.strictEqual(
        OC.OpCodes[L.ci.u.l.savedpc[L.ci.pcOff - 1].opcode],
        "OP_GETTABLE",
        "Stopped at correct opcode"
    );

    t.comment("We unset the breakpoint and correct pcOff");
    L.ci.pcOff--;
    L.stack[0].p.code[4].breakpoint = false;

    t.ok(
        L.stack[2].ttistable() && !L.stack[2].value.hash.get("__index"),
        "t is on stack at 2"
    );

    t.ok(
        L.stack[1].ttistable() && L.stack[1].value.hash.get("__index"),
        "mt is on stack at 1"
    );

    t.comment("We manually set t's metatable to mt");
    L.stack[2].metatable = L.stack[1];

    t.doesNotThrow(function () {
        VM.luaV_execute(L);
    }, "Second part of the program executed without errors");

    t.strictEqual(
        L.stack[L.top - 1].value,
        "__index",
        "Program output is correct"
    );
});