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 first-class support for binary crates #1843

Merged
merged 5 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
61 changes: 61 additions & 0 deletions crates/cli-support/src/webidl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ pub fn process(
cx.program(program)?;
}

if !cx.start_found {
cx.discover_main()?;
}

if let Some(standard) = cx.module.customs.delete_typed::<ast::WebidlBindings>() {
cx.standard(&standard)?;
}
Expand Down Expand Up @@ -623,6 +627,63 @@ impl<'a> Context<'a> {
Ok(())
}

fn discover_main(&mut self) -> Result<(), Error> {
let main_info = self
.module
.functions()
.find(|x| x.name.as_ref().map_or(false, |x| x == "main"))
.map(|x| (x.ty(), x.id()));
if let Some((main_type, main_id)) = main_info {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this use a match with an early return to de-indent the rest of this body?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could indeed!

use walrus::ValType::I32;
// an entry point `main` has signature (i32, i32) -> i32
let type_matches = {
let ty = self.module.types.get(main_type);
ty.params() == [I32, I32] && ty.results() == [I32]
};
if !type_matches {
return Ok(());
}
let mut wrapper = walrus::FunctionBuilder::new(&mut self.module.types, &[], &[]);
const NAME: &str = "__wasm_bindgen_autodiscoveredmain_wrapper";
wrapper
.func_body()
.i32_const(0)
.i32_const(0)
.call(main_id)
.drop()
.return_();
let wrapper = wrapper.finish(vec![], &mut self.module.funcs);
let export = self.module.exports.add(NAME, wrapper);
self.function_exports
.insert(NAME.to_string(), (export, wrapper));
let descriptor = Descriptor::Function(Box::new(Function {
arguments: vec![],
shim_idx: 0, // TODO is this bad
ret: Descriptor::Unit,
}));
self.descriptors.insert(NAME.to_string(), descriptor);

let export = decode::Export {
class: None,
comments: vec![],
consumed: false,
function: decode::Function {
arg_names: vec![],
name: NAME,
},
method_kind: decode::MethodKind::Operation(decode::Operation {
is_static: true,
kind: decode::OperationKind::Regular,
}),
start: true,
};

self.export(export)?;
}

Ok(())
}

// Ensure that the `start` function for this module calls the
// `__wbindgen_init_anyref_table` function. This'll ensure that all
// instances of this module have the initial slots of the anyref table
Expand Down
46 changes: 46 additions & 0 deletions crates/cli/tests/wasm-bindgen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,49 @@ fn one_export_works() {
.wasm_bindgen("");
cmd.assert().success();
}

#[test]
fn bin_crate_works() {
let (mut cmd, out_dir) = Project::new("bin_crate_works")
.file(
"src/main.rs",
r#"
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(data: &str);
}

fn main() {
log("hello, world");
}
"#,
)
.file(
"Cargo.toml",
&format!(
"
[package]
name = \"bin_crate_works\"
authors = []
version = \"1.0.0\"
edition = '2018'

[dependencies]
wasm-bindgen = {{ path = '{}' }}

[workspace]
",
repo_root().display(),
),
)
.wasm_bindgen("--target nodejs");
cmd.assert().success();
Command::new("node")
.arg("bin_crate_works.js")
.current_dir(out_dir)
.assert()
.success()
.stdout("hello, world\n");
}