mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-01-12 17:09:28 -05:00
Simplify public API and move tests to tests/test_api.rs (#24)
This commit is contained in:
parent
b4674c62b8
commit
803ae45263
6 changed files with 128 additions and 111 deletions
18
src/V8.rs
18
src/V8.rs
|
@ -79,19 +79,6 @@ pub fn set_flags_from_command_line(args: Vec<String>) -> Vec<String> {
|
|||
.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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<CreateParams>) -> 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> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
22
src/lib.rs
22
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;
|
||||
|
|
|
@ -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<u32> = 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 {}
|
||||
}
|
106
tests/test_api.rs
Normal file
106
tests/test_api.rs
Normal file
|
@ -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<u32> = 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()]
|
||||
);
|
||||
}
|
Loading…
Reference in a new issue