@@ -26,9 +26,11 @@ class napi_env__ {
26
26
explicit napi_env__ (v8::Isolate* _isolate): isolate(_isolate), last_error() {}
27
27
~napi_env__ () {
28
28
last_exception.Reset ();
29
+ has_instance.Reset ();
29
30
}
30
31
v8::Isolate* isolate;
31
32
v8::Persistent<v8::Value> last_exception;
33
+ v8::Persistent<v8::Value> has_instance;
32
34
napi_extended_error_info last_error;
33
35
};
34
36
@@ -2113,41 +2115,59 @@ napi_status napi_instanceof(napi_env env,
2113
2115
return napi_set_last_error (env, napi_function_expected);
2114
2116
}
2115
2117
2116
- napi_value value, js_result;
2118
+ napi_value value, js_result, has_instance = nullptr ;
2117
2119
napi_status status;
2118
2120
napi_valuetype value_type;
2119
2121
2120
- // Get "Symbol" from the global object
2121
- status = napi_get_global (env, &value);
2122
- if (status != napi_ok) return status;
2123
- status = napi_get_named_property (env, value, " Symbol" , &value);
2124
- if (status != napi_ok) return status;
2125
- status = napi_typeof (env, value, &value_type);
2126
- if (status != napi_ok) return status;
2127
-
2128
- // Get "hasInstance" from Symbol
2129
- if (value_type == napi_function) {
2130
- status = napi_get_named_property (env, value, " hasInstance" , &value);
2122
+ fprintf (stderr, " Checking env->has_instance\n " );
2123
+
2124
+ if (env->has_instance .IsEmpty ()) {
2125
+
2126
+ fprintf (stderr, " env->has_instance is empty\n " );
2127
+
2128
+ // Get "Symbol" from the global object
2129
+ status = napi_get_global (env, &value);
2130
+ if (status != napi_ok) return status;
2131
+ status = napi_get_named_property (env, value, " Symbol" , &value);
2131
2132
if (status != napi_ok) return status;
2132
2133
status = napi_typeof (env, value, &value_type);
2133
2134
if (status != napi_ok) return status;
2134
2135
2135
- // Retrieve the function at the Symbol( hasInstance) key of the constructor
2136
- if (value_type == napi_symbol ) {
2137
- status = napi_get_property (env, constructor, value , &value);
2136
+ // Get " hasInstance" from Symbol
2137
+ if (value_type == napi_function ) {
2138
+ status = napi_get_named_property (env, value, " hasInstance " , &value);
2138
2139
if (status != napi_ok) return status;
2139
2140
status = napi_typeof (env, value, &value_type);
2140
2141
if (status != napi_ok) return status;
2141
2142
2142
- // Call the function to determine whether the object is an instance of the
2143
- // constructor
2144
- if (value_type == napi_function) {
2145
- status = napi_call_function (env, constructor, value, 1 , &object,
2146
- &js_result);
2147
- if (status != napi_ok) return status;
2148
- return napi_get_value_bool (env, js_result, result);
2143
+ // If Symbol.hasInstance is of type symbol we've found it
2144
+ if (value_type == napi_symbol) {
2145
+ has_instance = value;
2146
+ env->has_instance .Reset (env->isolate ,
2147
+ v8impl::V8LocalValueFromJsValue (value));
2149
2148
}
2150
2149
}
2150
+ } else {
2151
+ fprintf (stderr, " env->has_instance is not empty\n " );
2152
+ has_instance = v8impl::JsValueFromV8LocalValue (
2153
+ v8::Local<v8::Value>::New (env->isolate , env->has_instance ));
2154
+ fprintf (stderr, " env->has_instance assigned from cache\n " );
2155
+ }
2156
+
2157
+ if (has_instance) {
2158
+ status = napi_get_property (env, constructor, value, &value);
2159
+ if (status != napi_ok) return status;
2160
+ status = napi_typeof (env, value, &value_type);
2161
+ if (status != napi_ok) return status;
2162
+
2163
+ // Call the function to determine whether the object is an instance of the
2164
+ // constructor
2165
+ if (value_type == napi_function) {
2166
+ status = napi_call_function (env, constructor, value, 1 , &object,
2167
+ &js_result);
2168
+ if (status != napi_ok) return status;
2169
+ return napi_get_value_bool (env, js_result, result);
2170
+ }
2151
2171
}
2152
2172
2153
2173
// If running constructor[Symbol.hasInstance](object) did not work, we perform
0 commit comments