Skip to content

Commit 641ae5e

Browse files
committed
fix(runtime): preserve native engine lane boundaries
1 parent 847c54c commit 641ae5e

8 files changed

Lines changed: 35 additions & 35 deletions

File tree

NativeScript/jsi/jsc/JSCRuntime.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,6 @@ class Runtime {
483483
const void* identity() const { return state_.get(); }
484484
void detachState() { state_.reset(); }
485485

486-
// A stable, per-runtime identity.
487-
//
488-
// engine::Runtime is a value wrapper around shared engine state, and the
489-
// host-function trampolines construct a fresh one on the stack for every
490-
// callback. So `&runtime` is NOT stable and must never be used as a map key;
491-
// this is. The pointer is opaque and only ever compared or hashed.
492-
const void* identity() const { return state_.get(); }
493-
494486
// See RuntimeState::nativeStateKey. Created on first use.
495487
JSStringRef nativeStateKey() const {
496488
if (state_->nativeStateKey == nullptr) {

NativeScript/jsi/v8/V8HostObjects.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,6 @@ Function Function::createFromHostConstructor(Runtime& runtime, const PropNameID&
487487
info.GetReturnValue().Set(result.local(runtime));
488488
} catch (const JSError& error) {
489489
v8engine::throwV8Exception(info.GetIsolate(), error);
490-
} catch (const JSError& error) {
491-
v8engine::throwV8Exception(info.GetIsolate(), error);
492490
} catch (const std::exception& exception) {
493491
v8engine::throwV8Exception(info.GetIsolate(), exception);
494492
}

NativeScript/jsi/v8/V8Runtime.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,6 @@ class Runtime {
533533
return state_->nativeStateKey.Get(state_->isolate);
534534
}
535535
std::shared_ptr<v8engine::RuntimeState> state() const { return state_; }
536-
const void* identity() const { return state_.get(); }
537536

538537
Object global();
539538

NativeScript/napi/common/native_api_util.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ struct char_traits<unsigned short> {
6666
#include <android/log.h>
6767
#endif
6868

69-
#ifndef NAPI_PREAMBLE
70-
#define NAPI_PREAMBLE napi_status status;
71-
#endif
72-
7369
#ifndef NS_NAPI_PREAMBLE
7470
#define NS_NAPI_PREAMBLE napi_status status;
7571
#endif

NativeScript/napi/quickjs/jsr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ napi_status js_unlock_env(napi_env env) {
5555

5656
napi_status js_free_napi_env(napi_env env) {
5757
JSR* jsr = JSR::env_to_jsr_cache.Get(env);
58+
#ifndef __ANDROID__
59+
// Implemented by the Apple thread-safe-function layer; Android does not
60+
// provide environment cleanup hooks for this backend.
5861
js_run_env_cleanup_hooks(env);
62+
#endif
5963
JSR::env_to_jsr_cache.Remove(env);
6064
delete jsr;
6165
return qjs_free_napi_env(env);

NativeScript/runtime/modules/url/URLPattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ napi_value URLPattern::Exec(napi_env env, napi_callback_info info) {
496496
}
497497

498498
void URLPattern::Init(napi_env env, napi_value global) {
499-
NAPI_PREAMBLE
499+
NS_NAPI_PREAMBLE
500500
napi_value ctor;
501501
static const int instance_prop_count = 11;
502502
napi_property_descriptor properties[instance_prop_count] = {

platforms/android/test-app/runtime/CMakeLists.txt

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,14 @@ if (QUICKJS OR QUICKJS_NG)
216216
${QJS_SOURCE_DIR}/libunicode.c
217217
${QJS_SOURCE_DIR}/quickjs.c
218218
${QJS_SOURCE_DIR}/dtoa.c
219-
# napi
220-
${VENDOR_DIR}/quickjs/quickjs-api.c
221-
${NS_NAPI_DIR}/quickjs/jsr.cpp
222219
)
223220

221+
if (NS_BINDING STREQUAL "napi")
222+
set(SOURCES ${SOURCES}
223+
${VENDOR_DIR}/quickjs/quickjs-api.c
224+
${NS_NAPI_DIR}/quickjs/jsr.cpp)
225+
endif ()
226+
224227
include_directories(
225228
${NS_NAPI_DIR}/quickjs
226229
${NS_NAPI_DIR}/common
@@ -269,9 +272,9 @@ if (HERMES OR SHERMES)
269272
${NS_NAPI_DIR}/hermes
270273
${NS_NAPI_DIR}/common
271274
)
272-
set(SOURCES ${SOURCES}
273-
${NS_NAPI_DIR}/hermes/jsr.cpp
274-
)
275+
if (NS_BINDING STREQUAL "napi")
276+
set(SOURCES ${SOURCES} ${NS_NAPI_DIR}/hermes/jsr.cpp)
277+
endif ()
275278
endif ()
276279

277280
if (JSC)
@@ -284,10 +287,11 @@ if (JSC)
284287
# framework, whose JSC is a different build with a different ABI.
285288
${VENDOR_DIR}/jsc/include
286289
)
287-
set(SOURCES ${SOURCES}
288-
${VENDOR_DIR}/jsc/jsc-api.cpp
289-
${NS_NAPI_DIR}/jsc/jsr.cpp
290-
)
290+
if (NS_BINDING STREQUAL "napi")
291+
set(SOURCES ${SOURCES}
292+
${VENDOR_DIR}/jsc/jsc-api.cpp
293+
${NS_NAPI_DIR}/jsc/jsr.cpp)
294+
endif ()
291295
endif ()
292296

293297
if (V8)
@@ -329,11 +333,12 @@ if (V8)
329333
endif ()
330334
endif ()
331335

332-
set(SOURCES ${SOURCES}
333-
${VENDOR_DIR}/v8/v8-api.cpp
334-
${NS_NAPI_DIR}/v8/jsr.cpp
335-
${VENDOR_DIR}/v8/SimpleAllocator.cpp
336-
)
336+
if (NS_BINDING STREQUAL "napi")
337+
set(SOURCES ${SOURCES}
338+
${VENDOR_DIR}/v8/v8-api.cpp
339+
${NS_NAPI_DIR}/v8/jsr.cpp
340+
${VENDOR_DIR}/v8/SimpleAllocator.cpp)
341+
endif ()
337342

338343

339344
set(COMMON_CMAKE_ARGUMENTS "${COMMON_CMAKE_ARGUMENTS} -DV8_ENABLE_REGEXP_INTERPRETER_THREADED_DISPATCH -DV8_EMBEDDED_BUILTINS")

vendor/jsc/jsc-api.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ class JSString {
5959

6060
JSString(const JSChar* string, size_t length = NAPI_AUTO_LENGTH)
6161
: _string{JSStringCreateWithCharacters(
62-
string, length == NAPI_AUTO_LENGTH
63-
? std::char_traits<JSChar>::length(string)
64-
: length)} {}
62+
string, length == NAPI_AUTO_LENGTH ? NullTerminatedLength(string)
63+
: length)} {}
6564

6665
~JSString() {
6766
if (_string != nullptr) {
@@ -118,6 +117,13 @@ class JSString {
118117
}
119118

120119
private:
120+
static size_t NullTerminatedLength(const JSChar* string) {
121+
if (string == nullptr) return 0;
122+
const JSChar* end = string;
123+
while (*end != 0) ++end;
124+
return static_cast<size_t>(end - string);
125+
}
126+
121127
static JSStringRef CreateUTF8(const char* string, size_t length) {
122128
if (length == NAPI_AUTO_LENGTH) {
123129
return JSStringCreateWithUTF8CString(string);
@@ -3178,4 +3184,4 @@ napi_status napi_get_instance_data(napi_env env, void** data) {
31783184
return napi_ok;
31793185
}
31803186

3181-
#endif // USE_HOST_OBJECT
3187+
#endif // USE_HOST_OBJECT

0 commit comments

Comments
 (0)