ICU data must be 16-bit aligned (#617)

This commit is contained in:
Ryan Dahl 2021-02-12 05:45:02 -05:00 committed by GitHub
parent b7bffb83ac
commit 753b92fd96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 22 deletions

7
Cargo.lock generated
View File

@ -1,5 +1,11 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "align-data"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1926655ba000b19e21f0402be09a1d52d318c8a8a68622870bfb7af2a71315cd"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
@ -62,6 +68,7 @@ dependencies = [
name = "rusty_v8" name = "rusty_v8"
version = "0.18.2" version = "0.18.2"
dependencies = [ dependencies = [
"align-data",
"bitflags", "bitflags",
"fslock", "fslock",
"lazy_static", "lazy_static",

View File

@ -80,6 +80,7 @@ fslock = "0.1"
[dev-dependencies] [dev-dependencies]
trybuild = "1.0.35" trybuild = "1.0.35"
align-data = "0.1.0"
[[example]] [[example]]
name = "hello_world" name = "hello_world"

View File

@ -1,8 +1,5 @@
// Copyright 2019-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2019-2020 the Deno authors. All rights reserved. MIT license.
use lazy_static::lazy_static;
#[macro_use]
extern crate lazy_static;
use std::any::type_name; use std::any::type_name;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
@ -11,16 +8,11 @@ use std::ffi::c_void;
use std::hash::Hash; use std::hash::Hash;
use std::ptr::NonNull; use std::ptr::NonNull;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
use rusty_v8 as v8; use rusty_v8 as v8;
// TODO(piscisaureus): Ideally there would be no need to import this trait. // TODO(piscisaureus): Ideally there would be no need to import this trait.
use v8::MapFnTo; use v8::MapFnTo;
lazy_static! {
static ref INIT_LOCK: Mutex<u32> = Mutex::new(0);
}
#[must_use] #[must_use]
struct SetupGuard {} struct SetupGuard {}
@ -31,13 +23,17 @@ impl Drop for SetupGuard {
} }
fn setup() -> SetupGuard { fn setup() -> SetupGuard {
let mut g = INIT_LOCK.lock().unwrap(); static START: std::sync::Once = std::sync::Once::new();
*g += 1; START.call_once(|| {
if *g == 1 { assert!(v8::icu::set_common_data(align_data::include_aligned!(
align_data::Align16,
"../third_party/icu/common/icudtl.dat"
))
.is_ok());
v8::V8::set_flags_from_string("--expose_gc --harmony-import-assertions"); v8::V8::set_flags_from_string("--expose_gc --harmony-import-assertions");
v8::V8::initialize_platform(v8::new_default_platform().unwrap()); v8::V8::initialize_platform(v8::new_default_platform().unwrap());
v8::V8::initialize(); v8::V8::initialize();
} });
SetupGuard {} SetupGuard {}
} }
@ -4718,13 +4714,8 @@ fn prepare_stack_trace_callback() {
} }
} }
const ICU_DATA: &[u8; 10413584] =
include_bytes!("../third_party/icu/common/icudtl.dat");
#[test] #[test]
fn icu_date() { fn icu_date() {
assert!(v8::icu::set_common_data(ICU_DATA).is_ok());
let _setup_guard = setup(); let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default()); let isolate = &mut v8::Isolate::new(Default::default());
{ {
@ -4748,14 +4739,11 @@ fn icu_date() {
#[test] #[test]
fn icu_set_common_data_fail() { fn icu_set_common_data_fail() {
const BAD_DATA: &[u8; 3] = &[1, 2, 3]; assert!(v8::icu::set_common_data(&[1, 2, 3]).is_err());
assert!(v8::icu::set_common_data(BAD_DATA).is_err());
} }
#[test] #[test]
fn icu_format() { fn icu_format() {
assert!(v8::icu::set_common_data(ICU_DATA).is_ok());
let _setup_guard = setup(); let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default()); let isolate = &mut v8::Isolate::new(Default::default());
{ {
@ -4773,3 +4761,15 @@ fn icu_format() {
assert!(value.strict_equals(currency_jpy_val.into())); assert!(value.strict_equals(currency_jpy_val.into()));
} }
} }
#[test]
fn icu_collator() {
let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
let source = v8::String::new(scope, "new Intl.Collator('en-US')").unwrap();
let script = v8::Script::compile(scope, source, None).unwrap();
assert!(script.run(scope).is_some());
}