Esempio n. 1
0
    def test_load_script(self):
        state = _luabject.new()
        self.load_script_to_completion(state, "")

        # Can load multiple scripts in one state.
        self.load_script_to_completion(state, "")

        # Can load a syntactically correct script.
        state = _luabject.new()
        self.load_script_to_completion(state, "function foo() prant() end")

        # Can load multiple syntactically correct scripts in one state.
        thread = _luabject.new_thread(state)
        self.load_script_to_completion(state, "function bar() prant() end")

        # Loading a syntactically incorrect script raises an exception.
        state = _luabject.new()
        thread = _luabject.new_thread(state)
        with self.assertRaises(_luabject.LuaSyntaxError):
            _luabject.load_script(thread, "1+1")

        # Can load a syntactically correct script even after a load_script() exception.
        self.load_script_to_completion(state, "function foo() prant() end")

        # Loading a syntactically correct script that causes an error works until we evaluate it.
        state = _luabject.new()
        thread = _luabject.new_thread(state)
        _luabject.load_script(thread, "hi()")
        with self.assertRaises(_luabject.LuaRuntimeError):
            _luabject.pump_thread(thread)
            while _luabject.LUA_YIELD == _luabject.thread_status(thread):
                _luabject.pump_thread(thread)

        # Can load a syntactically correct script even after a load_script() exception.
        self.load_script_to_completion(state, "function foo() prant() end")
Esempio n. 2
0
 def load_script(self, script):
     thread = _luabject.new_thread(self._state)
     _luabject.load_script(thread, script)
     _luabject.pump_thread(thread)
     while _luabject.thread_status(thread) == 1:
         eventlet.sleep()
         _luabject.pump_thread(thread)
Esempio n. 3
0
 def run(self, funcname, args=None, kwargs=None):
     thread = _luabject.new_thread(self._state)
     _luabject.load_function(thread, funcname)
     # TODO: put args on the stack after the function
     # TODO: do the first pump with the number of args too
     _luabject.pump_thread(thread)
     while _luabject.thread_status(thread) == 1:
         eventlet.sleep()
         _luabject.pump_thread(thread)
Esempio n. 4
0
    def test_register_global(self):
        state = _luabject.new()

        class Test(object):
            def __init__(self):
                self.ran = False
            def __call__(self):
                self.ran = True

        tester = Test()
        _luabject.register_global(state, "hi", tester)

        self.load_script_to_completion(state, "function foo() hi() end")
        thread = _luabject.new_thread(state)
        _luabject.load_function(thread, "foo")
        _luabject.pump_thread(thread)
        while _luabject.LUA_YIELD == _luabject.thread_status(thread):
            _luabject.pump_thread(thread)

        self.assertTrue(tester.ran)
Esempio n. 5
0
    def test_pump_thread(self):
        # Errors in the script are raised as exceptions when run.
        state = _luabject.new()
        self.load_script_to_completion(state, "function foo() prant() end")
        thread = _luabject.new_thread(state)
        _luabject.load_function(thread, "foo")
        with self.assertRaises(_luabject.LuaRuntimeError):
            _luabject.pump_thread(thread)

        # A short error-free script runs to completion.
        state = _luabject.new()
        self.load_script_to_completion(state, "function foo() bar = 1 end")
        thread = _luabject.new_thread(state)
        _luabject.load_function(thread, "foo")
        self.assertEqual(0, _luabject.thread_status(thread))
        _luabject.pump_thread(thread)
        self.assertEqual(0, _luabject.thread_status(thread))

        # A long error-free script will take more than one pump to run.
        state = _luabject.new()
        self.load_script_to_completion(state, "function foo() for v = 1, 10000 do bar = 1 end end")
        thread = _luabject.new_thread(state)
        _luabject.load_function(thread, "foo")
        self.assertEqual(0, _luabject.thread_status(thread))
        _luabject.pump_thread(thread)
        self.assertEqual(_luabject.LUA_YIELD, _luabject.thread_status(thread))
        _luabject.pump_thread(thread)
        self.assertEqual(_luabject.LUA_YIELD, _luabject.thread_status(thread))

        # An infinite loop can be pumped without raising an exception.
        state = _luabject.new()
        self.load_script_to_completion(state, "function foo() while true do bar = 1 end end")
        thread = _luabject.new_thread(state)
        _luabject.load_function(thread, "foo")
        self.assertEqual(0, _luabject.thread_status(thread))
        _luabject.pump_thread(thread)
        self.assertEqual(_luabject.LUA_YIELD, _luabject.thread_status(thread))
        _luabject.pump_thread(thread)
        self.assertEqual(_luabject.LUA_YIELD, _luabject.thread_status(thread))
Esempio n. 6
0
 def load_script_to_completion(self, state, script):
     thread = _luabject.new_thread(state)
     _luabject.load_script(thread, script)
     _luabject.pump_thread(thread)
     while _luabject.LUA_YIELD == _luabject.thread_status(thread):
         _luabject.pump_thread(thread)