From 05ce4006c06a9b7bc3280dbb4d852f14e2251e4d Mon Sep 17 00:00:00 2001 From: Ry Dahl Date: Fri, 15 Nov 2019 14:29:19 -0500 Subject: [PATCH] bind v8::V8::Initialize and v8::V8::Dispose (#10) --- src/v8.cc | 4 ++++ src/v8.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/v8.cc b/src/v8.cc index 8555d3b1..db6d7dad 100644 --- a/src/v8.cc +++ b/src/v8.cc @@ -6,4 +6,8 @@ void v8__V8__SetFlagsFromCommandLine(int *argc, char **argv) { } const char *v8__V8__GetVersion() { return v8::V8::GetVersion(); } + +void v8__V8__Initialize() { v8::V8::Initialize(); } + +bool v8__V8__Dispose() { return v8::V8::Dispose(); } } diff --git a/src/v8.rs b/src/v8.rs index a586f2df..d90f5fd6 100644 --- a/src/v8.rs +++ b/src/v8.rs @@ -11,8 +11,9 @@ extern "C" { argc: *mut c_int, argv: *mut *mut c_char, ); - pub fn v8__V8__GetVersion() -> *const c_char; + pub fn v8__V8__Initialize(); + pub fn v8__V8__Dispose() -> bool; } /// Pass the command line arguments to v8. @@ -68,6 +69,7 @@ fn test_set_flags_from_command_line() { ); } +/// Get the version string. pub fn get_version() -> &'static str { let version = unsafe { v8__V8__GetVersion() }; let c_str = unsafe { CStr::from_ptr(version) }; @@ -78,3 +80,26 @@ pub fn get_version() -> &'static str { fn test_get_version() { assert!(get_version().len() > 3); } + +/// Initializes V8. This function needs to be called before the first Isolate +/// is created. It always returns true. +pub fn initialize() { + unsafe { v8__V8__Initialize() } +} + +/// Releases any resources used by v8 and stops any utility threads +/// that may be running. Note that disposing v8 is permanent, it +/// cannot be reinitialized. +/// +/// It should generally not be necessary to dispose v8 before exiting +/// a process, this should happen automatically. It is only necessary +/// to use if the process needs the resources taken up by v8. +pub fn dispose() -> bool { + unsafe { v8__V8__Dispose() } +} + +#[test] +fn test_initialize_dispose() { + initialize(); + dispose(); +}