summaryrefslogtreecommitdiff
path: root/serde_v8/src/utils.rs
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2021-03-26 03:36:46 +0100
committerGitHub <noreply@github.com>2021-03-25 22:36:46 -0400
commit3d2e05dc7b6735fe0b81ceb6fe469270da56aff4 (patch)
tree6f62852cb1ae3eee02ab51f7115703665ad232d1 /serde_v8/src/utils.rs
parente7954413e16d5814db5da6389f8d6e0c328812aa (diff)
Introduce serde_v8 (#9722)
Diffstat (limited to 'serde_v8/src/utils.rs')
-rw-r--r--serde_v8/src/utils.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/serde_v8/src/utils.rs b/serde_v8/src/utils.rs
new file mode 100644
index 000000000..f9f81837c
--- /dev/null
+++ b/serde_v8/src/utils.rs
@@ -0,0 +1,33 @@
+use rusty_v8 as v8;
+use std::sync::Once;
+
+pub fn js_exec<'s>(
+ scope: &mut v8::HandleScope<'s>,
+ src: &str,
+) -> v8::Local<'s, v8::Value> {
+ let code = v8::String::new(scope, src).unwrap();
+ let script = v8::Script::compile(scope, code, None).unwrap();
+ script.run(scope).unwrap()
+}
+
+pub fn v8_init() {
+ let platform = v8::new_default_platform().unwrap();
+ v8::V8::initialize_platform(platform);
+ v8::V8::initialize();
+}
+
+pub fn v8_shutdown() {
+ unsafe {
+ v8::V8::dispose();
+ }
+ v8::V8::shutdown_platform();
+}
+
+pub fn v8_do(f: impl FnOnce()) {
+ static V8_INIT: Once = Once::new();
+ V8_INIT.call_once(|| {
+ v8_init();
+ });
+ f();
+ // v8_shutdown();
+}