Skip to content

Instantly share code, notes, and snippets.

@r33drichards
Created April 14, 2025 21:26
Show Gist options
  • Save r33drichards/2188fab814b55a21ab85c622902444a1 to your computer and use it in GitHub Desktop.
Save r33drichards/2188fab814b55a21ab85c622902444a1 to your computer and use it in GitHub Desktop.
use log::info;
use std::io::Write;
use v8::OwnedIsolate;
fn eval<'s>(
scope: &mut v8::HandleScope<'s>,
code: &str,
) -> Option<v8::Local<'s, v8::Value>> {
let scope = &mut v8::EscapableHandleScope::new(scope);
let source = v8::String::new(scope, code).unwrap();
let script = v8::Script::compile(scope, source, None).unwrap();
let r = script.run(scope);
r.map(|v| scope.escape(v))
}
fn main() {
let platform = v8::new_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();
let startup_data = {
let mut snapshot_creator = v8::Isolate::snapshot_creator(None, None);
{
let scope = &mut v8::HandleScope::new(&mut snapshot_creator);
let context = v8::Context::new(scope, Default::default());
let scope = &mut v8::ContextScope::new(scope, context);
eval(scope, "b = 2 + 3").unwrap();
scope.set_default_context(context);
}
snapshot_creator
.create_blob(v8::FunctionCodeHandling::Clear)
.unwrap()
};
info!("snapshot created");
info!("writing snapshot to file snapshot.bin in current directory");
let mut file = std::fs::File::create("snapshot.bin").unwrap();
file.write_all(&startup_data).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment