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

Commit edf4afe

Browse files
authored
Support argument passing and return to do_file/string (#191)
1 parent 525d1e0 commit edf4afe

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed

.github/workflows/macos-build.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ jobs:
119119
- uses: actions/checkout@v3
120120
with:
121121
submodules: recursive
122-
123-
- name: Setup Vulkan SDK
124-
run: |
125-
sh scripts/install_vulkan_sdk_macos.sh
126122

127123
- name: Install .NET SDK 6.0 (Mono)
128124
if: ${{ contains(matrix.opts.build-options, 'module_mono_enabled=yes') }}
@@ -153,6 +149,10 @@ jobs:
153149
python --version
154150
scons --version
155151
152+
- name: Setup Vulkan SDK
153+
run: |
154+
sh scripts/install_vulkan_sdk_macos.sh
155+
156156
- name: Compilation
157157
working-directory: ./scripts
158158
env:

doc_classes/LuaAPI.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@
3131
</description>
3232
</method>
3333
<method name="do_file">
34-
<return type="LuaError" />
34+
<return type="Variant" />
3535
<param index="0" name="FilePath" type="String" />
36+
<param index="1" name="Args" type="Array" default="[]" />
3637
<description>
3738
Loads a file with luaL_loadfile() passing its absolute path.
38-
Similar to [code].DoString()[/code], this function loads a lua file into the LuaAPI Object and executes it as Lua. [code]FilePath[/code] must be an absolute path, and must exist or an error is returned.
39+
Similar to [code].do_string()[/code], this function loads a lua file into the LuaAPI Object and executes it as Lua. [code]FilePath[/code] must be an absolute path, and must exist or an error is returned.
3940
</description>
4041
</method>
4142
<method name="do_string">
42-
<return type="LuaError" />
43+
<return type="Variant" />
4344
<param index="0" name="Code" type="String" />
45+
<param index="1" name="Args" type="Array" default="[]" />
4446
<description>
4547
Loads a string with luaL_loadstring() and executes the top of the stack. Returns any errors.
46-
Use [code].DoString()[/code] to execute a lua script or snippet stored within a string variable or a string literal.
48+
Use [code].do_string()[/code] to execute a lua script or snippet stored within a string variable or a string literal.
4749
</description>
4850
</method>
4951
<method name="function_exists">

src/classes/luaAPI.cpp

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ LuaAPI::~LuaAPI() {
2525

2626
// Bind C++ functions to GDScript
2727
void LuaAPI::_bind_methods() {
28-
ClassDB::bind_method(D_METHOD("do_file", "FilePath"), &LuaAPI::doFile);
29-
ClassDB::bind_method(D_METHOD("do_string", "Code"), &LuaAPI::doString);
28+
ClassDB::bind_method(D_METHOD("do_file", "FilePath", "Args"), &LuaAPI::doFile, DEFVAL(Array()));
29+
ClassDB::bind_method(D_METHOD("do_string", "Code", "Args"), &LuaAPI::doString, DEFVAL(Array()));
3030

3131
ClassDB::bind_method(D_METHOD("bind_libraries", "Array"), &LuaAPI::bindLibraries);
3232
ClassDB::bind_method(D_METHOD("set_hook", "Hook", "HookMask", "Count"), &LuaAPI::setHook);
@@ -140,7 +140,7 @@ Ref<LuaError> LuaAPI::pushGlobalVariant(String name, Variant var) {
140140
}
141141

142142
// addFile() calls luaL_loadfille with the absolute file path
143-
Ref<LuaError> LuaAPI::doFile(String fileName) {
143+
Variant LuaAPI::doFile(String fileName, Array args) {
144144
// push the error handler onto the stack
145145
lua_pushcfunction(lState, LuaState::luaErrorHandler);
146146

@@ -163,39 +163,55 @@ Ref<LuaError> LuaAPI::doFile(String fileName) {
163163
path = file->get_path_absolute();
164164
}
165165

166-
int ret = luaL_loadfile(lState, path.utf8().get_data());
167-
if (ret != LUA_OK) {
168-
return state.handleError(ret);
166+
int err = luaL_loadfile(lState, path.utf8().get_data());
167+
if (err != LUA_OK) {
168+
return state.handleError(err);
169169
}
170170

171-
Ref<LuaError> err = execute(-2);
171+
int argc = args.size();
172+
for (int i = 0; i < argc; i++) {
173+
state.pushVariant(args[i]);
174+
}
175+
176+
int handlerIndex = -2 - argc;
177+
178+
Variant ret = execute(argc, handlerIndex);
172179
// pop the error handler from the stack
173180
lua_pop(lState, 1);
174-
return err;
181+
return ret;
175182
}
176183

177184
// Loads string into lua state and executes the top of the stack
178-
Ref<LuaError> LuaAPI::doString(String code) {
185+
Variant LuaAPI::doString(String code, Array args) {
179186
// push the error handler onto the stack
180187
lua_pushcfunction(lState, LuaState::luaErrorHandler);
181-
int ret = luaL_loadstring(lState, code.utf8().get_data());
182-
if (ret != LUA_OK) {
183-
return state.handleError(ret);
188+
189+
int err = luaL_loadstring(lState, code.utf8().get_data());
190+
if (err != LUA_OK) {
191+
return state.handleError(err);
184192
}
185193

186-
Ref<LuaError> err = execute(-2);
194+
int argc = args.size();
195+
for (int i = 0; i < argc; i++) {
196+
state.pushVariant(args[i]);
197+
}
198+
199+
int handlerIndex = -2 - argc;
200+
201+
Variant ret = execute(argc, handlerIndex);
187202
// pop the error handler from the stack
188203
lua_pop(lState, 1);
189-
return err;
204+
return ret;
190205
}
191206

192207
// Execute the current lua stack, return error as string if one occurs, otherwise return String()
193-
Ref<LuaError> LuaAPI::execute(int handlerIndex) {
194-
int ret = lua_pcall(lState, 0, 0, handlerIndex);
195-
if (ret != LUA_OK) {
196-
return state.handleError(ret);
208+
Variant LuaAPI::execute(int argc, int handlerIndex) {
209+
int err = lua_pcall(lState, argc, 1, handlerIndex);
210+
if (err != LUA_OK) {
211+
return state.handleError(err);
197212
}
198-
return nullptr;
213+
214+
return state.getVar();
199215
}
200216

201217
Ref<LuaCoroutine> LuaAPI::newCoroutine() {

src/classes/luaAPI.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ class LuaAPI : public RefCounted {
4848

4949
Variant pullVariant(String name);
5050
Variant callFunction(String functionName, Array args);
51-
51+
Variant doFile(String fileName, Array args);
52+
Variant doString(String code, Array args);
5253
Variant getRegistryValue(String name);
5354

5455
Ref<LuaError> setRegistryValue(String name, Variant var);
5556
Ref<LuaError> bindLibraries(TypedArray<String> libs);
56-
Ref<LuaError> doFile(String fileName);
57-
Ref<LuaError> doString(String code);
5857
Ref<LuaError> pushGlobalVariant(String name, Variant var);
5958

6059
Ref<LuaCoroutine> newCoroutine();
@@ -98,7 +97,7 @@ class LuaAPI : public RefCounted {
9897

9998
LuaAllocData luaAllocData;
10099

101-
Ref<LuaError> execute(int handlerIndex);
100+
Variant execute(int argc, int handlerIndex);
102101
};
103102

104103
VARIANT_ENUM_CAST(LuaAPI::HookMask)

0 commit comments

Comments
 (0)