Skip to content

Commit d13c6ac

Browse files
authored
Merge pull request #670 from cloudflare/zeb/worker-build-182
fix: support for externref
2 parents 9e1b665 + 6724fc8 commit d13c6ac

File tree

6 files changed

+44
-80
lines changed

6 files changed

+44
-80
lines changed

worker-build/src/js/glue.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

worker-build/src/js/shim.js

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,51 @@ export * from "./index_bg.js";
33
import wasmModule from "./index.wasm";
44
import { WorkerEntrypoint } from "cloudflare:workers";
55

6+
const instance = new WebAssembly.Instance(wasmModule, {
7+
"./index_bg.js": imports,
8+
});
9+
10+
imports.__wbg_set_wasm(instance.exports);
11+
612
// Run the worker's initialization function.
7-
imports.start?.();
13+
instance.exports.__wbindgen_start?.();
814

915
export { wasmModule };
1016

1117
class Entrypoint extends WorkerEntrypoint {
12-
async fetch(request) {
13-
let response = imports.fetch(request, this.env, this.ctx);
14-
$WAIT_UNTIL_RESPONSE
15-
return await response;
16-
}
17-
18-
async queue(batch) {
19-
return await imports.queue(batch, this.env, this.ctx)
20-
}
21-
22-
async scheduled(event) {
23-
return await imports.scheduled(event, this.env, this.ctx)
24-
}
18+
async fetch(request) {
19+
let response = imports.fetch(request, this.env, this.ctx);
20+
$WAIT_UNTIL_RESPONSE;
21+
return await response;
22+
}
23+
24+
async queue(batch) {
25+
return await imports.queue(batch, this.env, this.ctx);
26+
}
27+
28+
async scheduled(event) {
29+
return await imports.scheduled(event, this.env, this.ctx);
30+
}
2531
}
2632

2733
const EXCLUDE_EXPORT = [
28-
"IntoUnderlyingByteSource",
29-
"IntoUnderlyingSink",
30-
"IntoUnderlyingSource",
31-
"MinifyConfig",
32-
"PolishConfig",
33-
"R2Range",
34-
"RequestRedirect",
35-
"fetch",
36-
"queue",
37-
"scheduled",
38-
"getMemory"
34+
"IntoUnderlyingByteSource",
35+
"IntoUnderlyingSink",
36+
"IntoUnderlyingSource",
37+
"MinifyConfig",
38+
"PolishConfig",
39+
"R2Range",
40+
"RequestRedirect",
41+
"fetch",
42+
"queue",
43+
"scheduled",
44+
"getMemory",
3945
];
4046

41-
Object.keys(imports).map(k => {
42-
if (!(EXCLUDE_EXPORT.includes(k) | k.startsWith("__"))) {
43-
Entrypoint.prototype[k] = imports[k];
44-
}
45-
})
47+
Object.keys(imports).map((k) => {
48+
if (!(EXCLUDE_EXPORT.includes(k) | k.startsWith("__"))) {
49+
Entrypoint.prototype[k] = imports[k];
50+
}
51+
});
4652

47-
export default Entrypoint;
53+
export default Entrypoint;

