Skip to content

Commit 01774e1

Browse files
authored
Make runner support executing binaries. (#42)
* Execute the main entry * Wasm64 * Fix * Add TODO * Switch priority
1 parent 3a935a3 commit 01774e1

31 files changed

Lines changed: 453 additions & 235 deletions

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979

8080
# Medium Priority
8181

82+
- Binary Runner:
83+
- Consider how to run tests with `harness = false`.
8284
- Find a way to link to imports directly if no wrapper function is needed. This happens when no
8385
conversions on the WAT level are necessary.
8486
- Provide an absolutely minimal allocator.

host/cargo-shim/linker

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
if [ "$JBG_DEV_TOOLS" = "1" ]; then
44
js-bindgen-ld $@
55
else
6-
cd $(dirname $0)/..
6+
cd $(dirname $0)
77
# This is passed when running Doctests.
88
unset RUSTC_BOOTSTRAP
99
cargo +stable run -q -p js-bindgen-ld -- $@

host/cargo-shim/runner

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
if [ "$JBG_DEV_TOOLS" = "1" ]; then
44
js-bindgen-runner $@
55
else
6-
cd $(dirname $0)/..
6+
cd $(dirname $0)
77
cargo +stable run -q -p js-bindgen-runner -- $@
88
fi

host/ld/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn main() {
2222
output_path,
2323
main_memory,
2424
js_store,
25+
is_test,
2526
} = pre::processing(&args);
2627

2728
let status = Command::new("rust-lld")
@@ -43,7 +44,7 @@ fn main() {
4344
);
4445

4546
let wasm_output =
46-
post::processing(&wasm_input, &mut js_output, main_memory, js_store).unwrap();
47+
post::processing(&wasm_input, &mut js_output, main_memory, js_store, is_test).unwrap();
4748
drop(wasm_input);
4849

4950
// We could write into the file directly, but `wasm-encoder` doesn't support

host/ld/src/post.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use std::str;
33

44
use anyhow::{Context, Result, bail};
55
use itertools::{Itertools, Position};
6+
use js_bindgen_shared::IS_TEST_SECTION;
67
use wasm_encoder::{
7-
EntityType, ImportSection, Module, ProducersField, ProducersSection, RawSection, Section,
8+
CustomSection, EntityType, ImportSection, Module, ProducersField, ProducersSection, RawSection,
9+
Section,
810
};
911
use wasmparser::{Encoding, KnownCustom, Parser, Payload, TypeRef};
1012

@@ -19,6 +21,7 @@ pub fn processing(
1921
mut js_output: impl Write,
2022
main_memory: MainMemory<'_>,
2123
mut js_store: JsStore,
24+
is_test: bool,
2225
) -> Result<Vec<u8>> {
2326
// Start building final Wasm and JS.
2427
let mut wasm_output = Vec::new();
@@ -107,6 +110,14 @@ pub fn processing(
107110
}
108111
}
109112

113+
if is_test {
114+
CustomSection {
115+
name: IS_TEST_SECTION.into(),
116+
data: (&[]).into(),
117+
}
118+
.append_to(&mut wasm_output);
119+
}
120+
110121
let memory = memory.context("main memory should be present")?;
111122
js_store.assert_expected()?;
112123

host/ld/src/pre.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ffi::OsString;
1+
use std::ffi::{OsStr, OsString};
22
use std::fs;
33
use std::path::Path;
44
use std::time::SystemTime;
@@ -15,6 +15,7 @@ pub struct PreOutput<'args> {
1515
pub output_path: &'args Path,
1616
pub main_memory: MainMemory<'args>,
1717
pub js_store: JsStore,
18+
pub is_test: bool,
1819
}
1920

2021
#[derive(Clone, Copy)]
@@ -66,9 +67,12 @@ pub fn processing(args: &[OsString]) -> PreOutput<'_> {
6667
let main_memory = main_memory(arch, &wasm_ld_args, &mut add_args);
6768

6869
let mut js_store = JsStore::default();
70+
let mut is_test = false;
6971

7072
// Extract embedded WAT from object files.
7173
for input in wasm_ld_args.inputs() {
74+
is_test |= is_libtest(input);
75+
7276
js_bindgen_ld_shared::ld_input_parser(input, |path, data, object_mtime| {
7377
process_object(
7478
&mut js_store,
@@ -88,9 +92,17 @@ pub fn processing(args: &[OsString]) -> PreOutput<'_> {
8892
output_path,
8993
main_memory,
9094
js_store,
95+
is_test,
9196
}
9297
}
9398

99+
fn is_libtest(input: &OsStr) -> bool {
100+
Path::new(input)
101+
.file_name()
102+
.and_then(OsStr::to_str)
103+
.is_some_and(|name| name.starts_with("libtest-"))
104+
}
105+
94106
/// Extracts any WAT instructions from `js-bindgen`, builds object files from
95107
/// them and passes them to the linker.
96108
fn process_object(

host/runner/src/js/deno/deno.mjs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

host/runner/src/js/deno/deno.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { runTests, Stream, Status } from "../shared/shared.mjs"
1+
import { run, Stream } from "../shared/shared.mjs"
22
import { colorText } from "../shared/shared-terminal.mjs"
33
import { JsBindgen } from "../imports.mts"
44

55
const module = await WebAssembly.compileStreaming(fetch(new URL("../wasm.wasm", import.meta.url)))
66

7-
const success = await runTests(module, JsBindgen, (stream, text) => {
7+
const status = await run(module, JsBindgen, (stream, text) => {
88
function printSync(input: string, to: typeof Deno.stdout | typeof Deno.stderr) {
99
let bytesWritten = 0
1010
const bytes = new TextEncoder().encode(input)
@@ -25,4 +25,4 @@ const success = await runTests(module, JsBindgen, (stream, text) => {
2525
}
2626
})
2727

28-
Deno.exit(success === Status.Ok ? 0 : 101)
28+
Deno.exit(status)

host/runner/src/js/dom/browser-spawner.mjs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

host/runner/src/js/dom/server.mjs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)