use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::path::PathBuf;
use anyhow::{Context, Result, bail};
use heck::{ToLowerCamelCase, ToUpperCamelCase};
use wit_component::WitPrinter;
use wit_parser::{
CloneMaps, FunctionKind, Interface, InterfaceId, PackageId, Resolve, Span, TypeDefKind,
WorldId, WorldItem, WorldKey,
};
use super::{ENGINE_WORLD_NAME, SCRIPT_ENGINE_WIT};
const SCRIPT_LOADER_KEY: &str = "jco:script-engine/script-loader@0.1.0";
const SCRIPT_LOADER_EXPORT: &str = "scriptLoader";
#[derive(Debug, Clone, bon::Builder)]
#[non_exhaustive]
pub struct WitInput {
#[builder(into)]
pub path: PathBuf,
#[builder(into)]
pub world: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Backend {
StarlingMonkey,
QuickJs,
}
#[derive(Debug, Clone, bon::Builder)]
#[non_exhaustive]
pub struct EngineWorld {
#[builder(into)]
pub wit: String,
#[builder(into)]
pub world_name: String,
#[builder(into)]
pub bootstrap: String,
pub imports: Vec<String>,
pub exports: Vec<String>,
}
pub fn engine_world(inputs: &[WitInput], backend: Backend) -> Result<EngineWorld> {
if inputs.is_empty() {
bail!("at least one WIT world is required to build a script engine");
}
let mut resolve = Resolve::default();
let engine_pkg = resolve
.push_str("script-engine.wit", SCRIPT_ENGINE_WIT)
.context("parsing the jco:script-engine WIT")?;
let engine = resolve
.select_world(&[engine_pkg], Some(ENGINE_WORLD_NAME))
.context("selecting the jco:script-engine world")?;
let mut clone_maps = CloneMaps::default();
for input in inputs {
let mut input_resolve = Resolve::default();
let (pkg, _) = input_resolve
.push_path(&input.path)
.with_context(|| format!("parsing WIT at [{}]", input.path.display()))?;
let world = input_resolve
.select_world(&[pkg], input.world.as_deref())
.with_context(|| format!("selecting a world in [{}]", input.path.display()))?;
let world_name = input_resolve.worlds[world].name.clone();
let remap = resolve
.merge(input_resolve)
.with_context(|| format!("merging packages from [{}]", input.path.display()))?;
let world = remap
.map_world(world, Span::default())
.with_context(|| format!("locating world [{world_name}] after merge"))?;
resolve
.merge_worlds(world, engine, &mut clone_maps)
.with_context(|| {
format!(
"merging world [{world_name}] from [{}] into the engine world",
input.path.display()
)
})?;
}
let bootstrap = Bootstrap::new(&resolve, engine, backend)?;
let wit = print_wit(&resolve, engine_pkg)?;
Ok(EngineWorld::builder()
.wit(wit)
.world_name(ENGINE_WORLD_NAME)
.bootstrap(bootstrap.render())
.imports(bootstrap.import_specifiers())
.exports(bootstrap.export_names())
.build())
}
fn print_wit(resolve: &Resolve, main: PackageId) -> Result<String> {
let nested: Vec<PackageId> = resolve
.topological_packages()
.into_iter()
.filter(|id| *id != main)
.collect();
let mut printer = WitPrinter::default();
printer
.print(resolve, main, &nested)
.context("printing the engine world WIT")?;
Ok(printer.output.to_string())
}
#[derive(Debug)]
struct Import {
specifier: String,
world_function: bool,
}
#[derive(Debug)]
enum Export {
Function { js_name: String },
Interface {
js_name: String,
key: String,
functions: Vec<String>,
resources: Vec<ResourceExport>,
},
}
impl Export {
fn js_name(&self) -> &str {
match self {
Export::Function { js_name } | Export::Interface { js_name, .. } => js_name,
}
}
}
#[derive(Debug)]
struct ResourceExport {
js_name: String,
has_constructor: bool,
methods: Vec<String>,
statics: Vec<String>,
}
struct Bootstrap {
imports: Vec<Import>,
exports: Vec<Export>,
}
impl Bootstrap {
fn new(resolve: &Resolve, world: WorldId, backend: Backend) -> Result<Self> {
let world = &resolve.worlds[world];
let mut imports = Vec::new();
for (key, item) in world.imports.iter() {
match item {
WorldItem::Interface { .. } => imports.push(Import {
specifier: resolve.name_world_key(key),
world_function: false,
}),
WorldItem::Function(function) => {
if backend == Backend::QuickJs {
bail!(
"the engine world imports the function [{}] directly; the quickjs \
backend has no module specifier for world-level function imports, \
so move it into an interface or use the starlingmonkey backend",
function.name
);
}
imports.push(Import {
specifier: function.name.clone(),
world_function: true,
});
}
WorldItem::Type { .. } => {}
}
}
let mut exports = Vec::new();
let mut seen: BTreeMap<String, String> = BTreeMap::new();
for (key, item) in world.exports.iter() {
let key_name = resolve.name_world_key(key);
if key_name == SCRIPT_LOADER_KEY {
continue;
}
let export = match item {
WorldItem::Interface { id, .. } => interface_export(resolve, key, *id, &key_name)?,
WorldItem::Function(function) => Export::Function {
js_name: function.name.to_lower_camel_case(),
},
WorldItem::Type { .. } => continue,
};
let js_name = export.js_name().to_string();
if js_name == SCRIPT_LOADER_EXPORT {
bail!(
"the export [{key_name}] would be exported as [{js_name}], which the engine \
reserves for jco:script-engine/script-loader"
);
}
if let Some(previous) = seen.insert(js_name.clone(), key_name.clone()) {
bail!(
"the exports [{previous}] and [{key_name}] would both be exported as \
[{js_name}]; a script engine needs distinct JavaScript export names"
);
}
exports.push(export);
}
Ok(Self { imports, exports })
}
fn import_specifiers(&self) -> Vec<String> {
self.imports.iter().map(|i| i.specifier.clone()).collect()
}
fn export_names(&self) -> Vec<String> {
self.exports
.iter()
.map(|e| e.js_name().to_string())
.collect()
}
fn render(&self) -> String {
let mut out = String::new();
out.push_str(
"// Generated by `jco componentize --script-only`. This module is the engine: its\n\
// exports forward to whichever script `scriptLoader.load` evaluated last.\n\n",
);
for (idx, import) in self.imports.iter().enumerate() {
if import.world_function {
let _ = writeln!(
out,
"import $import{idx} from {};",
js_str(&import.specifier)
);
} else {
let _ = writeln!(
out,
"import * as $import{idx} from {};",
js_str(&import.specifier)
);
}
}
out.push_str("\nconst $modules = new Map([\n");
for (idx, import) in self.imports.iter().enumerate() {
if import.world_function {
let _ = writeln!(
out,
" [{}, $namespace({{ default: $import{idx} }})],",
js_str(&import.specifier)
);
} else {
let _ = writeln!(
out,
" [{}, $namespace($import{idx})],",
js_str(&import.specifier)
);
}
}
out.push_str("]);\n\n");
out.push_str(RUNTIME_JS);
out.push('\n');
let mut links = String::new();
for (idx, export) in self.exports.iter().enumerate() {
match export {
Export::Function { js_name } => {
let _ = writeln!(
out,
"function $export{idx}(...args) {{\n return $lookup([{}])(...args);\n}}\n\
export {{ $export{idx} as {js_name} }};\n",
js_str(js_name)
);
}
Export::Interface {
js_name,
key,
functions,
resources,
} => {
let _ = writeln!(out, "// {key}");
for resource in resources {
let class = format!("$export{idx}${}", resource.js_name);
let path = format!("{}, {}", js_str(js_name), js_str(&resource.js_name));
let _ = writeln!(out, "class {class} {{");
if resource.has_constructor {
let _ = writeln!(
out,
" constructor(...args) {{\n return new ($lookup([{path}]))(...args);\n }}"
);
} else {
let _ = writeln!(
out,
" constructor() {{\n throw new TypeError({});\n }}",
js_str(&format!(
"{}.{} has no constructor in the engine world",
js_name, resource.js_name
))
);
}
for method in &resource.methods {
let _ = writeln!(
out,
" {}(...args) {{\n return $method([{path}], {class}, {}).call(this, ...args);\n }}",
js_str(method),
js_str(method)
);
}
for stat in &resource.statics {
let _ = writeln!(
out,
" static {}(...args) {{\n return $lookup([{path}, {}])(...args);\n }}",
js_str(stat),
js_str(stat)
);
}
out.push_str("}\n");
let _ = writeln!(links, " $link([{path}], {class});");
}
let _ = writeln!(out, "const $export{idx} = {{");
for resource in resources {
let _ = writeln!(
out,
" {}: $export{idx}${},",
js_str(&resource.js_name),
resource.js_name
);
}
for function in functions {
let _ = writeln!(
out,
" {}(...args) {{\n return $lookup([{}, {}])(...args);\n }},",
js_str(function),
js_str(js_name),
js_str(function)
);
}
let _ = writeln!(out, "}};\nexport {{ $export{idx} as {js_name} }};\n");
}
}
}
let _ = writeln!(out, "function $linkResources() {{\n{links}}}\n");
let _ = writeln!(out, "export {{ $scriptLoader as {SCRIPT_LOADER_EXPORT} }};");
out
}
}
fn interface_export(
resolve: &Resolve,
key: &WorldKey,
id: InterfaceId,
key_name: &str,
) -> Result<Export> {
let iface: &Interface = &resolve.interfaces[id];
let js_name = match key {
WorldKey::Name(name) => name.to_lower_camel_case(),
WorldKey::Interface(_) => iface
.name
.as_deref()
.with_context(|| format!("exported interface [{key_name}] has no name"))?
.to_lower_camel_case(),
};
let mut resources: BTreeMap<String, ResourceExport> = BTreeMap::new();
for (name, type_id) in iface.types.iter() {
if resolve.types[*type_id].kind == TypeDefKind::Resource {
resources.insert(
name.clone(),
ResourceExport {
js_name: name.to_upper_camel_case(),
has_constructor: false,
methods: Vec::new(),
statics: Vec::new(),
},
);
}
}
let mut functions = Vec::new();
for function in iface.functions.values() {
let resource = match function.kind.resource() {
None => {
functions.push(function.item_name().to_lower_camel_case());
continue;
}
Some(resource) => resource,
};
let resource_name = resolve.types[resource]
.name
.as_deref()
.with_context(|| format!("resource of [{}] has no name", function.name))?;
let export = resources.get_mut(resource_name).with_context(|| {
format!("resource [{resource_name}] is not defined in [{key_name}]")
})?;
match function.kind {
FunctionKind::Constructor(_) => export.has_constructor = true,
FunctionKind::Method(_) | FunctionKind::AsyncMethod(_) => export
.methods
.push(function.item_name().to_lower_camel_case()),
FunctionKind::Static(_) | FunctionKind::AsyncStatic(_) => export
.statics
.push(function.item_name().to_lower_camel_case()),
FunctionKind::Freestanding | FunctionKind::AsyncFreestanding => unreachable!(),
}
}
Ok(Export::Interface {
js_name,
key: key_name.to_string(),
functions,
resources: resources.into_values().collect(),
})
}
const RUNTIME_JS: &str = include_str!("runtime.js");
fn js_str(s: &str) -> String {
format!("{s:?}")
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
fn write_wit(dir: &std::path::Path, name: &str, contents: &str) -> PathBuf {
let path = dir.join(name);
fs::write(&path, contents).unwrap();
path
}
const WORLD_A: &str = r#"
package local:a;
interface greeter {
resource counter {
constructor(start: u32);
next: static func() -> counter;
get: func() -> u32;
}
greet: func(name: string) -> string;
}
world a {
import log-it: func(message: string);
import wasi:cli/environment@0.2.3;
export greeter;
export run: func();
}
package wasi:cli@0.2.3 {
interface environment {
get-environment: func() -> list<tuple<string, string>>;
}
}
"#;
const WORLD_B: &str = r#"
package local:b;
interface calc {
add: func(a: u32, b: u32) -> u32;
}
world b {
import wasi:cli/environment@0.2.3;
export calc;
export run: func();
}
package wasi:cli@0.2.3 {
interface environment {
get-environment: func() -> list<tuple<string, string>>;
}
}
"#;
#[test]
fn merges_worlds_and_generates_bootstrap() {
let dir = tempdir();
let a = write_wit(&dir, "a.wit", WORLD_A);
let b = write_wit(&dir, "b.wit", WORLD_B);
let engine = engine_world(
&[
WitInput::builder().path(a).build(),
WitInput::builder().path(b).world("b").build(),
],
Backend::StarlingMonkey,
)
.unwrap();
let mut resolve = Resolve::default();
let pkg = resolve.push_str("engine.wit", &engine.wit).unwrap();
let world = resolve.select_world(&[pkg], Some("engine")).unwrap();
let world = &resolve.worlds[world];
let mut exports: Vec<String> = world
.exports
.keys()
.map(|key| resolve.name_world_key(key))
.collect();
exports.sort();
assert_eq!(
exports,
[
"jco:script-engine/script-loader@0.1.0",
"local:a/greeter",
"local:b/calc",
"run",
]
);
let mut imports: Vec<String> = world
.imports
.keys()
.map(|key| resolve.name_world_key(key))
.collect();
imports.sort();
assert_eq!(imports, ["log-it", "wasi:cli/environment@0.2.3"]);
let mut engine_imports = engine.imports.clone();
engine_imports.sort();
assert_eq!(engine_imports, ["log-it", "wasi:cli/environment@0.2.3"]);
let mut engine_exports = engine.exports.clone();
engine_exports.sort();
assert_eq!(engine_exports, ["calc", "greeter", "run"]);
let bootstrap = &engine.bootstrap;
assert!(bootstrap.contains(" from \"log-it\";"));
assert!(bootstrap.contains(" from \"wasi:cli/environment@0.2.3\";"));
assert!(bootstrap.contains("$namespace({ default: $import"));
assert!(bootstrap.contains("function $rewrite(source)"));
assert!(bootstrap.contains(" as greeter };"));
assert!(bootstrap.contains(" as run };"));
assert!(bootstrap.contains(" as calc };"));
assert!(bootstrap.contains("export { $scriptLoader as scriptLoader };"));
assert!(bootstrap.contains("$Counter {"));
assert!(bootstrap.contains("static \"next\"(...args)"));
assert!(bootstrap.contains("$method([\"greeter\", \"Counter\"], $export"));
assert!(bootstrap.contains("$link([\"greeter\", \"Counter\"], $export"));
}
#[test]
fn rejects_quickjs_world_function_imports() {
let dir = tempdir();
let a = write_wit(&dir, "a.wit", WORLD_A);
let err =
engine_world(&[WitInput::builder().path(a).build()], Backend::QuickJs).unwrap_err();
assert!(err.to_string().contains("log-it"), "{err}");
}
#[test]
fn rejects_conflicting_exports() {
let dir = tempdir();
let a = write_wit(&dir, "a.wit", WORLD_A);
let conflicting = write_wit(
&dir,
"c.wit",
"package local:c;\ninterface greeter { shout: func(); }\nworld c { export greeter; }\n",
);
let err = engine_world(
&[
WitInput::builder().path(a).build(),
WitInput::builder().path(conflicting).build(),
],
Backend::StarlingMonkey,
)
.unwrap_err();
assert!(err.to_string().contains("greeter"), "{err}");
}
#[test]
fn rejects_reserved_export_name() {
let dir = tempdir();
let reserved = write_wit(
&dir,
"r.wit",
"package local:r;\ninterface script-loader { reload: func(); }\nworld r { export script-loader; }\n",
);
let err = engine_world(
&[WitInput::builder().path(reserved).build()],
Backend::StarlingMonkey,
)
.unwrap_err();
assert!(err.to_string().contains("scriptLoader"), "{err}");
}
fn tempdir() -> PathBuf {
let dir = std::env::temp_dir().join(format!(
"jco-script-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
fs::create_dir_all(&dir).unwrap();
dir
}
}