From ea331233e1d6dc323f7c1692020ae5d7488311a8 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Fri, 27 Sep 2024 15:47:08 +0100 Subject: [PATCH] src: enable type cast checks in tests --- doc/value.md | 13 +++++ napi-inl.h | 22 ++++++-- napi.h | 6 +++ test/async_worker.cc | 6 +-- test/binding.gyp | 3 +- test/function.js | 2 +- .../global_object_delete_property.cc | 8 +-- .../global_object_get_property.cc | 8 +-- .../global_object_has_own_property.cc | 6 +-- .../global_object_set_property.cc | 10 ++-- test/name.cc | 18 ++++--- test/object/delete_property.cc | 10 ++-- test/object/get_property.cc | 10 ++-- test/object/has_own_property.cc | 8 +-- test/object/has_property.cc | 10 ++-- test/object/object.cc | 2 +- test/object/set_property.cc | 10 ++-- test/object/subscript_operator.cc | 6 +-- test/object_reference.cc | 36 ++++++------- test/run_script.cc | 2 +- test/type_taggable.cc | 2 +- test/typedarray.cc | 52 ++++++++++--------- 22 files changed, 146 insertions(+), 104 deletions(-) diff --git a/doc/value.md b/doc/value.md index 90e358236..f19532fa5 100644 --- a/doc/value.md +++ b/doc/value.md @@ -86,6 +86,19 @@ In order to enforce expected type, use `Napi::Value::Is*()` methods to check the type before calling `Napi::Value::As()`, or compile with definition `NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` to enforce type checks. +### UnsafeAs + +```cpp +template T Napi::Value::UnsafeAs() const; +``` + +Casts to another type of `Napi::Value`, when the actual type is known or +assumed. + +This conversion does not coerce the type. This does not check the type even if +`NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` is defined. This indicates intentional +unsafe type cast. Use `Napi::Value::As()` if possible. + ### Env ```cpp diff --git a/napi-inl.h b/napi-inl.h index e7e21a7b3..8631b9f9b 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -896,6 +896,16 @@ inline T Value::As() const { return T(_env, _value); } +template +inline T Value::UnsafeAs() const { + return T(_env, _value); +} + +// static +inline void Value::CheckCast(napi_env /* env */, napi_value value) { + NAPI_CHECK(value != nullptr, "Value::CheckCast", "empty value"); +} + inline MaybeOrValue Value::ToBoolean() const { napi_value result; napi_status status = napi_coerce_to_bool(_env, _value, &result); @@ -1303,12 +1313,15 @@ inline Symbol Symbol::New(napi_env env, napi_value description) { inline MaybeOrValue Symbol::WellKnown(napi_env env, const std::string& name) { + // No need to check if the return value is a symbol or undefined. + // Well known symbols are definite and it is an develop time error + // if the symbol does not exist. #if defined(NODE_ADDON_API_ENABLE_MAYBE) Value symbol_obj; Value symbol_value; if (Napi::Env(env).Global().Get("Symbol").UnwrapTo(&symbol_obj) && symbol_obj.As().Get(name).UnwrapTo(&symbol_value)) { - return Just(symbol_value.As()); + return Just(symbol_value.UnsafeAs()); } return Nothing(); #else @@ -1317,7 +1330,7 @@ inline MaybeOrValue Symbol::WellKnown(napi_env env, .Get("Symbol") .As() .Get(name) - .As(); + .UnsafeAs(); #endif } @@ -1535,7 +1548,10 @@ inline void Object::CheckCast(napi_env env, napi_value value) { napi_valuetype type; napi_status status = napi_typeof(env, value, &type); NAPI_CHECK(status == napi_ok, "Object::CheckCast", "napi_typeof failed"); - NAPI_INTERNAL_CHECK_EQ(type, napi_object, "%d", "Object::CheckCast"); + NAPI_INTERNAL_CHECK(type == napi_object || type == napi_function, + "Object::CheckCast", + "Expect napi_object or napi_function, but got %d.", + type); } inline Object::Object() : TypeTaggable() {} diff --git a/napi.h b/napi.h index edec5111e..ac896080d 100644 --- a/napi.h +++ b/napi.h @@ -461,6 +461,8 @@ class Value { template static Value From(napi_env env, const T& value); + static void CheckCast(napi_env env, napi_value value); + /// Converts to a Node-API value primitive. /// /// If the instance is _empty_, this returns `nullptr`. @@ -527,6 +529,10 @@ class Value { template T As() const; + // Unsafe Value::As(), should be avoided. + template + T UnsafeAs() const; + MaybeOrValue ToBoolean() const; ///< Coerces a value to a JavaScript boolean. MaybeOrValue ToNumber() diff --git a/test/async_worker.cc b/test/async_worker.cc index c0a20cb5c..34044e9c8 100644 --- a/test/async_worker.cc +++ b/test/async_worker.cc @@ -20,14 +20,14 @@ class TestWorkerWithUserDefRecv : public AsyncWorker { static void DoWorkWithAsyncRes(const CallbackInfo& info) { Object recv = info[0].As(); Function cb = info[1].As(); - Object resource = info[2].As(); + Value resource = info[2]; TestWorkerWithUserDefRecv* worker = nullptr; if (resource == info.Env().Null()) { worker = new TestWorkerWithUserDefRecv(recv, cb, "TestResource"); } else { - worker = - new TestWorkerWithUserDefRecv(recv, cb, "TestResource", resource); + worker = new TestWorkerWithUserDefRecv( + recv, cb, "TestResource", resource.As()); } worker->Queue(); diff --git a/test/binding.gyp b/test/binding.gyp index 28de3fe96..df8d5e84a 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -106,7 +106,8 @@ { 'target_name': 'binding', 'dependencies': ['../node_addon_api.gyp:node_addon_api_except'], - 'sources': ['>@(build_sources)'] + 'sources': ['>@(build_sources)'], + 'defines': ['NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS'] }, { 'target_name': 'binding_noexcept', diff --git a/test/function.js b/test/function.js index 04b020394..7cd8c04e2 100644 --- a/test/function.js +++ b/test/function.js @@ -89,7 +89,7 @@ function test (binding) { assert.deepStrictEqual(args, [7, 8, 9]); assert.throws(() => { - binding.callWithInvalidReceiver(); + binding.callWithInvalidReceiver(() => {}); }, /Invalid (pointer passed as )?argument/); obj = binding.callConstructorWithArgs(testConstructor, 5, 6, 7); diff --git a/test/globalObject/global_object_delete_property.cc b/test/globalObject/global_object_delete_property.cc index 295ed3f36..70738c2f4 100644 --- a/test/globalObject/global_object_delete_property.cc +++ b/test/globalObject/global_object_delete_property.cc @@ -5,27 +5,27 @@ using namespace Napi; Value DeletePropertyWithCStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); return Boolean::New( info.Env(), MaybeUnwrap(globalObject.Delete(key.Utf8Value().c_str()))); } Value DeletePropertyWithCppStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); return Boolean::New(info.Env(), MaybeUnwrap(globalObject.Delete(key.Utf8Value()))); } Value DeletePropertyWithInt32AsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Number key = info[0].As(); + Number key = info[0].UnsafeAs(); return Boolean::New(info.Env(), MaybeUnwrap(globalObject.Delete(key.Uint32Value()))); } Value DeletePropertyWithNapiValueAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Name key = info[0].As(); + Name key = info[0].UnsafeAs(); return Boolean::New(info.Env(), MaybeUnwrap(globalObject.Delete(key))); } diff --git a/test/globalObject/global_object_get_property.cc b/test/globalObject/global_object_get_property.cc index dd112043c..81f727d91 100644 --- a/test/globalObject/global_object_get_property.cc +++ b/test/globalObject/global_object_get_property.cc @@ -5,25 +5,25 @@ using namespace Napi; Value GetPropertyWithNapiValueAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Name key = info[0].As(); + Name key = info[0].UnsafeAs(); return MaybeUnwrap(globalObject.Get(key)); } Value GetPropertyWithInt32AsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Number key = info[0].As(); + Number key = info[0].UnsafeAs(); return MaybeUnwrapOr(globalObject.Get(key.Uint32Value()), Value()); } Value GetPropertyWithCStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String cStrkey = info[0].As(); + String cStrkey = info[0].UnsafeAs(); return MaybeUnwrapOr(globalObject.Get(cStrkey.Utf8Value().c_str()), Value()); } Value GetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String cppStrKey = info[0].As(); + String cppStrKey = info[0].UnsafeAs(); return MaybeUnwrapOr(globalObject.Get(cppStrKey.Utf8Value()), Value()); } diff --git a/test/globalObject/global_object_has_own_property.cc b/test/globalObject/global_object_has_own_property.cc index 89c299913..388788d97 100644 --- a/test/globalObject/global_object_has_own_property.cc +++ b/test/globalObject/global_object_has_own_property.cc @@ -5,7 +5,7 @@ using namespace Napi; Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); return Boolean::New( info.Env(), MaybeUnwrapOr(globalObject.HasOwnProperty(key.Utf8Value().c_str()), @@ -14,7 +14,7 @@ Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info) { Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); return Boolean::New( info.Env(), MaybeUnwrapOr(globalObject.HasOwnProperty(key.Utf8Value()), false)); @@ -22,7 +22,7 @@ Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { Value HasPropertyWithNapiValueAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Name key = info[0].As(); + Name key = info[0].UnsafeAs(); return Boolean::New(info.Env(), MaybeUnwrap(globalObject.HasOwnProperty(key))); } diff --git a/test/globalObject/global_object_set_property.cc b/test/globalObject/global_object_set_property.cc index 7065bee56..06da6315a 100644 --- a/test/globalObject/global_object_set_property.cc +++ b/test/globalObject/global_object_set_property.cc @@ -4,28 +4,28 @@ using namespace Napi; void SetPropertyWithCStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); Value value = info[1]; globalObject.Set(key.Utf8Value().c_str(), value); } void SetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); Value value = info[1]; globalObject.Set(key.Utf8Value(), value); } void SetPropertyWithInt32AsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Number key = info[0].As(); + Number key = info[0].UnsafeAs(); Value value = info[1]; globalObject.Set(key.Uint32Value(), value); } void SetPropertyWithNapiValueAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Name key = info[0].As(); + Name key = info[0].UnsafeAs(); Value value = info[1]; globalObject.Set(key, value); -} \ No newline at end of file +} diff --git a/test/name.cc b/test/name.cc index d8556e154..27bab9312 100644 --- a/test/name.cc +++ b/test/name.cc @@ -21,19 +21,21 @@ Value EchoString(const CallbackInfo& info) { Value CreateString(const CallbackInfo& info) { String encoding = info[0].As(); - Number length = info[1].As(); + Value length = info[1]; if (encoding.Utf8Value() == "utf8") { if (length.IsUndefined()) { return String::New(info.Env(), testValueUtf8); } else { - return String::New(info.Env(), testValueUtf8, length.Uint32Value()); + return String::New( + info.Env(), testValueUtf8, length.As().Uint32Value()); } } else if (encoding.Utf8Value() == "utf16") { if (length.IsUndefined()) { return String::New(info.Env(), testValueUtf16); } else { - return String::New(info.Env(), testValueUtf16, length.Uint32Value()); + return String::New( + info.Env(), testValueUtf16, length.As().Uint32Value()); } } else { Error::New(info.Env(), "Invalid encoding.").ThrowAsJavaScriptException(); @@ -44,12 +46,12 @@ Value CreateString(const CallbackInfo& info) { Value CheckString(const CallbackInfo& info) { String value = info[0].As(); String encoding = info[1].As(); - Number length = info[2].As(); + Value length = info[2]; if (encoding.Utf8Value() == "utf8") { std::string testValue = testValueUtf8; if (!length.IsUndefined()) { - testValue = testValue.substr(0, length.Uint32Value()); + testValue = testValue.substr(0, length.As().Uint32Value()); } std::string stringValue = value; @@ -57,7 +59,7 @@ Value CheckString(const CallbackInfo& info) { } else if (encoding.Utf8Value() == "utf16") { std::u16string testValue = testValueUtf16; if (!length.IsUndefined()) { - testValue = testValue.substr(0, length.Uint32Value()); + testValue = testValue.substr(0, length.As().Uint32Value()); } std::u16string stringValue = value; @@ -69,10 +71,10 @@ Value CheckString(const CallbackInfo& info) { } Value CreateSymbol(const CallbackInfo& info) { - String description = info[0].As(); + Value description = info[0]; if (!description.IsUndefined()) { - return Symbol::New(info.Env(), description); + return Symbol::New(info.Env(), description.As()); } else { return Symbol::New(info.Env()); } diff --git a/test/object/delete_property.cc b/test/object/delete_property.cc index ca69e5387..b05af20cc 100644 --- a/test/object/delete_property.cc +++ b/test/object/delete_property.cc @@ -4,13 +4,13 @@ using namespace Napi; Value DeletePropertyWithUint32(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Number key = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrap(obj.Delete(key.Uint32Value()))); } Value DeletePropertyWithNapiValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New( info.Env(), @@ -18,20 +18,20 @@ Value DeletePropertyWithNapiValue(const CallbackInfo& info) { } Value DeletePropertyWithNapiWrapperValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Delete(key), false)); } Value DeletePropertyWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New( info.Env(), MaybeUnwrapOr(obj.Delete(jsKey.Utf8Value().c_str()), false)); } Value DeletePropertyWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Delete(jsKey.Utf8Value()), false)); diff --git a/test/object/get_property.cc b/test/object/get_property.cc index 523f99199..2791ad2aa 100644 --- a/test/object/get_property.cc +++ b/test/object/get_property.cc @@ -4,31 +4,31 @@ using namespace Napi; Value GetPropertyWithNapiValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return MaybeUnwrapOr(obj.Get(static_cast(key)), Value()); } Value GetPropertyWithNapiWrapperValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return MaybeUnwrapOr(obj.Get(key), Value()); } Value GetPropertyWithUint32(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Number key = info[1].As(); return MaybeUnwrap(obj.Get(key.Uint32Value())); } Value GetPropertyWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return MaybeUnwrapOr(obj.Get(jsKey.Utf8Value().c_str()), Value()); } Value GetPropertyWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return MaybeUnwrapOr(obj.Get(jsKey.Utf8Value()), Value()); } diff --git a/test/object/has_own_property.cc b/test/object/has_own_property.cc index d7fbde98b..b566fefbb 100644 --- a/test/object/has_own_property.cc +++ b/test/object/has_own_property.cc @@ -4,7 +4,7 @@ using namespace Napi; Value HasOwnPropertyWithNapiValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New( info.Env(), @@ -12,14 +12,14 @@ Value HasOwnPropertyWithNapiValue(const CallbackInfo& info) { } Value HasOwnPropertyWithNapiWrapperValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.HasOwnProperty(key), false)); } Value HasOwnPropertyWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New( info.Env(), @@ -27,7 +27,7 @@ Value HasOwnPropertyWithCStyleString(const CallbackInfo& info) { } Value HasOwnPropertyWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New( info.Env(), MaybeUnwrapOr(obj.HasOwnProperty(jsKey.Utf8Value()), false)); diff --git a/test/object/has_property.cc b/test/object/has_property.cc index fa410833f..46c13de30 100644 --- a/test/object/has_property.cc +++ b/test/object/has_property.cc @@ -4,34 +4,34 @@ using namespace Napi; Value HasPropertyWithNapiValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New( info.Env(), MaybeUnwrapOr(obj.Has(static_cast(key)), false)); } Value HasPropertyWithNapiWrapperValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Has(key), false)); } Value HasPropertyWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Has(jsKey.Utf8Value().c_str()), false)); } Value HasPropertyWithUint32(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Number jsKey = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Has(jsKey.Uint32Value()), false)); } Value HasPropertyWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Has(jsKey.Utf8Value()), false)); diff --git a/test/object/object.cc b/test/object/object.cc index f6f0bd98b..be0bcadde 100644 --- a/test/object/object.cc +++ b/test/object/object.cc @@ -338,7 +338,7 @@ void Increment(const CallbackInfo& info) { #endif // NAPI_CPP_EXCEPTIONS Value InstanceOf(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Function constructor = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrap(obj.InstanceOf(constructor))); } diff --git a/test/object/set_property.cc b/test/object/set_property.cc index 8171568d6..da8c93bbd 100644 --- a/test/object/set_property.cc +++ b/test/object/set_property.cc @@ -4,7 +4,7 @@ using namespace Napi; Value SetPropertyWithNapiValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); Value value = info[2]; return Boolean::New( @@ -13,14 +13,14 @@ Value SetPropertyWithNapiValue(const CallbackInfo& info) { } Value SetPropertyWithNapiWrapperValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); Value value = info[2]; return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Set(key, value), false)); } Value SetPropertyWithUint32(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Number key = info[1].As(); Value value = info[2]; return Boolean::New(info.Env(), @@ -28,7 +28,7 @@ Value SetPropertyWithUint32(const CallbackInfo& info) { } Value SetPropertyWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); Value value = info[2]; return Boolean::New( @@ -37,7 +37,7 @@ Value SetPropertyWithCStyleString(const CallbackInfo& info) { } Value SetPropertyWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); Value value = info[2]; return Boolean::New(info.Env(), diff --git a/test/object/subscript_operator.cc b/test/object/subscript_operator.cc index 7f94b2dd3..15bb74620 100644 --- a/test/object/subscript_operator.cc +++ b/test/object/subscript_operator.cc @@ -8,7 +8,7 @@ Value SubscriptGetWithCStyleString(const CallbackInfo& info) { // make sure const case compiles const Object obj2 = info[0].As(); - MaybeUnwrap(obj2[jsKey.Utf8Value().c_str()]).As(); + MaybeUnwrap(obj2[jsKey.Utf8Value().c_str()]).As(); Object obj = info[0].As(); return obj[jsKey.Utf8Value().c_str()]; @@ -19,7 +19,7 @@ Value SubscriptGetWithCppStyleString(const CallbackInfo& info) { // make sure const case compiles const Object obj2 = info[0].As(); - MaybeUnwrap(obj2[jsKey.Utf8Value()]).As(); + MaybeUnwrap(obj2[jsKey.Utf8Value()]).As(); Object obj = info[0].As(); return obj[jsKey.Utf8Value()]; @@ -30,7 +30,7 @@ Value SubscriptGetAtIndex(const CallbackInfo& info) { // make sure const case compiles const Object obj2 = info[0].As(); - MaybeUnwrap(obj2[index]).As(); + MaybeUnwrap(obj2[index]).As(); Object obj = info[0].As(); return obj[index]; diff --git a/test/object_reference.cc b/test/object_reference.cc index 2d3fc4807..e74b14b2c 100644 --- a/test/object_reference.cc +++ b/test/object_reference.cc @@ -224,13 +224,13 @@ void SetCastedObjects(const CallbackInfo& info) { Value GetFromValue(const CallbackInfo& info) { Env env = info.Env(); - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { if (weak.IsEmpty()) { return String::New(env, "No Referenced Value"); } else { return weak.Value(); } - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { return persistent.Value(); } else { return reference.Value(); @@ -290,7 +290,7 @@ Value GetFromGetters(const CallbackInfo& info) { Value GetFromGetter(const CallbackInfo& info) { Env env = info.Env(); - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { if (weak.IsEmpty()) { return String::New(env, "No Referenced Value"); } else { @@ -300,7 +300,7 @@ Value GetFromGetter(const CallbackInfo& info) { return MaybeUnwrap(weak.Get(info[1].As().Uint32Value())); } } - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { if (info[1].IsString()) { return MaybeUnwrap(persistent.Get(info[1].As().Utf8Value())); } else if (info[1].IsNumber()) { @@ -322,13 +322,13 @@ Value GetFromGetter(const CallbackInfo& info) { Value GetCastedFromValue(const CallbackInfo& info) { Env env = info.Env(); - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { if (casted_weak.IsEmpty()) { return String::New(env, "No Referenced Value"); } else { return casted_weak.Value(); } - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { return casted_persistent.Value(); } else { return casted_reference.Value(); @@ -341,13 +341,13 @@ Value GetCastedFromValue(const CallbackInfo& info) { Value GetCastedFromGetter(const CallbackInfo& info) { Env env = info.Env(); - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { if (casted_weak.IsEmpty()) { return String::New(env, "No Referenced Value"); } else { return MaybeUnwrap(casted_weak.Get(info[1].As())); } - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { return MaybeUnwrap(casted_persistent.Get(info[1].As())); } else { return MaybeUnwrap(casted_reference.Get(info[1].As())); @@ -360,15 +360,15 @@ Number UnrefObjects(const CallbackInfo& info) { Env env = info.Env(); uint32_t num; - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { num = weak.Unref(); - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { num = persistent.Unref(); - } else if (info[0].As() == String::New(env, "references")) { + } else if (info[0] == String::New(env, "references")) { num = reference.Unref(); - } else if (info[0].As() == String::New(env, "casted weak")) { + } else if (info[0] == String::New(env, "casted weak")) { num = casted_weak.Unref(); - } else if (info[0].As() == String::New(env, "casted persistent")) { + } else if (info[0] == String::New(env, "casted persistent")) { num = casted_persistent.Unref(); } else { num = casted_reference.Unref(); @@ -383,15 +383,15 @@ Number RefObjects(const CallbackInfo& info) { Env env = info.Env(); uint32_t num; - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { num = weak.Ref(); - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { num = persistent.Ref(); - } else if (info[0].As() == String::New(env, "references")) { + } else if (info[0] == String::New(env, "references")) { num = reference.Ref(); - } else if (info[0].As() == String::New(env, "casted weak")) { + } else if (info[0] == String::New(env, "casted weak")) { num = casted_weak.Ref(); - } else if (info[0].As() == String::New(env, "casted persistent")) { + } else if (info[0] == String::New(env, "casted persistent")) { num = casted_persistent.Ref(); } else { num = casted_reference.Ref(); diff --git a/test/run_script.cc b/test/run_script.cc index cc7a74b71..e957df9eb 100644 --- a/test/run_script.cc +++ b/test/run_script.cc @@ -18,7 +18,7 @@ Value RunStdString(const CallbackInfo& info) { Value RunJsString(const CallbackInfo& info) { Env env = info.Env(); - return MaybeUnwrapOr(env.RunScript(info[0].As()), Value()); + return MaybeUnwrapOr(env.RunScript(info[0].UnsafeAs()), Value()); } Value RunWithContext(const CallbackInfo& info) { diff --git a/test/type_taggable.cc b/test/type_taggable.cc index 2320dd81c..ac58f9281 100644 --- a/test/type_taggable.cc +++ b/test/type_taggable.cc @@ -26,7 +26,7 @@ class TestTypeTaggable { static Value CheckTypeTag(const CallbackInfo& info) { uint32_t type_index = info[0].As().Int32Value(); - TypeTaggable instance = info[1].As(); + TypeTaggable instance = info[1].UnsafeAs(); return Boolean::New(info.Env(), instance.CheckTypeTag(&type_tags[type_index])); diff --git a/test/typedarray.cc b/test/typedarray.cc index 3ee9f3f2a..0b9e0970e 100644 --- a/test/typedarray.cc +++ b/test/typedarray.cc @@ -21,7 +21,7 @@ namespace { Value CreateTypedArray(const CallbackInfo& info) { std::string arrayType = info[0].As(); size_t length = info[1].As().Uint32Value(); - ArrayBuffer buffer = info[2].As(); + Value buffer = info[2]; size_t bufferOffset = info[3].IsUndefined() ? 0 : info[3].As().Uint32Value(); @@ -32,7 +32,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Int8Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_int8_array); } else if (arrayType == "uint8") { @@ -42,7 +42,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Uint8Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_uint8_array); } else if (arrayType == "uint8_clamped") { @@ -50,7 +50,7 @@ Value CreateTypedArray(const CallbackInfo& info) { ? Uint8Array::New(info.Env(), length, napi_uint8_clamped_array) : Uint8Array::New(info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_uint8_clamped_array); } else if (arrayType == "int16") { @@ -60,7 +60,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Int16Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_int16_array); } else if (arrayType == "uint16") { @@ -70,7 +70,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Uint16Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_uint16_array); } else if (arrayType == "int32") { @@ -80,7 +80,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Int32Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_int32_array); } else if (arrayType == "uint32") { @@ -90,7 +90,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Uint32Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_uint32_array); } else if (arrayType == "float32") { @@ -100,7 +100,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Float32Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_float32_array); } else if (arrayType == "float64") { @@ -110,7 +110,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Float64Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_float64_array); #if (NAPI_VERSION > 5) @@ -121,7 +121,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(BigInt64Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_bigint64_array); } else if (arrayType == "biguint64") { @@ -131,7 +131,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(BigUint64Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_biguint64_array); #endif @@ -208,8 +208,8 @@ Value CheckBufferContent(const CallbackInfo& info) { case napi_uint8_array: return Boolean::New( info.Env(), - TypedArrayDataIsEquivalent(info[0].As(), - info[1].As())); + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); case napi_uint8_clamped_array: return Boolean::New( @@ -335,35 +335,39 @@ Value GetTypedArrayElement(const CallbackInfo& info) { void SetTypedArrayElement(const CallbackInfo& info) { TypedArray array = info[0].As(); size_t index = info[1].As().Uint32Value(); - Number value = info[2].As(); + Value value = info[2]; switch (array.TypedArrayType()) { case napi_int8_array: - array.As()[index] = static_cast(value.Int32Value()); + array.As()[index] = + static_cast(value.As().Int32Value()); break; case napi_uint8_array: - array.As()[index] = static_cast(value.Uint32Value()); + array.As()[index] = + static_cast(value.As().Uint32Value()); break; case napi_uint8_clamped_array: - array.As()[index] = static_cast(value.Uint32Value()); + array.As()[index] = + static_cast(value.As().Uint32Value()); break; case napi_int16_array: - array.As()[index] = static_cast(value.Int32Value()); + array.As()[index] = + static_cast(value.As().Int32Value()); break; case napi_uint16_array: array.As()[index] = - static_cast(value.Uint32Value()); + static_cast(value.As().Uint32Value()); break; case napi_int32_array: - array.As()[index] = value.Int32Value(); + array.As()[index] = value.As().Int32Value(); break; case napi_uint32_array: - array.As()[index] = value.Uint32Value(); + array.As()[index] = value.As().Uint32Value(); break; case napi_float32_array: - array.As()[index] = value.FloatValue(); + array.As()[index] = value.As().FloatValue(); break; case napi_float64_array: - array.As()[index] = value.DoubleValue(); + array.As()[index] = value.As().DoubleValue(); break; #if (NAPI_VERSION > 5) case napi_bigint64_array: {