@@ -2042,7 +2042,6 @@ napi_status napi_instanceof(napi_env e,
2042
2042
*result = false ;
2043
2043
2044
2044
v8::Local<v8::Object> v8Cons;
2045
- v8::Local<v8::String> prototypeString;
2046
2045
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (e);
2047
2046
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
2048
2047
@@ -2054,6 +2053,49 @@ napi_status napi_instanceof(napi_env e,
2054
2053
return napi_set_last_error (napi_function_expected);
2055
2054
}
2056
2055
2056
+ napi_value value, key, jsRes;
2057
+ napi_status status;
2058
+ napi_valuetype valueType;
2059
+
2060
+ // Get "Symbol" from the global object
2061
+ status = napi_get_global (e, &value);
2062
+ if (status != napi_ok) return status;
2063
+ status = napi_create_string_utf8 (e, " Symbol" , 6 , &key);
2064
+ if (status != napi_ok) return status;
2065
+ status = napi_get_property (e, value, key, &value);
2066
+ if (status != napi_ok) return status;
2067
+ status = napi_get_type_of_value (e, value, &valueType);
2068
+ if (status != napi_ok) return status;
2069
+
2070
+ // Get "hasInstance" from Symbol
2071
+ if (valueType == napi_function) {
2072
+ status = napi_create_string_utf8 (e, " hasInstance" , 11 , &key);
2073
+ if (status != napi_ok) return status;
2074
+ status = napi_get_property (e, value, key, &value);
2075
+ if (status != napi_ok) return status;
2076
+ status = napi_get_type_of_value (e, value, &valueType);
2077
+ if (status != napi_ok) return status;
2078
+
2079
+ // Retrieve the function at the Symbol(hasInstance) key of the constructor
2080
+ if (valueType == napi_symbol) {
2081
+ status = napi_get_property (e, constructor, value, &value);
2082
+ if (status != napi_ok) return status;
2083
+ status = napi_get_type_of_value (e, value, &valueType);
2084
+
2085
+ // Call the function to determine whether the object is an instance of the
2086
+ // constructor
2087
+ if (valueType == napi_function) {
2088
+ status = napi_call_function (e, constructor, value, 1 , &object, &jsRes);
2089
+ if (status != napi_ok) return status;
2090
+ return napi_get_value_bool (e, jsRes, result);
2091
+ }
2092
+ }
2093
+ }
2094
+
2095
+ // If running constructor[Symbol.hasInstance](object) did not work, we perform
2096
+ // a traditional instanceof (early Node.js 6.x).
2097
+
2098
+ v8::Local<v8::String> prototypeString;
2057
2099
CHECK_NEW_FROM_UTF8 (isolate, prototypeString, " prototype" );
2058
2100
2059
2101
auto maybe = v8Cons->Get (context, prototypeString);
0 commit comments