Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support argument passing and return to do_file/string #191

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/macos-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Setup Vulkan SDK
run: |
sh scripts/install_vulkan_sdk_macos.sh

- name: Install .NET SDK 6.0 (Mono)
if: ${{ contains(matrix.opts.build-options, 'module_mono_enabled=yes') }}
Expand Down Expand Up @@ -153,6 +149,10 @@ jobs:
python --version
scons --version

- name: Setup Vulkan SDK
run: |
sh scripts/install_vulkan_sdk_macos.sh

- name: Compilation
working-directory: ./scripts
env:
Expand Down
10 changes: 6 additions & 4 deletions doc_classes/LuaAPI.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@
</description>
</method>
<method name="do_file">
<return type="LuaError" />
<return type="Variant" />
<param index="0" name="FilePath" type="String" />
<param index="1" name="Args" type="Array" default="[]" />
<description>
Loads a file with luaL_loadfile() passing its absolute path.
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.
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.
</description>
</method>
<method name="do_string">
<return type="LuaError" />
<return type="Variant" />
<param index="0" name="Code" type="String" />
<param index="1" name="Args" type="Array" default="[]" />
<description>
Loads a string with luaL_loadstring() and executes the top of the stack. Returns any errors.
Use [code].DoString()[/code] to execute a lua script or snippet stored within a string variable or a string literal.
Use [code].do_string()[/code] to execute a lua script or snippet stored within a string variable or a string literal.
</description>
</method>
<method name="function_exists">
Expand Down
54 changes: 35 additions & 19 deletions src/classes/luaAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ LuaAPI::~LuaAPI() {

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

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

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

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

int ret = luaL_loadfile(lState, path.utf8().get_data());
if (ret != LUA_OK) {
return state.handleError(ret);
int err = luaL_loadfile(lState, path.utf8().get_data());
if (err != LUA_OK) {
return state.handleError(err);
}

Ref<LuaError> err = execute(-2);
int argc = args.size();
for (int i = 0; i < argc; i++) {
state.pushVariant(args[i]);
}

int handlerIndex = -2 - argc;

Variant ret = execute(argc, handlerIndex);
// pop the error handler from the stack
lua_pop(lState, 1);
return err;
return ret;
}

// Loads string into lua state and executes the top of the stack
Ref<LuaError> LuaAPI::doString(String code) {
Variant LuaAPI::doString(String code, Array args) {
// push the error handler onto the stack
lua_pushcfunction(lState, LuaState::luaErrorHandler);
int ret = luaL_loadstring(lState, code.utf8().get_data());
if (ret != LUA_OK) {
return state.handleError(ret);

int err = luaL_loadstring(lState, code.utf8().get_data());
if (err != LUA_OK) {
return state.handleError(err);
}

Ref<LuaError> err = execute(-2);
int argc = args.size();
for (int i = 0; i < argc; i++) {
state.pushVariant(args[i]);
}

int handlerIndex = -2 - argc;

Variant ret = execute(argc, handlerIndex);
// pop the error handler from the stack
lua_pop(lState, 1);
return err;
return ret;
}

// Execute the current lua stack, return error as string if one occurs, otherwise return String()
Ref<LuaError> LuaAPI::execute(int handlerIndex) {
int ret = lua_pcall(lState, 0, 0, handlerIndex);
if (ret != LUA_OK) {
return state.handleError(ret);
Variant LuaAPI::execute(int argc, int handlerIndex) {
int err = lua_pcall(lState, argc, 1, handlerIndex);
if (err != LUA_OK) {
return state.handleError(err);
}
return nullptr;

return state.getVar();
}

Ref<LuaCoroutine> LuaAPI::newCoroutine() {
Expand Down
7 changes: 3 additions & 4 deletions src/classes/luaAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ class LuaAPI : public RefCounted {

Variant pullVariant(String name);
Variant callFunction(String functionName, Array args);

Variant doFile(String fileName, Array args);
Variant doString(String code, Array args);
Variant getRegistryValue(String name);

Ref<LuaError> setRegistryValue(String name, Variant var);
Ref<LuaError> bindLibraries(TypedArray<String> libs);
Ref<LuaError> doFile(String fileName);
Ref<LuaError> doString(String code);
Ref<LuaError> pushGlobalVariant(String name, Variant var);

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

LuaAllocData luaAllocData;

Ref<LuaError> execute(int handlerIndex);
Variant execute(int argc, int handlerIndex);
};

VARIANT_ENUM_CAST(LuaAPI::HookMask)
Expand Down