Skip to content

Commit 662e50a

Browse files
committed
Added Test for mozilla::Range::new
1 parent 5344b06 commit 662e50a

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

mozjs-sys/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const ENV_VARS: &'static [&'static str] = &[
3030

3131
const EXTRA_FILES: &'static [&'static str] = &[
3232
"makefile.cargo",
33-
"src/rustfmt.toml",
3433
"src/jsglue.hpp",
3534
"src/jsglue.cpp",
3635
];

mozjs/tests/range.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::ptr;
2+
3+
use mozjs::jsapi::mozilla::Range;
4+
use mozjs::jsapi::{BigIntIsUint64, JS_NewGlobalObject, StringToBigInt, StringToBigInt1};
5+
use mozjs::jsapi::{JSAutoRealm, OnNewGlobalHookOption};
6+
use mozjs::rooted;
7+
use mozjs::rust::{JSEngine, RealmOptions, Runtime, SIMPLE_GLOBAL_CLASS};
8+
9+
#[test]
10+
fn range() {
11+
let engine = JSEngine::init().unwrap();
12+
let runtime = Runtime::new(engine.handle());
13+
let context = runtime.cx();
14+
let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
15+
let c_option = RealmOptions::default();
16+
17+
unsafe {
18+
rooted!(in(context) let global = JS_NewGlobalObject(
19+
context,
20+
&SIMPLE_GLOBAL_CLASS,
21+
ptr::null_mut(),
22+
h_option,
23+
&*c_option,
24+
));
25+
let _ac = JSAutoRealm::new(context, global.get());
26+
27+
// Number.MAX_SAFE_INTEGER + 10
28+
let int = 9007199254741001;
29+
let mut string = int.to_string();
30+
let range = string.as_bytes_mut().as_mut_ptr_range();
31+
let chars = Range::new(range.start, range.end);
32+
rooted!(in(context) let bigint = StringToBigInt(context, chars));
33+
assert!(!bigint.get().is_null());
34+
35+
let mut result = 0;
36+
assert!(BigIntIsUint64(bigint.get(), &mut result));
37+
assert_eq!(result, int);
38+
39+
let mut chars: Vec<_> = string.encode_utf16().collect();
40+
let range = chars.as_mut_ptr_range();
41+
let chars = Range::new(range.start, range.end);
42+
rooted!(in(context) let bigint = StringToBigInt1(context, chars));
43+
assert!(!bigint.get().is_null());
44+
45+
let mut result = 0;
46+
assert!(BigIntIsUint64(bigint.get(), &mut result));
47+
assert_eq!(result, int);
48+
}
49+
}

0 commit comments

Comments
 (0)