Skip to content

Commit 38f9693

Browse files
quickjs-ng update
1 parent 4f0bc39 commit 38f9693

File tree

6 files changed

+112
-8
lines changed

6 files changed

+112
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.13.1
2+
3+
* update quickjs-ng to 4.0.1
4+
* fixed bigint support for quickjs-ng
5+
16
# 0.13.0
27

38
* support quickjs-ng (v 0.3.0) as feature, it compiles, some test cases fail (bigint) but should be a nice first step

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "quickjs_runtime"
3-
version = "0.13.0"
3+
version = "0.13.1"
44
authors = ["Andries Hiemstra <[email protected]>"]
55
edition = "2021"
66
description = "Wrapper API and utils for the QuickJS JavaScript engine with support for Promise, Modules, Async/await"
@@ -32,7 +32,7 @@ backtrace = "0.3.67"
3232

3333
#libquickjs-sys = {package="hirofa-quickjs-sys", git='https://github.com/HiRoFa/quickjs-sys'}
3434
#libquickjs-sys = {package="hirofa-quickjs-sys", path='../quickjs-sys'}
35-
libquickjs-sys = {package="hirofa-quickjs-sys", version="0.3.0", default-features=false}
35+
libquickjs-sys = {package="hirofa-quickjs-sys", version="0.4.0", default-features=false}
3636
lazy_static = "1.4.0"
3737
log = "0.4"
3838
num_cpus = "1"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ The fun stuff about QuickJS:
8989
Cargo.toml
9090
```toml
9191
[dependencies]
92-
quickjs_runtime = "0.12"
92+
quickjs_runtime = "0.13.1"
9393
```
9494

9595
Here are some quickstarts:

src/quickjs_utils/bigints.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ pub fn new_bigint_i64_q(
1212
unsafe { new_bigint_i64(context.context, int) }
1313
}
1414

