Browse Source

Fix WASM playground (#2992)

pull/2994/head
José Julián Espina 2 years ago committed by GitHub
parent
commit
65bf54d7e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      Cargo.lock
  2. 25
      boa_engine/src/context/mod.rs
  3. 28
      boa_engine/src/module/mod.rs
  4. 1
      boa_wasm/Cargo.toml
  5. 5
      boa_wasm/src/lib.rs

11
Cargo.lock generated

@ -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"

25
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<dyn ModuleLoader> = 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<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,

28
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<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 {
@ -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()

1
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"]

5
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<String, JsValue> {

Loading…
Cancel
Save