Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ set(napi_sources
${CMAKE_CURRENT_SOURCE_DIR}/src/napi/env/napi_runtime.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/napi/quickjs/js_native_api_QuickJS.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/napi/napi_module.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/napi/js_native_api_adaptor.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/napi/napi.cc)

if(${ENABLE_CODECACHE})
Expand Down
2 changes: 2 additions & 0 deletions src/napi/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ napi_source_set("napi") {
public = [
"js_native_api.h",
"js_native_api_types.h",
"js_native_api_adaptor.h",
"napi.h",
"napi_module.h",
]
sources = [
"napi.cc",
"napi_module.cc",
"js_native_api_adaptor.cc"
]
public_deps = [ "./common:common" ]
public_configs = [ ":napi_public_config" ]
Expand Down
5 changes: 4 additions & 1 deletion src/napi/env/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ napi_source_set("env") {
}

napi_source_set("runtime") {
sources = [ "napi_runtime.cc" ]
sources = [
"napi_runtime_internal.h",
"napi_runtime.cc"
]
public = [ "napi_runtime.h" ]
configs = [ ":config" ]
}
68 changes: 39 additions & 29 deletions src/napi/env/napi_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,31 +363,32 @@ class Work : AutoCloseable {
std::atomic_bool canceled_{false};
};

napi_status napi_create_async_work(napi_env env, napi_value async_resource,
napi_value async_resource_name,
napi_async_execute_callback execute,
napi_async_complete_callback complete,
void* data, napi_async_work* result) {
napi_status napi_create_async_work_internal(
napi_env env, napi_value async_resource, napi_value async_resource_name,
napi_async_execute_callback execute, napi_async_complete_callback complete,
void* data, napi_async_work* result) {
*result = reinterpret_cast<napi_async_work>(
Work::New(env->rt, execute, complete, data));
return napi_clear_last_error(env);
}

napi_status napi_delete_async_work(napi_env env, napi_async_work work) {
napi_status napi_delete_async_work_internal(napi_env env,
napi_async_work work) {
Work::Delete(reinterpret_cast<Work*>(work));

return napi_clear_last_error(env);
}

napi_status napi_queue_async_work(napi_env env, napi_async_work work) {
napi_status napi_queue_async_work_internal(napi_env env, napi_async_work work) {
Work* w = reinterpret_cast<Work*>(work);

w->ScheduleWork();

return napi_clear_last_error(env);
}

napi_status napi_cancel_async_work(napi_env env, napi_async_work work) {
napi_status napi_cancel_async_work_internal(napi_env env,
napi_async_work work) {
Work* w = reinterpret_cast<Work*>(work);

if (!w->CancelWork()) {
Expand Down Expand Up @@ -599,7 +600,7 @@ class ThreadSafeFunction {
std::shared_ptr<Mutex<SharedState>> state_;
};

napi_status napi_create_threadsafe_function(
napi_status napi_create_threadsafe_function_internal(
napi_env env, void* thread_finalize_data, napi_finalize thread_finalize_cb,
void* context, napi_threadsafe_function_call_js call_js_cb,
napi_threadsafe_function* result) {
Expand All @@ -611,23 +612,6 @@ napi_status napi_create_threadsafe_function(
return napi_clear_last_error(env);
}

napi_status napi_get_threadsafe_function_context(napi_threadsafe_function func,
void** result) {
*result = reinterpret_cast<ThreadSafeFunction*>(func)->Context();
return napi_ok;
}

napi_status napi_call_threadsafe_function(
napi_threadsafe_function func, void* data,
napi_threadsafe_function_call_mode is_blocking) {
return reinterpret_cast<ThreadSafeFunction*>(func)->Call(data, is_blocking);
}

napi_status napi_delete_threadsafe_function(napi_threadsafe_function func) {
ThreadSafeFunction::Delete(reinterpret_cast<ThreadSafeFunction*>(func));
return napi_ok;
}

class ErrorScope {
public:
explicit ErrorScope(napi_env env) : env_(env) {}
Expand All @@ -646,12 +630,14 @@ class ErrorScope {
napi_env env_;
};

napi_status napi_open_error_scope(napi_env env, napi_error_scope* result) {
napi_status napi_open_error_scope_internal(napi_env env,
napi_error_scope* result) {
*result = reinterpret_cast<napi_error_scope>(new ErrorScope(env));
return napi_ok;
}

napi_status napi_close_error_scope(napi_env env, napi_error_scope scope) {
napi_status napi_close_error_scope_internal(napi_env env,
napi_error_scope scope) {
delete reinterpret_cast<ErrorScope*>(scope);
return napi_ok;
}
Expand Down Expand Up @@ -714,6 +700,24 @@ napi_status napi_dump_code_cache_status(napi_env env, void* dump_vec) {

} // namespace

napi_status napi_get_threadsafe_function_context_internal(
napi_threadsafe_function func, void** result) {
*result = reinterpret_cast<ThreadSafeFunction*>(func)->Context();
return napi_ok;
}

napi_status napi_call_threadsafe_function_internal(
napi_threadsafe_function func, void* data,
napi_threadsafe_function_call_mode is_blocking) {
return reinterpret_cast<ThreadSafeFunction*>(func)->Call(data, is_blocking);
}

napi_status napi_delete_threadsafe_function_internal(
napi_threadsafe_function func) {
ThreadSafeFunction::Delete(reinterpret_cast<ThreadSafeFunction*>(func));
return napi_ok;
}

napi_runtime_configuration napi_create_runtime_configuration() {
return new napi_runtime_configuration__{};
}
Expand Down Expand Up @@ -756,9 +760,15 @@ void napi_attach_runtime_with_configuration(
napi_env env, napi_runtime_configuration configuration) {
env->rt = new napi_runtime__(env, configuration);

#define SET_INTERNAL_METHOD(API) env->napi_##API = napi_##API##_internal;

FOR_EACH_NAPI_RUNTIME_CALL(SET_INTERNAL_METHOD)

#undef SET_INTERNAL_METHOD

#define SET_METHOD(API) env->napi_##API = napi_##API;

FOR_EACH_NAPI_RUNTIME_CALL(SET_METHOD)
NAPI_RUNTIME_CODECACHE_CALL(SET_METHOD)

#undef SET_METHOD
}
Expand Down
33 changes: 33 additions & 0 deletions src/napi/env/napi_runtime_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2017 Node.js API collaborators. All Rights Reserved.
*
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file in the root of the source tree.
*/

// Copyright 2025 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

#ifndef SRC_NAPI_ENV_NAPI_RUNTIME_INTERNAL_H_
#define SRC_NAPI_ENV_NAPI_RUNTIME_INTERNAL_H_

#include "js_native_api.h"
#ifdef USE_PRIMJS_NAPI
#include "primjs_napi_defines.h"
#endif

napi_status napi_get_threadsafe_function_context_internal(
napi_threadsafe_function func, void** result);

napi_status napi_call_threadsafe_function_internal(
napi_threadsafe_function func, void* data,
napi_threadsafe_function_call_mode is_blocking);

napi_status napi_delete_threadsafe_function_internal(
napi_threadsafe_function func);

#ifdef USE_PRIMJS_NAPI
#include "primjs_napi_undefs.h"
#endif
#endif // SRC_NAPI_ENV_NAPI_RUNTIME_INTERNAL_H_
3 changes: 1 addition & 2 deletions src/napi/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,7 @@ struct napi_env__ {
V(call_threadsafe_function) \
V(delete_threadsafe_function) \
V(open_error_scope) \
V(close_error_scope) \
NAPI_RUNTIME_CODECACHE_CALL(V)
V(close_error_scope)

#define NAPI_ENV_CALL(API, ENV, ...) \
napi_env(ENV)->napi_##API((ENV), __VA_ARGS__)
Expand Down
Loading
Loading