forked from Tencent/ScriptX
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0001-Add-new-APIs-for-ScriptX-for-QuickJs-2021-03-27.patch
93 lines (89 loc) · 3.11 KB
/
0001-Add-new-APIs-for-ScriptX-for-QuickJs-2021-03-27.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
From 58ac957eee57e301ed0cc52b5de5495a7e1c1827 Mon Sep 17 00:00:00 2001
From: landerlyoung <[email protected]>
Date: Tue, 6 Apr 2021 16:18:13 +0800
Subject: [PATCH] Add new APIs for ScriptX based on QuickJs version
"2021-03-27" Changes: 1. add JS_StrictEqual 2. add JS_NewWeakRef 3. add
JS_GetWeakRef
WeakRef is implemented based on WeakSet
---
quickjs.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
quickjs.h | 5 +++++
2 files changed, 51 insertions(+)
diff --git a/quickjs.c b/quickjs.c
index 48aeffc..89eba07 100644
--- a/quickjs.c
+++ b/quickjs.c
@@ -1111,6 +1111,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);
@@ -54034,3 +54038,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 d4a5cd3..6ffffa3 100644
--- a/quickjs.h
+++ b/quickjs.h
@@ -678,6 +678,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.29.2