summaryrefslogtreecommitdiff
path: root/script/src/vm/ipairs.lua
blob: cb8356dad2de06211f083436f4776f84480847c7 (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
local mt = require 'vm.manager'
local library = require 'vm.library'

---@param func emmyFunction
function mt:callIpairs(func, values, source)
    local tbl = values[1]
    func:setReturn(1, library.special['@ipairs'])
    func:setReturn(2, tbl)
end

---@param func emmyFunction
function mt:callAtIpairs(func, values, source)
    local tbl = values[1]
    if tbl then
        local emmy = tbl:getEmmy()
        if emmy then
            if emmy.type == 'emmy.arrayType' then
                local value = self:createValue(emmy:getName(), source)
                func:setReturn(2, value)
            end
        end
    end
end

---@param func emmyFunction
function mt:callPairs(func, values, source)
    local tbl = values[1]
    func:setReturn(1, library.special['next'])
    func:setReturn(2, tbl)
end

---@param func emmyFunction
function mt:callNext(func, values, source)
    local tbl = values[1]
    if tbl then
        local emmy = tbl:getEmmy()
        if emmy then
            if emmy.type == 'emmy.arrayType' then
                local key = self:createValue('integer', source)
                local value = self:createValue(emmy:getName(), source)
                func:setReturn(1, key)
                func:setReturn(2, value)
            elseif emmy.type == 'emmy.tableType' then
                local key = self:createValue(emmy:getKeyType():getType(), source)
                local value = self:createValue(emmy:getValueType():getType(), source)
                func:setReturn(1, key)
                func:setReturn(2, value)
            end
        end
    end
end