node-api: implement external strings

Introduce APIs that allow for the creation of JavaScript strings without
copying the underlying native string into the engine. The APIs fall back
to regular string creation if the engine's external string APIs are
unavailable. In this case, an optional boolean out-parameter indicates
that the string was copied, and the optional finalizer is called if
given.

PR-URL: https://github.com/nodejs/node/pull/48339
Fixes: https://github.com/nodejs/node/issues/48198
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Signed-off-by: Gabriel Schulhof <gabrielschulhof@gmail.com>
This commit is contained in:
Gabriel Schulhof authored and Ruy Adorno committed 2023-09-12 10:58:24 -04:00
1 parent a30f2fbcc1
commit feb6a54dc3
11 files changed
+724 -187

No files matched your search

@@ -2,17 +2,6 @@
#include "../../js-native-api/common.h"
#include "stdlib.h"
#define NODE_API_ASSERT_STATUS(env, assertion, message) \
NODE_API_ASSERT_BASE(env, assertion, message, napi_generic_failure)
#define NODE_API_CHECK_STATUS(env, the_call) \
do { \
napi_status status = (the_call); \
if (status != napi_ok) { \
return status; \
} \
} while (0)
static uint32_t finalizeCount = 0;
static void FreeData(napi_env env, void* data, void* hint) {
@@ -29,7 +18,7 @@ static napi_status GetArgValue(napi_env env,
napi_value* argValue) {
size_t argc = 1;
NODE_API_CHECK_STATUS(
env, napi_get_cb_info(env, info, &argc, argValue, NULL, NULL));
napi_get_cb_info(env, info, &argc, argValue, NULL, NULL));
NODE_API_ASSERT_STATUS(env, argc == 1, "Expects one arg.");
return napi_ok;
@@ -39,10 +28,10 @@ static napi_status GetArgValueAsIndex(napi_env env,
napi_callback_info info,
uint32_t* index) {
napi_value argValue;
NODE_API_CHECK_STATUS(env, GetArgValue(env, info, &argValue));
NODE_API_CHECK_STATUS(GetArgValue(env, info, &argValue));
napi_valuetype valueType;
NODE_API_CHECK_STATUS(env, napi_typeof(env, argValue, &valueType));
NODE_API_CHECK_STATUS(napi_typeof(env, argValue, &valueType));
NODE_API_ASSERT_STATUS(
env, valueType == napi_number, "Argument must be a number.");
@@ -53,10 +42,10 @@ static napi_status GetRef(napi_env env,
napi_callback_info info,
napi_ref* ref) {
uint32_t index;
NODE_API_CHECK_STATUS(env, GetArgValueAsIndex(env, info, &index));
NODE_API_CHECK_STATUS(GetArgValueAsIndex(env, info, &index));
napi_ref* refValues;
NODE_API_CHECK_STATUS(env, napi_get_instance_data(env, (void**)&refValues));
NODE_API_CHECK_STATUS(napi_get_instance_data(env, (void**)&refValues));
NODE_API_ASSERT_STATUS(env, refValues != NULL, "Cannot get instance data.");
*ref = refValues[index];