Skip to content
This repository was archived by the owner on Dec 29, 2024. It is now read-only.

Commit f254a24

Browse files
authored
Workaround for lack of CallableCustom & permissive defaulted to true (#131)
1 parent 4b5d77b commit f254a24

16 files changed

Lines changed: 150 additions & 262 deletions

.github/workflows/windows.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ jobs:
145145
146146
mv scripts/godot/bin/godot.windows.template_release.x86_64.luaAPI.exe scripts/godot/templates/windows_release_x86_64.exe || true
147147
mv scripts/godot/bin/godot.windows.template_debug.x86_64.luaAPI.exe scripts/godot/templates/windows_debug_x86_64.exe || true
148-
mv scripts/godot/bin/godot.windows.template_release.x86_32.luaAPI.exe scripts/godot/templates/windows_release_x32_64.exe || true
148+
mv scripts/godot/bin/godot.windows.template_release.x86_32.luaAPI.exe scripts/godot/templates/windows_release_x86_32.exe || true
149149
mv scripts/godot/bin/godot.windows.template_debug.x86_32.luaAPI.exe scripts/godot/templates/windows_debug_x86_32.exe || true
150150
151151
mv scripts/godot/bin/godot.windows.template_release.x86_64.luaAPI.console.exe scripts/godot/templates/windows_release_x86_64_console.exe || true
152152
mv scripts/godot/bin/godot.windows.template_debug.x86_64.luaAPI.console.exe scripts/godot/templates/windows_debug_x86_64_console.exe || true
153-
mv scripts/godot/bin/godot.windows.template_release.x86_32.luaAPI.console.exe scripts/godot/templates/windows_release_x32_64_console.exe || true
153+
mv scripts/godot/bin/godot.windows.template_release.x86_32.luaAPI.console.exe scripts/godot/templates/windows_release_x86_32_console.exe || true
154154
mv scripts/godot/bin/godot.windows.template_debug.x86_32.luaAPI.console.exe scripts/godot/templates/windows_debug_x86_32_console.exe || true
155155
156156

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ About
1414
![Logo](.github/LuaAPI.png)
1515
Art created by [Alex](https://www.instagram.com/redheadalex1)
1616

17-
***WARNING!!!*** this is a **beta** version of the addon made for Godot v4.0-stable. Which means frequent recompiles may be required and compatibility is not guaranteed between updates.
18-
1917
This is a Godot addon that adds Lua API support via GDScript, C# or GDExtension. Importantly this is **NOT** meant to be a replacement for or alternative to GDScript. This addon provides no functionality to program your game out of the box. This addon allows you to create custom Modding API's in a sandboxed environment. You have control of what people can and can not do within that sandbox.
2018

2119
To use you can either [Compile from source](#compiling) or you can download one of the [release builds](#release-builds).
@@ -39,10 +37,9 @@ Features
3937
- Push any Variant as a global.
4038
- Call Lua functions from GDScript.
4139
- Choose which libraries you want Lua to have access to.
42-
- Custom LuaCallable type which allows you to get a Lua function as a Callable. See [wiki](https://luaapi.weaselgames.info/v2.0-beta/examples/lua_callable/).
4340
- LuaError type which is used to report any errors this addon or Lua run into.
4441
- LuaCoroutine type which creates a Lua thread. This is not a OS thread but a coroutine.
45-
- Object passed as userdata. See [wiki](https://luaapi.weaselgames.info/v2.0-beta/examples/objects/).
42+
- Object passed as userdata. See [wiki](https://luaapi.weaselgames.info/latest/examples/objects/).
4643
- Objects can override most of the Lua metamethods. I.E. __index by defining a function with the same name.
4744
- Callables passed as userdata, which allows you to push a Callable as a Lua function.
4845
- Basic types are passed as userdata (currently: Vector2, Vector3, Color, Rect2, Plane) with a useful metatable. This means you can do things like:
@@ -72,9 +69,7 @@ Release Builds
7269

7370
TODO
7471
-----
75-
- Finish v2 documentation
7672
- Workaround for lack of CallableCustoms in GDExtension
77-
- More up to date todo list on the v2 [project](https://github.com/WeaselGames/godot_luaAPI/projects/1)
7873

7974
Compiling
8075
------------
@@ -97,7 +92,7 @@ var lua: LuaAPI = LuaAPI.new()
9792
func _lua_print(message: String) -> void:
9893
print(message)
9994
100-
func _ready():
95+
func _ready() -> void:
10196
lua.push_variant("print", _lua_print)
10297
lua.push_variant("message", "Hello lua!")
10398
@@ -122,7 +117,7 @@ func _ready():
122117
print("ERROR %d: %s" % [err.type, err.message])
123118
return
124119
125-
var message = val.call()
120+
var message = val.call([])
126121
print(message)
127122
```
128123
Contributing And Feature Requests

doc_classes/LuaAPI.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@
6464
Calls a function inside current Lua state. This can be either a exposed function or a function defined with with Lua. You may want to check if the function actually exists with [code]function_exists(LuaFunctionName)[/code]. This function supports 1 return value from lua. It will be returned as a variant and if Lua returns no value it will be null. If an error occurs while calling this function, a LuaError object will be returned.
6565
</description>
6666
</method>
67+
<method name="call_function_ref">
68+
<return type="Variant" />
69+
<param index="0" name="Args" type="Array" />
70+
<param index="1" name="LuaFunctionRef" type="int" />
71+
<description>
72+
This method is used to create a Callable when pulling a lua function from the stack. It is not intended to be called directly.
73+
When returning a Callable, the function ref is bound to the Callable. You must only supply the arguments.
74+
</description>
75+
</method>
6776
<method name="pull_variant">
6877
<return type="Variant" />
6978
<param index="0" name="Name" type="String" />
@@ -97,8 +106,8 @@
97106
</description>
98107
</methods>
99108
<members>
100-
<member name="permissive" type="bool" setter="set_permissive" getter="get_permissive" default="false">
101-
Set weather an objects lua_fields method should be treated as a blacklist instead of a whitelist.
109+
<member name="permissive" type="bool" setter="set_permissive" getter="get_permissive" default="true">
110+
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.
102111
</member>
103112
</members>
104113
<constants>

doc_classes/LuaCoroutine.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<return type="LuaError" />
2121
<param index="0" name="args" type="Array" />
2222
<description>
23-
Must be called from a lua hook. Will yield the lua state and pass arguments to `reumse()`. Can be destructive.
23+
Must be called from a lua hook. Will yield the lua state and pass arguments to `resume()`. Can be destructive.
2424
</description>
2525
</method>
2626
<method name="bind">

project/testing/tests/LuaAPI.call_function.gd

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,14 @@ func _process(delta):
4848

4949
var testCallable = lua.pull_variant("test")
5050
if testCallable is LuaError:
51-
if testCallable.type == LuaError.ERR_RUNTIME:
52-
done = true # This is because GDExtension does not support luaCallables
53-
return
5451
errors.append(testCallable)
5552
return fail()
5653

5754
if not testCallable is Callable:
5855
errors.append(LuaError.new_error("testCallable is not Callable but is '%d'" % typeof(testCallable), LuaError.ERR_TYPE))
5956
return fail()
6057

61-
var cret = testCallable.call(5)
58+
var cret = testCallable.call([5])
6259
if cret is LuaError:
6360
errors.append(cret)
6461
return fail()

register_types.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "register_types.h"
22
#include "src/classes/luaAPI.h"
3-
#include "src/classes/luaCallable.h"
43
#include "src/classes/luaCallableExtra.h"
54
#include "src/classes/luaCoroutine.h"
65
#include "src/classes/luaError.h"

src/classes/luaAPI.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "luaCoroutine.h"
44

5+
#include <luaState.h>
6+
57
#ifdef LAPI_GDEXTENSION
68
#include <godot_cpp/classes/file_access.hpp>
79
#endif
@@ -28,6 +30,7 @@ void LuaAPI::_bind_methods() {
2830
ClassDB::bind_method(D_METHOD("pull_variant", "Name"), &LuaAPI::pullVariant);
2931
ClassDB::bind_method(D_METHOD("expose_constructor", "LuaConstructorName", "Object"), &LuaAPI::exposeObjectConstructor);
3032
ClassDB::bind_method(D_METHOD("call_function", "LuaFunctionName", "Args"), &LuaAPI::callFunction);
33+
ClassDB::bind_method(D_METHOD("call_function_ref", "Args", "LuaFunctionRef"), &LuaAPI::callFunctionRef);
3134
ClassDB::bind_method(D_METHOD("function_exists", "LuaFunctionName"), &LuaAPI::luaFunctionExists);
3235

3336
ClassDB::bind_method(D_METHOD("new_coroutine"), &LuaAPI::newCoroutine);
@@ -77,6 +80,31 @@ Variant LuaAPI::callFunction(String functionName, Array args) {
7780
return state.callFunction(functionName, args);
7881
}
7982

83+
// Invokes the passed lua reference
84+
Variant LuaAPI::callFunctionRef(Array args, int funcRef) {
85+
lua_pushcfunction(lState, LuaState::luaErrorHandler);
86+
87+
// Getting the lua function via the reference stored in funcRef
88+
lua_rawgeti(lState, LUA_REGISTRYINDEX, funcRef);
89+
90+
// Push all the argument on to the stack
91+
for (int i = 0; i < args.size(); i++) {
92+
LuaState::pushVariant(lState, args[i]);
93+
}
94+
95+
Variant toReturn;
96+
// execute the function using a protected call.
97+
int ret = lua_pcall(lState, args.size(), 1, -2 - args.size());
98+
if (ret != LUA_OK) {
99+
toReturn = LuaState::handleError(lState, ret);
100+
} else {
101+
toReturn = LuaState::getVariant(lState, -1, this);
102+
}
103+
104+
lua_pop(lState, 1);
105+
return toReturn;
106+
}
107+
80108
// Calls LuaState::pushGlobalVariant()
81109
LuaError *LuaAPI::pushGlobalVariant(String name, Variant var) {
82110
return state.pushGlobalVariant(name, var);

src/classes/luaAPI.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class LuaAPI : public RefCounted {
4848

4949
Variant pullVariant(String name);
5050
Variant callFunction(String functionName, Array args);
51+
Variant callFunctionRef(Array args, int funcRef);
5152

5253
LuaError *doFile(String fileName);
5354
LuaError *doString(String code);
@@ -82,7 +83,7 @@ class LuaAPI : public RefCounted {
8283
LuaState state;
8384
lua_State *lState = nullptr;
8485

85-
bool permissive = false;
86+
bool permissive = true;
8687

8788
LuaError *execute(int handlerIndex);
8889
};

src/classes/luaCallable.cpp

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/classes/luaCallable.h

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)