worker-build/src/main.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Arguments are forwarded directly to wasm-pack
22
33
use std::{
4-
convert::TryInto,
54
env::{self, VarError},
65
fs::{self, File},
76
io::{Read, Write},
@@ -20,21 +19,6 @@ const WORKER_SUBDIR: &str = "worker";
2019

2120
const SHIM_TEMPLATE: &str = include_str!("./js/shim.js");
2221

23-
const WASM_IMPORT: &str = r#"let wasm;
24-
export function __wbg_set_wasm(val) {
25-
wasm = val;
26-
}
27-
28-
"#;
29-
30-
const WASM_IMPORT_REPLACEMENT: &str = r#"
31-
import wasm from './glue.js';
32-
33-
export function getMemory() {
34-
return wasm.memory;
35-
}
36-
"#;
37-
3822
mod install;
3923

4024
pub fn main() -> Result<()> {
@@ -53,9 +37,7 @@ pub fn main() -> Result<()> {
5337

5438
create_worker_dir()?;
5539
copy_generated_code_to_worker_dir()?;
56-
use_glue_import()?;
5740

58-
write_string_to_file(worker_path("glue.js"), include_str!("./js/glue.js"))?;
5941
let shim = if env::var("RUN_TO_COMPLETION").is_ok() {
6042
SHIM_TEMPLATE.replace("$WAIT_UNTIL_RESPONSE", "this.ctx.waitUntil(response);")
6143
} else {
@@ -220,15 +202,6 @@ fn copy_generated_code_to_worker_dir() -> Result<()> {
220202
Ok(())
221203
}
222204

223-
// Replaces the wasm import with an import that instantiates the WASM modules itself.
224-
fn use_glue_import() -> Result<()> {
225-
let bindgen_glue_path = worker_path(format!("{OUT_NAME}_bg.js"));
226-
let old_bindgen_glue = read_file_to_string(&bindgen_glue_path)?;
227-
let fixed_bindgen_glue = old_bindgen_glue.replace(WASM_IMPORT, WASM_IMPORT_REPLACEMENT);
228-
write_string_to_file(bindgen_glue_path, fixed_bindgen_glue)?;
229-
Ok(())
230-
}
231-
232205
// Bundles the snippets and worker-related code into a single file.
233206
fn bundle(esbuild_path: &Path) -> Result<()> {
234207
let no_minify = !matches!(env::var("NO_MINIFY"), Err(VarError::NotPresent));
@@ -266,25 +239,12 @@ fn remove_unused_js() -> Result<()> {
266239
std::fs::remove_dir_all(&snippets_dir)?;
267240
}
268241

269-
for to_remove in [
270-
format!("{OUT_NAME}_bg.js"),
271-
"shim.js".into(),
272-
"glue.js".into(),
273-
] {
274-
std::fs::remove_file(worker_path(to_remove))?;
275-
}
242+
std::fs::remove_file(worker_path(format!("{OUT_NAME}_bg.js")))?;
243+
std::fs::remove_file(worker_path("shim.js"))?;
276244

277245
Ok(())
278246
}
279247

280-
fn read_file_to_string<P: AsRef<Path>>(path: P) -> Result<String> {
281-
let file_size = path.as_ref().metadata()?.len().try_into()?;
282-
let mut file = File::open(path)?;
283-
let mut buf = Vec::with_capacity(file_size);
284-
file.read_to_end(&mut buf)?;
285-
String::from_utf8(buf).map_err(anyhow::Error::from)
286-
}
287-
288248
fn write_string_to_file<P: AsRef<Path>>(path: P, contents: impl AsRef<str>) -> Result<()> {
289249
let mut file = File::create(path)?;
290250
file.write_all(contents.as_ref().as_bytes())?;

worker-sandbox/wrangler.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ main = "./shim.mjs"
6868
[[build.upload.rules]]
6969
globs = ["**/*.wasm"]
7070
type = "CompiledWasm"
71+
72+
[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
73+
dwarf-debug-info = true

worker/src/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<D> RouteContext<D> {
113113
}
114114
}
115115

116-
impl<'a> Router<'a, ()> {
116+
impl Router<'_, ()> {
117117
/// Construct a new `Router`. Or, call `Router::with_data(D)` to add arbitrary data that will be
118118
/// available to your various routes.
119119
pub fn new() -> Self {

worker/src/websocket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ pub struct EventStream<'ws> {
300300
)>,
301301
}
302302

303-
impl<'ws> Stream for EventStream<'ws> {
303+
impl Stream for EventStream<'_> {
304304
type Item = Result<WebsocketEvent>;
305305

306306
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {

0 commit comments

Comments
 (0)