MCPcopy Create free account
hub / github.com/BabylonJS/BabylonNative / napi_wrap

Function napi_wrap

Dependencies/napi/source/js_native_api_chakra.cc:1639–1674  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1637}
1638
1639napi_status napi_wrap(napi_env env,
1640 napi_value js_object,
1641 void* native_object,
1642 napi_finalize finalize_cb,
1643 void* finalize_hint,
1644 napi_ref* result) {
1645 CHECK_ENV(env);
1646 CHECK_ARG(env, js_object);
1647
1648 JsValueRef value = reinterpret_cast<JsValueRef>(js_object);
1649
1650 JsValueRef wrapper = JS_INVALID_REFERENCE;
1651 CHECK_NAPI(FindWrapper(env, value, &wrapper));
1652 RETURN_STATUS_IF_FALSE(env, wrapper == JS_INVALID_REFERENCE, napi_invalid_arg);
1653
1654 ExternalData* externalData = new ExternalData(
1655 env, native_object, finalize_cb, finalize_hint);
1656 if (externalData == nullptr) return napi_set_last_error(env, napi_generic_failure);
1657
1658 // Create an external object that will hold the external data pointer.
1659 JsValueRef external = JS_INVALID_REFERENCE;
1660 CHECK_JSRT(env, JsCreateExternalObject(
1661 externalData, ExternalData::Finalize, &external));
1662
1663 // Insert the external object into the value's prototype chain.
1664 JsValueRef valuePrototype = JS_INVALID_REFERENCE;
1665 CHECK_JSRT(env, JsGetPrototype(value, &valuePrototype));
1666 CHECK_JSRT(env, JsSetPrototype(external, valuePrototype));
1667 CHECK_JSRT(env, JsSetPrototype(value, external));
1668
1669 if (result != nullptr) {
1670 CHECK_NAPI(napi_create_reference(env, js_object, 0, result));
1671 }
1672
1673 return napi_ok;
1674}
1675
1676napi_status napi_unwrap(napi_env env, napi_value js_object, void** result) {
1677 CHECK_ENV(env);

Callers

nothing calls this directly

Calls 3

FindWrapperFunction · 0.85
napi_set_last_errorFunction · 0.70
napi_create_referenceFunction · 0.70

Tested by

no test coverage detected