luapy (12) lua exception

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guzhou_diaoke/article/details/85345883
  1. error, pcall
    def error(self):
        err = self.stack.pop()
        raise Exception(err)

    def pcall(self, nargs, nresults, msgh):
        caller = self.stack
        try:
            self.call(nargs, nresults)
            return ThreadStatus.OK
        except BaseException as e:
            if msgh != 0:
                raise Exception(str(e))

            while self.stack != caller:
                self.pop_lua_stack()
            self.stack.push(str(e))
            return ThreadStatus.ERRRUN

  1. test
function div0(a, b)
    if b == 0 then
        error("DIV BY 0!")
    else
        return a/b
    end
end

function div1(a, b) return div0(a, b) end
function div2(a, b) return div1(a, b) end

ok, result = pcall(div2, 4, 2); print(ok, result)
ok, err = pcall(div2, 5, 0); print(ok, err)
ok, err = pcall(div2, {}, {}); print(ok, err)

from lua_state import LuaState
from thread_state import ThreadStatus


def py_print(ls):
    nargs = ls.get_top()
    for i in range(1, nargs+1):
        if ls.is_boolean(i):
            print('true' if ls.to_boolean(i) else 'false', end='')
        elif ls.is_string(i):
            print(ls.to_string(i), end='')
        else:
            print(ls.type_name(ls.type(i)), end='')

        if i < nargs:
            print('\t', end='')

    print()
    return 0


def error(ls):
    return ls.error()


def pcall(ls):
    nargs = ls.get_top() - 1
    status = ls.pcall(nargs, -1, 0)
    ls.push_boolean(status == ThreadStatus.OK)
    ls.insert(1)
    return ls.get_top()


def main():
    with open('./test/exception.luac', 'rb') as f:
        data = f.read()
        ls = LuaState()
        ls.register('print', py_print)
        ls.register('error', error)
        ls.register('pcall', pcall)
        ls.load(data)
        ls.call(0, 0)


if __name__ == '__main__':
    main()

  1. result
env: {'print': <closure.Closure object at 0x7fb03e930320>, 'error': <closure.Closure object at 0x7fb03e930390>, 'pcall': <closure.Closure object at 0x7fb03e930588>}
(  0) [01] CLOSURE      [function][nil][nil][nil]
(  1) [02] SETTABUP     [function][nil][nil][nil]
(  2) [03] CLOSURE      [function][nil][nil][nil]
(  3) [04] SETTABUP     [function][nil][nil][nil]
(  4) [05] CLOSURE      [function][nil][nil][nil]
(  5) [06] SETTABUP     [function][nil][nil][nil]
(  6) [07] GETTABUP     [function][nil][nil][nil]
(  7) [08] GETTABUP     [function][function][nil][nil]
(  8) [09] LOADK        [function][function][4][nil]
(  9) [10] LOADK        [function][function][4][2]
( 10) [01] GETTABUP     [4][2][function][nil][nil]
( 11) [02] MOVE         [4][2][function][4][nil]
( 12) [03] MOVE         [4][2][function][4][2]
( 13) [01] GETTABUP     [4][2][function][nil][nil]
( 14) [02] MOVE         [4][2][function][4][nil]
( 15) [03] MOVE         [4][2][function][4][2]
( 16) [01] EQ           [4][2][nil][nil]
( 17) [02] JMP          [4][2][nil][nil]
( 18) [07] DIV          [4][2][2][nil]
( 19) [08] RETURN       [4][2][2][nil][2]
( 20) [04] TAILCALL     [4][2][function][4][2][2][3]
( 21) [05] RETURN       [4][2][function][4][2][2]
( 22) [04] TAILCALL     [4][2][function][4][2][2][3]
( 23) [05] RETURN       [4][2][function][4][2][2]
( 24) [11] CALL         [true][2][4][2]
( 25) [12] SETTABUP     [true][2][4][2]
( 26) [13] SETTABUP     [true][2][4][2]
( 27) [14] GETTABUP     [function][2][4][2]
( 28) [15] GETTABUP     [function][true][4][2]
( 29) [16] GETTABUP     [function][true][2][2]
true	2.0
( 30) [17] CALL         [function][true][2][2]
( 31) [18] GETTABUP     [function][true][2][2]
( 32) [19] GETTABUP     [function][function][2][2]
( 33) [20] LOADK        [function][function][5][2]
( 34) [21] LOADK        [function][function][5][0]
( 35) [01] GETTABUP     [5][0][function][nil][nil]
( 36) [02] MOVE         [5][0][function][5][nil]
( 37) [03] MOVE         [5][0][function][5][0]
( 38) [01] GETTABUP     [5][0][function][nil][nil]
( 39) [02] MOVE         [5][0][function][5][nil]
( 40) [03] MOVE         [5][0][function][5][0]
( 41) [01] EQ           [5][0][nil][nil]
( 42) [03] GETTABUP     [5][0][function][nil]
( 43) [04] LOADK        [5][0][function]["DIV BY 0!"]
( 44) [22] CALL         [false]["DIV BY 0!"][5][0]
( 45) [23] SETTABUP     [false]["DIV BY 0!"][5][0]
( 46) [24] SETTABUP     [false]["DIV BY 0!"][5][0]
( 47) [25] GETTABUP     [function]["DIV BY 0!"][5][0]
( 48) [26] GETTABUP     [function][false][5][0]
( 49) [27] GETTABUP     [function][false]["DIV BY 0!"][0]
false	DIV BY 0!
( 50) [28] CALL         [function][false]["DIV BY 0!"][0]
( 51) [29] GETTABUP     [function][false]["DIV BY 0!"][0]
( 52) [30] GETTABUP     [function][function]["DIV BY 0!"][0]
( 53) [31] NEWTABLE     [function][function][table][0]
( 54) [32] NEWTABLE     [function][function][table][table]
( 55) [01] GETTABUP     [table][table][function][nil][nil]
( 56) [02] MOVE         [table][table][function][table][nil]
( 57) [03] MOVE         [table][table][function][table][table]
( 58) [01] GETTABUP     [table][table][function][nil][nil]
( 59) [02] MOVE         [table][table][function][table][nil]
( 60) [03] MOVE         [table][table][function][table][table]
( 61) [01] EQ           [table][table][nil][nil]
( 62) [02] JMP          [table][table][nil][nil]
( 63) [33] CALL         [false]["arith error"][table][table]
( 64) [34] SETTABUP     [false]["arith error"][table][table]
( 65) [35] SETTABUP     [false]["arith error"][table][table]
( 66) [36] GETTABUP     [function]["arith error"][table][table]
( 67) [37] GETTABUP     [function][false][table][table]
( 68) [38] GETTABUP     [function][false]["arith error"][table]
false	arith error
( 69) [39] CALL         [function][false]["arith error"][table]
( 70) [40] RETURN       [function][false]["arith error"][table]

猜你喜欢

转载自blog.csdn.net/guzhou_diaoke/article/details/85345883
LUA