15+
pub fn new_bigint_u64_q(
16+
context: &QuickJsRealmAdapter,
17+
int: u64,
18+
) -> Result<QuickJsValueAdapter, JsError> {
19+
unsafe { new_bigint_u64(context.context, int) }
20+
}
21+
1522
#[allow(dead_code)]
1623
/// # Safety
1724
/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
@@ -83,17 +90,34 @@ pub unsafe fn to_string(
8390
#[cfg(test)]
8491
pub mod tests {
8592
use crate::facades::tests::init_test_rt;
93+
use crate::jsutils::Script;
8694
use crate::quickjs_utils::bigints;
8795
use crate::quickjs_utils::bigints::new_bigint_str_q;
8896

89-
//#[test]
90-
fn _test_bigint() {
97+
#[test]
98+
fn test_bigint() {
9199
let rt = init_test_rt();
92100
rt.exe_rt_task_in_event_loop(|q_js_rt| {
93101
let q_ctx = q_js_rt.get_main_realm();
94102

95-
let bi_ref = bigints::new_bigint_i64_q(q_ctx, 659863456456)
103+
let res = q_ctx
104+
.eval(Script::new("createABigInt.js", "BigInt(1234567890)"))
105+
.expect("script failed");
106+
log::info!(
107+
"script bi was {} {}",
108+
res.get_tag(),
109+
res.to_string().expect("could not toString")
110+
);
111+
112+
let bi_ref = bigints::new_bigint_u64_q(q_ctx, 659863456456)
96113
.expect("could not create bigint from u64");
114+
115+
unsafe {
116+
if let Some(e) = crate::quickjs_utils::errors::get_exception(q_ctx.context) {
117+
log::error!("ex: {}", e);
118+
}
119+
}
120+
97121
let to_str = bigints::to_string_q(q_ctx, &bi_ref).expect("could not tostring bigint");
98122
assert_eq!(to_str, "659863456456");
99123
let bi_ref = bigints::new_bigint_i64_q(q_ctx, 659863456457)

src/quickjs_utils/errors.rs

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ pub mod tests {
147147
use crate::jsutils::{JsError, Script};
148148
use crate::quickjs_utils::functions;
149149
use crate::values::{JsValueConvertable, JsValueFacade};
150+
use std::thread;
150151
//use log::LevelFilter;
151152
use std::time::Duration;
152153

@@ -162,9 +163,12 @@ pub mod tests {
162163
"console.log('foo');\nconsole.log('bar');let a = __c_v__ * 7;",
163164
),
164165
);
165-
let ex = res.expect_err("sciprt should have failed;");
166+
let ex = res.expect_err("script should have failed;");
166167

168+
#[cfg(feature = "bellard")]
167169
assert_eq!(ex.get_message(), "'__c_v__' is not defined");
170+
#[cfg(feature = "quickjs-ng")]
171+
assert_eq!(ex.get_message(), "__c_v__ is not defined");
168172
}
169173

170174
#[test]
@@ -179,9 +183,12 @@ pub mod tests {
179183
"console.log('foo');\nconsole.log('bar');let a = __c_v__ * 7;",
180184
),
181185
);
182-
let ex = res.expect_err("sciprt should have failed;");
186+
let ex = res.expect_err("script should have failed;");
183187

188+
#[cfg(feature = "bellard")]
184189
assert_eq!(ex.get_message(), "'__c_v__' is not defined");
190+
#[cfg(feature = "quickjs-ng")]
191+
assert_eq!(ex.get_message(), "__c_v__ is not defined");
185192
}
186193

187194
#[test]
@@ -221,6 +228,71 @@ pub mod tests {
221228
std::thread::sleep(Duration::from_secs(1));
222229
}
223230

231+
#[test]
232+
fn test_ex3() {
233+
let rt = init_test_rt();
234+
rt.eval_sync(
235+
None,
236+
Script::new(
237+
"test_ex3.js",
238+
r#"
239+
async function asyncDebugStackPreserve(delegate, invokerName) {
240+
241+
const startStack = new Error().stack;
242+
try {
243+
return await delegate();
244+
} catch (ex) {
245+
const err = Error(ex.message);
246+
247+
err.fileName = ex.fileName;
248+
err.lineNumber = ex.lineNumber;
249+
err.columnNumber = ex.columnNumber;
250+
err.cause = ex.cause;
251+
252+
err.stack = ex.stack + startStack;
253+
throw err;
254+
}
255+
256+
}
257+
258+
async function sleep(ms) {
259+
return new Promise((res) => {
260+
setTimeout(res, ms);
261+
});
262+
}
263+
264+
async function a(){
265+
266+
return new Promise(async (res, rej) => {
267+
try {
268+
let ret = await asyncDebugStackPreserve(async function aInner() {
269+
await sleep(100);
270+
let ret = await b();
271+
});
272+
res(ret);
273+
} catch(ex) {
274+
rej(ex);
275+
}
276+
});
277+
}
278+
279+
async function b(){
280+
throw Error("poof");
281+
}
282+
283+
a().catch((ex) => {
284+
console.error(ex);
285+
});
286+
287+
288+
289+
"#,
290+
),
291+
)
292+
.expect("script failed");
293+
thread::sleep(Duration::from_secs(1));
294+
}
295+
224296
#[test]
225297
fn test_ex2() {
226298
// check if stacktrace is preserved when invoking native methods

src/quickjsvalueadapter.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ impl QuickJsValueAdapter {
250250
}
251251
}
252252

253+
#[cfg(feature = "bellard")]
253254
pub(crate) const TAG_BIG_INT: i64 = -10;
255+
#[cfg(feature = "quickjs-ng")]
256+
pub(crate) const TAG_BIG_INT: i64 = -9;
254257
//pub(crate) const TAG_BIG_FLOAT: i64 = -9;
255258
//pub(crate) const TAG_SYMBOL: i64 = -8;
256259
pub(crate) const TAG_STRING: i64 = -7;

0 commit comments

Comments
 (0)