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>
3539
3640#ifdef USE_MIMALLOC
3741
42+
43+ #ifdef __QJS_NG__
3844static 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
40204145void 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