camelCase and direct access

This commit is contained in:
Face
2025-08-04 17:04:35 +03:00
parent 6c0a08d501
commit 7f90dfb716
5 changed files with 136 additions and 98 deletions

View File

@@ -35,17 +35,17 @@ 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_select_all_handler, "gurt.selectAll")
vm.lua_setfield(-2, "selectAll")
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_set_timeout_handler, "gurt.setTimeout")
vm.lua_setfield(-2, "setTimeout")
vm.lua_pushcallable(lua_api._gurt_clear_timeout_handler, "gurt.clear_timeout")
vm.lua_setfield(-2, "clear_timeout")
vm.lua_pushcallable(lua_api._gurt_clear_timeout_handler, "gurt.clearTimeout")
vm.lua_setfield(-2, "clearTimeout")
# Add body element access
var body_element = dom_parser.find_first("body")
@@ -54,19 +54,11 @@ static func setup_gurt_api(vm: LuauVM, lua_api, dom_parser: HTMLParser) -> void:
vm.lua_pushstring("body")
vm.lua_setfield(-2, "_element_id")
# NOTE: same code as add_element_methods, but lazy to handle body.xxxx prop
vm.lua_pushcallable(lua_api._element_set_text_handler, "body.set_text")
vm.lua_setfield(-2, "set_text")
vm.lua_pushcallable(lua_api._element_get_text_handler, "body.get_text")
vm.lua_setfield(-2, "get_text")
lua_api.add_element_methods(vm)
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

@@ -3,7 +3,6 @@ extends RefCounted
var active_timeouts: Dictionary = {}
var next_timeout_id: int = 1
var next_callback_ref: int = 1
class TimeoutInfo:
var id: int
@@ -25,12 +24,23 @@ func set_timeout_handler(vm: LuauVM, parent_node: Node) -> int:
var timeout_id = next_timeout_id
next_timeout_id += 1
# Store the callback function in the registry
# Store callback in isolated registry table
vm.lua_pushstring("GURT_TIMEOUTS")
vm.lua_rawget(vm.LUA_REGISTRYINDEX)
if vm.lua_isnil(-1):
vm.lua_pop(1)
vm.lua_newtable()
vm.lua_pushstring("GURT_TIMEOUTS")
vm.lua_pushvalue(-2)
vm.lua_rawset(vm.LUA_REGISTRYINDEX)
vm.lua_pushinteger(timeout_id)
vm.lua_pushvalue(1)
var callback_ref = vm.luaL_ref(vm.LUA_REGISTRYINDEX)
vm.lua_rawset(-3)
vm.lua_pop(1)
# Create timeout info
var timeout_info = TimeoutInfo.new(timeout_id, callback_ref, vm, self)
var timeout_info = TimeoutInfo.new(timeout_id, timeout_id, vm, self)
# Create and configure timer
var timer = Timer.new()
@@ -45,7 +55,6 @@ func set_timeout_handler(vm: LuauVM, parent_node: Node) -> int:
parent_node.add_child(timer)
timer.start()
# Return timeout ID
vm.lua_pushinteger(timeout_id)
return 1
@@ -60,8 +69,13 @@ func clear_timeout_handler(vm: LuauVM) -> int:
timeout_info.timer.queue_free()
# Clean up callback reference
vm.lua_pushnil()
vm.lua_rawseti(vm.LUA_REGISTRYINDEX, timeout_info.callback_ref)
vm.lua_pushstring("GURT_TIMEOUTS")
vm.lua_rawget(vm.LUA_REGISTRYINDEX)
if not vm.lua_isnil(-1):
vm.lua_pushinteger(timeout_info.callback_ref)
vm.lua_pushnil()
vm.lua_rawset(-3)
vm.lua_pop(1)
# Remove from active timeouts
active_timeouts.erase(timeout_id)
@@ -73,7 +87,12 @@ func _on_timeout_triggered(timeout_info: TimeoutInfo) -> void:
return
# Execute the callback
timeout_info.vm.lua_rawgeti(timeout_info.vm.LUA_REGISTRYINDEX, timeout_info.callback_ref)
timeout_info.vm.lua_pushstring("GURT_TIMEOUTS")
timeout_info.vm.lua_rawget(timeout_info.vm.LUA_REGISTRYINDEX)
timeout_info.vm.lua_pushinteger(timeout_info.callback_ref)
timeout_info.vm.lua_rawget(-2)
timeout_info.vm.lua_remove(-2)
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))
@@ -83,8 +102,13 @@ func _on_timeout_triggered(timeout_info: TimeoutInfo) -> void:
# 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)
timeout_info.vm.lua_pushstring("GURT_TIMEOUTS")
timeout_info.vm.lua_rawget(timeout_info.vm.LUA_REGISTRYINDEX)
if not timeout_info.vm.lua_isnil(-1):
timeout_info.vm.lua_pushinteger(timeout_info.callback_ref)
timeout_info.vm.lua_pushnil()
timeout_info.vm.lua_rawset(-3)
timeout_info.vm.lua_pop(1)
active_timeouts.erase(timeout_info.id)
func cleanup_all_timeouts():
@@ -97,6 +121,11 @@ func cleanup_all_timeouts():
# 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()
timeout_info.vm.lua_pushstring("GURT_TIMEOUTS")
timeout_info.vm.lua_rawget(timeout_info.vm.LUA_REGISTRYINDEX)
if not timeout_info.vm.lua_isnil(-1):
timeout_info.vm.lua_pushinteger(timeout_info.callback_ref)
timeout_info.vm.lua_pushnil()
timeout_info.vm.lua_rawset(-3)
timeout_info.vm.lua_pop(1)
active_timeouts.clear()