|
13 | 13 | #include "third_party/weak-node-api/headers/weak_napi_defines.h" |
14 | 14 | #endif |
15 | 15 |
|
| 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 | + |
16 | 27 | // Structure to store instance data information |
17 | 28 | struct InstanceDataInfo { |
18 | 29 | void* data = nullptr; |
@@ -108,6 +119,39 @@ LYNX_EXTERN_C void lynx_napi_get_instance_data(napi_env env, uint64_t key, |
108 | 119 | } |
109 | 120 | } |
110 | 121 |
|
| 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 | + |
111 | 155 | namespace lynx { |
112 | 156 | namespace embedder { |
113 | 157 |
|
@@ -306,3 +350,40 @@ std::unique_ptr<pub::Value> LynxNativeModuleNAPI::GetAttributeValue( |
306 | 350 |
|
307 | 351 | } // namespace embedder |
308 | 352 | } // 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 |
0 commit comments