This commit reimplements how we disambiguate function names on overloading.
Previously functions would be first be disambiguated if they had multiple
instances of the same name, and *then* functions would be disambiguated
aftewards by if their arguments expanded to more than one type to generate.
This commit instead collects everything into one list during the first pass.
This one list contains all signatures known for a given name. Later this list is
walked in one pass to generate all methods necessary, expanding names all at
once instead of two steps.
This should improve the naming of methods across multiple functions which also
have optional arguments. Support in this commit is just enough for namespaces,
but following commits will update the strategy for mixins/interfaces.
Additionally only new code was added in this commit which duplicates a lot of
functionality, but subsequent commits will remove the old code that will
eventually no longer be used.
This commit refactors the lowest-level primitive for creating functions into a
new `create_one_function` function. This doesn't take into account overloading
but is suitable for things like `create_{getter,setter}`. Eventually the
overloading will be implemented in terms of this function.
This commit refactors WebIDL code generation to walk over the fields of
`FirstPassRecord` instead of walking the AST again. This helps remove
redundancies like checking `is_chrome_only` as well as revisiting partial
interfaces and such. This should make it more clear that the first pass's job is
to walk the AST and collect all relevant information, while the codegen pass is
purely about appending items to a `Program`.
Additionally this refactoring will also soon be used to prepare different data
structures for operation overloadings, avoiding the need to walk those ASTs
twice.
This commit adds further support for the `Global` attribute to not only emit
structural accessors but also emit functions that don't take `&self`. All
methods on a `[Global]` interface will not require `&self` and will call
functions and/or access properties on the global scope.
This should enable things like:
Window::location() // returns `Location`
Window::fetch(...) // invokes the `fetch` function
Closes#659
We currently pass a raw view into wasm's memory for `getStringFromWasm`, but if
the memory is actually shared then `TextDecoder` rejects `SharedArrayBuffer` and
won't actually decode anything. Work around this for now with an extra copy into
a local buffer, and then pass that buffer to `getStringFromWasm` whenever memory
is shared.
In addition to closing #495 this'll be useful eventually when instantiating
multiple wasm modules from Rust as you'd now be able to acquire a reference to
the current module in Rust itself.
The default of Rust wasm binaries is to export the memory that they contain, but
LLD also supports an `--import-memory` option where memory is imported into a
module instead. It's looking like importing memory is along the lines of how
shared memory wasm modules will work (they'll all import the same memory).
This commit adds support to wasm-bindgen to support modules which import memory.
Memory accessors are tweaked to no longer always assume that the wasm module
exports its memory. Additionally JS bindings will create a `memory` option
automatically because LLD always imports memory from an `env` module which won't
actually exist.
I discussed this with @fitzgen awhile back and this sort of casting seems
especially problematic when you have code along the lines of:
let mut x: HtmlElement = ...;
{
let y: &mut JsValue = x.as_ref();
*y = 3.into();
}
x.some_html_element_method();
as that will immediately throw! We didn't have a use case for mutable casting
other than consistency, so this commit removes it for now. We can possibly add
it back in later if motivated, but for now it seems reasonable to try to avoid
these sorts of pitfalls!
This is a pretty heavyweight dependency which accounts for a surprising amount
of runtime for larger modules in `wasm-bindgen`. We don't need 90% of the crate
and so this commit bundles a small interpreter for instructions we know are only
going to appear in describe-related functions.
This commit adds experimental support for `WeakRef` to be used to automatically
free wasm objects instead of having to always call the `free` function manually.
Note that when enabled the `free` function for all exported objects is still
generated, it's just optionally invoked by the application.
Support isn't exposed through a CLI flag right now due to the early stages of
the `WeakRef` proposal, but the env var `WASM_BINDGEN_WEAKREF` can be used to
enable this generation. Upon doing so the output can then be edited slightly as
well to work in the SpiderMonkey shell and it looks like this is working!
Closes#704
This commit fixes annotations that include both the `constructor` and `catch`
attributes on imported types, ensuring that we infer the right type being
returned after extracting the first type parameter of the `Result`.
Closes#735
Instead of `dom_str`, `byte_str`, and `usv_str`, emit `str` for all of them.
Similarly for `unrestricted_f64` just do `f64` instead. This reflects how we
interpret the types already in terms of Rust types and although technically
makes it possible to have name collisions in WebIDL they don't come up in
practice.
This commit updates how we name overloaded methods. Previously all argument
names were concatenated, but after this commit it only concatenates argument
names where at least one possibility has a different type. Otherwise if all
possibilities have the same type name it in theory isn't adding too much more
information!
Additionally this commit also switches to using `_with_` consistently everywhere
instead of `_with_` for constructors and `_using_` for methods.
Closes#712
Without the "mixin" keyword, wasm_bindgen_webidl would report:
Partial interface WindowOrWorkerGlobalScope missing non-partial interface
Also, including the "mixin" keyword here is consistent with the official
webidl spec (for example see https://fetch.spec.whatwg.org/#fetch-method)
This commit adds support for generating bindings for dictionaries defined in
WebIDL. Dictionaries are associative arrays which are simply objects in JS with
named keys and some values. In Rust given a dictionary like:
dictionary Foo {
long field;
};
we'll generate a struct like:
pub struct Foo {
obj: js_sys::Object,
}
impl Foo {
pub fn new() -> Foo { /* make a blank object */ }
pub fn field(&mut self, val: i32) -> &mut Self {
// set the field using `js_sys::Reflect`
}
}
// plus a bunch of AsRef, From, and wasm abi impls
At the same time this adds support for partial dictionaries and dictionary
inheritance. All dictionary fields are optional by default and hence only have
builder-style setters, but dictionaries can also have required fields. Required
fields are exposed as arguments to the `new` constructor.
Closes#241
I think these might all be from before WebIDL mixins existed. Either way,
multiple inheritance of interfaces that don't have exposed interface objects is
equivalent to mixins.
This commit makes these changes:
* Unsupported constructs always log "unsupported" for easy `grep`ing
* There is always a "<generic message> : <details>" format now, so we can easily
use `cut` to grab the generic message and count which kinds of things are our
biggest missing features.
* Make sure that we have different `warn!` logs for each kind of unsupported
thing, instead of grouping them together.
Put all that together and this is the current state of `wasm-bindgen-webidl` and
`web-sys`:
```
$ grep WARN stderr.txt | grep wasm_bindgen_webidl | grep -i unsupported | cut -d ' ' -f5- | cut -d ':' -f 1 | sort | uniq -c | sort -rn
387 Unsupported WebIDL Dictionary definition
139 Unsupported argument type
70 Unsupported return type
47 Unsupported WebIDL Callback definition
22 Unsupported WebIDL extended attribute
18 Unsupported unnamed operation
9 Unsupported WebIDL CallbackInterface definition
7 Unsupported WebIDL Stringifier interface member
7 Unsupported WebIDL Maplike interface member
2 Unsupported webidl stringifier
2 Unsupported WebIDL Setlike interface member
2 Unsupported stringifier on type
```
This does involve us losing the type argument present in the WebIDL, but for now
that should be fine as we're exposing low-level bindings and it's not otherwise
clear whether having a typed wrapper is a great option. Usage of `JsCast` can
convert the incoming value to a different object fairly easily.
The purpose of the `web-sys` crate is to expose functionality of the web, not
necessarily take an opinionated stance on how it should be exposed. In that
sense it should be able to work with the `Promise` as you would a typed promise
in terms of no functionality is left out. That being said we'll likely want to
be sure to reconsider this before 1.0-stabilizing the `web-sys` crate!
This commit adds support for two different features of the "special" operations
in WebIDL. First, it implements the desugaring [described by WebIDL][1] where
this:
interface Dictionary {
getter double getProperty(DOMString propertyName);
setter void setProperty(DOMString propertyName, double propertyValue);
};
becomes ...
interface Dictionary {
double getProperty(DOMString propertyName);
void setProperty(DOMString propertyName, double propertyValue);
getter double (DOMString propertyName);
setter void (DOMString propertyName, double propertyValue);
};
where specifically a named `getter` generates both a getter and a named
function.
Second it implements the distinction between two different types of getters in
WebIDL, described as:
> Getters and setters come in two varieties: ones that take a DOMString as a
> property name, known as named property getters and named property setters, and
> ones that take an unsigned long as a property index, known as indexed property
> getters and indexed property setters.
The name `get` is given to DOMString arguments, and the name `get_idx` is given
to index property getters.
[1]: https://heycam.github.io/webidl/#idl-special-operations
This commit implements the `extends` attribute for `#[wasm_bindgen]` to
statically draw the inheritance hierarchy in the generated bindings, generating
appropriate `AsRef`, `AsMut`, and `From` implementations.
This commit implements the `JsCast` trait automatically for all imported types
in `#[wasm_bindgen] extern { ... }` blocks. The main change here was to generate
an `instanceof` shim for all imported types in case it's needed.
All imported types now also implement `AsRef<JsValue>` and `AsMut<JsValue>`
First added in #161 this never ended up panning out, so let's remove the
experimental suport which isn't actually used by anything today and hold off on
any other changes until an RFC happens.
This commit migrates the `wasm-bindgen-webidl` crate from the `webidl` parser to
`weedle`. The main rationale for doing this is that `webidl` depends on
`lalrpop`, which is quite a large dependency and takes a good deal of time to
compile. The `weedle` crate, however, depends on `nom` and is much faster to
compile.
Almost all translations were pretty straightforward. Some abstractions changed
and/or were introduced in this commit when moving to `weedle` like the
`ToSynType` trait, but otherwise the generated bindings should be the same. It's
been verified that the `weedle`-generated bindings are exactly the same as the
`webidl`-generated bindings, with the one exception of `weedle` generates one
more method, `WebGpuCommandEncoder::transition_buffer`. It's not clear currently
why `webidl` didn't generate this method, as its [idl] is pretty straightforward!
This commit is using a [fork] of `weedle` currently which has a number of fixes
for parsing our WebIDL, although all the fixes are quite minor!
Closes#620
[idl]: d66b834afd/crates/web-sys/webidls/enabled/WebGPU.webidl (L499)
[fork]: https://github.com/alexcrichton/weedle/tree/fix-for-web-sys
We've gotten a number of reports that the interactive tests are a bit surprising
and confusing (also because it barely prints anything!). Instead let's default
to headless testing which matches the Rust style of testing much better.
The error message for a missing WebDriver binary has been updated with a note of
how to *not* do headless testing and the message for interactive testing was
also updated to display more information as well.
* Make ConvertToAst trait fallible
It's got some panics, and we'll be switching those to errors!
* First example of a diagnostic-driven error
Add a diagnostic-driven error `#[wasm_bindgen]` being attached to public
functions, and add some macros to boot to make it easier to generate errors!
* Result-ify `src/parser.rs`
This commit converts all of `src/parser.rs` away from panics to using
`Diagnostic` instead. Along the way this adds a test case per changed `panic!`,
ensuring that we don't regress in these areas!
This commit starts to add infrastructure for targeted diagnostics in the
`#[wasm_bindgen]` attribute, intended eventually at providing much better errors
as they'll be pointing to exactly the code in question rather than always to a
`#[wasm_bindgen]` attribute.
The general changes are are:
* A new `Diagnostic` error type is added to the backend. A `Diagnostic` is
created with a textual error or with a span, and it can also be created from a
list of diagnostics. A `Diagnostic` implements `ToTokens` which emits a bunch
of invocations of `compile_error!` that will cause rustc to later generate
errors.
* Fallible implementations of `ToTokens` have switched to using a new trait,
`TryToTokens`, which returns a `Result` to use `?` with.
* The `MacroParse` trait has changed to returning a `Result` to propagate errors
upwards.
* A new `ui-tests` crate was added which uses `compiletest_rs` to add UI tests.
These UI tests will verify that our output improves over time and does not
regress. This test suite is added to CI as a new builder as well.
* No `Diagnostic` instances are created just yet, everything continues to panic
and return `Ok`, with the one exception of the top-level invocations of
`syn::parse` which now create a `Diagnostic` and pass it along.
This commit does not immediately improve diagnostics but the intention is that
it is laying the groundwork for improving diagnostics over time. It should
ideally be much easier to contribute improved diagnostics after this commit!
cc #601
* Tweak the implementation of heap closures
This commit updates the implementation of the `Closure` type to internally store
an `Rc` and be suitable for dropping a `Closure` during the execution of the
closure. This is currently needed for promises but may be generally useful as
well!
* Support asynchronous tests
This commit adds support for executing tests asynchronously. This is modeled
by tests returning a `Future` instead of simply executing inline, and is
signified with `#[wasm_bindgen_test(async)]`.
Support for this is added through a new `wasm-bindgen-futures` crate which is a
binding between the `futures` crate and JS `Promise` objects.
Lots more details can be found in the details of the commit, but one of the end
results is that the `web-sys` tests are now entirely contained in the same test
suite and don't need `npm install` to be run to execute them!
* Review tweaks
* Add some bindings for `Function.call` to `js_sys`
Name them `call0`, `call1`, `call2`, ... for the number of arguments being
passed.
* Use oneshots channels with `JsFuture`
It did indeed clean up the implementation!
This commit starts migrating the `wasm_bindgen` tests to the `wasm_bindgen_test`
framework, starting to assemble the coffin for
`wasm-bindgen-test-project-builder`. Over time all of the tests in
`tests/all/*.rs` should be migrated to `wasm_bindgen_test`, although they may
not all want to go into a monolithic test suite so we can continue to test for
some more subtle situations with `#[wasm_bindgen]`.
In the meantime those, the `tests/all/api.rs` tests can certainly migrate!
In an actual browser, the changing of the history using the binding
worked a little too well, and caused the test to fail if you refreshed
the page or manually used the back and forward buttons. The stateful
stuff has been removed - the remaining two assertions should adequately
test that the binding works, which is the point of these tests anyways.
This commit adds support to the `wasm-bindgen-test-runner` binary to
perform headless testing via browsers. The previous commit introduced a
local server to serve up files and run tests in a browser, and this
commit adds support for executing that in an automated fashion.
The general idea here is that each browser has a binary that implements
the WebDriver specification. These binaries (typically `foodriver` for
the browser "Foo") are interfaced with using HTTP and JSON messages. The
implementation was simple enough and the crates.io support was lacking
enough that a small implementation of the WebDriver protocol was added
directly to this crate.
Currently Firefox (`geckodriver`), Chrome (`chromedriver`), and Safari
(`safaridriver`) are supported for running tests. The test harness will
recognize env vars like `GECKODRIVER=foo` to specifically use one or
otherwise detects the first driver in `PATH`. Eventually we may wish to
automatically download a driver if one isn't found, but that isn't
implemented yet.
Headless testing is turned on with the `CI=1` env var currently to be
amenable with things like Travis and AppVeyor, but this may wish to grow
an explicit option to run headless tests in the future.
This commit updates the test harness for in-browser testing. It now no longer
unconditionally uses `fs.writeSync`, for example. Instead a `Formatter` trait is
introduced for both Node/browser environments and at runtime we detect which is
the appropriate one to use.
This commit starts to add support for in-browser testing with
`wasm-bindgen-test-runner`. The current idea here is that somehow it'll be
configured and it'll spawn a little HTTP server serving up files from the
filesystem. This has been tested in various ways but isn't hooked up just yet,
wanted to make sure this was somewhat standalone! Future support for actually
running these tests will be coming in later commits.
This commit moves the `webidl/tests` folder to a new `crates/webidl-tests` crate
(to have a test-only build script) and ports them to the `#[wasm_bindgen_test]`
attribute, which should hopefully make testing much speedier for execution!
* Fix importing the same identifier from two modules
This needed a fix in two locations:
* First the generated descriptor function needed its hash to include the module
that the import came from in order to generate unique descriptor functions.
* Second the generation of the JS shim needed to handle duplicate identifiers in
a more uniform fashion, ensuring that imported names didn't clash.
* Fix importing the same name in two modules
Previously two descriptor functions with duplicate symbols were emitted, and now
only one function is emitted by using a global table to keep track of state
across macro invocations.
* Add renaming of conflicting constructors and operations
* Rename conflicting to overloaded
* Fix newlines
* Use or_insert_with, add a comment to TypeToString
* Use more Rust-like names
* Use opt instead of nullable
* Use argument names instead of argument types if possible
* Drop new for overloaded constructots
* Remove extra newline
* Move WebIDL files from unavailable_overloaded_fn
* Move RTCDataChannel, RTCPeerConnection and Selection to unavailable_option_primitive
* Create bindings for RegExp
* Address review comments
- Split the constructor into two: `new` and `new_regexp`. This way we
can write RegExp::new("foo", "g") rather than
RegExp::new(&JsValue::from("foo"), "g").
- The js_name for the setter for lastIndex should be `lastIndex` and
not `set_lastIndex`. But fixing this causes a panic. Remove the
method for now.
* Try to enable all webidls
* Separate out unavailable webidl files by reason.
* Create record of fully tested WebIDL files
* Update notes to reflect new situation with web-idl
* Make a blank ident fail, disable the necessary widls.
It turns out that all the blank idents came from blank enum variants,
which is allowed in webidl apparently.
* Adding in initial support for all HTML*Element interfaces.
* Fix camelcasing of short HTML interface names
* Disabling span test as breaks on taskcluster
The JS iterator protocol uses duck typing and we don't need separate
ArrayIterator and SetIterator etc types, we can have a single iterator type for
the whole protocol.
Currently it generates a lot of shim functions which delegate to the wasm module
when loaded, but it turns out with `export let` we can just update the bindings!
Instead of exporting a bunch of shims this updates the export functionality to
only update the `export let` directives with the direct values from the wasm
module once the module is done loading.
In addition to being more ergonomic these are much more efficient at reading
large files as they preallocate internally. This provides a nice speed boost
locally, reducing the overhead of `wasm-bindgen-test-runner` from 0.23s to
0.19s, yay!