Skip to content

Commit 9aecc72

Browse files
ci_lynxjianliang00
authored andcommitted
[Feature][Desktop] Add Lynx-specific NAPI helper APIs
- Add public CAPI declarations and native module implementations for running script content with filename metadata and for reporting then clearing pending exceptions. - Expose these helpers as Lynx-specific extensions because the standard NAPI surface does not provide the embedder hooks needed for these workflows. - Verified by reviewing the staged diff and ensuring the changes are limited to the new CAPI surface and its implementation. SkipChecks: macro
1 parent 731f26e commit 9aecc72

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

platform/embedder/module/lynx_native_module_napi.cc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
#include "third_party/weak-node-api/headers/weak_napi_defines.h"
1414
#endif
1515

16+
namespace lynx {
17+
namespace binding {
18+
void ReportRawException(void* raw_env, void* raw_exception);
19+
}
20+
} // namespace lynx
21+
namespace {
22+
23+
napi_status RunScriptFromFile(napi_env env, const char* script, size_t length,
24+
const char* filename, napi_value* result);
25+
} // namespace
26+
1627
// Structure to store instance data information
1728
struct InstanceDataInfo {
1829
void* data = nullptr;
@@ -108,6 +119,39 @@ LYNX_EXTERN_C void lynx_napi_get_instance_data(napi_env env, uint64_t key,
108119
}
109120
}
110121

122+
LYNX_EXTERN_C napi_status lynx_napi_run_script_from_file(napi_env env,
123+
const char* script,
124+
size_t length,
125+
const char* filename,
126+
napi_value* result) {
127+
if (env == nullptr || script == nullptr) {
128+
return napi_invalid_arg;
129+
}
130+
const char* safe_filename = filename != nullptr ? filename : "";
131+
napi_value unused_result = nullptr;
132+
napi_value* safe_result = result != nullptr ? result : &unused_result;
133+
return RunScriptFromFile(env, script, length, safe_filename, safe_result);
134+
}
135+
136+
LYNX_EXTERN_C void lynx_napi_report_and_clear_exception(napi_env env) {
137+
if (env == nullptr) {
138+
return;
139+
}
140+
bool has_exception = false;
141+
napi_status status = napi_is_exception_pending(env, &has_exception);
142+
if (status != napi_ok || !has_exception) {
143+
return;
144+
}
145+
146+
napi_value exception = nullptr;
147+
status = napi_get_and_clear_last_exception(env, &exception);
148+
if (status != napi_ok || exception == nullptr) {
149+
return;
150+
}
151+
152+
lynx::binding::ReportRawException(env, exception);
153+
}
154+
111155
namespace lynx {
112156
namespace embedder {
113157

@@ -306,3 +350,40 @@ std::unique_ptr<pub::Value> LynxNativeModuleNAPI::GetAttributeValue(
306350

307351
} // namespace embedder
308352
} // namespace lynx
353+
354+
// Since the standard napi run_script interface does not support the filename
355+
// parameter, we need to use the lower-level primjs napi_run_script interface.
356+
// Since both weak-node-api and primjs napi symbols appear here, and their
357+
// symbols may be renamed, macros are needed to handle various cases.
358+
#ifdef USE_WEAK_SUFFIX_NAPI
359+
#include "third_party/weak-node-api/headers/weak_napi_undefs.h"
360+
#endif
361+
#include "third_party/napi/include/js_native_api.h"
362+
363+
namespace {
364+
#ifdef USE_WEAK_SUFFIX_NAPI
365+
napi_status_weak RunScriptFromFile(napi_env_weak env, const char* script,
366+
size_t length, const char* filename,
367+
napi_value_weak* result) {
368+
#else
369+
napi_status RunScriptFromFile(napi_env env, const char* script, size_t length,
370+
const char* filename, napi_value* result) {
371+
#endif
372+
#ifdef USE_PRIMJS_NAPI
373+
napi_env_primjs env_primjs = reinterpret_cast<napi_env_primjs>(env);
374+
napi_value_primjs* result_primjs =
375+
reinterpret_cast<napi_value_primjs*>(result);
376+
#else
377+
napi_env env_primjs = reinterpret_cast<napi_env>(env);
378+
napi_value* result_primjs = reinterpret_cast<napi_value*>(result);
379+
#endif
380+
381+
#ifdef USE_WEAK_SUFFIX_NAPI
382+
return static_cast<napi_status_weak>(env_primjs->napi_run_script(
383+
env_primjs, script, length, filename, result_primjs));
384+
#else
385+
return static_cast<napi_status>(env_primjs->napi_run_script(
386+
env_primjs, script, length, filename, result_primjs));
387+
#endif
388+
}
389+
} // namespace

platform/embedder/public/capi/lynx_native_module_capi.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ LYNX_CAPI_EXPORT void lynx_napi_set_instance_data(napi_env env, uint64_t key,
2727
LYNX_CAPI_EXPORT void lynx_napi_get_instance_data(napi_env env, uint64_t key,
2828
void** data);
2929

30+
// Runs a script from the provided source content in the given NAPI
31+
// environment. This is not part of the standard NAPI surface, but Lynx needs
32+
// to expose it as an embedder-specific helper for loading and evaluating script
33+
// content with explicit source metadata. `filename` is used for script
34+
// identification in diagnostics and stack traces.
35+
LYNX_CAPI_EXPORT napi_status
36+
lynx_napi_run_script_from_file(napi_env env, const char* script, size_t length,
37+
const char* filename, napi_value* result);
38+
39+
// Reports the current exception associated with the given NAPI environment and
40+
// then clears the recorded exception state. This is also a Lynx-specific
41+
// extension instead of a standard NAPI API, provided because the embedder needs
42+
// an explicit hook to consume and reset pending exception information.
43+
LYNX_CAPI_EXPORT void lynx_napi_report_and_clear_exception(napi_env env);
44+
3045
LYNX_EXTERN_C_END
3146

3247
#ifdef USE_WEAK_SUFFIX_NAPI

0 commit comments

Comments
 (0)