Skip to content

Commit 2c9084d

Browse files
committed
Update web-sys test to only test compilation
1 parent 8ba467e commit 2c9084d

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

crates/web-sys/tests/wasm/main.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ extern crate wasm_bindgen_futures;
77
extern crate wasm_bindgen_test;
88
extern crate web_sys;
99

10-
use wasm_bindgen::{JsValue, JsCast};
1110
use wasm_bindgen_test::*;
1211

1312
wasm_bindgen_test_configure!(run_in_browser);
@@ -60,8 +59,10 @@ pub mod indexeddb;
6059

6160
#[wasm_bindgen_test]
6261
fn deref_works() {
63-
let x = JsValue::from(3);
64-
let x = x.unchecked_into::<web_sys::XmlHttpRequestUpload>();
65-
let y: &web_sys::XmlHttpRequestEventTarget = &x;
66-
drop(y);
62+
fn _check(a: &web_sys::XmlHttpRequestUpload) {
63+
let _x: &web_sys::XmlHttpRequestEventTarget = a;
64+
let _x: &web_sys::EventTarget = a;
65+
let _x: &js_sys::Object = a;
66+
let _x: &wasm_bindgen::JsValue = a;
67+
}
6768
}

guide/src/reference/attributes/on-js-imports/host_binding.md renamed to guide/src/reference/attributes/on-js-imports/final.md

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
# `host_binding`
1+
# `final`
22

3-
The `host_binding` attribute is the converse of the [`structural`
3+
The `final` attribute is the converse of the [`structural`
44
attribute](structural.html). It configures how `wasm-bindgen` will generate JS
5-
glue to call the imported function. The naming here is intended convey that this
6-
attribute is intended to implement the semantics of the future [host bindings
7-
proposal][host-bindings] for WebAssembly.
5+
imports to call the imported function. Notably a function imported by `final`
6+
never changes after it was imported, whereas a function imported by default (or
7+
with `structural`) is subject to runtime lookup rules such as walking the
8+
prototype chain of an object.
89

910
[host-bindings]: https://github.com/WebAssembly/host-bindings
1011
[reference-types]: https://github.com/WebAssembly/reference-types
1112

12-
The `host_binding` attribute is intended to be purely related to performance. It
13-
ideally has no user-visible effect, and well-typed `structural` imports (the
14-
default) should be able to transparently switch to `host_binding` eventually.
13+
The `final` attribute is intended to be purely related to performance. It
14+
ideally has no user-visible effect, and `structural` imports (the default)
15+
should be able to transparently switch to `host_binding` eventually.
1516

1617
The eventual performance aspect is that with the [host bindings
1718
proposal][host-bindings] then `wasm-bindgen` will need to generate far fewer JS
18-
shims to import than it does today. For example, consider this import today:
19+
functino shims to import than it does today. For example, consider this import
20+
today:
1921

2022
```rust
2123
#[wasm_bindgen]
@@ -26,28 +28,28 @@ extern {
2628
}
2729
```
2830

29-
**Without the `host_binding` attribute** the generated JS looks like this:
31+
**Without the `final` attribute** the generated JS looks like this:
3032

3133
```js
32-
// without `host_binding`
34+
// without `final`
3335
export function __wbg_bar_a81456386e6b526f(arg0, arg1, arg2) {
3436
let varg1 = getStringFromWasm(arg1, arg2);
3537
return addHeapObject(getObject(arg0).bar(varg1));
3638
}
3739
```
3840

39-
We can see here that this JS shim is required, but it's all relatively
41+
We can see here that this JS function shim is required, but it's all relatively
4042
self-contained. It does, however, execute the `bar` method in a duck-type-y
41-
fashion in the sense that it never validates `getObject(arg0)` is of type
42-
`Foo` to actually call the `Foo.prototype.bar` method.
43+
fashion in the sense that it never validates `getObject(arg0)` is of type `Foo`
44+
to actually call the `Foo.prototype.bar` method.
4345

4446
If we instead, however, write this:
4547

4648
```rust
4749
#[wasm_bindgen]
4850
extern {
4951
type Foo;
50-
#[wasm_bindgen(method, host_binding)] // note the change here
52+
#[wasm_bindgen(method, final)] // note the change here
5153
fn bar(this: &Foo, argument: &str) -> JsValue;
5254
}
5355
```
@@ -68,7 +70,7 @@ called is hoisted out of the generated shim and is bound to always be
6870
`Foo.prototype.bar`. This then uses the `Function.call` method to invoke that
6971
function with `getObject(arg0)` as the receiver.
7072

71-
But wait, there's still a JS shim here even with `host_binding`! That's true,
73+
But wait, there's still a JS function shim here even with `final`! That's true,
7274
and this is simply a fact of future WebAssembly proposals not being implemented
7375
yet. The semantics, though, match the future [host bindings
7476
proposal][host-bindings] because the method being called is determined exactly
@@ -77,8 +79,8 @@ runtime when the function is called.
7779

7880
## Interaction with future proposals
7981

80-
If you're curious to see how our JS shim will be eliminated entirely, let's take
81-
a look at the generated bindings. We're starting off with this:
82+
If you're curious to see how our JS function shim will be eliminated entirely,
83+
let's take a look at the generated bindings. We're starting off with this:
8284

8385
```js
8486
const __wbg_bar_target = Foo.prototype.bar;
@@ -135,5 +137,18 @@ export const __wbg_bar_a81456386e6b526f = Foo.prototype.bar;
135137
```
136138

137139
and voila! We, with [reference types][reference-types] and [host
138-
bindings][host-bindings], now have no JS shim at all necessary to call the
139-
imported function!
140+
bindings][host-bindings], now have no JS function shim at all necessary to call
141+
the imported function. Additionally future wasm proposals to the ES module
142+
system may also mean that don't even need the `export const ...` here too.
143+
144+
It's also worth pointing out that with all these wasm proposals implemented the
145+
default way to import the `bar` function (aka `structural`) would generate a JS
146+
function shim that looks like:
147+
148+
```js
149+
export function __wbg_bar_a81456386e6b526f(varg1) {
150+
return this.bar(varg1);
151+
}
152+
```
153+
154+
where this import is still subject to runtime prototype chain lookups and such.

guide/src/reference/attributes/on-js-imports/structural.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
> functions. This attribute is largely ignored today and is only retained for
55
> backwards compatibility and learning purposes.
66
>
7-
> The inverse of this attribute, [the `host_binding`
8-
> attribute](host_binding.html) is more functionally interesting than
7+
> The inverse of this attribute, [the `final`
8+
> attribute](final.html) is more functionally interesting than
99
> `structural` (as `structural` is simply the default)
1010
1111
[RFC 5]: https://rustwasm.github.io/rfcs/005-structural-and-deref.html

0 commit comments

Comments
 (0)