diff --git a/flumi/Scripts/B9/HTMLParser.gd b/flumi/Scripts/B9/HTMLParser.gd index b239d71..0144b66 100644 --- a/flumi/Scripts/B9/HTMLParser.gd +++ b/flumi/Scripts/B9/HTMLParser.gd @@ -451,7 +451,7 @@ func process_scripts(lua_api: LuaAPI, _lua_vm) -> void: parse_result.external_scripts = [] parse_result.external_scripts.append(src) elif not inline_code.is_empty(): - lua_api.execute_lua_script(inline_code) + lua_api.execute_lua_script(inline_code, "") func process_external_scripts(lua_api: LuaAPI, _lua_vm, base_url: String = "") -> void: if not lua_api or not parse_result.external_scripts or parse_result.external_scripts.is_empty(): @@ -462,7 +462,7 @@ func process_external_scripts(lua_api: LuaAPI, _lua_vm, base_url: String = "") - for script_url in parse_result.external_scripts: var script_content = await Network.fetch_external_resource(script_url, base_url) if not script_content.is_empty(): - lua_api.execute_lua_script(script_content) + lua_api.execute_lua_script(script_content, script_url) func process_postprocess() -> HTMLParser.HTMLElement: var postprocess_elements = find_all("postprocess") diff --git a/flumi/Scripts/B9/Lua.gd b/flumi/Scripts/B9/Lua.gd index ac55130..4153075 100644 --- a/flumi/Scripts/B9/Lua.gd +++ b/flumi/Scripts/B9/Lua.gd @@ -660,13 +660,13 @@ func get_dom_node(node: Node, purpose: String = "general") -> Node: return node # Main execution function -func execute_lua_script(code: String): +func execute_lua_script(code: String, chunk_name: String = "dostring"): if not threaded_vm.lua_thread or not threaded_vm.lua_thread.is_alive(): # Start the thread if it's not running threaded_vm.start_lua_thread(dom_parser, self) script_start_time = Time.get_ticks_msec() / 1000.0 - threaded_vm.execute_script_async(code) + threaded_vm.execute_script_async(code, chunk_name) func _on_threaded_script_completed(_result: Dictionary): pass diff --git a/flumi/Scripts/Browser/DevToolsConsole.gd b/flumi/Scripts/Browser/DevToolsConsole.gd index e6a3658..c81fe36 100644 --- a/flumi/Scripts/Browser/DevToolsConsole.gd +++ b/flumi/Scripts/Browser/DevToolsConsole.gd @@ -245,9 +245,9 @@ func execute_lua_command(code: String) -> void: var is_expression = is_likely_expression(code) if is_expression: var wrapped_code = "print(" + code + ")" - lua_api.execute_lua_script(wrapped_code) + lua_api.execute_lua_script(wrapped_code, "") else: - lua_api.execute_lua_script(code) + lua_api.execute_lua_script(code, "") return add_log_entry("No Lua context available", "error", Time.get_ticks_msec() / 1000.0) diff --git a/flumi/Scripts/Utils/Lua/ThreadedVM.gd b/flumi/Scripts/Utils/Lua/ThreadedVM.gd index 1ef170d..5ac9646 100644 --- a/flumi/Scripts/Utils/Lua/ThreadedVM.gd +++ b/flumi/Scripts/Utils/Lua/ThreadedVM.gd @@ -56,11 +56,12 @@ func stop_lua_thread(): lua_thread.wait_to_finish() lua_thread = null -func execute_script_async(script_code: String): +func execute_script_async(script_code: String, chunk_name: String = "dostring"): queue_mutex.lock() command_queue.append({ "type": "execute_script", - "code": script_code + "code": script_code, + "chunk_name": chunk_name }) queue_mutex.unlock() thread_semaphore.post() @@ -143,21 +144,28 @@ func _process_command_queue(): for command in commands_to_process: match command.type: "execute_script": - _execute_script_in_thread(command.code) + _execute_script_in_thread(command.code, command.get("chunk_name", "dostring")) "execute_callback": _execute_callback_in_thread(command.callback_ref, command.args) "execute_timeout": _execute_timeout_in_thread(command.timeout_id) -func _execute_script_in_thread(script_code: String): +func _execute_script_in_thread(script_code: String, chunk_name: String = "dostring"): if not lua_vm: call_deferred("_emit_script_error", "Lua VM not initialized") return - var result = lua_vm.lua_dostring(script_code) + # Use load_string with custom chunk name, then lua_pcall + var load_result = lua_vm.load_string(script_code, chunk_name) - if result == lua_vm.LUA_OK: - call_deferred("_emit_script_completed", {"success": true}) + if load_result == lua_vm.LUA_OK: + var call_result = lua_vm.lua_pcall(0, 0, 0) + if call_result == lua_vm.LUA_OK: + call_deferred("_emit_script_completed", {"success": true}) + else: + var error_msg = lua_vm.lua_tostring(-1) + lua_vm.lua_pop(1) + call_deferred("_emit_script_error", error_msg) else: var error_msg = lua_vm.lua_tostring(-1) lua_vm.lua_pop(1)