| 231 | } |
| 232 | |
| 233 | inline napi_status FindWrapper(napi_env env, JsValueRef obj, JsValueRef* wrapper, JsValueRef* parent = nullptr) { |
| 234 | // Search the object's prototype chain for the wrapper with external data. |
| 235 | // Usually the wrapper would be the first in the chain, but it is OK for |
| 236 | // other objects to be inserted in the prototype chain. |
| 237 | JsValueRef candidate = obj; |
| 238 | JsValueRef current = JS_INVALID_REFERENCE; |
| 239 | bool hasExternalData = false; |
| 240 | |
| 241 | JsValueRef nullValue = JS_INVALID_REFERENCE; |
| 242 | CHECK_JSRT(env, JsGetNullValue(&nullValue)); |
| 243 | |
| 244 | do { |
| 245 | current = candidate; |
| 246 | |
| 247 | CHECK_JSRT(env, JsGetPrototype(current, &candidate)); |
| 248 | if (candidate == JS_INVALID_REFERENCE || candidate == nullValue) { |
| 249 | if (parent != nullptr) { |
| 250 | *parent = JS_INVALID_REFERENCE; |
| 251 | } |
| 252 | |
| 253 | *wrapper = JS_INVALID_REFERENCE; |
| 254 | return napi_ok; |
| 255 | } |
| 256 | |
| 257 | CHECK_JSRT(env, JsHasExternalData(candidate, &hasExternalData)); |
| 258 | } while (!hasExternalData); |
| 259 | |
| 260 | if (parent != nullptr) { |
| 261 | *parent = current; |
| 262 | } |
| 263 | |
| 264 | *wrapper = candidate; |
| 265 | |
| 266 | return napi_ok; |
| 267 | } |
| 268 | |
| 269 | inline napi_status Unwrap(napi_env env, JsValueRef obj, ExternalData** externalData, JsValueRef* wrapper = nullptr, JsValueRef* parent = nullptr) { |
| 270 | JsValueRef candidate = JS_INVALID_REFERENCE; |