Example #1
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)
Example #2
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)
Example #3
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))
Example #4
0
    def test_load_function(self):
        # Trying to load an unknown function raises an exception.
        state = _luabject.new()
        self.load_script_to_completion(state, "bar = 1")

        thread = _luabject.new_thread(state)
        with self.assertRaises(ValueError):
            _luabject.load_function(thread, "foo")  # nil
        with self.assertRaises(ValueError):
            _luabject.load_function(thread, "bar")  # not a function

        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))

        # Functions that throw runtime errors can still be loaded without error.
        state = _luabject.new()
        self.load_script_to_completion(state, "function foo() prant() end")
        _luabject.load_function(thread, "foo")
        self.assertEqual(0, _luabject.thread_status(thread))