mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-03-09 05:27:08 -04:00
Add new_single_threaded_default_platform() (#659)
This is a v8::Platform implementation that doesn't spawn additional threads. Useful in combination with the --single_threaded flag.
This commit is contained in:
parent
de9a7e2698
commit
03e74c6f1b
4 changed files with 41 additions and 1 deletions
|
@ -1934,10 +1934,15 @@ v8::StartupData v8__SnapshotCreator__CreateBlob(
|
||||||
}
|
}
|
||||||
|
|
||||||
v8::Platform* v8__platform__NewDefaultPlatform() {
|
v8::Platform* v8__platform__NewDefaultPlatform() {
|
||||||
// TODO: support optional arguments.
|
// TODO(bnoordhuis) Support optional arguments.
|
||||||
return v8::platform::NewDefaultPlatform().release();
|
return v8::platform::NewDefaultPlatform().release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Platform* v8__platform__NewSingleThreadedDefaultPlatform() {
|
||||||
|
// TODO(bnoordhuis) Support optional arguments.
|
||||||
|
return v8::platform::NewSingleThreadedDefaultPlatform().release();
|
||||||
|
}
|
||||||
|
|
||||||
void v8__Platform__DELETE(v8::Platform* self) { delete self; }
|
void v8__Platform__DELETE(v8::Platform* self) { delete self; }
|
||||||
|
|
||||||
void v8_inspector__V8Inspector__Channel__BASE__sendResponse(
|
void v8_inspector__V8Inspector__Channel__BASE__sendResponse(
|
||||||
|
|
|
@ -110,6 +110,7 @@ pub use isolate_create_params::CreateParams;
|
||||||
pub use module::*;
|
pub use module::*;
|
||||||
pub use object::*;
|
pub use object::*;
|
||||||
pub use platform::new_default_platform;
|
pub use platform::new_default_platform;
|
||||||
|
pub use platform::new_single_threaded_default_platform;
|
||||||
pub use platform::Platform;
|
pub use platform::Platform;
|
||||||
pub use primitives::*;
|
pub use primitives::*;
|
||||||
pub use private::*;
|
pub use private::*;
|
||||||
|
|
|
@ -3,13 +3,23 @@ use crate::support::UniquePtr;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn v8__platform__NewDefaultPlatform() -> *mut Platform;
|
fn v8__platform__NewDefaultPlatform() -> *mut Platform;
|
||||||
|
fn v8__platform__NewSingleThreadedDefaultPlatform() -> *mut Platform;
|
||||||
fn v8__Platform__DELETE(this: *mut Platform);
|
fn v8__Platform__DELETE(this: *mut Platform);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a new instance of the default v8::Platform implementation.
|
||||||
pub fn new_default_platform() -> UniquePtr<Platform> {
|
pub fn new_default_platform() -> UniquePtr<Platform> {
|
||||||
unsafe { UniquePtr::from_raw(v8__platform__NewDefaultPlatform()) }
|
unsafe { UniquePtr::from_raw(v8__platform__NewDefaultPlatform()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The same as new_default_platform() but disables the worker thread pool.
|
||||||
|
/// It must be used with the --single-threaded V8 flag.
|
||||||
|
pub fn new_single_threaded_default_platform() -> UniquePtr<Platform> {
|
||||||
|
unsafe {
|
||||||
|
UniquePtr::from_raw(v8__platform__NewSingleThreadedDefaultPlatform())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Platform(Opaque);
|
pub struct Platform(Opaque);
|
||||||
|
|
24
tests/test_single_threaded_default_platform.rs
Normal file
24
tests/test_single_threaded_default_platform.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
use rusty_v8 as v8;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn single_threaded_default_platform() {
|
||||||
|
v8::V8::set_flags_from_string("--single_threaded");
|
||||||
|
v8::V8::initialize_platform(
|
||||||
|
v8::new_single_threaded_default_platform().unwrap(),
|
||||||
|
);
|
||||||
|
v8::V8::initialize();
|
||||||
|
|
||||||
|
{
|
||||||
|
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, "Math.random()").unwrap();
|
||||||
|
let script = v8::Script::compile(scope, source, None).unwrap();
|
||||||
|
let result = script.run(scope).unwrap();
|
||||||
|
let _ = result.to_string(scope).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe { v8::V8::dispose() };
|
||||||
|
v8::V8::shutdown_platform();
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue