Skip to content

Commit 84c7cf0

Browse files
address pr comments
1 parent 79f96af commit 84c7cf0

File tree

13 files changed

+151
-36
lines changed

13 files changed

+151
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ members = [
6565
"examples/char",
6666
"examples/closures",
6767
"examples/console_log",
68+
"examples/deno",
6869
"examples/dom",
6970
"examples/duck-typed-interfaces",
7071
"examples/fetch",

azure-pipelines.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ jobs:
167167
- script: mv _package.json package.json && npm install && rm package.json
168168
displayName: "run npm install"
169169
- script: |
170-
for dir in `ls examples | grep -v README | grep -v asm.js | grep -v raytrace | grep -v without-a-bundler | grep -v websockets | grep -v webxr`; do
170+
for dir in `ls examples | grep -v README | grep -v asm.js | grep -v raytrace | grep -v without-a-bundler | grep -v websockets | grep -v webxr | grep -v deno`; do
171171
(cd examples/$dir &&
172172
ln -fs ../../node_modules . &&
173173
npm run build -- --output-path $BUILD_ARTIFACTSTAGINGDIRECTORY/exbuild/$dir) || exit 1;
@@ -178,6 +178,16 @@ jobs:
178178
artifactName: examples1
179179
targetPath: '$(Build.ArtifactStagingDirectory)'
180180

181+
- job: test_deno
182+
displayName: "Build and test the deno example"
183+
steps:
184+
- template: ci/azure-install-rust.yml
185+
- script: rustup target add wasm32-unknown-unknown
186+
displayName: "install wasm target"
187+
- template: ci/azure-install-deno.yml
188+
- script: cd examples/deno && ./build.sh && $HOME/.deno/bin/deno run --allow-read test.ts
189+
displayName: "build and run deno example"
190+
181191
- job: build_raytrace
182192
displayName: "Build raytrace examples"
183193
steps:

ci/azure-install-deno.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
steps:
2+
- script: curl -fsSL https://deno.land/x/install/install.sh | sh
3+
displayName: "install deno"

crates/cli-support/src/js/mod.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ impl<'a> Context<'a> {
294294
}
295295

296296
fn generate_deno_wasm_loading(&self, module_name: &str) -> String {
297+
// Deno removed support for .wasm imports in https://github.com/denoland/deno/pull/5135
298+
// the issue for bringing it back is https://github.com/denoland/deno/issues/5609.
297299
format!(
298300
"const file = new URL(import.meta.url).pathname;
299301
const wasmFile = file.substring(0, file.lastIndexOf(Deno.build.os === 'windows' ? '\\\\' : '/') + 1) + '{}_bg.wasm';
@@ -1283,27 +1285,36 @@ impl<'a> Context<'a> {
12831285
}
12841286

12851287
fn expose_text_processor(&mut self, s: &str, args: &str) -> Result<(), Error> {
1286-
if self.config.mode.nodejs() {
1287-
let name = self.import_name(&JsImport {
1288-
name: JsImportName::Module {
1289-
module: "util".to_string(),
1290-
name: s.to_string(),
1291-
},
1292-
fields: Vec::new(),
1293-
})?;
1294-
self.global(&format!("let cached{} = new {}{};", s, name, args));
1295-
} else if !self.config.mode.always_run_in_browser() && !self.config.mode.deno() {
1296-
self.global(&format!(
1297-
"
1288+
match &self.config.mode {
1289+
OutputMode::Node { .. } => {
1290+
let name = self.import_name(&JsImport {
1291+
name: JsImportName::Module {
1292+
module: "util".to_string(),
1293+
name: s.to_string(),
1294+
},
1295+
fields: Vec::new(),
1296+
})?;
1297+
self.global(&format!("let cached{} = new {}{};", s, name, args));
1298+
}
1299+
OutputMode::Bundler {
1300+
browser_only: false,
1301+
} => {
1302+
self.global(&format!(
1303+
"
12981304
const l{0} = typeof {0} === 'undefined' ? \
12991305
(0, module.require)('util').{0} : {0};\
13001306
",
1301-
s
1302-
));
1303-
self.global(&format!("let cached{0} = new l{0}{1};", s, args));
1304-
} else {
1305-
self.global(&format!("let cached{0} = new {0}{1};", s, args));
1306-
}
1307+
s
1308+
));
1309+
self.global(&format!("let cached{0} = new l{0}{1};", s, args));
1310+
}
1311+
OutputMode::Deno
1312+
| OutputMode::Web
1313+
| OutputMode::NoModules { .. }
1314+
| OutputMode::Bundler { browser_only: true } => {
1315+
self.global(&format!("let cached{0} = new {0}{1};", s, args))
1316+
}
1317+
};
13071318

13081319
Ok(())
13091320
}

crates/cli-support/src/lib.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -564,29 +564,13 @@ impl OutputMode {
564564
}
565565
}
566566

567-
fn always_run_in_browser(&self) -> bool {
568-
match self {
569-
OutputMode::Web => true,
570-
OutputMode::NoModules { .. } => true,
571-
OutputMode::Bundler { browser_only } => *browser_only,
572-
_ => false,
573-
}
574-
}
575-
576567
fn web(&self) -> bool {
577568
match self {
578569
OutputMode::Web => true,
579570
_ => false,
580571
}
581572
}
582573

583-
fn deno(&self) -> bool {
584-
match self {
585-
OutputMode::Deno { .. } => true,
586-
_ => false,
587-
}
588-
}
589-
590574
fn esm_integration(&self) -> bool {
591575
match self {
592576
OutputMode::Bundler { .. }

examples/deno/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "deno"
3+
version = "0.1.0"
4+
authors = ["The wasm-bindgen Developers"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
wasm-bindgen = "0.2.63"

examples/deno/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Using deno
2+
3+
You can build the example with
4+
5+
```sh
6+
$ ./build.sh
7+
```
8+
9+
and test it with
10+
11+
```sh
12+
$ deno run --allow-read test.ts
13+
```
14+
15+
The `--allow-read` flag is needed because the wasm file is read during runtime.
16+
This will be fixed when https://github.com/denoland/deno/issues/5609 is resolved.

examples/deno/build.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
set -eux
4+
5+
cargo build --target wasm32-unknown-unknown --release
6+
cargo run --package wasm-bindgen-cli --bin wasm-bindgen -- \
7+
--out-dir pkg --target deno ${CARGO_TARGET_DIR:-../../target}/wasm32-unknown-unknown/release/deno.wasm

examples/deno/crate/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/deno.wasm

examples/deno/defined-in-js.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export class MyClass {
2+
constructor() {
3+
this._number = 42;
4+
}
5+
6+
get number() {
7+
return this._number;
8+
}
9+
10+
set number(n) {
11+
return (this._number = n);
12+
}
13+
14+
render() {
15+
return `My number is: ${this.number}`;
16+
}
17+
}

examples/deno/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen(module = "/defined-in-js.js")]
4+
extern "C" {
5+
type MyClass;
6+
7+
#[wasm_bindgen(constructor)]
8+
fn new() -> MyClass;
9+
10+
#[wasm_bindgen(method, getter)]
11+
fn number(this: &MyClass) -> u32;
12+
#[wasm_bindgen(method, setter)]
13+
fn set_number(this: &MyClass, number: u32) -> MyClass;
14+
#[wasm_bindgen(method)]
15+
fn render(this: &MyClass) -> String;
16+
}
17+
18+
// lifted from the `console_log` example
19+
#[wasm_bindgen]
20+
extern "C" {
21+
#[wasm_bindgen(js_namespace = console)]
22+
fn log(s: &str);
23+
}
24+
25+
#[wasm_bindgen(inline_js = "export function add(a, b) { return a + b; }")]
26+
extern "C" {
27+
fn add(a: u32, b: u32) -> u32;
28+
}
29+
30+
#[wasm_bindgen]
31+
pub fn greet(name: String) {
32+
log(&format!("Hello from {}!", name)); // should output "Hello from Rust!"
33+
34+
let x = MyClass::new();
35+
assert_eq!(x.number(), add(40, 2));
36+
log(&x.render());
37+
}

examples/deno/test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { greet } from "./pkg/deno.js";
2+
3+
greet("Deno");

guide/src/reference/deployment.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ should behave the same way in this respect. The values possible here are:
1414
| [`bundler`] | Suitable for loading in bundlers like Webpack |
1515
| [`web`] | Directly loadable in a web browser |
1616
| [`nodejs`] | Loadable via `require` as a Node.js module |
17+
| [`deno`] | Loadable using imports from Deno modules |
1718
| [`no-modules`] | Like `web`, but older and doesn't use ES modules |
1819

1920
[`bundler`]: #bundlers
2021
[`web`]: #without-a-bundler
2122
[`no-modules`]: #without-a-bundler
2223
[`nodejs`]: #nodejs
24+
[`deno`]: #Deno
2325

2426
## Bundlers
2527

@@ -69,7 +71,7 @@ documentation, but the highlights of this output are:
6971
The CLI also supports an output mode called `--target no-modules` which is
7072
similar to the `web` target in that it requires manual initialization of the
7173
wasm and is intended to be included in web pages without any further
72-
postprocessing. See the [without a bundler example][nomex] for some more
74+
postprocessing. See the [without a bundler example][nomex] for some more
7375
information about `--target no-modules`.
7476

7577
## Node.js
@@ -88,6 +90,18 @@ as it has a JS shim generated as well).
8890
Note that this method requires a version of Node.js with WebAssembly support,
8991
which is currently Node 8 and above.
9092

93+
## Deno
94+
95+
**`--target deno`**
96+
97+
To deploy WebAssembly to Deno, use the `--target deno` flag.
98+
To then import your module inside deno, use
99+
100+
```ts
101+
// @deno-types="./out/crate_name.d.ts"
102+
import { yourFunction } from "./out/crate_name.js";
103+
```
104+
91105
## NPM
92106

93107
If you'd like to deploy compiled WebAssembly to NPM, then the tool for the job

0 commit comments

Comments
 (0)