provide script name in errors

This commit is contained in:
Face
2025-09-10 17:19:03 +03:00
parent 6cf5d2f19c
commit e49f9887f0
4 changed files with 21 additions and 13 deletions

View File

@@ -451,7 +451,7 @@ func process_scripts(lua_api: LuaAPI, _lua_vm) -> void:
parse_result.external_scripts = [] parse_result.external_scripts = []
parse_result.external_scripts.append(src) parse_result.external_scripts.append(src)
elif not inline_code.is_empty(): elif not inline_code.is_empty():
lua_api.execute_lua_script(inline_code) lua_api.execute_lua_script(inline_code, "<inline script>")
func process_external_scripts(lua_api: LuaAPI, _lua_vm, base_url: String = "") -> void: 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(): 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: for script_url in parse_result.external_scripts:
var script_content = await Network.fetch_external_resource(script_url, base_url) var script_content = await Network.fetch_external_resource(script_url, base_url)
if not script_content.is_empty(): 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: func process_postprocess() -> HTMLParser.HTMLElement:
var postprocess_elements = find_all("postprocess") var postprocess_elements = find_all("postprocess")

View File

@@ -660,13 +660,13 @@ func get_dom_node(node: Node, purpose: String = "general") -> Node:
return node return node
# Main execution function # 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(): if not threaded_vm.lua_thread or not threaded_vm.lua_thread.is_alive():
# Start the thread if it's not running # Start the thread if it's not running
threaded_vm.start_lua_thread(dom_parser, self) threaded_vm.start_lua_thread(dom_parser, self)
script_start_time = Time.get_ticks_msec() / 1000.0 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): func _on_threaded_script_completed(_result: Dictionary):
pass pass

View File

@@ -245,9 +245,9 @@ func execute_lua_command(code: String) -> void:
var is_expression = is_likely_expression(code) var is_expression = is_likely_expression(code)
if is_expression: if is_expression:
var wrapped_code = "print(" + code + ")" var wrapped_code = "print(" + code + ")"
lua_api.execute_lua_script(wrapped_code) lua_api.execute_lua_script(wrapped_code, "<console>")
else: else:
lua_api.execute_lua_script(code) lua_api.execute_lua_script(code, "<console>")
return return
add_log_entry("No Lua context available", "error", Time.get_ticks_msec() / 1000.0) add_log_entry("No Lua context available", "error", Time.get_ticks_msec() / 1000.0)

View File

@@ -56,11 +56,12 @@ func stop_lua_thread():
lua_thread.wait_to_finish() lua_thread.wait_to_finish()
lua_thread = null lua_thread = null
func execute_script_async(script_code: String): func execute_script_async(script_code: String, chunk_name: String = "dostring"):
queue_mutex.lock() queue_mutex.lock()
command_queue.append({ command_queue.append({
"type": "execute_script", "type": "execute_script",
"code": script_code "code": script_code,
"chunk_name": chunk_name
}) })
queue_mutex.unlock() queue_mutex.unlock()
thread_semaphore.post() thread_semaphore.post()
@@ -143,25 +144,32 @@ func _process_command_queue():
for command in commands_to_process: for command in commands_to_process:
match command.type: match command.type:
"execute_script": "execute_script":
_execute_script_in_thread(command.code) _execute_script_in_thread(command.code, command.get("chunk_name", "dostring"))
"execute_callback": "execute_callback":
_execute_callback_in_thread(command.callback_ref, command.args) _execute_callback_in_thread(command.callback_ref, command.args)
"execute_timeout": "execute_timeout":
_execute_timeout_in_thread(command.timeout_id) _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: if not lua_vm:
call_deferred("_emit_script_error", "Lua VM not initialized") call_deferred("_emit_script_error", "Lua VM not initialized")
return 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: 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}) call_deferred("_emit_script_completed", {"success": true})
else: else:
var error_msg = lua_vm.lua_tostring(-1) var error_msg = lua_vm.lua_tostring(-1)
lua_vm.lua_pop(1) lua_vm.lua_pop(1)
call_deferred("_emit_script_error", error_msg) call_deferred("_emit_script_error", error_msg)
else:
var error_msg = lua_vm.lua_tostring(-1)
lua_vm.lua_pop(1)
call_deferred("_emit_script_error", error_msg)
func _call_lua_function_with_args(args: Array) -> bool: func _call_lua_function_with_args(args: Array) -> bool:
# Push arguments # Push arguments