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

Support the web Target of wasm-bindgen #852

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading