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

Fix WASM playground #2992

Merged
merged 1 commit into from
Jun 3, 2023
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
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions boa_engine/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<dyn ModuleLoader> = Rc::new(loader);
loader.into()
},
)
};

let mut context = Context {
realm,
interner: self.interner.unwrap_or_default(),
Expand All @@ -892,13 +907,7 @@ impl<'icu, 'hooks, 'queue, 'module> ContextBuilder<'icu, 'hooks, 'queue, 'module
let queue: Rc<dyn JobQueue> = Rc::new(SimpleJobQueue::new());
queue.into()
}),
module_loader: self.module_loader.unwrap_or_else(|| {
let loader: Rc<dyn ModuleLoader> = 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,
Expand Down
28 changes: 28 additions & 0 deletions boa_engine/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn FnOnce(JsResult<Module>, &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 {
Expand All @@ -139,6 +162,11 @@ pub struct SimpleModuleLoader {
impl SimpleModuleLoader {
/// Creates a new `SimpleModuleLoader` from a root module path.
pub fn new<P: AsRef<Path>>(root: P) -> JsResult<Self> {
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()
Expand Down
1 change: 1 addition & 0 deletions boa_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
5 changes: 5 additions & 0 deletions boa_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JsValue> {
Expand Down