Skip to content

Commit

Permalink
Add LuaObjectMetatable type (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey2k committed Jul 13, 2023
1 parent 6d6248a commit 2c1b23d
Show file tree
Hide file tree
Showing 26 changed files with 1,211 additions and 213 deletions.
2 changes: 2 additions & 0 deletions config.py
Expand Up @@ -26,6 +26,8 @@ def get_doc_classes():
"LuaError",
"LuaTuple",
"LuaCallableExtra",
"LuaObjectMetatable",
"LuaDefaultObjectMetatable",
]

def get_doc_path():
Expand Down
6 changes: 3 additions & 3 deletions doc_classes/LuaAPI.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="LuaAPI" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<class name="LuaAPI" inherits="RefCounted" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Execute Lua code at runtime and make your own API.
</brief_description>
Expand Down Expand Up @@ -105,8 +105,8 @@
</method>
</methods>
<members>
<member name="permissive" type="bool" setter="set_permissive" getter="get_permissive" default="true">
When set to true all methods will be allowed on Objects be default and lua_fields is treated as a blacklist. When set to false, lua_fields is treated as a whitelist.
<member name="object_metatable" type="LuaObjectMetatable" setter="set_object_metatable" getter="get_object_metatable" default="LuaDefaultObjectMetatable">
This is the default LuaMetatable to use for object which do not define a lua_metatable field. By default it is a LuaDefaultObjectMetatable. You can change this to a custom metatable to change the behavior of all objects.
</member>
<member name="memory_limit" type="int" setter="set_memory_limit" getter="get_memory_limit" default="0">
Sets the memory limit for the state in bytes. If the limit is 0, there is no limit.
Expand Down
2 changes: 1 addition & 1 deletion doc_classes/LuaCallableExtra.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="LuaCallableExtra" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<class name="LuaCallableExtra" inherits="RefCounted" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
A tuple.
</brief_description>
Expand Down
2 changes: 1 addition & 1 deletion doc_classes/LuaCoroutine.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="LuaCoroutine" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<class name="LuaCoroutine" inherits="RefCounted" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
A coroutine.
</brief_description>
Expand Down
17 changes: 17 additions & 0 deletions doc_classes/LuaDefaultObjectMetatable.xml
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="LuaDefaultObjectMetatable" inherits="LuaObjectMetatable" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
The default LuaObjectMetatable used by the LuaAPI class as the object_metatable value.
</brief_description>
<description>
This metatable by default checks if the object has a lua_fields method. If it does depending on how permissice is set. The listed fields will be allowed or disallowed.
This metatable also checks if the object overrides any of the metamethods, if it does it will call the overridden method.
</description>
<tutorials>
</tutorials>
<members>
<member name="permissive" type="bool" setter="set_permissive" getter="get_permissive" default="true">
Sets weather the Objects lua_fields method is treated as a whitelist or a blacklist. If true, the fields method is treated as a blacklist and returned fields will be disallowed. If false, the fields method is treated as a whitelist and returned fields will be allowed.
</member>
</members>
</class>
2 changes: 1 addition & 1 deletion doc_classes/LuaError.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="LuaError" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<class name="LuaError" inherits="RefCounted" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
LuaAPI Error.
</brief_description>
Expand Down
244 changes: 244 additions & 0 deletions doc_classes/LuaObjectMetatable.xml
@@ -0,0 +1,244 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="LuaObjectMetatable" inherits="RefCounted" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Represents a Lua Object Metatable.
</brief_description>
<description>
This interface class allows for the definition of lua metamethods to be used for an objects metatable. Objects can define a [code]lua_metatable[/code] property that returns a LuaObjectMetatable. This metatable will be used for the object when it is passed to Lua. If one does not exists the luaAPI.object_metatable will be used instead.
<tutorials>
</tutorials>
<methods>
<method name="__add" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The addition (+) operation. If any operand for an addition is not a number, Lua will try to call a metamethod. It starts by checking the first operand (even if it is a number); if that operand does not define a metamethod for __add, then Lua will check the second operand. If Lua can find a metamethod, it calls the metamethod with the two operands as arguments, and the result of the call (adjusted to one value) is the result of the operation. Otherwise, if no metamethod is found, Lua raises an error.
</description>
</method>
<method name="__band" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The bitwise AND (&) operation. Behavior similar to the addition operation, except that Lua will try a metamethod if any operand is neither an integer nor a float coercible to an integer
</description>
</method>
<method name="__bnot" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<description>
The bitwise NOT (unary ~) operation. Behavior similar to the bitwise AND operation.
</description>
</method>
<method name="__bor" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The bitwise OR (|) operation. Behavior similar to the bitwise AND operation.
</description>
</method>
<method name="__bxor" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The bitwise exclusive OR (binary ~) operation. Behavior similar to the bitwise AND operation.
</description>
</method>
<method name="__call" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="args" type="LuaTuple" />
<description>
The bitwise NOT (unary ~) operation. Behavior similar to the bitwise AND operation.
</description>
</method>
<method name="__concat" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The concatenation (..) operation. Behavior similar to the addition operation, except that Lua will try a metamethod if any operand is neither a string nor a number (which is always coercible to a string).
</description>
</method>
<method name="__div" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The division (/) operation. Behavior similar to the addition operation.
</description>
</method>
<method name="__eq" qualifiers="virtual">
<return type="bool" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The equal (==) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are either both tables or both full userdata and they are not primitively equal. The result of the call is always converted to a boolean.
</description>
</method>
<method name="__gc" qualifiers="virtual">
<return type="LuaError" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<description>
https://www.lua.org/manual/5.4/manual.html#2.5.3
</description>
</method>
<method name="__idiv" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The floor division (//) operation. Behavior similar to the addition operation.
</description>
</method>
<method name="__index" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="index" type="String" />
<description>
The indexing access operation table[key]. This event happens when table is not a table or when key is not present in table. The metavalue is looked up in the metatable of table.

The metavalue for this event can be either a function, a table, or any value with an __index metavalue. If it is a function, it is called with table and key as arguments, and the result of the call (adjusted to one value) is the result of the operation. Otherwise, the final result is the result of indexing this metavalue with key. This indexing is regular, not raw, and therefore can trigger another __index metavalue.
</description>
</method>
<method name="__le" qualifiers="virtual">
<return type="bool" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The less equal (<=) operation. Behavior similar to the less than operation.
</description>
</method>
<method name="__len" qualifiers="virtual">
<return type="int" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<description>
The length (#) operation. If the object is not a string, Lua will try its metamethod. If there is a metamethod, Lua calls it with the object as argument, and the result of the call (always adjusted to one value) is the result of the operation. If there is no metamethod but the object is a table, then Lua uses the table length operation. Otherwise, Lua raises an error.
</description>
</method>
<method name="__lt" qualifiers="virtual">
<return type="bool" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The less than (<) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are neither both numbers nor both strings. Moreover, the result of the call is always converted to a boolean.
</description>
</method>
<method name="__metatable" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<description>
If object does not have a metatable, returns nil. Otherwise, if the object's metatable has a __metatable field, returns the associated value. Otherwise, returns the metatable of the given object.
</description>
</method>
<method name="__mod" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The modulo (%) operation. Behavior similar to the addition operation.
</description>
</method>
<method name="__mul" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The multiplication (*) operation. Behavior similar to the addition operation.
</description>
</method>
<method name="__newindex" qualifiers="virtual">
<return type="LuaError" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="index" type="String" />
<param index="3" name="value" type="Variant" />
<description>
The indexing assignment table[key] = value. Like the index event, this event happens when table is not a table or when key is not present in table. The metavalue is looked up in the metatable of table.

Like with indexing, the metavalue for this event can be either a function, a table, or any value with an __newindex metavalue. If it is a function, it is called with table, key, and value as arguments. Otherwise, Lua repeats the indexing assignment over this metavalue with the same key and value. This assignment is regular, not raw, and therefore can trigger another __newindex metavalue.

Whenever a __newindex metavalue is invoked, Lua does not perform the primitive assignment. If needed, the metamethod itself can call rawset to do the assignment.
</description>
</method>
<method name="__pow" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The exponentiation (^) operation. Behavior similar to the addition operation.
</description>
</method>
<method name="__shl" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The bitwise left shift (<<) operation. Behavior similar to the bitwise AND operation.
</description>
</method>
<method name="__shr" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The bitwise right shift (>>) operation. Behavior similar to the bitwise AND operation.
</description>
</method>
<method name="__sub" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<param index="2" name="other" type="Variant" />
<description>
The subtraction (-) operation. Behavior similar to the addition operation.
</description>
</method>
<method name="__tostring" qualifiers="virtual">
<return type="String" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<description>
The tostring operation. Used when tostring is called on the object.
</description>
</method>
<method name="__unm" qualifiers="virtual">
<return type="Variant" />
<param index="0" name="obj" type="Object" />
<param index="1" name="lua" type="LuaAPI" />
<description>
The negation (unary -) operation. Behavior similar to the addition operation.
</description>
</method>
</methods>

</methods>
<members>
</members>
<constants>
</constants>
</class>
2 changes: 1 addition & 1 deletion doc_classes/LuaTuple.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="LuaTuple" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<class name="LuaTuple" inherits="RefCounted" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
A Lua Tuple.
</brief_description>
Expand Down
1 change: 0 additions & 1 deletion project/testing/tests/LuaAPI.call_function.gd
Expand Up @@ -8,7 +8,6 @@ func _ready():
id = 9970

lua = LuaAPI.new()
lua.permissive = true

# testName and testDescription are for any needed context about the test.
testName = "LuaAPI.call_function"
Expand Down
1 change: 0 additions & 1 deletion project/testing/tests/LuaAPI.expose_constructor.gd
Expand Up @@ -12,7 +12,6 @@ func _ready():
id = 9950

lua = LuaAPI.new()
lua.permissive = true
var err = lua.push_variant("TestObj", TestObject.new)
if err is LuaError:
errors.append(err)
Expand Down
1 change: 0 additions & 1 deletion project/testing/tests/LuaAPI.expose_function.gd
Expand Up @@ -40,7 +40,6 @@ func _ready():
id = 9940

lua = LuaAPI.new()
lua.permissive = true
lua.set_meta("isValid", true)
var err = lua.push_variant("test1", LuaCallableExtra.with_tuple(testFuncTuple, 2))
if err is LuaError:
Expand Down
1 change: 0 additions & 1 deletion project/testing/tests/LuaCoroutine.resume.gd
Expand Up @@ -9,7 +9,6 @@ func _ready():
id = 9500

lua = LuaAPI.new()
lua.permissive = true
co = lua.new_coroutine()

co.load_string("
Expand Down
1 change: 0 additions & 1 deletion project/testing/tests/LuaCoroutine.yield_await.gd
Expand Up @@ -13,7 +13,6 @@ func _ready():
id = 9490

lua = LuaAPI.new()
lua.permissive = true
co = lua.new_coroutine()
co.push_variant("test_yield_await", _test_yield_await)

Expand Down
1 change: 0 additions & 1 deletion project/testing/tests/general.base_types.gd
Expand Up @@ -8,7 +8,6 @@ func _ready():
id = 9825

lua = LuaAPI.new()
lua.permissive = true

# testName and testDescription are for any needed context about the test.
testName = "General.base_types"
Expand Down
1 change: 0 additions & 1 deletion project/testing/tests/general.object_metamethods.gd
Expand Up @@ -16,7 +16,6 @@ func _ready():
id = 9850

lua = LuaAPI.new()
lua.permissive = true
lua.set_meta("isValid", true)
testObj = TestObject.new()
lua.push_variant("testObj", testObj)
Expand Down
1 change: 0 additions & 1 deletion project/testing/tests/general.object_push.gd
Expand Up @@ -14,7 +14,6 @@ func _ready():

testObj = TestObject.new()
lua = LuaAPI.new()
lua.permissive = true
var err = lua.push_variant("testObj", testObj)
if err is LuaError:
errors.append(err)
Expand Down
1 change: 0 additions & 1 deletion project/testing/tests/luaAPI.do_file.gd
Expand Up @@ -8,7 +8,6 @@ func _ready():
id = 9990

lua = LuaAPI.new()
lua.permissive = true

# testName and testDescription are for any needed context about the test.
testName = "LuaAPI.do_file()"
Expand Down
1 change: 0 additions & 1 deletion project/testing/tests/luaAPI.do_string.gd
Expand Up @@ -8,7 +8,6 @@ func _ready():
id = 10000

lua = LuaAPI.new()
lua.permissive = true

# testName and testDescription are for any needed context about the test.
testName = "LuaAPI.do_string()"
Expand Down

0 comments on commit 2c1b23d

Please sign in to comment.