Skip to content

Commit f612731

Browse files
authored
feat: Complete NAPICreateRuntime and NAPIFreeRuntime. (#25)
* fix(QuickJS): Fix bytecode function free twice error. * feat(QuickJS): Add NAPICreateRuntime and NAPIFreeRuntime. * feat(Hermes): Migrate hermes to new interface. * feat(JavaScriptCore): Add NAPICreateRuntime and NAPIFreeRuntime
1 parent 03bbb74 commit f612731

7 files changed

Lines changed: 165 additions & 161 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ JavaScript 值,概念和操作通常映射到 ECMA-262 语言规范,API 具
9191
1. 建议使用 BUILDCONFIG.gn 中定义的 LTS NDK 版本
9292
2. Hermes 引擎需要先 `cd third_party/hermes && git apply ../hermes_patch.diff`
9393
3. Android 版本 libhermes.so 包括 fbjni 库,内含 OnLoad.cpp,需要使用 System.load("hermes") 显式加载,不能依赖 Linux 内核的动态库隐式加载
94-
4. Hermes 引擎 0.8.1 版本内置字节码,需要先编译主机 hermesc,后输入如下命令
94+
4. Hermes 引擎 0.8.1 版本内置字节码,需要先编译主机 hermesc,指令 `./utils/build/configure.py`后输入如下命令
9595
```
96-
> cd third_party/hermes/lib/InternalBytecode
96+
> cd lib/InternalBytecode
9797
> cat 00-header.js 01-Promise.js 02-AsyncFn.js 99-footer.js > InternalBytecode.js
98-
> hermesc -O -Wno-undefined-variable -fno-enable-tdz -emit-binary -out=./InternalBytecode.hbc ./InternalBytecode.js
98+
> ../../build/bin/hermesc -O -Wno-undefined-variable -fno-enable-tdz -emit-binary -out=./InternalBytecode.hbc ./InternalBytecode.js
9999
> ./xxd.py ./InternalBytecode.hbc > ./InternalBytecode.inc
100100
```
101101

include/napi/js_native_api.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#include <napi/js_native_api_types.h>
55

6-
#define NAPI_EXPORT __attribute__((visibility("default")))
7-
86
EXTERN_C_START
97

108
#include <stdbool.h> // NOLINT(modernize-deprecated-headers)
@@ -119,20 +117,25 @@ NAPI_EXPORT NAPIExceptionStatus NAPIRunScript(NAPIEnv env, const char *script, c
119117
NAPI_EXPORT NAPIExceptionStatus NAPIDefineClass(NAPIEnv env, const char *utf8name, NAPICallback constructor, void *data,
120118
NAPIValue *result);
121119

122-
NAPI_EXPORT NAPIErrorStatus NAPICreateEnv(NAPIEnv *env);
120+
NAPI_EXPORT NAPIErrorStatus NAPICreateRuntime(NAPIRuntime *runtime);
123121

122+
NAPI_EXPORT NAPIErrorStatus NAPICreateEnv(NAPIEnv *env, NAPIRuntime runtime);
124123

125124
NAPI_EXPORT NAPICommonStatus NAPIFreeEnv(NAPIEnv env);
126125

126+
NAPI_EXPORT NAPICommonStatus NAPIFreeRuntime(NAPIRuntime runtime);
127+
127128
NAPI_EXPORT NAPIErrorStatus NAPIGetValueStringUTF8(NAPIEnv env, NAPIValue value, const char **result);
128129

129130
NAPI_EXPORT NAPICommonStatus NAPIFreeUTF8String(NAPIEnv env, const char *cString);
130131

131-
NAPI_EXPORT NAPIExceptionStatus NAPICompileToByteBuffer(NAPIEnv env, const char *script, const char *sourceUrl, const uint8_t **byteBuffer, size_t *bufferSize);
132+
NAPI_EXPORT NAPIExceptionStatus NAPICompileToByteBuffer(NAPIEnv env, const char *script, const char *sourceUrl,
133+
const uint8_t **byteBuffer, size_t *bufferSize);
132134

133135
NAPI_EXPORT NAPICommonStatus NAPIFreeByteBuffer(NAPIEnv env, const uint8_t *byteBuffer);
134136

135-
NAPI_EXPORT NAPIExceptionStatus NAPIRunByteBuffer(NAPIEnv env, const uint8_t *byteBuffer, size_t bufferSize, NAPIValue *result);
137+
NAPI_EXPORT NAPIExceptionStatus NAPIRunByteBuffer(NAPIEnv env, const uint8_t *byteBuffer, size_t bufferSize,
138+
NAPIValue *result);
136139

137140
#pragma mark - 间接函数
138141

include/napi/js_native_api_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
#define EXTERN_C_END
1212
#endif
1313

14+
#define NAPI_EXPORT __attribute__((visibility("default")))
15+
1416
EXTERN_C_START
1517

18+
typedef struct OpaqueNAPIRuntime *NAPIRuntime;
1619
typedef struct OpaqueNAPIEnv *NAPIEnv;
1720
typedef struct OpaqueNAPIValue *NAPIValue;
1821
typedef struct OpaqueNAPIRef *NAPIRef;

src/js_native_api_hermes.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <napi/js_native_api.h>
1515
#include <napi/js_native_api_debugger.h>
1616
#include <napi/js_native_api_debugger_hermes_types.h>
17-
#include <string>
1817
#include <sys/queue.h>
1918
#include <unordered_set>
2019

@@ -24,7 +23,6 @@
2423
#ifdef HERMES_ENABLE_DEBUGGER
2524
#include <cxxreact/MessageQueueThread.h>
2625
#include <hermes/inspector/RuntimeAdapter.h>
27-
#include <hermes/inspector/chrome/Registration.h>
2826
#endif
2927

3028
#ifndef LIST_FOREACH_SAFE
@@ -1430,7 +1428,17 @@ NAPIExceptionStatus NAPIDefineClass(NAPIEnv env, const char *utf8name, NAPICallb
14301428
return NAPIExceptionOK;
14311429
}
14321430

1433-
NAPIErrorStatus NAPICreateEnv(NAPIEnv *env)
1431+
NAPIErrorStatus NAPICreateRuntime(NAPIRuntime *runtime)
1432+
{
1433+
return NAPIErrorOK;
1434+
}
1435+
1436+
NAPICommonStatus NAPIFreeRuntime(NAPIRuntime runtime)
1437+
{
1438+
return NAPICommonOK;
1439+
}
1440+
1441+
NAPIErrorStatus NAPICreateEnv(NAPIEnv *env, NAPIRuntime runtime)
14341442
{
14351443
CHECK_ARG(env, Error)
14361444

src/js_native_api_jsc.c

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,9 +1341,9 @@ NAPIExceptionStatus NAPIRunScript(NAPIEnv env, const char *utf8Script, const cha
13411341
return NAPIExceptionOK;
13421342
}
13431343

1344-
static JSContextGroupRef virtualMachine = NULL;
1344+
// static JSContextGroupRef virtualMachine = NULL;
13451345

1346-
static uint8_t contextCount = 0;
1346+
// static uint8_t contextCount = 0;
13471347

13481348
NAPICommonStatus NAPIEnableDebugger(__attribute__((unused)) NAPIEnv env,
13491349
__attribute__((unused)) const char *debuggerTitle,
@@ -1363,48 +1363,39 @@ NAPICommonStatus NAPISetMessageQueueThread(__attribute__((unused)) NAPIEnv env,
13631363
return NAPICommonOK;
13641364
}
13651365

1366-
NAPIErrorStatus NAPICreateEnv(NAPIEnv *env)
1366+
NAPIErrorStatus NAPICreateRuntime(NAPIRuntime *runtime)
13671367
{
1368-
// *env 才是 NAPIEnv
1369-
if (!env)
1370-
{
1371-
return NAPIErrorInvalidArg;
1372-
}
1368+
CHECK_ARG(runtime, Error)
13731369

1374-
if ((virtualMachine && !contextCount) || (!virtualMachine && contextCount))
1375-
{
1376-
assert(false);
1370+
*runtime = (NAPIRuntime)JSContextGroupCreate();
1371+
RETURN_STATUS_IF_FALSE(*runtime, NAPIErrorMemoryError);
13771372

1378-
return NAPIErrorGenericFailure;
1379-
}
1373+
return NAPIErrorOK;
1374+
}
13801375

1381-
*env = malloc(sizeof(struct OpaqueNAPIEnv));
1382-
if (!*env)
1383-
{
1384-
return NAPIErrorMemoryError;
1385-
}
1376+
NAPICommonStatus NAPIFreeRuntime(NAPIRuntime runtime)
1377+
{
1378+
CHECK_ARG(runtime, Common)
13861379

1387-
if (!virtualMachine)
1388-
{
1389-
virtualMachine = JSContextGroupCreate();
1390-
if (!virtualMachine)
1391-
{
1392-
free(*env);
1380+
JSContextGroupRelease((JSContextGroupRef)runtime);
13931381

1394-
return NAPIErrorMemoryError;
1395-
}
1396-
}
1397-
JSGlobalContextRef globalContext = JSGlobalContextCreateInGroup(virtualMachine, NULL);
1398-
if (!globalContext)
1382+
return NAPICommonOK;
1383+
}
1384+
1385+
NAPIErrorStatus NAPICreateEnv(NAPIEnv *env, NAPIRuntime runtime)
1386+
{
1387+
CHECK_ARG(env, Error)
1388+
1389+
*env = malloc(sizeof(struct OpaqueNAPIEnv));
1390+
RETURN_STATUS_IF_FALSE(*env, NAPIErrorMemoryError)
1391+
1392+
(*env)->context = JSGlobalContextCreateInGroup((JSContextGroupRef)runtime, NULL);
1393+
if (!(*env)->context)
13991394
{
14001395
free(*env);
1401-
JSContextGroupRelease(virtualMachine);
1402-
virtualMachine = NULL;
14031396

14041397
return NAPIErrorMemoryError;
14051398
}
1406-
contextCount += 1;
1407-
(*env)->context = globalContext;
14081399
(*env)->lastException = NULL;
14091400
LIST_INIT(&(*env)->strongRefList);
14101401
LIST_INIT(&(*env)->valueList);
@@ -1441,13 +1432,6 @@ NAPICommonStatus NAPIFreeEnv(NAPIEnv env)
14411432
free(ref);
14421433
}
14431434
JSGlobalContextRelease(env->context);
1444-
if (--contextCount == 0 && virtualMachine)
1445-
{
1446-
// virtualMachine 不能为 NULL
1447-
JSContextGroupRelease(virtualMachine);
1448-
virtualMachine = NULL;
1449-
}
1450-
14511435
free(env);
14521436

14531437
return NAPICommonOK;

0 commit comments

Comments
 (0)