-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. `[QuickJs]` add support for QuickJs 2024-01-13
- Loading branch information
1 parent
582d1c5
commit df62328
Showing
8 changed files
with
104 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.4.0 | ||
3.5.0 |
92 changes: 92 additions & 0 deletions
92
backend/QuickJs/patch/0001-Add-new-APIs-for-ScriptX-for-QuickJs-2024-01-13.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
From 8f4fa37cfbb34fdd314d9af831408993ba6f4d5a Mon Sep 17 00:00:00 2001 | ||
From: landerlyoung <[email protected]> | ||
Date: Fri, 9 Feb 2024 03:49:37 +0800 | ||
Subject: [PATCH] Add new APIs for ScriptX based on QuickJs version | ||
"2024-01-13" Changes: 1. add JS_StrictEqual 2. add JS_NewWeakRef 3. add | ||
JS_GetWeakRef | ||
|
||
--- | ||
quickjs.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ | ||
quickjs.h | 5 +++++ | ||
2 files changed, 51 insertions(+) | ||
|
||
diff --git a/quickjs.c b/quickjs.c | ||
index 7958f81..b24873a 100644 | ||
--- a/quickjs.c | ||
+++ b/quickjs.c | ||
@@ -1130,6 +1130,10 @@ typedef enum JSStrictEqModeEnum { | ||
static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2, | ||
JSStrictEqModeEnum eq_mode); | ||
static BOOL js_strict_eq(JSContext *ctx, JSValue op1, JSValue op2); | ||
+int JS_StrictEqual(JSContext *ctx, JSValueConst op1, JSValueConst op2) | ||
+{ | ||
+ return js_strict_eq(ctx, JS_DupValue(ctx, op1), JS_DupValue(ctx, op2)); | ||
+} | ||
static BOOL js_same_value(JSContext *ctx, JSValueConst op1, JSValueConst op2); | ||
static BOOL js_same_value_zero(JSContext *ctx, JSValueConst op1, JSValueConst op2); | ||
static JSValue JS_ToObject(JSContext *ctx, JSValueConst val); | ||
@@ -55566,3 +55570,45 @@ void JS_AddIntrinsicTypedArrays(JSContext *ctx) | ||
JS_AddIntrinsicAtomics(ctx); | ||
#endif | ||
} | ||
+ | ||
+ | ||
+/************* WeakRef ***********/ | ||
+JSValue JS_NewWeakRef(JSContext* ctx, JSValueConst v) | ||
+{ | ||
+ if (JS_IsObject(v)) { | ||
+ JSValue map = js_map_constructor(ctx, JS_UNDEFINED, 0, NULL, MAGIC_SET | MAGIC_WEAK); | ||
+ if (JS_IsException(map)) return JS_EXCEPTION; | ||
+ // check | ||
+ JSValue ret = js_map_set(ctx, map, 1, &v, MAGIC_SET | MAGIC_WEAK); | ||
+ if (JS_IsException(ret)) return JS_EXCEPTION; | ||
+ JS_FreeValue(ctx, ret); | ||
+ return map; | ||
+ } else { | ||
+ return JS_DupValue(ctx, v); | ||
+ } | ||
+} | ||
+ | ||
+static JSValue js_map_get_first_key(JSContext *ctx, JSValueConst this_val) | ||
+{ | ||
+ JSMapState *s = JS_GetOpaque2(ctx, this_val, JS_CLASS_WEAKSET); | ||
+ JSMapRecord *mr; | ||
+ JSValueConst key = JS_UNDEFINED; | ||
+ struct list_head *el; | ||
+ | ||
+ if (!s) return JS_EXCEPTION; | ||
+ el = s->records.next; | ||
+ if (el != &(s->records)) { | ||
+ mr = list_entry(el, JSMapRecord, link); | ||
+ key = mr->key; | ||
+ } | ||
+ return JS_DupValue(ctx, key); | ||
+} | ||
+ | ||
+JSValue JS_GetWeakRef(JSContext* ctx, JSValueConst w) | ||
+{ | ||
+ if (JS_IsObject(w)) { | ||
+ return js_map_get_first_key(ctx, w); | ||
+ } else { | ||
+ return JS_DupValue(ctx, w); | ||
+ } | ||
+} | ||
diff --git a/quickjs.h b/quickjs.h | ||
index 56bac64..0908325 100644 | ||
--- a/quickjs.h | ||
+++ b/quickjs.h | ||
@@ -681,6 +681,11 @@ static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v) | ||
return (JSValue)v; | ||
} | ||
|
||
+#define QUICK_JS_HAS_SCRIPTX_PATCH | ||
+JSValue JS_NewWeakRef(JSContext* ctx, JSValueConst v); | ||
+JSValue JS_GetWeakRef(JSContext* ctx, JSValueConst w); | ||
+int JS_StrictEqual(JSContext *ctx, JSValueConst op1, JSValueConst op2); | ||
+ | ||
int JS_ToBool(JSContext *ctx, JSValueConst val); /* return -1 for JS_EXCEPTION */ | ||
int JS_ToInt32(JSContext *ctx, int32_t *pres, JSValueConst val); | ||
static inline int JS_ToUint32(JSContext *ctx, uint32_t *pres, JSValueConst val) | ||
-- | ||
2.42.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
# QuickJs 引擎 | ||
|
||
目前支持的QuickJs引擎版本为2021-03-27,其他版本理论上也能支持。 | ||
目前支持的QuickJs引擎版本为2021-03-27和2024-01-13,其他版本理论上也能支持。 | ||
|
||
## 时间循环 | ||
## 事件循环 | ||
QuickJs通过 JS_ExecutePendingJob 来执行promise相关的异步事件,ScriptX中提供了MessageQueue机制。 | ||
因此ScriptX内部会主动在合适的时机post事件来驱动执行 `JS_ExecutePendingJob`。 | ||
|
||
## 关于补丁 | ||
由于QuickJs的C-API比较受限,ScriptX将部分需要的能力通过JS来实现。 | ||
|
||
但是仍然有部分能力(如弱引用)在JS中也不受支持。这个情况下你需要QuickJs打一个ScriptX提供的补丁包[backend/QuickJs/patch](../../backend/QuickJs/patch),或直接使用笔者的 [fork](https://github.com/LanderlYoung/quickjs/tree/58ac957eee57e301ed0cc52b5de5495a7e1c1827) 。 | ||
但是仍然有部分能力(如弱引用)在JS中也不受支持。这个情况下你需要QuickJs打一个ScriptX提供的补丁包[backend/QuickJs/patch](../../backend/QuickJs/patch),或直接使用笔者的 [fork](https://github.com/LanderlYoung/quickjs/) 。 | ||
|
||
目前这个补丁仅影响 `script::Weak<T>` 的功能。 | ||
即使不打该补丁包,也仅仅是 `script::Weak<T>` 表现为强引用即`script::Global<T>`,除此之外无差别。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters