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

Add failing es_snapshot test #6111

Merged
merged 5 commits into from
Jun 5, 2020
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 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();
}
}