diff --git a/.gitignore b/.gitignore index 3725931e..0e760438 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ build/ .build/ +.godot/ .mingw/ .vscode/ program/test diff --git a/.gitmodules b/.gitmodules index c041a5a3..22361ffd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "godot-cpp"] path = godot-cpp url = https://github.com/godotengine/godot-cpp.git +[submodule "ext/Gut"] + path = ext/Gut + url = https://github.com/bitwes/Gut.git diff --git a/ext/Gut b/ext/Gut new file mode 160000 index 00000000..a55b6c5e --- /dev/null +++ b/ext/Gut @@ -0,0 +1 @@ +Subproject commit a55b6c5e5f7d13ae83cfb4830f61e3d800ebc864 diff --git a/program/cpp/api/array.hpp b/program/cpp/api/array.hpp index f639b8b9..0f8311e7 100644 --- a/program/cpp/api/array.hpp +++ b/program/cpp/api/array.hpp @@ -64,6 +64,10 @@ inline Variant::Variant(const Array& a) { v.i = a.get_variant_index(); } +inline Variant::operator Array() const { + return as_array(); +} + class ArrayIterator { public: ArrayIterator(const Array &array, unsigned idx) : m_array(array), m_idx(idx) {} diff --git a/program/cpp/api/dictionary.hpp b/program/cpp/api/dictionary.hpp index 6caa19fc..0bdf3bd8 100644 --- a/program/cpp/api/dictionary.hpp +++ b/program/cpp/api/dictionary.hpp @@ -49,6 +49,10 @@ inline Variant::Variant(const Dictionary &d) { v.i = d.get_variant_index(); } +inline Variant::operator Dictionary() const { + return as_dictionary(); +} + struct DictAccessor { DictAccessor(const Dictionary &dict, const Variant &key) : m_dict(dict), m_key(key) {} diff --git a/program/cpp/api/string.hpp b/program/cpp/api/string.hpp index c46097ac..fd3bcc36 100644 --- a/program/cpp/api/string.hpp +++ b/program/cpp/api/string.hpp @@ -53,10 +53,7 @@ inline Variant::Variant(const String &s) { } inline Variant::operator String() const { - if (m_type != STRING) { - api_throw("std::bad_cast", "Failed to cast Variant to String", this); - } - return String::from_variant_index(v.i); + return as_string(); } inline String Variant::as_string() const { diff --git a/program/cpp/api/variant.hpp b/program/cpp/api/variant.hpp index 50e26d7e..230e81b2 100644 --- a/program/cpp/api/variant.hpp +++ b/program/cpp/api/variant.hpp @@ -158,6 +158,8 @@ struct Variant operator std::string() const; // String for STRING and PACKED_BYTE_ARRAY operator std::u32string() const; // u32string for STRING, STRING_NAME operator String() const; + operator Array() const; + operator Dictionary() const; Object as_object() const; Node as_node() const; diff --git a/src/sandbox.cpp b/src/sandbox.cpp index 49dba7b1..092d6a87 100644 --- a/src/sandbox.cpp +++ b/src/sandbox.cpp @@ -19,6 +19,7 @@ String Sandbox::_to_string() const { void Sandbox::_bind_methods() { // Methods. ClassDB::bind_method(D_METHOD("get_functions"), &Sandbox::get_functions); + ClassDB::bind_method(D_METHOD("set_program", "program"), &Sandbox::set_program); { MethodInfo mi; mi.arguments.push_back(PropertyInfo(Variant::STRING, "function")); diff --git a/tests/addons/gut b/tests/addons/gut new file mode 120000 index 00000000..00881ed3 --- /dev/null +++ b/tests/addons/gut @@ -0,0 +1 @@ +../../ext/Gut/addons/gut \ No newline at end of file diff --git a/tests/extensions/bin/libgodot-riscv.linux.debug.x86_64.so b/tests/extensions/bin/libgodot-riscv.linux.debug.x86_64.so new file mode 120000 index 00000000..6d58d4e4 --- /dev/null +++ b/tests/extensions/bin/libgodot-riscv.linux.debug.x86_64.so @@ -0,0 +1 @@ +/home/gonzo/github/godot-riscv/.build/libgodot-riscv.so \ No newline at end of file diff --git a/tests/extensions/bin/libgodot-riscv.linux.release.x86_64.so b/tests/extensions/bin/libgodot-riscv.linux.release.x86_64.so new file mode 120000 index 00000000..6d58d4e4 --- /dev/null +++ b/tests/extensions/bin/libgodot-riscv.linux.release.x86_64.so @@ -0,0 +1 @@ +/home/gonzo/github/godot-riscv/.build/libgodot-riscv.so \ No newline at end of file diff --git a/tests/extensions/bin/libgodot_riscv.web.template_debug.wasm32.wasm b/tests/extensions/bin/libgodot_riscv.web.template_debug.wasm32.wasm new file mode 120000 index 00000000..8e3a5f62 --- /dev/null +++ b/tests/extensions/bin/libgodot_riscv.web.template_debug.wasm32.wasm @@ -0,0 +1 @@ +/home/gonzo/github/godot-riscv/bin/addons/godot_sandbox/bin/libgodot_riscv.web.template_debug.wasm32.wasm \ No newline at end of file diff --git a/tests/extensions/bin/libgodot_riscv.web.template_release.wasm32.wasm b/tests/extensions/bin/libgodot_riscv.web.template_release.wasm32.wasm new file mode 120000 index 00000000..d9781fa2 --- /dev/null +++ b/tests/extensions/bin/libgodot_riscv.web.template_release.wasm32.wasm @@ -0,0 +1 @@ +/home/gonzo/github/godot-riscv/bin/addons/godot_sandbox/bin/libgodot_riscv.web.template_release.wasm32.wasm \ No newline at end of file diff --git a/tests/extensions/libgodot-riscv.gdextension b/tests/extensions/libgodot-riscv.gdextension new file mode 100644 index 00000000..49dc0187 --- /dev/null +++ b/tests/extensions/libgodot-riscv.gdextension @@ -0,0 +1,18 @@ +[configuration] + +entry_symbol = "riscv_library_init" +compatibility_minimum = "4.1" + +[icons] + + +[libraries] + +macos.debug = "bin/libgodot-riscv.macos.debug.framework" +macos.release = "bin/libgodot-riscv.macos.release.framework" +windows.debug.x86_64 = "bin/libgodot-riscv.windows.debug.x86_64.dll" +windows.release.x86_64 = "bin/libgodot-riscv.windows.release.x86_64.dll" +linux.debug.x86_64 = "bin/libgodot-riscv.linux.debug.x86_64.so" +linux.release.x86_64 = "bin/libgodot-riscv.linux.release.x86_64.so" +web.debug.wasm32 = "bin/libgodot_riscv.web.template_debug.wasm32.wasm" +web.release.wasm32 = "bin/libgodot_riscv.web.template_release.wasm32.wasm" diff --git a/tests/icon.svg b/tests/icon.svg new file mode 100644 index 00000000..9d8b7fa1 --- /dev/null +++ b/tests/icon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/icon.svg.import b/tests/icon.svg.import new file mode 100644 index 00000000..52eb3206 --- /dev/null +++ b/tests/icon.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://6xqmeymr5t3" +path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.svg" +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/tests/project.godot b/tests/project.godot new file mode 100644 index 00000000..881db353 --- /dev/null +++ b/tests/project.godot @@ -0,0 +1,19 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="tests" +config/features=PackedStringArray("4.3", "Forward Plus") +config/icon="res://icon.svg" + +[editor_plugins] + +enabled=PackedStringArray("res://addons/gut/plugin.cfg") diff --git a/tests/tests/test_basic.cpp b/tests/tests/test_basic.cpp new file mode 100644 index 00000000..8d6b91a8 --- /dev/null +++ b/tests/tests/test_basic.cpp @@ -0,0 +1,9 @@ +#include "api.hpp" + +extern "C" Variant public_function() { + return "Hello from the other side"; +} + +extern "C" Variant test_dictionary(Variant dict) { + return Dictionary(dict)["1"]; +} diff --git a/tests/tests/test_basic.gd b/tests/tests/test_basic.gd new file mode 100644 index 00000000..52ab91f6 --- /dev/null +++ b/tests/tests/test_basic.gd @@ -0,0 +1,17 @@ +extends GutTest + +func test_instantiation(): + # Create a new sandbox + var s = Sandbox.new() + # Set the test program + s.set_program(Sandbox_TestsTests) + + # Verify that a basic function that returns a String works + assert_eq(s.vmcall("public_function"), "Hello from the other side") + + # Verify that the sandbox can receive a Dictionary and return an element + var d : Dictionary + d["1"] = Dictionary() + d["1"]["2"] = "3" + + assert_eq(s.vmcall("test_dictionary", d), d["1"])