|
| 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