Skip to content

Commit

Permalink
Support the web Target of wasm-bindgen
Browse files Browse the repository at this point in the history
This emits the bindings in a way such that they are compatible with not
only the `bundler` target of `wasm-bindgen`, but also the `web` target.
This allows us to circumvent `webpack`, which means we are not limited
by the features it supports anymore. In particular, this unblocks the
usage of reference types.
  • Loading branch information
CryZe committed Nov 5, 2024
1 parent a1a130b commit 3956a73
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 11 deletions.
34 changes: 30 additions & 4 deletions capi/bind_gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,38 @@ fn write_files(classes: &BTreeMap<String, Class>, opt: &Opt) -> Result<()> {
path.push("wasm_bindgen");
create_dir_all(&path)?;
{
path.push("index.js");
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, false)?;
path.push("bundler");
create_dir_all(&path)?;
{
path.push("index.js");
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, false, true)?;
path.pop();

path.push("index.ts");
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, true, true)?;
path.pop();
}
path.pop();

path.push("index.ts");
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, true)?;
path.push("web");
create_dir_all(&path)?;
{
path.push("index.js");
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, false, false)?;
path.pop();

path.push("index.ts");
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, true, false)?;
path.pop();

path.push("preload.js");
wasm_bindgen::write_preload(BufWriter::new(File::create(&path)?), false)?;
path.pop();

path.push("preload.ts");
wasm_bindgen::write_preload(BufWriter::new(File::create(&path)?), true)?;
path.pop();
}
path.pop();
}
path.pop();
Expand Down
69 changes: 62 additions & 7 deletions capi/bind_gen/src/wasm_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,19 +336,60 @@ fn write_fn<W: Write>(mut writer: W, function: &Function, type_script: bool) ->
Ok(())
}

pub fn write_preload<W: Write>(mut writer: W, type_script: bool) -> Result<()> {
let content = if type_script {
r#"import init, { InitOutput } from "./livesplit_core.js";
let promise: Promise<InitOutput> | null = null;
export async function preloadWasm(): Promise<InitOutput> {
if (!promise) {
promise = init();
}
return await promise;
}"#
} else {
r#"import init from "./livesplit_core.js
let promise = null;
export async function preloadWasm() {
if (!promise) {
promise = init();
}
return await promise;
}"#
};
writeln!(writer, "{content}")
}

pub fn write<W: Write>(
mut writer: W,
classes: &BTreeMap<String, Class>,
type_script: bool,
bundler: bool,
) -> Result<()> {
if type_script {
if bundler {
writeln!(
writer,
"{}",
r#"// tslint:disable
import * as wasm from "./livesplit_core_bg.wasm";
import "./livesplit_core.js";"#
)?;
} else {
writeln!(
writer,
"{}",
r#"// tslint:disable
import { preloadWasm } from "./preload";
const wasm = await preloadWasm();"#
)?;
}
writeln!(
writer,
"{}{}",
r#"// tslint:disable
import * as wasm from "./livesplit_core_bg.wasm";
import "./livesplit_core.js";
r#"
const encoder = new TextEncoder();
const decoder = new TextDecoder();
Expand Down Expand Up @@ -402,12 +443,26 @@ function dealloc(slice: Slice) {
typescript::HEADER,
)?;
} else {
if bundler {
writeln!(
writer,
"{}",
r#"import * as wasm from "./livesplit_core_bg.wasm";
import "./livesplit_core.js";"#
)?;
} else {
writeln!(
writer,
"{}",
r#"import { preloadWasm } from "./preload";
const wasm = await preloadWasm();"#
)?;
}
writeln!(
writer,
"{}",
r#"import * as wasm from "./livesplit_core_bg.wasm";
import "./livesplit_core.js";
r#"
const encoder = new TextEncoder();
const decoder = new TextDecoder();
Expand Down

0 comments on commit 3956a73

Please sign in to comment.