Skip to content

Commit 9864051

Browse files
author
ci_lynx
committed
[Feature] add napi adapter to adapt napi-rs library
Add an adapter to expose function symbols for napi - rs library integration, as PrimJS NAPI exposes pointers in structs, different from the original NAPI which directly exposes function symbols. The adapter will forward the corresponding calls to PrimJS NAPI. doc: https://bytedance.larkoffice.com/docx/BqISdsnClo6oqOx68YZc4NOpntg
1 parent bda3ebe commit 9864051

File tree

10 files changed

+1605
-32
lines changed

10 files changed

+1605
-32
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ set(napi_sources
168168
${CMAKE_CURRENT_SOURCE_DIR}/src/napi/env/napi_runtime.cc
169169
${CMAKE_CURRENT_SOURCE_DIR}/src/napi/quickjs/js_native_api_QuickJS.cc
170170
${CMAKE_CURRENT_SOURCE_DIR}/src/napi/napi_module.cc
171+
${CMAKE_CURRENT_SOURCE_DIR}/src/napi/js_native_api_adaptor.cc
171172
${CMAKE_CURRENT_SOURCE_DIR}/src/napi/napi.cc)
172173

173174
if(${ENABLE_CODECACHE})

src/napi/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ napi_source_set("napi") {
1919
public = [
2020
"js_native_api.h",
2121
"js_native_api_types.h",
22+
"js_native_api_adaptor.h",
2223
"napi.h",
2324
"napi_module.h",
2425
]
2526
sources = [
2627
"napi.cc",
2728
"napi_module.cc",
29+
"js_native_api_adaptor.cc"
2830
]
2931
public_deps = [ "./common:common" ]
3032
public_configs = [ ":napi_public_config" ]

src/napi/env/BUILD.gn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ napi_source_set("env") {
1818
}
1919

2020
napi_source_set("runtime") {
21-
sources = [ "napi_runtime.cc" ]
21+
sources = [
22+
"napi_runtime_internal.h",
23+
"napi_runtime.cc"
24+
]
2225
public = [ "napi_runtime.h" ]
2326
configs = [ ":config" ]
2427
}

src/napi/env/napi_runtime.cc

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -363,31 +363,32 @@ class Work : AutoCloseable {
363363
std::atomic_bool canceled_{false};
364364
};
365365

366-
napi_status napi_create_async_work(napi_env env, napi_value async_resource,
367-
napi_value async_resource_name,
368-
napi_async_execute_callback execute,
369-
napi_async_complete_callback complete,
370-
void* data, napi_async_work* result) {
366+
napi_status napi_create_async_work_internal(
367+
napi_env env, napi_value async_resource, napi_value async_resource_name,
368+
napi_async_execute_callback execute, napi_async_complete_callback complete,
369+
void* data, napi_async_work* result) {
371370
*result = reinterpret_cast<napi_async_work>(
372371
Work::New(env->rt, execute, complete, data));
373372
return napi_clear_last_error(env);
374373
}
375374

376-
napi_status napi_delete_async_work(napi_env env, napi_async_work work) {
375+
napi_status napi_delete_async_work_internal(napi_env env,
376+
napi_async_work work) {
377377
Work::Delete(reinterpret_cast<Work*>(work));
378378

379379
return napi_clear_last_error(env);
380380
}
381381

382-
napi_status napi_queue_async_work(napi_env env, napi_async_work work) {
382+
napi_status napi_queue_async_work_internal(napi_env env, napi_async_work work) {
383383
Work* w = reinterpret_cast<Work*>(work);
384384

385385
w->ScheduleWork();
386386

387387
return napi_clear_last_error(env);
388388
}
389389

390-
napi_status napi_cancel_async_work(napi_env env, napi_async_work work) {
390+
napi_status napi_cancel_async_work_internal(napi_env env,
391+
napi_async_work work) {
391392
Work* w = reinterpret_cast<Work*>(work);
392393

393394
if (!w->CancelWork()) {
@@ -599,7 +600,7 @@ class ThreadSafeFunction {
599600
std::shared_ptr<Mutex<SharedState>> state_;
600601
};
601602

602-
napi_status napi_create_threadsafe_function(
603+
napi_status napi_create_threadsafe_function_internal(
603604
napi_env env, void* thread_finalize_data, napi_finalize thread_finalize_cb,
604605
void* context, napi_threadsafe_function_call_js call_js_cb,
605606
napi_threadsafe_function* result) {
@@ -611,23 +612,6 @@ napi_status napi_create_threadsafe_function(
611612
return napi_clear_last_error(env);
612613
}
613614

614-
napi_status napi_get_threadsafe_function_context(napi_threadsafe_function func,
615-
void** result) {
616-
*result = reinterpret_cast<ThreadSafeFunction*>(func)->Context();
617-
return napi_ok;
618-
}
619-
620-
napi_status napi_call_threadsafe_function(
621-
napi_threadsafe_function func, void* data,
622-
napi_threadsafe_function_call_mode is_blocking) {
623-
return reinterpret_cast<ThreadSafeFunction*>(func)->Call(data, is_blocking);
624-
}
625-
626-
napi_status napi_delete_threadsafe_function(napi_threadsafe_function func) {
627-
ThreadSafeFunction::Delete(reinterpret_cast<ThreadSafeFunction*>(func));
628-
return napi_ok;
629-
}
630-
631615
class ErrorScope {
632616
public:
633617
explicit ErrorScope(napi_env env) : env_(env) {}
@@ -646,12 +630,14 @@ class ErrorScope {
646630
napi_env env_;
647631
};
648632

649-
napi_status napi_open_error_scope(napi_env env, napi_error_scope* result) {
633+
napi_status napi_open_error_scope_internal(napi_env env,
634+
napi_error_scope* result) {
650635
*result = reinterpret_cast<napi_error_scope>(new ErrorScope(env));
651636
return napi_ok;
652637
}
653638

654-
napi_status napi_close_error_scope(napi_env env, napi_error_scope scope) {
639+
napi_status napi_close_error_scope_internal(napi_env env,
640+
napi_error_scope scope) {
655641
delete reinterpret_cast<ErrorScope*>(scope);
656642
return napi_ok;
657643
}
@@ -714,6 +700,24 @@ napi_status napi_dump_code_cache_status(napi_env env, void* dump_vec) {
714700

715701
} // namespace
716702

703+
napi_status napi_get_threadsafe_function_context_internal(
704+
napi_threadsafe_function func, void** result) {
705+
*result = reinterpret_cast<ThreadSafeFunction*>(func)->Context();
706+
return napi_ok;
707+
}
708+
709+
napi_status napi_call_threadsafe_function_internal(
710+
napi_threadsafe_function func, void* data,
711+
napi_threadsafe_function_call_mode is_blocking) {
712+
return reinterpret_cast<ThreadSafeFunction*>(func)->Call(data, is_blocking);
713+
}
714+
715+
napi_status napi_delete_threadsafe_function_internal(
716+
napi_threadsafe_function func) {
717+
ThreadSafeFunction::Delete(reinterpret_cast<ThreadSafeFunction*>(func));
718+
return napi_ok;
719+
}
720+
717721
napi_runtime_configuration napi_create_runtime_configuration() {
718722
return new napi_runtime_configuration__{};
719723
}
@@ -756,9 +760,15 @@ void napi_attach_runtime_with_configuration(
756760
napi_env env, napi_runtime_configuration configuration) {
757761
env->rt = new napi_runtime__(env, configuration);
758762

763+
#define SET_INTERNAL_METHOD(API) env->napi_##API = napi_##API##_internal;
764+
765+
FOR_EACH_NAPI_RUNTIME_CALL(SET_INTERNAL_METHOD)
766+
767+
#undef SET_INTERNAL_METHOD
768+
759769
#define SET_METHOD(API) env->napi_##API = napi_##API;
760770

761-
FOR_EACH_NAPI_RUNTIME_CALL(SET_METHOD)
771+
NAPI_RUNTIME_CODECACHE_CALL(SET_METHOD)
762772

763773
#undef SET_METHOD
764774
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) 2017 Node.js API collaborators. All Rights Reserved.
3+
*
4+
* Use of this source code is governed by a MIT license that can be
5+
* found in the LICENSE file in the root of the source tree.
6+
*/
7+
8+
// Copyright 2025 The Lynx Authors. All rights reserved.
9+
// Licensed under the Apache License Version 2.0 that can be found in the
10+
// LICENSE file in the root directory of this source tree.
11+
12+
#ifndef SRC_NAPI_ENV_NAPI_RUNTIME_INTERNAL_H_
13+
#define SRC_NAPI_ENV_NAPI_RUNTIME_INTERNAL_H_
14+
15+
#include "js_native_api.h"
16+
#ifdef USE_PRIMJS_NAPI
17+
#include "primjs_napi_defines.h"
18+
#endif
19+
20+
napi_status napi_get_threadsafe_function_context_internal(
21+
napi_threadsafe_function func, void** result);
22+
23+
napi_status napi_call_threadsafe_function_internal(
24+
napi_threadsafe_function func, void* data,
25+
napi_threadsafe_function_call_mode is_blocking);
26+
27+
napi_status napi_delete_threadsafe_function_internal(
28+
napi_threadsafe_function func);
29+
30+
#ifdef USE_PRIMJS_NAPI
31+
#include "primjs_napi_undefs.h"
32+
#endif
33+
#endif // SRC_NAPI_ENV_NAPI_RUNTIME_INTERNAL_H_

src/napi/js_native_api.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,7 @@ struct napi_env__ {
648648
V(call_threadsafe_function) \
649649
V(delete_threadsafe_function) \
650650
V(open_error_scope) \
651-
V(close_error_scope) \
652-
NAPI_RUNTIME_CODECACHE_CALL(V)
651+
V(close_error_scope)
653652

654653
#define NAPI_ENV_CALL(API, ENV, ...) \
655654
napi_env(ENV)->napi_##API((ENV), __VA_ARGS__)

0 commit comments

Comments
 (0)