@@ -25,8 +25,8 @@ LuaAPI::~LuaAPI() {
2525
2626// Bind C++ functions to GDScript
2727void 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
201217Ref<LuaCoroutine> LuaAPI::newCoroutine () {
0 commit comments