Skip to content

Commit

Permalink
Merge pull request #43 from gilzoide/load-string-and-file
Browse files Browse the repository at this point in the history
Add `load_string` and `load_file` methods to `LuaState`
  • Loading branch information
gilzoide authored Nov 5, 2023
2 parents 486ade3 + 178025f commit fc710eb
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/LuaError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
namespace luagdextension {

LuaError::LuaError(Status status, const String& message) : status(status), message(message) {}

LuaError::LuaError(const sol::load_result& load_result) : status((LuaError::Status) load_result.status()), message(((sol::error) load_result).what()) {}
LuaError::LuaError(const sol::protected_function_result& function_result) : status((LuaError::Status) function_result.status()), message(((sol::error) function_result).what()) {}

void LuaError::_bind_methods() {
Expand Down
1 change: 1 addition & 0 deletions src/LuaError.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class LuaError : public RefCounted {

LuaError() = default;
LuaError(Status status, const String& message);
LuaError(const sol::load_result& load_result);
LuaError(const sol::protected_function_result& function_result);

String get_message() const;
Expand Down
10 changes: 10 additions & 0 deletions src/LuaState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ LuaTable *LuaState::create_table(const Dictionary& initial_values) {
return memnew(LuaTable(to_table(lua_state, initial_values)));
}

Variant LuaState::load_string(const String& chunk, const String& chunkname) {
return ::luagdextension::load_string(lua_state, chunk, chunkname);
}

Variant LuaState::load_file(const String& filename, int buffer_size) {
return ::luagdextension::load_file(lua_state, filename);
}

Variant LuaState::do_string(const String& chunk, const String& chunkname) {
return ::luagdextension::do_string(lua_state, chunk, chunkname);
}
Expand Down Expand Up @@ -180,6 +188,8 @@ void LuaState::_bind_methods() {
// Methods
ClassDB::bind_method(D_METHOD("open_libraries", "libraries"), &LuaState::open_libraries, DEFVAL(BitField<Library>(LUA_ALL_LIBS | GODOT_ALL_LIBS)));
ClassDB::bind_method(D_METHOD("create_table", "initial_values"), &LuaState::create_table, DEFVAL(Dictionary()));
ClassDB::bind_method(D_METHOD("load_string", "chunk", "chunkname"), &LuaState::load_string, DEFVAL(""));
ClassDB::bind_method(D_METHOD("load_file", "filename", "buffer_size"), &LuaState::load_file, DEFVAL(1024));
ClassDB::bind_method(D_METHOD("do_string", "chunk", "chunkname"), &LuaState::do_string, DEFVAL(""));
ClassDB::bind_method(D_METHOD("do_file", "filename", "buffer_size"), &LuaState::do_file, DEFVAL(1024));
ClassDB::bind_method(D_METHOD("get_globals"), &LuaState::get_globals);
Expand Down
2 changes: 2 additions & 0 deletions src/LuaState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class LuaState : public RefCounted {
void open_libraries(BitField<Library> libraries);

LuaTable *create_table(const Dictionary& initial_values = {});
Variant load_string(const String& chunk, const String& chunkname = "");
Variant load_file(const String& filename, int buffer_size = 1024);
Variant do_string(const String& chunk, const String& chunkname = "");
Variant do_file(const String& filename, int buffer_size = 1024);

Expand Down
26 changes: 26 additions & 0 deletions src/utils/convert_godot_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ Variant to_variant(const sol::protected_function_result& function_result) {
}
}

Variant to_variant(const sol::load_result& load_result) {
if (!load_result.valid()) {
return memnew(LuaError(load_result));
}
else {
return memnew(LuaFunction(load_result.get<sol::protected_function>()));
}
}

Variant to_variant(lua_State *L, int index) {
return to_variant(sol::stack_object(L, index));
}
Expand Down Expand Up @@ -278,6 +287,23 @@ Variant do_file(sol::state_view& lua_state, const String& filename, int buffer_s
return to_variant(lua_state.safe_script((lua_Reader) file_reader, (void *) &reader_data, sol::script_pass_on_error, to_std_string(filename)));
}

Variant load_string(sol::state_view& lua_state, const String& chunk, const String& chunkname) {
PackedByteArray bytes = chunk.to_utf8_buffer();
return to_variant(lua_state.load(to_string_view(bytes), to_std_string(chunkname)));
}

Variant load_file(sol::state_view& lua_state, const String& filename, int buffer_size) {
auto file = FileAccess::open(filename, godot::FileAccess::READ);
if (file == nullptr) {
return memnew(LuaError(LuaError::Status::FILE, String("Cannot open file '%s': " + UtilityFunctions::error_string(FileAccess::get_open_error())) % filename));
}

FileReaderData reader_data;
reader_data.file = file.ptr();
reader_data.buffer_size = buffer_size;
return to_variant(lua_state.load((lua_Reader) file_reader, (void *) &reader_data, to_std_string(filename)));
}

void lua_error(lua_State *L, const GDExtensionCallError& call_error, const String& prefix_message) {
CharString prefix = prefix_message.ascii();
CharString error_str = to_string(call_error).ascii();
Expand Down
3 changes: 3 additions & 0 deletions src/utils/convert_godot_lua.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Variant to_variant(const sol::object& object);
Variant to_variant(const sol::stack_object& object);
Variant to_variant(const sol::stack_proxy_base& object);
Variant to_variant(const sol::protected_function_result& function_result);
Variant to_variant(const sol::load_result& load_result);
Variant to_variant(lua_State *L, int index);
sol::stack_object to_lua(lua_State *lua_state, const Variant& value);

Expand All @@ -50,6 +51,8 @@ std::tuple<bool, sol::object> variant_pcall(sol::this_state state, Variant& vari

Variant do_string(sol::state_view& lua_state, const String& chunk, const String& chunkname = "");
Variant do_file(sol::state_view& lua_state, const String& filename, int buffer_size = 1024);
Variant load_string(sol::state_view& lua_state, const String& chunk, const String& chunkname = "");
Variant load_file(sol::state_view& lua_state, const String& filename, int buffer_size = 1024);

void lua_error(lua_State *L, const GDExtensionCallError& call_error, const String& prefix_message);

Expand Down
25 changes: 25 additions & 0 deletions test/gdscript_tests/load_file.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
extends RefCounted


var lua_state
var function


func _init():
lua_state = LuaState.new()
lua_state.open_libraries()


func test_load_fail() -> bool:
var result = lua_state.load_file("res://invalid file")
assert(result is LuaError, "load_file with invalid Lua script should return LuaError")
return true


func test_load_function() -> bool:
var result = lua_state.load_file("res://gdscript_tests/lua_files/load_file.gd.lua")
assert(result is LuaFunction, "load_file with valid Lua script should return LuaFunction")
assert(result.invoke(1) == 1)
assert(result.invoke(2) == 2)
assert(result.invoke(1, 2, 3) == [1, 2, 3])
return true
25 changes: 25 additions & 0 deletions test/gdscript_tests/load_string.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
extends RefCounted


var lua_state
var function


func _init():
lua_state = LuaState.new()
lua_state.open_libraries()


func test_load_fail() -> bool:
var result = lua_state.load_string("this is an invalid Lua script")
assert(result is LuaError, "load_string with invalid Lua script should return LuaError")
return true


func test_load_function() -> bool:
var result = lua_state.load_string("return ...")
assert(result is LuaFunction, "load_string with valid Lua script should return LuaFunction")
assert(result.invoke(1) == 1)
assert(result.invoke(2) == 2)
assert(result.invoke(1, 2, 3) == [1, 2, 3])
return true
1 change: 1 addition & 0 deletions test/gdscript_tests/lua_files/load_file.gd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return ...

0 comments on commit fc710eb

Please sign in to comment.