From 280060acebeef06f4b774c3f1db026d22760ba67 Mon Sep 17 00:00:00 2001 From: jedel1043 Date: Fri, 2 Jun 2023 15:54:05 -0600 Subject: [PATCH] Fix WASM playground --- Cargo.lock | 11 +++++++++++ boa_engine/src/context/mod.rs | 25 +++++++++++++++++-------- boa_engine/src/module/mod.rs | 28 ++++++++++++++++++++++++++++ boa_wasm/Cargo.toml | 1 + boa_wasm/src/lib.rs | 5 +++++ 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1be9b71422d..483d21dee69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -604,6 +604,7 @@ version = "0.16.0" dependencies = [ "boa_engine", "chrono", + "console_error_panic_hook", "getrandom", "wasm-bindgen", ] @@ -936,6 +937,16 @@ dependencies = [ "windows-sys 0.42.0", ] +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if 1.0.0", + "wasm-bindgen", +] + [[package]] name = "const_fn" version = "0.4.9" diff --git a/boa_engine/src/context/mod.rs b/boa_engine/src/context/mod.rs index d6b517784f5..3863cff98d3 100644 --- a/boa_engine/src/context/mod.rs +++ b/boa_engine/src/context/mod.rs @@ -20,7 +20,7 @@ use crate::{ builtins, class::{Class, ClassBuilder}, job::{JobQueue, NativeJob, SimpleJobQueue}, - module::{ModuleLoader, SimpleModuleLoader}, + module::{IdleModuleLoader, ModuleLoader, SimpleModuleLoader}, native_function::NativeFunction, object::{shape::RootShape, FunctionObjectBuilder, JsObject}, optimizer::{Optimizer, OptimizerOptions, OptimizerStatistics}, @@ -873,6 +873,21 @@ impl<'icu, 'hooks, 'queue, 'module> ContextBuilder<'icu, 'hooks, 'queue, 'module let realm = Realm::create(&*host_hooks, &root_shape); let vm = Vm::new(realm.environment().clone()); + let module_loader = if let Some(loader) = self.module_loader { + loader + } else { + SimpleModuleLoader::new(Path::new(".")).map_or_else( + |_| { + let loader: &dyn ModuleLoader = &IdleModuleLoader; + loader.into() + }, + |loader| { + let loader: Rc = Rc::new(loader); + loader.into() + }, + ) + }; + let mut context = Context { realm, interner: self.interner.unwrap_or_default(), @@ -892,13 +907,7 @@ impl<'icu, 'hooks, 'queue, 'module> ContextBuilder<'icu, 'hooks, 'queue, 'module let queue: Rc = Rc::new(SimpleJobQueue::new()); queue.into() }), - module_loader: self.module_loader.unwrap_or_else(|| { - let loader: Rc = Rc::new( - SimpleModuleLoader::new(Path::new(".")) - .expect("failed to initialize default module loader"), - ); - loader.into() - }), + module_loader, optimizer_options: OptimizerOptions::OPTIMIZE_ALL, root_shape, parser_identifier: 0, diff --git a/boa_engine/src/module/mod.rs b/boa_engine/src/module/mod.rs index 01a5914b151..c9e798f6039 100644 --- a/boa_engine/src/module/mod.rs +++ b/boa_engine/src/module/mod.rs @@ -129,6 +129,29 @@ pub trait ModuleLoader { } } +/// A module loader that throws when trying to load any modules. +/// +/// Useful to disable the module system on platforms that don't have a filesystem, for example. +#[derive(Debug, Clone, Copy)] +pub struct IdleModuleLoader; + +impl ModuleLoader for IdleModuleLoader { + fn load_imported_module( + &self, + _referrer: Referrer, + _specifier: JsString, + finish_load: Box, &mut Context<'_>)>, + context: &mut Context<'_>, + ) { + finish_load( + Err(JsNativeError::typ() + .with_message("module resolution is disabled for this context") + .into()), + context, + ); + } +} + /// A simple module loader that loads modules relative to a root path. #[derive(Debug)] pub struct SimpleModuleLoader { @@ -139,6 +162,11 @@ pub struct SimpleModuleLoader { impl SimpleModuleLoader { /// Creates a new `SimpleModuleLoader` from a root module path. pub fn new>(root: P) -> JsResult { + if cfg!(target_family = "wasm") { + return Err(JsNativeError::typ() + .with_message("cannot resolve a relative path in WASM targets") + .into()); + } let root = root.as_ref(); let absolute = root.canonicalize().map_err(|e| { JsNativeError::typ() diff --git a/boa_wasm/Cargo.toml b/boa_wasm/Cargo.toml index beaf443d6fa..852a7b1b9d3 100644 --- a/boa_wasm/Cargo.toml +++ b/boa_wasm/Cargo.toml @@ -16,6 +16,7 @@ boa_engine.workspace = true wasm-bindgen = "0.2.86" getrandom = { version = "0.2.9", features = ["js"] } chrono = { version = "0.4.26", features = ["clock", "std", "wasmbind"] } +console_error_panic_hook = "0.1.7" [features] default = ["boa_engine/annex-b"] diff --git a/boa_wasm/src/lib.rs b/boa_wasm/src/lib.rs index 07ebe66a49d..a0ce5ba8424 100644 --- a/boa_wasm/src/lib.rs +++ b/boa_wasm/src/lib.rs @@ -63,6 +63,11 @@ use chrono as _; use getrandom as _; use wasm_bindgen::prelude::*; +#[wasm_bindgen(start)] +fn main() { + console_error_panic_hook::set_once(); +} + /// Evaluate the given ECMAScript code. #[wasm_bindgen] pub fn evaluate(src: &str) -> Result {