Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

platform: pump_message_loop, run_idle_tasks, GLOBAL_PLATFORM #706

Merged
merged 3 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rusty_v8 as v8;

fn main() {
// Initialize V8.
let platform = v8::new_default_platform().unwrap();
let platform = v8::new_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();

Expand Down
2 changes: 1 addition & 1 deletion examples/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn log_callback(

fn main() {
// Initialize V8.
let platform = v8::new_default_platform().unwrap();
let platform = v8::new_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();

Expand Down
22 changes: 17 additions & 5 deletions examples/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rusty_v8 as v8;

fn main() {
// Initialize V8.
let platform = v8::new_default_platform().unwrap();
let platform = v8::new_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();

Expand Down Expand Up @@ -79,11 +79,15 @@ fn run_main(
let script: &str = &args[i + 1];
skip_next = true;

// TODO: pump event loop (not implemented on rusty_v8?)
// while v8::Platform::pump_message_loop(&platform, isolate) {
// // do nothing
// }
execute_string(scope, script, "unnamed", false, true);

while v8::Platform::pump_message_loop(
&v8::V8::get_current_platform(),
scope,
false,
) {
// do nothing
}
}
arg => {
if arg.starts_with("--") {
Expand All @@ -94,6 +98,14 @@ fn run_main(
// Use all other arguments as names of files to load and run.
let script = std::fs::read_to_string(arg).expect("failed to read file");
execute_string(scope, &script, arg, false, true);

while v8::Platform::pump_message_loop(
&v8::V8::get_current_platform(),
scope,
false,
) {
// do nothing
}
}
}
}
Expand Down
67 changes: 45 additions & 22 deletions src/V8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::Mutex;
use std::vec::Vec;

use crate::platform::Platform;
use crate::support::UniqueRef;
use crate::support::SharedRef;
use crate::support::UnitType;

extern "C" {
Expand Down Expand Up @@ -61,24 +61,26 @@ where
}
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug)]
enum GlobalState {
Uninitialized,
PlatformInitialized,
Initialized,
Disposed,
PlatformInitialized(SharedRef<Platform>),
Initialized(SharedRef<Platform>),
Disposed(SharedRef<Platform>),
PlatformShutdown,
}
use GlobalState::*;

lazy_static! {
static ref GLOBAL_STATE: Mutex<GlobalState> =
Mutex::new(GlobalState::Uninitialized);
static ref GLOBAL_STATE: Mutex<GlobalState> = Mutex::new(Uninitialized);
}

pub fn assert_initialized() {
let global_state_guard = GLOBAL_STATE.lock().unwrap();
assert_eq!(*global_state_guard, Initialized);
match *global_state_guard {
Initialized(_) => {}
_ => panic!("Invalid global state"),
};
}

/// Pass the command line arguments to v8.
Expand Down Expand Up @@ -173,25 +175,41 @@ pub fn get_version() -> &'static str {
c_str.to_str().unwrap()
}

// 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.
/// Sets the v8::Platform to use. This should be invoked before V8 is
/// initialized.
pub fn initialize_platform(platform: UniqueRef<Platform>) {
pub fn initialize_platform(platform: SharedRef<Platform>) {
let mut global_state_guard = GLOBAL_STATE.lock().unwrap();
assert_eq!(*global_state_guard, Uninitialized);
unsafe { v8__V8__InitializePlatform(platform.into_raw()) };
*global_state_guard = PlatformInitialized;
*global_state_guard = match *global_state_guard {
Uninitialized => PlatformInitialized(platform.clone()),
_ => panic!("Invalid global state"),
};

{
unsafe {
v8__V8__InitializePlatform(&*platform as *const Platform as *mut _)
};
}
}

/// Initializes V8. This function needs to be called before the first Isolate
/// is created. It always returns true.
pub fn initialize() {
let mut global_state_guard = GLOBAL_STATE.lock().unwrap();
assert_eq!(*global_state_guard, PlatformInitialized);
unsafe { v8__V8__Initialize() };
*global_state_guard = Initialized;
*global_state_guard = match *global_state_guard {
PlatformInitialized(ref platform) => Initialized(platform.clone()),
_ => panic!("Invalid global state"),
};
unsafe { v8__V8__Initialize() }
}

/// Sets the v8::Platform to use. This should be invoked before V8 is
/// initialized.
pub fn get_current_platform() -> SharedRef<Platform> {
let global_state_guard = GLOBAL_STATE.lock().unwrap();
match *global_state_guard {
Initialized(ref platform) => platform.clone(),
_ => panic!("Invalid global state"),
}
}

/// Releases any resources used by v8 and stops any utility threads
Expand All @@ -208,17 +226,22 @@ pub fn initialize() {
/// to a crash.
pub unsafe fn dispose() -> bool {
let mut global_state_guard = GLOBAL_STATE.lock().unwrap();
assert_eq!(*global_state_guard, Initialized);
*global_state_guard = match *global_state_guard {
Initialized(ref platform) => Disposed(platform.clone()),
_ => panic!("Invalid global state"),
};
assert!(v8__V8__Dispose());
*global_state_guard = Disposed;
true
}

/// Clears all references to the v8::Platform. This should be invoked after
/// V8 was disposed.
pub fn shutdown_platform() {
let mut global_state_guard = GLOBAL_STATE.lock().unwrap();
assert_eq!(*global_state_guard, Disposed);
// First shutdown platform, then drop platform
unsafe { v8__V8__ShutdownPlatform() };
*global_state_guard = PlatformShutdown;
*global_state_guard = match *global_state_guard {
Disposed(_) => PlatformShutdown,
_ => panic!("Invalid global state"),
};
}
58 changes: 52 additions & 6 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1967,18 +1967,64 @@ v8::StartupData v8__SnapshotCreator__CreateBlob(
return self->CreateBlob(function_code_handling);
}

v8::Platform* v8__platform__NewDefaultPlatform() {
// TODO(bnoordhuis) Support optional arguments.
return v8::platform::NewDefaultPlatform().release();
v8::Platform* v8__Platform__NewDefaultPlatform(int thread_pool_size,
bool idle_task_support) {
return v8::platform::NewDefaultPlatform(
thread_pool_size,
idle_task_support ? v8::platform::IdleTaskSupport::kEnabled
: v8::platform::IdleTaskSupport::kDisabled,
v8::platform::InProcessStackDumping::kDisabled, nullptr)
.release();
}

v8::Platform* v8__platform__NewSingleThreadedDefaultPlatform() {
// TODO(bnoordhuis) Support optional arguments.
return v8::platform::NewSingleThreadedDefaultPlatform().release();
v8::Platform* v8__Platform__NewSingleThreadedDefaultPlatform(
bool idle_task_support) {
return v8::platform::NewSingleThreadedDefaultPlatform(
idle_task_support ? v8::platform::IdleTaskSupport::kEnabled
: v8::platform::IdleTaskSupport::kDisabled,
v8::platform::InProcessStackDumping::kDisabled, nullptr)
.release();
}

bool v8__Platform__PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate,
bool wait_for_work) {
return v8::platform::PumpMessageLoop(
platform, isolate,
wait_for_work ? v8::platform::MessageLoopBehavior::kWaitForWork
: v8::platform::MessageLoopBehavior::kDoNotWait);
}

void v8__Platform__RunIdleTasks(v8::Platform* platform, v8::Isolate* isolate,
double idle_time_in_seconds) {
v8::platform::RunIdleTasks(platform, isolate, idle_time_in_seconds);
}

void v8__Platform__DELETE(v8::Platform* self) { delete self; }

two_pointers_t std__shared_ptr__v8__Platform__CONVERT__std__unique_ptr(
v8::Platform* unique_ptr) {
return make_pod<two_pointers_t>(std::shared_ptr<v8::Platform>(unique_ptr));
}

v8::Platform* std__shared_ptr__v8__Platform__get(
const std::shared_ptr<v8::Platform>& ptr) {
return ptr.get();
}

two_pointers_t std__shared_ptr__v8__Platform__COPY(
const std::shared_ptr<v8::Platform>& ptr) {
return make_pod<two_pointers_t>(ptr);
}

void std__shared_ptr__v8__Platform__reset(std::shared_ptr<v8::Platform>* ptr) {
ptr->reset();
}

long std__shared_ptr__v8__Platform__use_count(
const std::shared_ptr<v8::Platform>& ptr) {
return ptr.use_count();
}

void v8_inspector__V8Inspector__Channel__BASE__sendResponse(
v8_inspector::V8Inspector::Channel* self, int callId,
v8_inspector::StringBuffer* message);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! ```rust
//! use rusty_v8 as v8;
//!
//! let platform = v8::new_default_platform().unwrap();
//! let platform = v8::new_default_platform(0, false).make_shared();
//! v8::V8::initialize_platform(platform);
//! v8::V8::initialize();
//!
Expand Down
Loading