address pr comments

This commit is contained in:
Jakob Hellermann 2020-06-02 10:53:21 +02:00
parent 79f96af262
commit 84c7cf01ce
13 changed files with 151 additions and 36 deletions

View File

@ -65,6 +65,7 @@ members = [
"examples/char", "examples/char",
"examples/closures", "examples/closures",
"examples/console_log", "examples/console_log",
"examples/deno",
"examples/dom", "examples/dom",
"examples/duck-typed-interfaces", "examples/duck-typed-interfaces",
"examples/fetch", "examples/fetch",

View File

@ -167,7 +167,7 @@ jobs:
- script: mv _package.json package.json && npm install && rm package.json - script: mv _package.json package.json && npm install && rm package.json
displayName: "run npm install" displayName: "run npm install"
- script: | - script: |
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 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
(cd examples/$dir && (cd examples/$dir &&
ln -fs ../../node_modules . && ln -fs ../../node_modules . &&
npm run build -- --output-path $BUILD_ARTIFACTSTAGINGDIRECTORY/exbuild/$dir) || exit 1; npm run build -- --output-path $BUILD_ARTIFACTSTAGINGDIRECTORY/exbuild/$dir) || exit 1;
@ -178,6 +178,16 @@ jobs:
artifactName: examples1 artifactName: examples1
targetPath: '$(Build.ArtifactStagingDirectory)' targetPath: '$(Build.ArtifactStagingDirectory)'
- job: test_deno
displayName: "Build and test the deno example"
steps:
- template: ci/azure-install-rust.yml
- script: rustup target add wasm32-unknown-unknown
displayName: "install wasm target"
- template: ci/azure-install-deno.yml
- script: cd examples/deno && ./build.sh && $HOME/.deno/bin/deno run --allow-read test.ts
displayName: "build and run deno example"
- job: build_raytrace - job: build_raytrace
displayName: "Build raytrace examples" displayName: "Build raytrace examples"
steps: steps:

View File

@ -0,0 +1,3 @@
steps:
- script: curl -fsSL https://deno.land/x/install/install.sh | sh
displayName: "install deno"

View File

@ -294,6 +294,8 @@ impl<'a> Context<'a> {
} }
fn generate_deno_wasm_loading(&self, module_name: &str) -> String { fn generate_deno_wasm_loading(&self, module_name: &str) -> String {
// Deno removed support for .wasm imports in https://github.com/denoland/deno/pull/5135
// the issue for bringing it back is https://github.com/denoland/deno/issues/5609.
format!( format!(
"const file = new URL(import.meta.url).pathname; "const file = new URL(import.meta.url).pathname;
const wasmFile = file.substring(0, file.lastIndexOf(Deno.build.os === 'windows' ? '\\\\' : '/') + 1) + '{}_bg.wasm'; const wasmFile = file.substring(0, file.lastIndexOf(Deno.build.os === 'windows' ? '\\\\' : '/') + 1) + '{}_bg.wasm';
@ -1283,7 +1285,8 @@ impl<'a> Context<'a> {
} }
fn expose_text_processor(&mut self, s: &str, args: &str) -> Result<(), Error> { fn expose_text_processor(&mut self, s: &str, args: &str) -> Result<(), Error> {
if self.config.mode.nodejs() { match &self.config.mode {
OutputMode::Node { .. } => {
let name = self.import_name(&JsImport { let name = self.import_name(&JsImport {
name: JsImportName::Module { name: JsImportName::Module {
module: "util".to_string(), module: "util".to_string(),
@ -1292,7 +1295,10 @@ impl<'a> Context<'a> {
fields: Vec::new(), fields: Vec::new(),
})?; })?;
self.global(&format!("let cached{} = new {}{};", s, name, args)); self.global(&format!("let cached{} = new {}{};", s, name, args));
} else if !self.config.mode.always_run_in_browser() && !self.config.mode.deno() { }
OutputMode::Bundler {
browser_only: false,
} => {
self.global(&format!( self.global(&format!(
" "
const l{0} = typeof {0} === 'undefined' ? \ const l{0} = typeof {0} === 'undefined' ? \
@ -1301,9 +1307,14 @@ impl<'a> Context<'a> {
s s
)); ));
self.global(&format!("let cached{0} = new l{0}{1};", s, args)); self.global(&format!("let cached{0} = new l{0}{1};", s, args));
} else {
self.global(&format!("let cached{0} = new {0}{1};", s, args));
} }
OutputMode::Deno
| OutputMode::Web
| OutputMode::NoModules { .. }
| OutputMode::Bundler { browser_only: true } => {
self.global(&format!("let cached{0} = new {0}{1};", s, args))
}
};
Ok(()) Ok(())
} }

View File

@ -564,15 +564,6 @@ impl OutputMode {
} }
} }
fn always_run_in_browser(&self) -> bool {
match self {
OutputMode::Web => true,
OutputMode::NoModules { .. } => true,
OutputMode::Bundler { browser_only } => *browser_only,
_ => false,
}
}
fn web(&self) -> bool { fn web(&self) -> bool {
match self { match self {
OutputMode::Web => true, OutputMode::Web => true,
@ -580,13 +571,6 @@ impl OutputMode {
} }
} }
fn deno(&self) -> bool {
match self {
OutputMode::Deno { .. } => true,
_ => false,
}
}
fn esm_integration(&self) -> bool { fn esm_integration(&self) -> bool {
match self { match self {
OutputMode::Bundler { .. } OutputMode::Bundler { .. }

11
examples/deno/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "deno"
version = "0.1.0"
authors = ["The wasm-bindgen Developers"]
edition = "2018"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.63"

16
examples/deno/README.md Normal file
View File

@ -0,0 +1,16 @@
# Using deno
You can build the example with
```sh
$ ./build.sh
```
and test it with
```sh
$ deno run --allow-read test.ts
```
The `--allow-read` flag is needed because the wasm file is read during runtime.
This will be fixed when https://github.com/denoland/deno/issues/5609 is resolved.

7
examples/deno/build.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/sh
set -eux
cargo build --target wasm32-unknown-unknown --release
cargo run --package wasm-bindgen-cli --bin wasm-bindgen -- \
--out-dir pkg --target deno ${CARGO_TARGET_DIR:-../../target}/wasm32-unknown-unknown/release/deno.wasm

1
examples/deno/crate/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/deno.wasm

View File

@ -0,0 +1,17 @@
export class MyClass {
constructor() {
this._number = 42;
}
get number() {
return this._number;
}
set number(n) {
return (this._number = n);
}
render() {
return `My number is: ${this.number}`;
}
}

37
examples/deno/src/lib.rs Normal file
View File

@ -0,0 +1,37 @@
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "/defined-in-js.js")]
extern "C" {
type MyClass;
#[wasm_bindgen(constructor)]
fn new() -> MyClass;
#[wasm_bindgen(method, getter)]
fn number(this: &MyClass) -> u32;
#[wasm_bindgen(method, setter)]
fn set_number(this: &MyClass, number: u32) -> MyClass;
#[wasm_bindgen(method)]
fn render(this: &MyClass) -> String;
}
// lifted from the `console_log` example
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen(inline_js = "export function add(a, b) { return a + b; }")]
extern "C" {
fn add(a: u32, b: u32) -> u32;
}
#[wasm_bindgen]
pub fn greet(name: String) {
log(&format!("Hello from {}!", name)); // should output "Hello from Rust!"
let x = MyClass::new();
assert_eq!(x.number(), add(40, 2));
log(&x.render());
}

3
examples/deno/test.ts Normal file
View File

@ -0,0 +1,3 @@
import { greet } from "./pkg/deno.js";
greet("Deno");

View File

@ -14,12 +14,14 @@ should behave the same way in this respect. The values possible here are:
| [`bundler`] | Suitable for loading in bundlers like Webpack | | [`bundler`] | Suitable for loading in bundlers like Webpack |
| [`web`] | Directly loadable in a web browser | | [`web`] | Directly loadable in a web browser |
| [`nodejs`] | Loadable via `require` as a Node.js module | | [`nodejs`] | Loadable via `require` as a Node.js module |
| [`deno`] | Loadable using imports from Deno modules |
| [`no-modules`] | Like `web`, but older and doesn't use ES modules | | [`no-modules`] | Like `web`, but older and doesn't use ES modules |
[`bundler`]: #bundlers [`bundler`]: #bundlers
[`web`]: #without-a-bundler [`web`]: #without-a-bundler
[`no-modules`]: #without-a-bundler [`no-modules`]: #without-a-bundler
[`nodejs`]: #nodejs [`nodejs`]: #nodejs
[`deno`]: #Deno
## Bundlers ## Bundlers
@ -88,6 +90,18 @@ as it has a JS shim generated as well).
Note that this method requires a version of Node.js with WebAssembly support, Note that this method requires a version of Node.js with WebAssembly support,
which is currently Node 8 and above. which is currently Node 8 and above.
## Deno
**`--target deno`**
To deploy WebAssembly to Deno, use the `--target deno` flag.
To then import your module inside deno, use
```ts
// @deno-types="./out/crate_name.d.ts"
import { yourFunction } from "./out/crate_name.js";
```
## NPM ## NPM
If you'd like to deploy compiled WebAssembly to NPM, then the tool for the job If you'd like to deploy compiled WebAssembly to NPM, then the tool for the job