WeaselGames / godot_luaAPI

Godot LuaAPI
https://luaapi.weaselgames.info
Other
371 stars 28 forks source link

Bug Report: Setting a property in a custom meta table while having object_metatable.permissive set to false freezes the lua code #218

Open leandrodreamer opened 4 weeks ago

leandrodreamer commented 4 weeks ago

Describe the bug Hi!, when setting a custom meta table's property with the default object_metatable.permissive set to false the __newindex function completes in the godot side but the lua script freezes on that line

To Reproduce

extends Node

var lua: LuaAPI = LuaAPI.new()
var lua_script: String = """
print( "name_property: " .. mytable.name_property )
mytable.name_property = "asdasd"
print( "name_property: " .. mytable.name_property )
mytable.name_property = "123123"
print( "name_property: " .. mytable.name_property )
"""

class TestMetaTable extends LuaObjectMetatable:
    var name_property := "default"

    func __index(ref: LuaAPI, index) -> Variant:
        printt("GETTING", index)
        if index == "name_property":
            return name_property
        else:
            return LuaError.new_error("invalid property")

    func __newindex(ref: LuaAPI, index, value):
        printt("SETTING", index, value)
        if index == "name_property":
            name_property = str(value)
            printt("SETTED", index, value)
        else:
            return LuaError.new_error("invalid property")

func _ready():
    lua.object_metatable.permissive = false
    lua.bind_libraries(["base"])
    lua.push_variant("mytable", TestMetaTable.new())
    lua.do_string(lua_script)

This example prints:

GETTING name_property
name_property: default
SETTING name_property   asdasd
SETTED  name_property   asdasd

and nothing else, i get no errors and i can still call other functions inside lua, they will run until another property is set the property gets set properly and even if i remove every functionality of TestMetaTable so index returns 0 and newindex just returns null it still gets stuck in the ' self.name_property = "asdasd" ' line.

Expected behavior I expect my custom meta table to take over the default one (on the 'mytable' table in this example) making object_metatable.permissive irrelevant. Not sure if there is any way to workaround this other than setting permissive to true (which is not the best option for safesty)

Enviromint (please complete the following information):

edit: noticed the latest version is meant for 4.2.2 while i was using 4.3, but tested it on that version too and can confirm the problem persist

leandrodreamer commented 4 weeks ago

Update: turns out lua_fields is still required inside metatables, adding it to the custom table with the required properties on it fixes it.

tho it feels unintended since u can still get any value and even run methods, only __newindex fails (or just blocks the execution), also in the docs there is no mention of the lua_fields method in the LuaObjectMetatable table.

my expectation would be that when creating a new LuaObjectMetatable you fully handle the access using index and newindex, and that lua_fields is just for the default metatable.