11cb0ef41Sopenharmony_ci#include <assert.h> 21cb0ef41Sopenharmony_ci#define NAPI_EXPERIMENTAL 31cb0ef41Sopenharmony_ci#include <node_api.h> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#define NAPI_CALL(call) \ 61cb0ef41Sopenharmony_ci do { \ 71cb0ef41Sopenharmony_ci napi_status status = call; \ 81cb0ef41Sopenharmony_ci assert(status == napi_ok && #call " failed"); \ 91cb0ef41Sopenharmony_ci } while (0); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci#define EXPORT_FUNC(env, exports, name, func) \ 121cb0ef41Sopenharmony_ci do { \ 131cb0ef41Sopenharmony_ci napi_value js_func; \ 141cb0ef41Sopenharmony_ci NAPI_CALL(napi_create_function( \ 151cb0ef41Sopenharmony_ci (env), (name), NAPI_AUTO_LENGTH, (func), NULL, &js_func)); \ 161cb0ef41Sopenharmony_ci NAPI_CALL(napi_set_named_property((env), (exports), (name), js_func)); \ 171cb0ef41Sopenharmony_ci } while (0); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciconst char* one_byte_string = "The Quick Brown Fox Jumped Over The Lazy Dog."; 201cb0ef41Sopenharmony_ciconst char16_t* two_byte_string = 211cb0ef41Sopenharmony_ci u"The Quick Brown Fox Jumped Over The Lazy Dog."; 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci#define DECLARE_BINDING(CapName, lowercase_name, var_name) \ 241cb0ef41Sopenharmony_ci static napi_value CreateString##CapName(napi_env env, \ 251cb0ef41Sopenharmony_ci napi_callback_info info) { \ 261cb0ef41Sopenharmony_ci size_t argc = 4; \ 271cb0ef41Sopenharmony_ci napi_value argv[4]; \ 281cb0ef41Sopenharmony_ci uint32_t n; \ 291cb0ef41Sopenharmony_ci uint32_t index; \ 301cb0ef41Sopenharmony_ci napi_handle_scope scope; \ 311cb0ef41Sopenharmony_ci napi_value js_string; \ 321cb0ef41Sopenharmony_ci \ 331cb0ef41Sopenharmony_ci NAPI_CALL(napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); \ 341cb0ef41Sopenharmony_ci NAPI_CALL(napi_get_value_uint32(env, argv[0], &n)); \ 351cb0ef41Sopenharmony_ci NAPI_CALL(napi_open_handle_scope(env, &scope)); \ 361cb0ef41Sopenharmony_ci NAPI_CALL(napi_call_function(env, argv[1], argv[2], 0, NULL, NULL)); \ 371cb0ef41Sopenharmony_ci for (index = 0; index < n; index++) { \ 381cb0ef41Sopenharmony_ci NAPI_CALL(napi_create_string_##lowercase_name( \ 391cb0ef41Sopenharmony_ci env, (var_name), NAPI_AUTO_LENGTH, &js_string)); \ 401cb0ef41Sopenharmony_ci } \ 411cb0ef41Sopenharmony_ci NAPI_CALL(napi_call_function(env, argv[1], argv[3], 1, &argv[0], NULL)); \ 421cb0ef41Sopenharmony_ci NAPI_CALL(napi_close_handle_scope(env, scope)); \ 431cb0ef41Sopenharmony_ci \ 441cb0ef41Sopenharmony_ci return NULL; \ 451cb0ef41Sopenharmony_ci } 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciDECLARE_BINDING(Latin1, latin1, one_byte_string) 481cb0ef41Sopenharmony_ciDECLARE_BINDING(Utf8, utf8, one_byte_string) 491cb0ef41Sopenharmony_ciDECLARE_BINDING(Utf16, utf16, two_byte_string) 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ciNAPI_MODULE_INIT() { 521cb0ef41Sopenharmony_ci EXPORT_FUNC(env, exports, "createStringLatin1", CreateStringLatin1); 531cb0ef41Sopenharmony_ci EXPORT_FUNC(env, exports, "createStringUtf8", CreateStringUtf8); 541cb0ef41Sopenharmony_ci EXPORT_FUNC(env, exports, "createStringUtf16", CreateStringUtf16); 551cb0ef41Sopenharmony_ci return exports; 561cb0ef41Sopenharmony_ci} 57