add create, append, remove, set_timeout, clear_timeout, get_children, refactor ul/ol and dynamic recounting

This commit is contained in:
Face
2025-08-04 16:31:16 +03:00
parent e22ad21fd0
commit 6c0a08d501
17 changed files with 786 additions and 242 deletions

View File

@@ -35,6 +35,18 @@ static func setup_gurt_api(vm: LuauVM, lua_api, dom_parser: HTMLParser) -> void:
vm.lua_pushcallable(lua_api._gurt_select_handler, "gurt.select")
vm.lua_setfield(-2, "select")
vm.lua_pushcallable(lua_api._gurt_select_all_handler, "gurt.select_all")
vm.lua_setfield(-2, "select_all")
vm.lua_pushcallable(lua_api._gurt_create_handler, "gurt.create")
vm.lua_setfield(-2, "create")
vm.lua_pushcallable(lua_api._gurt_set_timeout_handler, "gurt.set_timeout")
vm.lua_setfield(-2, "set_timeout")
vm.lua_pushcallable(lua_api._gurt_clear_timeout_handler, "gurt.clear_timeout")
vm.lua_setfield(-2, "clear_timeout")
# Add body element access
var body_element = dom_parser.find_first("body")
if body_element:
@@ -52,6 +64,9 @@ static func setup_gurt_api(vm: LuauVM, lua_api, dom_parser: HTMLParser) -> void:
vm.lua_pushcallable(lua_api._body_on_event_handler, "body.on")
vm.lua_setfield(-2, "on")
vm.lua_pushcallable(lua_api._element_append_handler, "body.append")
vm.lua_setfield(-2, "append")
vm.lua_setfield(-2, "body")
vm.lua_setglobal("gurt")

View File

@@ -10,9 +10,12 @@ static func lua_print(vm: LuauVM) -> int:
message_parts.append(value_str)
var final_message = "\t".join(message_parts)
print("GURT LOG: ", final_message)
lua_print_direct(final_message)
return 0
static func lua_print_direct(msg) -> void:
print("GURT LOG: ", msg)
static func lua_value_to_string(vm: LuauVM, index: int) -> String:
var lua_type = vm.lua_type(index)
@@ -84,4 +87,4 @@ static func table_to_string(vm: LuauVM, index: int, max_depth: int = 3, current_
result += ", ..."
result += "}"
return result
return result

View File

@@ -0,0 +1,102 @@
class_name LuaTimeoutManager
extends RefCounted
var active_timeouts: Dictionary = {}
var next_timeout_id: int = 1
var next_callback_ref: int = 1
class TimeoutInfo:
var id: int
var callback_ref: int
var vm: LuauVM
var timer: Timer
var timeout_manager: LuaTimeoutManager
func _init(timeout_id: int, cb_ref: int, lua_vm: LuauVM, manager: LuaTimeoutManager):
id = timeout_id
callback_ref = cb_ref
vm = lua_vm
timeout_manager = manager
func set_timeout_handler(vm: LuauVM, parent_node: Node) -> int:
vm.luaL_checktype(1, vm.LUA_TFUNCTION)
var delay_ms: int = vm.luaL_checkint(2)
var timeout_id = next_timeout_id
next_timeout_id += 1
# Store the callback function in the registry
vm.lua_pushvalue(1)
var callback_ref = vm.luaL_ref(vm.LUA_REGISTRYINDEX)
# Create timeout info
var timeout_info = TimeoutInfo.new(timeout_id, callback_ref, vm, self)
# Create and configure timer
var timer = Timer.new()
timer.wait_time = delay_ms / 1000.0
timer.one_shot = true
timer.timeout.connect(_on_timeout_triggered.bind(timeout_info))
timeout_info.timer = timer
active_timeouts[timeout_id] = timeout_info
# Add timer to scene tree
parent_node.add_child(timer)
timer.start()
# Return timeout ID
vm.lua_pushinteger(timeout_id)
return 1
func clear_timeout_handler(vm: LuauVM) -> int:
var timeout_id: int = vm.luaL_checkint(1)
var timeout_info = active_timeouts.get(timeout_id, null)
if timeout_info:
# Stop and remove timer
if timeout_info.timer:
timeout_info.timer.stop()
timeout_info.timer.queue_free()
# Clean up callback reference
vm.lua_pushnil()
vm.lua_rawseti(vm.LUA_REGISTRYINDEX, timeout_info.callback_ref)
# Remove from active timeouts
active_timeouts.erase(timeout_id)
return 0
func _on_timeout_triggered(timeout_info: TimeoutInfo) -> void:
if not active_timeouts.has(timeout_info.id):
return
# Execute the callback
timeout_info.vm.lua_rawgeti(timeout_info.vm.LUA_REGISTRYINDEX, timeout_info.callback_ref)
if timeout_info.vm.lua_isfunction(-1):
if timeout_info.vm.lua_pcall(0, 0, 0) != timeout_info.vm.LUA_OK:
print("GURT ERROR in timeout callback: ", timeout_info.vm.lua_tostring(-1))
timeout_info.vm.lua_pop(1)
else:
timeout_info.vm.lua_pop(1)
# Clean up timeout
timeout_info.timer.queue_free()
timeout_info.vm.lua_pushnil()
timeout_info.vm.lua_rawseti(timeout_info.vm.LUA_REGISTRYINDEX, timeout_info.callback_ref)
active_timeouts.erase(timeout_info.id)
func cleanup_all_timeouts():
# Clean up all active timeouts
for timeout_id in active_timeouts:
var timeout_info = active_timeouts[timeout_id]
if timeout_info.timer:
timeout_info.timer.stop()
timeout_info.timer.queue_free()
# Release Lua callback reference
if timeout_info.vm and timeout_info.callback_ref:
timeout_info.vm.lua_pushnil()
timeout_info.vm.lua_rawseti(timeout_info.vm.LUA_REGISTRYINDEX, timeout_info.callback_ref)
active_timeouts.clear()

View File

@@ -0,0 +1 @@
uid://dlhj6h7qyo5hp