Skip to content

Commit

Permalink
Version 3.5.0 (2023-05):
Browse files Browse the repository at this point in the history
1. `[QuickJs]` add support for QuickJs 2024-01-13
  • Loading branch information
LanderlYoung committed Feb 8, 2024
1 parent 582d1c5 commit df62328
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Version 3.5.0 (2023-05):
1. `[QuickJs]` add support for QuickJs 2024-01-13

Version 3.4.0 (2023-05):
1. `[V8]` **BEHAVIOR CHANGE:** `V8Platform` now being a singleton, not ref-counted by `V8Engine`s any more.
Expand Down
2 changes: 1 addition & 1 deletion README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ScriptX的术语中,"前端"指对外的C++ API,"后端"则指不同的底
| V8 | JavaScript | 7.4+ | done |
| JavaScriptCore | JavaScript | 7604.1.38.0.7+<br>(iOS 10+/macOS10.12+) | done |
| Node.js | JavaScript | 14.x+ | done |
| QuickJs | JavaScript | 2021-03-27 | done |
| QuickJs | JavaScript | 2024-01-13 | done |
| WebAssembly | JavaScript | Emscripten-2.0.5+ | done |
| Lua | Lua | 5.4+ | done |
| CPython | Python | | todo |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ In ScriptX terminology, "front-end" refers to the external C++ API, and "back-en
| V8 | JavaScript | 7.4+ | done |
| JavaScriptCore | JavaScript | 7604.1.38.0.7+<br>(iOS 10+/macOS10.12+) | done |
| Node.js | JavaScript | 14.x+ | done |
| QuickJs | JavaScript | 2021-03-27 | done |
| QuickJs | JavaScript | 2024-01-13 | done |
| WebAssembly | JavaScript | Emscripten-2.0.5+ | done |
| Lua | Lua | 5.4+ | done |
| CPython | Python | | todo |
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.0
3.5.0
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

2 changes: 1 addition & 1 deletion docs/en/QuickJs.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# QuickJs engine

Current support QuickJs version is 2021-03-27.
Current support QuickJs version is 2021-03-27 and 2024-01-13.

Other version should be also supported.

Expand Down
6 changes: 3 additions & 3 deletions docs/zh/QuickJs.md
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>`,除此之外无差别。
6 changes: 3 additions & 3 deletions src/version.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Tencent is pleased to support the open source community by making ScriptX available.
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,9 +19,9 @@

// ScriptX version config files
// auto generated from the file VERSION
#define SCRIPTX_VERSION_STRING "3.4.0"
#define SCRIPTX_VERSION_STRING "3.5.0"
#define SCRIPTX_VERSION_MAJOR 3
#define SCRIPTX_VERSION_MINOR 4
#define SCRIPTX_VERSION_MINOR 5
#define SCRIPTX_VERSION_PATCH 0

namespace script {
Expand Down

0 comments on commit df62328

Please sign in to comment.