Skip to content

Commit dee6757

Browse files
authored
Merge pull request #36 from NativeScript/support_qjs_bellard
quickjs: support multiple quickjs versions
2 parents 2029167 + ebcfa20 commit dee6757

File tree

114 files changed

+116230
-18227
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+116230
-18227
lines changed

scripts/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const { spawn } = require('child_process');
77
const path = require('path');
88
const readline = require('readline');
99

10-
const VALID_ENGINES = ['V8-10',"V8-11","V8-13", 'QUICKJS', 'HERMES', 'JSC', 'SHERMES', 'PRIMJS'];
11-
const HOST_OBJECTS_SUPPORTED = new Set(['V8-10','V8-11',"V8-13", 'QUICKJS', 'PRIMJS']);
10+
const VALID_ENGINES = ['V8-10',"V8-11","V8-13", 'QUICKJS', "QUICKJS_NG", 'HERMES', 'JSC', 'SHERMES', 'PRIMJS'];
11+
const HOST_OBJECTS_SUPPORTED = new Set(['V8-10','V8-11',"V8-13", 'QUICKJS',"QUICKJS_NG", 'PRIMJS']);
1212

1313
function parseArgs(argv) {
1414
const opts = {};

test-app/runtime/CMakeLists.txt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,32 @@ file(GLOB_RECURSE MODULE_FILES
9898

9999
set(SOURCES ${RUNTIME_FILES} ${MODULE_FILES})
100100

101-
if (QUICKJS)
101+
if (QUICKJS OR QUICKJS_NG)
102102
add_subdirectory(${PROJECT_SOURCE_DIR}/src/main/cpp/napi/quickjs/mimalloc-dev mimalloc)
103+
104+
if (QUICKJS_NG)
105+
set(QJS_SOURCE_DIR src/main/cpp/napi/quickjs/source_ng)
106+
else()
107+
set(QJS_SOURCE_DIR src/main/cpp/napi/quickjs/source)
108+
endif()
109+
103110
set(SOURCES ${SOURCES}
104111
# quickjs
105-
src/main/cpp/napi/quickjs/source/cutils.c
106-
src/main/cpp/napi/quickjs/source/libregexp.c
107-
src/main/cpp/napi/quickjs/source/libunicode.c
108-
src/main/cpp/napi/quickjs/source/quickjs.c
109-
src/main/cpp/napi/quickjs/source/dtoa.c
112+
${QJS_SOURCE_DIR}/cutils.c
113+
${QJS_SOURCE_DIR}/libregexp.c
114+
${QJS_SOURCE_DIR}/libunicode.c
115+
${QJS_SOURCE_DIR}/quickjs.c
116+
${QJS_SOURCE_DIR}/dtoa.c
110117
# napi
111118
src/main/cpp/napi/quickjs/quickjs-api.c
112119
src/main/cpp/napi/quickjs/jsr.cpp
113120

114121

115122
)
123+
116124
include_directories(
117125
src/main/cpp/napi/quickjs
118-
src/main/cpp/napi/quickjs/source
119-
src/main/cpp/napi/quickjs/napi-new
126+
${QJS_SOURCE_DIR}
120127
src/main/cpp/napi/common
121128
# mimalloc
122129
src/main/cpp/napi/quickjs/mimalloc-dev/include
@@ -325,8 +332,11 @@ if (PRIMJS)
325332
add_compile_definitions(NativeScript, PRIVATE __PRIMJS__)
326333
endif ()
327334

328-
if (QUICKJS)
335+
if (QUICKJS OR QUICKJS_NG)
329336
add_compile_definitions(NativeScript, PRIVATE __QJS__)
337+
if (QUICKJS_NG)
338+
add_compile_definitions(NativeScript, PRIVATE __QJS_NG__)
339+
endif ()
330340
if (USE_MIMALLOC)
331341
add_compile_definitions(NativeScript, PRIVATE USE_MIMALLOC)
332342
endif ()

test-app/runtime/build.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apply plugin: 'com.android.library'
22

3-
// can be: "V8-11", "V8-10","V8-13", "JSC", "HERMES", "QUICKJS", "SHERMES", "PRIMJS"
3+
// can be: "V8-11", "V8-10","V8-13", "JSC", "HERMES", "QUICKJS", "QUICKJS_NG", "SHERMES", "PRIMJS"
44

5-
def jsEngine = "V8-10"
5+
def jsEngine = "QUICKJS_NG"
66
def hasEngine = project.hasProperty("engine")
77
if (hasEngine) {
88
jsEngine = engine
@@ -133,9 +133,12 @@ android {
133133
arguments.add("-DIS_NAPI_MODULE=true")
134134
}
135135

136-
if (jsEngine == "QUICKJS") {
136+
if (jsEngine == "QUICKJS" || jsEngine == "QUICKJS_NG") {
137137
arguments.add("-DQUICKJS=1")
138138
arguments.add("-DUSE_MIMALLOC=1")
139+
if (jsEngine == "QUICKJS_NG") {
140+
arguments.add("-DQUICKJS_NG=1")
141+
}
139142
} else if (jsEngine == "HERMES") {
140143
arguments.add("-DHERMES=1")
141144
} else if (jsEngine == "SHERMES") {
@@ -216,7 +219,7 @@ android {
216219
exclude "**/libhermes.so"
217220
exclude "**/libjsi.so"
218221
}
219-
} else if (jsEngine == "QUICKJS" || jsEngine == "PRIMJS") {
222+
} else if (jsEngine == "QUICKJS" || jsEngine == "PRIMJS" || jsEngine == "QUICKJS_NG") {
220223
packagingOptions {
221224
exclude "**/libhermes.so"
222225
exclude "**/libjsc.so"

test-app/runtime/src/main/cpp/napi/quickjs/quickjs-api.c

Lines changed: 137 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include "js_native_api.h"
66
#include "quicks-runtime.h"
77

8+
#ifndef __QJS_NG__
9+
#include "cutils.h"
10+
#endif
11+
812
#ifdef __ANDROID__
913

1014
#include <android/log.h>
@@ -35,6 +39,8 @@
3539

3640
#ifdef USE_MIMALLOC
3741

42+
43+
#ifdef __QJS_NG__
3844
static void *js_mi_calloc(void *opaque, size_t count, size_t size) {
3945
return mi_calloc(count, size);
4046
}
@@ -60,6 +66,85 @@ static const JSMallocFunctions mi_mf = {
6066
js_mi_realloc,
6167
mi_malloc_usable_size
6268
};
69+
#endif
70+
71+
#ifndef __QJS_NG__
72+
73+
#if defined(__APPLE__)
74+
#define MALLOC_OVERHEAD 0
75+
#else
76+
#define MALLOC_OVERHEAD 8
77+
#endif
78+
79+
static void *js_def_malloc(JSMallocState *s, size_t size)
80+
{
81+
void *ptr;
82+
83+
/* Do not allocate zero bytes: behavior is platform dependent */
84+
assert(size != 0);
85+
86+
if (unlikely(s->malloc_size + size > s->malloc_limit))
87+
return NULL;
88+
89+
ptr = mi_malloc(size);
90+
if (!ptr)
91+
return NULL;
92+
93+
s->malloc_count++;
94+
s->malloc_size += mi_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
95+
return ptr;
96+
}
97+
98+
static void js_def_free(JSMallocState *s, void *ptr)
99+
{
100+
if (!ptr)
101+
return;
102+
103+
s->malloc_count--;
104+
s->malloc_size -= mi_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
105+
mi_free(ptr);
106+
}
107+
108+
static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
109+
{
110+
size_t old_size;
111+
112+
if (!ptr) {
113+
if (size == 0)
114+
return NULL;
115+
return js_def_malloc(s, size);
116+
}
117+
old_size = mi_malloc_usable_size(ptr);
118+
if (size == 0) {
119+
s->malloc_count--;
120+
s->malloc_size -= old_size + MALLOC_OVERHEAD;
121+
mi_free(ptr);
122+
return NULL;
123+
}
124+
if (s->malloc_size + size - old_size > s->malloc_limit)
125+
return NULL;
126+
127+
ptr = mi_realloc(ptr, size);
128+
if (!ptr)
129+
return NULL;
130+
131+
s->malloc_size += mi_malloc_usable_size(ptr) - old_size;
132+
return ptr;
133+
}
134+
135+
static const JSMallocFunctions mi_mf = {
136+
js_def_malloc,
137+
js_def_free,
138+
js_def_realloc,
139+
mi_malloc_usable_size,
140+
};
141+
142+
#endif
143+
144+
145+
146+
147+
63148

64149
#endif
65150

@@ -1507,7 +1592,11 @@ napi_status napi_get_array_length(napi_env env,
15071592

15081593
JSValue jsValue = *((JSValue *) value);
15091594

1595+
#ifdef __QJS_NG__
15101596
if (!JS_IsArray(jsValue))
1597+
#else
1598+
if (!JS_IsArray(env->context,jsValue))
1599+
#endif
15111600
return napi_set_last_error(env, napi_array_expected, NULL, 0, NULL);
15121601

15131602
int64_t length = 0;
@@ -1822,7 +1911,11 @@ napi_status napi_get_value_bigint_int64(napi_env env,
18221911
CHECK_ARG(value)
18231912
CHECK_ARG(result)
18241913

1825-
if (!JS_IsBigInt(*(JSValue *) value)) {
1914+
if (!JS_IsBigInt(
1915+
#ifndef __QJS_NG__
1916+
env->context,
1917+
#endif
1918+
*(JSValue *) value)) {
18261919
return napi_set_last_error(env, napi_bigint_expected, NULL, 0, NULL);
18271920
}
18281921

@@ -1839,7 +1932,11 @@ napi_status napi_get_value_bigint_uint64(napi_env env,
18391932
CHECK_ARG(value)
18401933
CHECK_ARG(result)
18411934

1842-
if (!JS_IsBigInt(*(JSValue *) value)) {
1935+
if (!JS_IsBigInt(
1936+
#ifndef __QJS_NG__
1937+
env->context,
1938+
#endif
1939+
*(JSValue *) value)) {
18431940
return napi_set_last_error(env, napi_bigint_expected, NULL, 0, NULL);
18441941
}
18451942

@@ -1861,7 +1958,11 @@ napi_status napi_get_value_bigint_words(napi_env env,
18611958

18621959
JSValue jsValue = *(JSValue *) value;
18631960

1864-
if (!JS_IsBigInt(jsValue)) {
1961+
if (!JS_IsBigInt(
1962+
#ifndef __QJS_NG__
1963+
env->context,
1964+
#endif
1965+
jsValue)) {
18651966
return napi_set_last_error(env, napi_bigint_expected, NULL, 0, NULL);
18661967
}
18671968

@@ -2258,7 +2359,11 @@ napi_status napi_typeof(napi_env env, napi_value value, napi_valuetype *result)
22582359
*result = napi_string;
22592360
} else if (JS_IsSymbol(jsValue)) {
22602361
*result = napi_symbol;
2261-
} else if (JS_IsBigInt(jsValue)) {
2362+
} else if (JS_IsBigInt(
2363+
#ifndef __QJS_NG__
2364+
env->context,
2365+
#endif
2366+
jsValue)) {
22622367
*result = napi_bigint;
22632368
} else if (JS_IsFunction(env->context, jsValue)) {
22642369
*result = napi_function;
@@ -2306,7 +2411,11 @@ napi_status napi_is_array(napi_env env, napi_value value, bool *result) {
23062411
CHECK_ARG(result)
23072412

23082413
JSValue jsValue = *((JSValue *) value);
2309-
int status = JS_IsArray(jsValue);
2414+
int status = JS_IsArray(
2415+
#ifndef __QJS_NG__
2416+
env->context,
2417+
#endif,
2418+
jsValue);
23102419
RETURN_STATUS_IF_FALSE(status != -1, napi_pending_exception);
23112420
*result = status;
23122421

@@ -2375,7 +2484,11 @@ napi_status napi_is_error(napi_env env, napi_value value, bool *result) {
23752484
CHECK_ARG(value)
23762485
CHECK_ARG(result)
23772486

2378-
int status = JS_IsError(*((JSValue *) value));
2487+
int status = JS_IsError(
2488+
#ifndef __QJS_NG__
2489+
env->context,
2490+
#endif
2491+
*((JSValue *) value));
23792492
*result = status;
23802493
return napi_clear_last_error(env);
23812494
}
@@ -3957,8 +4070,10 @@ napi_status qjs_create_runtime(napi_runtime *runtime) {
39574070
(*runtime)->runtime = JS_NewRuntime();
39584071
#endif
39594072

3960-
#ifndef NDEBUG
4073+
#ifdef __QJS_NG__
4074+
#ifndef NDEBUG
39614075
JS_SetDumpFlags((*runtime)->runtime, JS_DUMP_LEAKS);
4076+
#endif
39624077
#endif
39634078

39644079
JS_SetMaxStackSize((*runtime)->runtime, 0);
@@ -3973,12 +4088,17 @@ napi_status qjs_create_runtime(napi_runtime *runtime) {
39734088
JSClassDef ConstructorClassDef = {"ConstructorInfo", function_finalizer, NULL, NULL, NULL};
39744089
JSClassDef NapiHostObjectClassDef = {"NapiHostObject", host_object_finalizer, NULL, NULL,
39754090
&NapiHostObjectExoticMethods};
3976-
3977-
4091+
#ifndef __QJS_NG__
4092+
JS_NewClassID( &(*runtime)->napiHostObjectClassId);
4093+
JS_NewClassID( &(*runtime)->constructorClassId);
4094+
JS_NewClassID(&(*runtime)->functionClassId);
4095+
JS_NewClassID(&(*runtime)->externalClassId);
4096+
#else
39784097
JS_NewClassID((*runtime)->runtime, &(*runtime)->napiHostObjectClassId);
39794098
JS_NewClassID((*runtime)->runtime, &(*runtime)->constructorClassId);
39804099
JS_NewClassID((*runtime)->runtime, &(*runtime)->functionClassId);
39814100
JS_NewClassID((*runtime)->runtime, &(*runtime)->externalClassId);
4101+
#endif
39824102

39834103
JS_NewClass((*runtime)->runtime, (*runtime)->napiHostObjectClassId, &NapiHostObjectClassDef);
39844104
JS_NewClass((*runtime)->runtime, (*runtime)->externalClassId, &ExternalClassDef);
@@ -4017,9 +4137,16 @@ JSEngineCallback(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *
40174137
return JS_UNDEFINED;
40184138
}
40194139

4140+
#ifndef __QJS_NG__
4141+
void JSR_PromiseRejectionTracker(JSContext *ctx, JSValueConst promise,
4142+
JSValueConst reason,
4143+
JS_BOOL is_handled, void *opaque)
4144+
#else
40204145
void JSR_PromiseRejectionTracker(JSContext *ctx, JSValue promise,
40214146
JSValue reason,
4022-
bool is_handled, void *opaque) {
4147+
bool is_handled, void *opaque)
4148+
#endif
4149+
{
40234150
JSValue global = JS_GetGlobalObject(ctx);
40244151
JSValue onUnhandledRejection = JS_GetPropertyStr(ctx, global, "onUnhandledPromiseRejectionTracker");
40254152
if (JS_IsFunction(ctx, onUnhandledRejection)) {

0 commit comments

Comments
 (0)