From c515fce42272d0d2cb119451cdbbb46b184e0fcb Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Sat, 20 Feb 2021 23:40:46 +0000 Subject: [PATCH] Update JS tests. --- tests2/Builtins.h | 8 +++- tests2/Classes.h | 28 ++++++++++++ tests2/Classes2.h | 4 ++ tests2/Delegates.h | 14 ++++++ tests2/Overloads.h | 6 +++ tests2/quickjs/premake5.lua | 21 +++++++-- tests2/quickjs/test-prop.js | 15 ++++++ tests2/quickjs/test.js | 91 +++++++++++++++++++++++++++++++++++++ tests2/quickjs/test.sh | 23 ++++++---- tests2/test.sh | 6 +++ 10 files changed, 201 insertions(+), 15 deletions(-) create mode 100644 tests2/Classes2.h create mode 100644 tests2/Delegates.h create mode 100644 tests2/quickjs/test-prop.js create mode 100755 tests2/test.sh diff --git a/tests2/Builtins.h b/tests2/Builtins.h index f5fd6d31..3aa8015c 100644 --- a/tests2/Builtins.h +++ b/tests2/Builtins.h @@ -50,4 +50,10 @@ uint16_t PassAndReturnsUInt16 (uint16_t v) { return v; } int32_t PassAndReturnsInt32 (int32_t v) { return v; } uint32_t PassAndReturnsUInt32 (uint32_t v) { return v; } int64_t PassAndReturnsInt64 (int64_t v) { return v; } -uint64_t PassAndReturnsUInt64 (uint64_t v) { return v; } \ No newline at end of file +uint64_t PassAndReturnsUInt64 (uint64_t v) { return v; } + +// Pointer types +const char* ReturnsConstCharPtr() { return "Hello"; } +const char* PassAndReturnsConstCharPtr(const char* ptr) { return ptr; } + + diff --git a/tests2/Classes.h b/tests2/Classes.h index b239183a..7a2162b1 100644 --- a/tests2/Classes.h +++ b/tests2/Classes.h @@ -1,8 +1,28 @@ +#include "Classes2.h" + class Class { public: void ReturnsVoid() {} int ReturnsInt() { return 0; } + Class* PassAndReturnsClassPtr(Class* obj) { return obj; } +}; + +class ClassWithField +{ +public: + ClassWithField() : Field(10) {} + int Field; + int ReturnsField() { return Field; } +}; + +class ClassWithOverloads +{ +public: + ClassWithOverloads() {} + ClassWithOverloads(int) {} + void Overload() {} + void Overload(int) {} }; class ClassWithSingleInheritance : public Class @@ -10,3 +30,11 @@ class ClassWithSingleInheritance : public Class public: int ChildMethod() { return 2; } }; + +class ClassWithExternalInheritance : public ClassFromAnotherUnit +{ + +}; + +void FunctionPassClassByRef(Class* klass) { } +Class* FunctionReturnsClassByRef() { return new Class(); } \ No newline at end of file diff --git a/tests2/Classes2.h b/tests2/Classes2.h new file mode 100644 index 00000000..fc62b7b9 --- /dev/null +++ b/tests2/Classes2.h @@ -0,0 +1,4 @@ +class ClassFromAnotherUnit +{ + +}; diff --git a/tests2/Delegates.h b/tests2/Delegates.h new file mode 100644 index 00000000..688a2573 --- /dev/null +++ b/tests2/Delegates.h @@ -0,0 +1,14 @@ +#include + +using namespace fastdelegate; + +class ClassWithDelegate +{ +public: + FastDelegate OnEvent0; + void FireEvent0(int value) { if (OnEvent0) OnEvent0(value); } +}; + +class ClassInheritsDelegate : public ClassWithDelegate +{ +}; diff --git a/tests2/Overloads.h b/tests2/Overloads.h index 931432d9..9c619bde 100644 --- a/tests2/Overloads.h +++ b/tests2/Overloads.h @@ -6,3 +6,9 @@ int Overload1(int) { return 2; } int Overload(int, int) { return 1; } int Overload(int, float) { return 2; } int Overload(float, int) { return 3; } + +int DefaultParamsOverload() { return 0; } +int DefaultParamsOverload(int a, int b) { return 2; } +int DefaultParamsOverload(int a, float b = 2) { return 3; } + +int DefaultParamsOverload2(int a = 0, int b = 0, int c = 0) { return 1; } diff --git a/tests2/quickjs/premake5.lua b/tests2/quickjs/premake5.lua index 4166454d..d80e004d 100644 --- a/tests2/quickjs/premake5.lua +++ b/tests2/quickjs/premake5.lua @@ -1,8 +1,8 @@ -qjs_inc_dir = path.getabsolute("../../deps/txiki.js/deps/quickjs/include") -qjs_lib_dir = path.getabsolute("../../deps/txiki.js/deps/quickjs/include") +local qjs_dir = path.getabsolute("../../deps/quickjs") +local runtime = "../../src/Generator/Generators/QuickJS/Runtime" workspace "qjs" - configurations { "release" } + configurations { "debug", "release" } location "gen" symbols "On" optimize "Off" @@ -10,8 +10,19 @@ workspace "qjs" project "test" kind "SharedLib" language "C++" - files {"gen/**.cpp"} - includedirs { qjs_inc_dir, ".." } + files + { + "gen/**.cpp", + runtime .. "/*.cpp", + runtime .. "/*.c" + } + includedirs + { + qjs_dir, + runtime, + "..", + "../../include" + } libdirs { qjs_lib_dir } filter { "kind:StaticLib" } links { "quickjs" } diff --git a/tests2/quickjs/test-prop.js b/tests2/quickjs/test-prop.js new file mode 100644 index 00000000..d4c0733c --- /dev/null +++ b/tests2/quickjs/test-prop.js @@ -0,0 +1,15 @@ +class A +{ + get foo() { return 1; } +} + +class B extends A +{ + +} + +let a = new A(); +console.log(a.foo) + +let b = new B(); +console.log(b.foo) diff --git a/tests2/quickjs/test.js b/tests2/quickjs/test.js index 18140099..1f2b7875 100644 --- a/tests2/quickjs/test.js +++ b/tests2/quickjs/test.js @@ -103,5 +103,96 @@ function enums() eq(test.PassAndReturnsEnum(test.Enum0.Item1), test.Enum0.Item1); } +function overloads() +{ + eq(test.Overload0(), undefined); + + eq(test.Overload1(), 1); + eq(test.Overload1(2), 2); + + eq(test.Overload(1, 2), 1); + //eq(test.Overload(1, 2.032), 2); + //eq(test.Overload(1.23, 2), 3); + + eq(test.DefaultParamsOverload(0, 0), 2); + eq(test.DefaultParamsOverload(0, 0.0), 2); + eq(test.DefaultParamsOverload(0), 3); + + eq(test.DefaultParamsOverload2(), 1); + eq(test.DefaultParamsOverload2(1), 1); + eq(test.DefaultParamsOverload2(1, 2), 1); + eq(test.DefaultParamsOverload2(1, 2, 3), 1); +} + +function classes() +{ + var c = new test.Class(); + eq(typeof(c), "object") + eq(c.ReturnsVoid(), undefined) + eq(c.ReturnsInt(), 0) + eq(c.PassAndReturnsClassPtr(null), null) + + var c1 = new test.ClassWithSingleInheritance(); + eq(c1.__proto__.constructor.name, 'ClassWithSingleInheritance') + eq(c1.__proto__.__proto__.constructor.name, 'Class') + eq(c1.ReturnsVoid(), undefined); + eq(c1.ReturnsInt(), 0); + eq(c1.ChildMethod(), 2); + + var classWithField = new test.ClassWithField(); + eq(classWithField.ReturnsField(), 10); +} + +function delegates() +{ + const signal = new test.Signal(); + eq(signal.toString(), "Signal"); + + const classWithDelegate = new test.ClassWithDelegate(); + const event0 = classWithDelegate.OnEvent0; + eq(event0.__proto__.constructor.name, "Signal") + + const event0_1 = classWithDelegate.OnEvent0; + eq(event0 === event0_1, true); + + let anon_cb_called = false; + const anon_cb = () => { anon_cb_called = true; return 32; }; + event0.connect(anon_cb); + classWithDelegate.FireEvent0(10); + eq(anon_cb_called, true); +} + +function delegates2() +{ + let anon_cb_called = false; + const anon_cb = () => { anon_cb_called = true; return 32; }; + const classInheritsDelegate = new test.ClassInheritsDelegate(); + const event0 = classInheritsDelegate.OnEvent0; + event0.connect(anon_cb); + classInheritsDelegate.FireEvent0(10); + eq(anon_cb_called, true); +} + builtins(); enums(); +overloads(); +classes(); +delegates(); +delegates2(); + +function printChain(obj) +{ + console.log("\nprototypes:") + let proto = obj.__proto__ + if (proto == null) + { + console.log("invalid proto") + return + } + while (proto != null) + { + console.log(proto.constructor.name) + proto = proto.__proto__ + } + console.log("") +} diff --git a/tests2/quickjs/test.sh b/tests2/quickjs/test.sh index 3b805897..b97be557 100755 --- a/tests2/quickjs/test.sh +++ b/tests2/quickjs/test.sh @@ -2,25 +2,30 @@ set -e dir=$(cd "$(dirname "$0")"; pwd) rootdir="$dir/../.." -configuration=Release +dotnet_configuration=Release +configuration=debug platform=x64 -jsinterp="$rootdir/deps/quickjs/qjs" +jsinterp="$rootdir/deps/quickjs/qjs-debug" red=`tput setaf 1` green=`tput setaf 2` reset=`tput sgr0` -echo "${green}Generating bindings${reset}" -dotnet $rootdir/bin/${configuration}_${platform}/CppSharp.CLI.dll \ - --gen=qjs -I$dir/.. -o $dir/gen -m tests $dir/../Builtins.h $dir/../Enums.h +generate=true + +if [ $generate = true ]; then + echo "${green}Generating bindings${reset}" + dotnet $rootdir/bin/${dotnet_configuration}_${platform}/CppSharp.CLI.dll \ + --gen=qjs -I$dir/.. -I$rootdir/include -o $dir/gen -m tests $dir/../*.h +fi echo "${green}Building generated binding files${reset}" premake=$rootdir/build/premake.sh -$premake --file=$dir/premake5.lua gmake +config=$configuration $premake --file=$dir/premake5.lua gmake make -C $dir/gen echo echo "${green}Executing JS tests with QuickJS${reset}" -cp $dir/gen/bin/release/libtest.so $dir -#cp $dir/gen/bin/release/libtest.dylib $dir -$jsinterp $dir/test.js \ No newline at end of file +cp $dir/gen/bin/$configuration/libtest.so $dir +#cp $dir/gen/bin/$configuration/libtest.dylib $dir +$jsinterp --std $dir/test.js \ No newline at end of file diff --git a/tests2/test.sh b/tests2/test.sh new file mode 100755 index 00000000..622401ef --- /dev/null +++ b/tests2/test.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -e +dir=$(cd "$(dirname "$0")"; pwd) + +$dir/napi/test.sh +$dir/quickjs/test.sh