Skip to content

Commit

Permalink
fix(core): ES module snapshots (#6111)
Browse files Browse the repository at this point in the history
Co-authored-by: Bert Belder <bertbelder@gmail.com>
  • Loading branch information
ry and piscisaureus authored Jun 5, 2020
1 parent d2243b1 commit 79d9cf5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/core_isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl CoreIsolate {
}

/// Executes a bit of built-in JavaScript to provide Deno.sharedQueue.
fn shared_init(&mut self) {
pub(crate) fn shared_init(&mut self) {
if self.needs_init {
self.needs_init = false;
js_check(self.execute("core.js", include_str!("core.js")));
Expand Down
54 changes: 53 additions & 1 deletion core/es_isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ impl EsIsolate {
/// the V8 exception. By default this type is JSError, however it may be a
/// different type if CoreIsolate::set_js_error_create_fn() has been used.
pub fn mod_evaluate(&mut self, id: ModuleId) -> Result<(), ErrBox> {
self.shared_init();
let state_rc = Self::state(self);
let state = state_rc.borrow();

Expand Down Expand Up @@ -553,6 +554,7 @@ impl EsIsolate {
specifier: &ModuleSpecifier,
code: Option<String>,
) -> Result<ModuleId, ErrBox> {
self.shared_init();
let loader = {
let state_rc = Self::state(self);
let state = state_rc.borrow();
Expand All @@ -573,6 +575,12 @@ impl EsIsolate {
self.mod_instantiate(root_id).map(|_| root_id)
}

pub fn snapshot(&mut self) -> v8::StartupData {
let state_rc = Self::state(self);
std::mem::take(&mut state_rc.borrow_mut().modules);
CoreIsolate::snapshot(self)
}

pub fn state(isolate: &v8::Isolate) -> Rc<RefCell<EsIsolateState>> {
let s = isolate.get_slot::<Rc<RefCell<EsIsolateState>>>().unwrap();
s.clone()
Expand Down Expand Up @@ -677,7 +685,7 @@ pub mod tests {

#[test]
fn test_mods() {
#[derive(Clone, Default)]
#[derive(Default)]
struct ModsLoader {
pub count: Arc<AtomicUsize>,
}
Expand Down Expand Up @@ -966,4 +974,48 @@ pub mod tests {
let _ = isolate.poll_unpin(cx);
})
}

#[test]
fn es_snapshot() {
#[derive(Default)]
struct ModsLoader;

impl ModuleLoader for ModsLoader {
fn resolve(
&self,
specifier: &str,
referrer: &str,
_is_main: bool,
) -> Result<ModuleSpecifier, ErrBox> {
assert_eq!(specifier, "file:///main.js");
assert_eq!(referrer, ".");
let s = ModuleSpecifier::resolve_import(specifier, referrer).unwrap();
Ok(s)
}

fn load(
&self,
_module_specifier: &ModuleSpecifier,
_maybe_referrer: Option<ModuleSpecifier>,
_is_dyn_import: bool,
) -> Pin<Box<ModuleSourceFuture>> {
unreachable!()
}
}

let loader = std::rc::Rc::new(ModsLoader::default());
let mut runtime_isolate = EsIsolate::new(loader, StartupData::None, true);

let specifier = ModuleSpecifier::resolve_url("file:///main.js").unwrap();
let source_code = "Deno.core.print('hello\\n')".to_string();

let module_id = futures::executor::block_on(
runtime_isolate.load_module(&specifier, Some(source_code)),
)
.unwrap();

js_check(runtime_isolate.mod_evaluate(module_id));

let _snapshot = runtime_isolate.snapshot();
}
}

0 comments on commit 79d9cf5

Please sign in to comment.