From 803ae4526349140c75489b896a3d7f9de525d496 Mon Sep 17 00:00:00 2001 From: Ry Dahl Date: Wed, 4 Dec 2019 00:57:06 -0500 Subject: [PATCH] Simplify public API and move tests to tests/test_api.rs (#24) --- src/V8.rs | 18 -------- src/handle_scope.rs | 33 -------------- src/isolate.rs | 26 +++++------ src/lib.rs | 22 +++++---- src/test_util.rs | 34 -------------- tests/test_api.rs | 106 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 128 insertions(+), 111 deletions(-) delete mode 100644 src/test_util.rs create mode 100644 tests/test_api.rs diff --git a/src/V8.rs b/src/V8.rs index 25aeef1e..19a142f0 100644 --- a/src/V8.rs +++ b/src/V8.rs @@ -79,19 +79,6 @@ pub fn set_flags_from_command_line(args: Vec) -> Vec { .collect() } -#[test] -fn test_set_flags_from_command_line() { - let r = set_flags_from_command_line(vec![ - "binaryname".to_string(), - "--log-colour".to_string(), - "--should-be-ignored".to_string(), - ]); - assert_eq!( - r, - vec!["binaryname".to_string(), "--should-be-ignored".to_string()] - ); -} - /// Get the version string. pub fn get_version() -> &'static str { let version = unsafe { v8__V8__GetVersion() }; @@ -99,11 +86,6 @@ pub fn get_version() -> &'static str { c_str.to_str().unwrap() } -#[test] -fn test_get_version() { - assert!(get_version().len() > 3); -} - // TODO: V8::InitializePlatform does not actually take a UniquePtr but rather // a raw pointer. This means that the Platform object is not released when // V8::ShutdownPlatform is called. diff --git a/src/handle_scope.rs b/src/handle_scope.rs index b153d7c6..cf0b4504 100644 --- a/src/handle_scope.rs +++ b/src/handle_scope.rs @@ -37,36 +37,3 @@ impl<'sc> LockedIsolate for HandleScope<'sc> { unsafe { v8__HandleScope__GetIsolate(self) } } } - -#[cfg(test)] -mod tests { - use super::*; - use crate::array_buffer::Allocator; - use crate::isolate::*; - use crate::Integer; - use crate::Locker; - use crate::Number; - - #[test] - #[allow(clippy::float_cmp)] - fn test_handle_scope() { - let g = crate::test_util::setup(); - let mut params = CreateParams::new(); - params.set_array_buffer_allocator(Allocator::new_default_allocator()); - let mut isolate = Isolate::new(params); - let mut locker = Locker::new(&mut isolate); - HandleScope::enter(&mut locker, |scope| { - let l1 = Integer::new(scope, -123); - let l2 = Integer::new_from_unsigned(scope, 456); - HandleScope::enter(scope, |scope2| { - let l3 = Number::new(scope2, 78.9); - assert_eq!(l1.value(), -123); - assert_eq!(l2.value(), 456); - assert_eq!(l3.value(), 78.9); - assert_eq!(Number::value(&l1), -123f64); - assert_eq!(Number::value(&l2), 456f64); - }); - }); - drop(g); - } -} diff --git a/src/isolate.rs b/src/isolate.rs index 3ccee1bf..54a6d53d 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -29,11 +29,23 @@ pub trait LockedIsolate { pub struct Isolate(&'static mut CxxIsolate); impl Isolate { + /// Creates a new isolate. Does not change the currently entered + /// isolate. + /// + /// When an isolate is no longer used its resources should be freed + /// by calling V8::dispose(). Using the delete operator is not allowed. + /// + /// V8::initialize() must have run prior to this. pub fn new(params: UniqueRef) -> Self { // TODO: support CreateParams. crate::V8::assert_initialized(); Self(unsafe { v8__Isolate__New(params.into_raw()) }) } + + /// Initial configuration parameters for a new Isolate. + pub fn create_params() -> UniqueRef { + CreateParams::new() + } } impl Drop for Isolate { @@ -78,17 +90,3 @@ impl Delete for CreateParams { unsafe { v8__Isolate__CreateParams__DELETE(self) } } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_isolate() { - let g = crate::test_util::setup(); - let mut params = CreateParams::new(); - params.set_array_buffer_allocator(Allocator::new_default_allocator()); - Isolate::new(params); - drop(g); - } -} diff --git a/src/lib.rs b/src/lib.rs index 17746e2e..68083d7b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,24 +9,22 @@ extern crate lazy_static; extern crate libc; +mod handle_scope; +mod inspector; +mod isolate; +mod local; +mod locker; +mod string_buffer; +mod string_view; +mod support; +mod value; + pub mod array_buffer; -pub mod handle_scope; -pub mod inspector; -pub mod isolate; -pub mod local; -pub mod locker; pub mod platform; -pub mod string_buffer; -pub mod string_view; -pub mod support; - -mod test_util; - // This module is intentionally named "V8" rather than "v8" to match the // C++ namespace "v8::V8". #[allow(non_snake_case)] pub mod V8; -pub mod value; pub use handle_scope::HandleScope; pub use isolate::Isolate; diff --git a/src/test_util.rs b/src/test_util.rs deleted file mode 100644 index 316af693..00000000 --- a/src/test_util.rs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#![cfg(test)] -use std::sync::Mutex; - -lazy_static! { - static ref INIT_LOCK: Mutex = Mutex::new(0); -} - -pub struct TestGuard {} - -impl Drop for TestGuard { - fn drop(&mut self) { - // TODO shutdown process cleanly. - /* - *g -= 1; - if *g == 0 { - unsafe { crate::V8::dispose() }; - crate::V8::shutdown_platform(); - } - drop(g); - */ - } -} - -pub fn setup() -> TestGuard { - let mut g = INIT_LOCK.lock().unwrap(); - *g += 1; - if *g == 1 { - crate::V8::initialize_platform(crate::platform::new_default_platform()); - crate::V8::initialize(); - } - drop(g); - TestGuard {} -} diff --git a/tests/test_api.rs b/tests/test_api.rs new file mode 100644 index 00000000..688065a6 --- /dev/null +++ b/tests/test_api.rs @@ -0,0 +1,106 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#[macro_use] +extern crate lazy_static; + +use rusty_v8 as v8; +use std::sync::Mutex; + +lazy_static! { + static ref INIT_LOCK: Mutex = Mutex::new(0); +} + +struct TestGuard {} + +impl Drop for TestGuard { + fn drop(&mut self) { + // TODO shutdown process cleanly. + /* + *g -= 1; + if *g == 0 { + unsafe { v8::V8::dispose() }; + v8::V8::shutdown_platform(); + } + drop(g); + */ + } +} + +fn setup() -> TestGuard { + let mut g = INIT_LOCK.lock().unwrap(); + *g += 1; + if *g == 1 { + v8::V8::initialize_platform(v8::platform::new_default_platform()); + v8::V8::initialize(); + } + drop(g); + TestGuard {} +} + +#[test] +fn handle_scope_nested() { + let g = setup(); + let mut params = v8::Isolate::create_params(); + params.set_array_buffer_allocator( + v8::array_buffer::Allocator::new_default_allocator(), + ); + let mut isolate = v8::Isolate::new(params); + let mut locker = v8::Locker::new(&mut isolate); + v8::HandleScope::enter(&mut locker, |scope| { + v8::HandleScope::enter(scope, |_scope| {}); + }); + drop(g); +} + +#[test] +#[allow(clippy::float_cmp)] +fn handle_scope_numbers() { + let g = setup(); + let mut params = v8::Isolate::create_params(); + params.set_array_buffer_allocator( + v8::array_buffer::Allocator::new_default_allocator(), + ); + let mut isolate = v8::Isolate::new(params); + let mut locker = v8::Locker::new(&mut isolate); + v8::HandleScope::enter(&mut locker, |scope| { + let l1 = v8::Integer::new(scope, -123); + let l2 = v8::Integer::new_from_unsigned(scope, 456); + v8::HandleScope::enter(scope, |scope2| { + let l3 = v8::Number::new(scope2, 78.9); + assert_eq!(l1.value(), -123); + assert_eq!(l2.value(), 456); + assert_eq!(l3.value(), 78.9); + assert_eq!(v8::Number::value(&l1), -123f64); + assert_eq!(v8::Number::value(&l2), 456f64); + }); + }); + drop(g); +} + +#[test] +fn isolate_new() { + let g = setup(); + let mut params = v8::Isolate::create_params(); + params.set_array_buffer_allocator( + v8::array_buffer::Allocator::new_default_allocator(), + ); + v8::Isolate::new(params); + drop(g); +} + +#[test] +fn get_version() { + assert!(v8::V8::get_version().len() > 3); +} + +#[test] +fn set_flags_from_command_line() { + let r = v8::V8::set_flags_from_command_line(vec![ + "binaryname".to_string(), + "--log-colour".to_string(), + "--should-be-ignored".to_string(), + ]); + assert_eq!( + r, + vec!["binaryname".to_string(), "--should-be-ignored".to_string()] + ); +}