diff --git a/test/src/ByteBufferTest.cc b/test/src/ByteBufferTest.cc index dde11757..01aad006 100644 --- a/test/src/ByteBufferTest.cc +++ b/test/src/ByteBufferTest.cc @@ -49,7 +49,16 @@ void testByteBufferReadWrite(ScriptEngine* engine, const Local& buf) { auto buffer = buf.asByteBuffer(); ASSERT_TRUE(buffer.byteLength() == 8); uint8_t* ptr = static_cast(buffer.getRawBytes()); + + ASSERT_EQ(ptr, buffer.getRawBytesShared().get()); ASSERT_TRUE(ptr != nullptr); +#if defined(SCRIPTX_BACKEND_V8) || defined(SCRIPTX_BACKEND_JAVASCRIPTCORE) || defined(SCRIPTX_BACKEND_LUA) + ASSERT_EQ(buffer.isShared(), true); +#endif + + // if buffer is in shared mode + // copy data from script into buffer + buffer.sync(); EXPECT_EQ(ptr[0], 1); EXPECT_EQ(ptr[1], 0); @@ -61,6 +70,8 @@ void testByteBufferReadWrite(ScriptEngine* engine, const Local& buf) { ptr[6] = 4; ptr[7] = 8; + // if buffer is in shared mode + // copy data from buffer into script buffer.commit(); auto success = @@ -115,10 +126,14 @@ return view TEST_F(ByteBufferTest, DataView) { for (auto&& type : std::initializer_list>{ {"Int8Array", ByteBuffer::Type::kInt8}, + {"Uint8Array", ByteBuffer::Type::kUint8}, {"Int16Array", ByteBuffer::Type::kInt16}, + {"Uint16Array", ByteBuffer::Type::kUint16}, {"Int32Array", ByteBuffer::Type::kInt32}, + {"Uint32Array", ByteBuffer::Type::kUint32}, #ifdef SCRIPTX_BACKEND_V8 {"BigInt64Array", ByteBuffer::Type::kInt64}, + {"BigUint64Array", ByteBuffer::Type::kUint64}, #endif {"Float32Array", ByteBuffer::Type::KFloat32}, {"Float64Array", ByteBuffer::Type::kFloat64}, diff --git a/test/src/EngineTest.cc b/test/src/EngineTest.cc index 5b6f20dd..e99f613b 100644 --- a/test/src/EngineTest.cc +++ b/test/src/EngineTest.cc @@ -29,6 +29,14 @@ TEST_F(EngineTest, Misc) { #if defined(SCRIPTX_BACKEND_V8) || defined(SCRIPTX_BACKEND_LUA) EXPECT_GT(size, 0); #endif + + EXPECT_FALSE(engine->getEngineVersion().empty()); + +#ifdef SCRIPTX_LANG_JAVASCRIPT + EXPECT_EQ(engine->getLanguageType(), ScriptLanguage::kJavaScript); +#elif defined(SCRIPTX_LANG_LUA) + EXPECT_EQ(engine->getLanguageType(), ScriptLanguage::kLua); +#endif } TEST_F(EngineTest, UserData) { diff --git a/test/src/NativeTest.cc b/test/src/NativeTest.cc index 73b212cf..baccf6e7 100644 --- a/test/src/NativeTest.cc +++ b/test/src/NativeTest.cc @@ -787,6 +787,54 @@ TEST_F(NativeTest, TypedAPIClassDefineTest) { EXPECT_THROW({ engine->getNativeInstance(ins); }, Exception); } +TEST_F(NativeTest, MissMatchedType) { + class Instance : public ScriptClass { + public: + using ScriptClass::ScriptClass; + + std::string name; + + static int getAge() { return 0; } + static void setAge(int) {} + + void fun(int) {} + + static void sfun(int) {} + }; + + EngineScope scope(engine); + + auto def = defineClass("Instance") + .constructor() + .function("sfun", &Instance::sfun) + .instanceFunction("fun", &Instance::fun) + .build(); + engine->registerNativeClass(def); + + auto sfun = + engine->eval(TS().js("Instance.sfun;").lua("return Instance.sfun").select()).asFunction(); + auto ins = engine->newNativeClass(); + auto fun = ins.get("fun").asFunction(); + +#ifdef SCRIPTX_NO_EXCEPTION_ON_BIND_FUNCTION + ASSERT_TRUE(sfun.call().isNull()); + ASSERT_TRUE(sfun.call({}, "empty").isNull()); + ASSERT_TRUE(sfun.call({}, 0, 0).isNull()); + ASSERT_TRUE(fun.call().isNull()); + ASSERT_TRUE(fun.call(ins).isNull()); + ASSERT_TRUE(fun.call(ins, "empty").isNull()); + ASSERT_TRUE(fun.call(ins, 0, 0).isNull()); +#else + EXPECT_THROW({ sfun.call(); }, Exception); + EXPECT_THROW({ sfun.call({}, "empty"); }, Exception); + EXPECT_THROW({ sfun.call({}, 0, 0); }, Exception); + EXPECT_THROW({ fun.call(); }, Exception); + EXPECT_THROW({ fun.call(ins); }, Exception); + EXPECT_THROW({ fun.call(ins, "empty"); }, Exception); + EXPECT_THROW({ fun.call(ins, 0, 0); }, Exception); +#endif +} + namespace { static const bool gender = true; @@ -973,6 +1021,48 @@ TEST_F(NativeTest, NativeFounction) { EXPECT_THROW({ func.call(); }, Exception); EXPECT_THROW({ func.call({}, 1, 2); }, Exception); EXPECT_THROW({ func.call({}, ""); }, Exception); + + class Instance : public ScriptClass { + public: + using ScriptClass::ScriptClass; + }; + + auto def = defineClass("Instance") + .constructor() + .instanceFunction("f", [](Instance*, int) {}) + .build(); + engine->registerNativeClass(def); + auto ins = engine->newNativeClass(); + auto insFunc = ins.get("f"); + + func.call({}, 0); + EXPECT_THROW({ func.call(ins); }, Exception); + EXPECT_THROW({ func.call(ins, 1, 2); }, Exception); + EXPECT_THROW({ func.call(ins, ""); }, Exception); +} + +TEST_F(NativeTest, OverloadedFunction) { + EngineScope scope(engine); + auto func1 = [](int) { return 1; }; + auto func2 = [](const std::string&) { return 2; }; + auto func3 = [](int, int) { return 3; }; + + auto overloaded = script::adaptOverLoadedFunction(func1, func2, func3); + auto fun = Function::newFunction(overloaded); + + auto ret = fun.call({}, 0); + EXPECT_EQ(ret.asNumber().toInt32(), 1); + + ret = fun.call({}, 3.14); + EXPECT_EQ(ret.asNumber().toInt32(), 1); + + ret = fun.call({}, "hello"); + EXPECT_EQ(ret.asNumber().toInt32(), 2); + + ret = fun.call({}, 1, 2); + EXPECT_EQ(ret.asNumber().toInt32(), 3); + + EXPECT_THROW({ fun.call({}, false); }, Exception); } TEST_F(NativeTest, SelectOverloadedFunction) { diff --git a/test/src/ValueTest.cc b/test/src/ValueTest.cc index 4010e060..fce5413f 100644 --- a/test/src/ValueTest.cc +++ b/test/src/ValueTest.cc @@ -636,6 +636,28 @@ TEST_F(ValueTest, Kinds) { test(Function::newFunction([]() {})); test(Array::newArray()); test(ByteBuffer::newByteBuffer(0)); + + EXPECT_THROW({Number::newNumber(0).asValue().asObject();}, Exception); + EXPECT_THROW({String::newString("hello").asValue().asArray();}, Exception); +} + +TEST_F(ValueTest, Unsupported) { + EngineScope engineScope(engine); +#ifdef SCRIPTX_LANG_JAVASCRIPT + auto strange = engine->eval("Symbol('x')"); +#elif defined(SCRIPTX_LANG_LUA) + auto lua = lua_interop::currentEngineLua(); + lua_newuserdata(lua, 4); + auto strange = lua_interop::makeLocal(lua_gettop(lua)); +#else + FAIL() << "add test here"; + auto strange = Local(); +#endif + + EXPECT_EQ(strange.getKind(), ValueKind::kUnsupported); + strange.asUnsupported(); + + EXPECT_THROW({Number::newNumber(0).asValue().asUnsupported();}, Exception); } TEST_F(ValueTest, KindNames) {