Commit Graph

1005 Commits

Author SHA1 Message Date
Alex Crichton
a83c3af4bf Ensure our JS heap is a dense array
Turns out `heap.fill(undefined)` is required to ensure it's a dense
array, otherwise we'll accidentally be a sparse array and much slower
than necessary!
2018-11-30 13:23:41 -08:00
Alex Crichton
29531c0abf Run rustfmt 2018-11-30 13:04:27 -08:00
Alex Crichton
49d835a7bc Switch from heap/stack to just a heap
This commit switches strategies for storing `JsValue` from a heap/stack
to just one heap. This mirrors the new strategy for `JsValue` storage
in #1002 and should make multiplexing those strategies at
`wasm-bindgen`-time much easier.

Instead of having one array which acts as a stack for borrowed values
and one array for a heap of borrowed values, only one JS array is used
for storage of JS values now. This makes `getObject` far simpler by
simply being an array access, but it means that cloning an object now
reserves a new slot instead of reference counting it. If the old
reference counting behavior is needed it's thought that `Rc<JsValue>`
can be used in Rust.

The new "heap" has an initial stack pointer which grows downwards, and a
heap which grows upwards. The heap is a singly-linked-list which is
allocated/deallocated from. The stack grows downwards to zero and
presumably starts generating errors once it underflows. An initial stack
size of 32 is chosen as that should encompass all use cases today, but
we can eventually probably add configuration for this!

Note that the heap is initialized to all `null` for the stack and then
the initial JS values (`undefined`, `null`, `true`, `false`) are pushed
onto the heap in reserved locations.
2018-11-30 12:07:16 -08:00
Alex Crichton
07b148789d Defer exposing methods until they're needed
Previously `catch` and `variadic` would exopse methods in our JS shims,
but they did so earlier than necessary. Turns out `variadic` didn't
actually need to expose anything and `catch` could do so much later!
2018-11-29 17:50:13 -08:00
Sendil Kumar N
fbad34a4cb
Merge pull request #1064 from alexcrichton/wasm2es6js-imports
wasm2es6js: Fix handling of exported imports
2018-11-30 01:01:45 +01:00
Alex Crichton
91e9495805
Merge pull request #1065 from alexcrichton/describe-closures
Move closure shims into the descriptor
2018-11-29 17:30:58 -06:00
Alex Crichton
5f966c5a8f
Merge pull request #1063 from alexcrichton/wasm2es6js-start
wasm2es6js: Fix handling of start function
2018-11-29 17:30:24 -06:00
Alex Crichton
2bd9c0eafb
Merge pull request #1067 from alexcrichton/minor-tweaks
A few minor CLI tweaks during work on #1002
2018-11-29 15:16:14 -06:00
Alex Crichton
42053ddd4e Move closure shims into the descriptor
Currently closure shims are communicated to JS at runtime, although at
runtime the same constant value is always passed to JS! More pressing,
however, work in #1002 requires knowledge of closure descriptor indices
at `wasm-bindgen` time which is not currently known.

Since the closure descriptor shims and such are already constant values,
this commit moves the descriptor function indices into the *descriptor*
for a closure/function pointer. This way we can learn about these values
at `wasm-bindgen` time instead of only knowing them at runtime.

This should have no semantic change on users of `wasm-bindgen`, although
some closure invocations may be slightly speedier because there's less
arguments being transferred over the boundary. Overall though this will
help #1002 as the closure shims that the Rust compiler generates may not
be the exact ones we hand out to JS, but rather wrappers around them
which do `anyref` business things.
2018-11-29 12:42:44 -08:00
Alex Crichton
430fce7a8b Enable env_logger for the wasm-bindgen CLI tool
This was intended earlier, but fogotten! Found during work on #1002
2018-11-29 12:25:24 -08:00
Alex Crichton
82bfbf9d20 Add more context to a wasm-bindgen-test-runner error
Minor cleanup I found during #1002
2018-11-29 12:25:10 -08:00
Alex Crichton
167274c9b4 Remove an unused crate from wasm-bindgen-webidl
Apparently this is no longer needed according to rustc!
2018-11-29 12:22:54 -08:00
Alex Crichton
b4171d0bb2 wasm2es6js: Fix handling of exported imports
This commit fixes a case in `wasm2es6js` where if an imported function
was reexported it wasn't handled correctly. This doesn't have a direct
test but came up during the development of #1002
2018-11-29 11:56:12 -08:00
Alex Crichton
522e973694 wasm2es6js: Fix handling of start function
This is split out from #1002 and is intended to fix the tool's handling
of the `start` function. For the most accurate emulation of the wasm ESM
spec I believe we need to defer execution of the start function until
all our exports are wired up which should allow valid cyclical
references during instantiation.

The fix here is to remove the start function, if one is present, and
inject an invocation of it at the end of initialization (after our
exports are wired up). This fixes tests on #1002, but doesn't have any
direct analogue for tests here just yet.

Along the way because multiple files now come out of `wasm2es6js` by
default I've added an `--out-dir` argument as well as `-o` to ensure
that a folder for all outputs can be specified.
2018-11-29 11:52:23 -08:00
Alex Crichton
a2aa28e4d3 Add a #[wasm_bindgen(start)] attribute
This commit adds a new attribute to `#[wasm_bindgen]`: `start`. The
`start` attribute can be used to indicate that a function should be
executed when the module is loaded, configuring the `start` function of
the wasm executable. While this doesn't necessarily literally configure
the `start` section, it does its best!

Only one crate in a crate graph may indicate `#[wasm_bindgen(start)]`,
so it's not recommended to be used in libraries but only end-user
applications. Currently this still must be used with the `crate-type =
["cdylib"]` annotation in `Cargo.toml`.

The implementation here is somewhat tricky because of the circular
dependency between our generated JS and the wasm file that we emit. This
circular dependency makes running initialization routines (like the
`start` shim) particularly fraught with complications because one may
need to run before the other but bundlers may not necessarily respect
it. Workarounds have been implemented for various emission strategies,
for example calling the start function directly after exports are wired
up with `--no-modules` and otherwise working around what appears to be
a Webpack bug with initializers running in a different order than we'd
like. In any case, this in theory doesn't show up to the end user!

Closes #74
2018-11-28 22:11:15 -08:00
Alex Crichton
c8a352189b Assert all attributes are used by default
This commit implements a system that will assert that all
`#[wasm_bindgen]` attributes are actually used during compilation. This
should help ensure that we don't sneak in stray attributes that don't
actually end up having any meaning, and hopefully make it a bit easier
to learn `#[wasm_bindgen]`!
2018-11-28 11:42:48 -08:00
Alex Crichton
e3b628689f
Merge pull request #1053 from alexcrichton/dts-wasm
Generate a `*.d.ts` file for wasm files
2018-11-27 16:55:25 -06:00
Alex Crichton
047c41c1ec Generate a *.d.ts file for wasm files
This generates a `*.d.ts` file for the wasm file that wasm-bindgen emits
whenever typescript is enable *in addition* to the `*.d.ts` file that
already exists for the JS shim.

Closes #1040
2018-11-27 12:36:55 -08:00
Alex Crichton
151ed58b69 Consistently use extern "C"
This is what rustfmt favors, so let's favor it too!

Closes #1042
2018-11-27 12:27:00 -08:00
Alex Crichton
48f4adfa8c Run rustfmt over everything 2018-11-27 12:07:59 -08:00
Tim Ryan
90193eab51 Adds support for #[wasm_bindgen(typescript_custom_section)]. 2018-11-24 00:49:28 -05:00
Ingvar Stepanyan
26f1903bee
Fix variable name reference for path_or_module 2018-11-22 18:26:37 +00:00
Alex Crichton
6f1e3c8135 Add wasm-bindgen to the producers section
Recently proposed in WebAssembly/tool-conventions#65 each wasm file will
now have an optional `producers` section listing the tooling that went
into producing it. Let's add `wasm-bindgen` in when it processes a wasm
file!
2018-11-19 15:52:58 -08:00
Alex Crichton
992fad85ab
Merge pull request #1030 from alexcrichton/wire-up-directly
Add an optimization to directly wire up imported functions
2018-11-14 09:05:47 -06:00
Alex Crichton
68537b9649 Add an optimization to directly wire up imported functions
This commit adds an optimization to `wasm-bindgen` to directly import
and invoke other modules' functions from the wasm module, rather than
going through a shim in the imported bindings. This will be an important
optimization in the future for the host bindings proposal, but for now
it's largely just a proof-of-concept to show that we can do it and is
unlikely to bring about many performance benefits.

The implementation in this commit is largely refactoring to reorganize a
bit how functions are imported, but the implementation happens in
`generate_import_function`.

With this commit, 71/287 imports in the `tests/wasm/main.rs` suite get
hooked up directly to the ES modules, no shims needed!
2018-11-13 13:16:38 -08:00
Alex Crichton
c915870526 Remove temporary object allocation
When returning a ptr/length for allocations and such wasm-bindgen's
generated JS would previously return an array with two elements. It
turns out this doesn't optimize well in all engines! (See #1031). It
looks like we can optimize the array destructuring a bit more, but this
is all generated code which doesn't need to be too readable so we can
also remove the temporary allocation entirely and just pass the second
element of this array through a global instead of the return value.

Closes #1031
2018-11-13 08:10:05 -08:00
Richard Dodd
4fa9865cc9 Add manual docs for single manual function. 2018-11-13 14:36:33 +00:00
Alex Crichton
22ca15f81e Bump to 0.2.28 2018-11-12 09:28:01 -08:00
Alex Crichton
dc4e78550a
Merge pull request #1019 from alexcrichton/rfc-5
Implement rustwasm/rfcs#5, implement `Deref` for imports and `structural` by default
2018-11-12 10:59:46 -06:00
dependabot[bot]
d673d5cfff
Update env_logger requirement from 0.5 to 0.6
Updates the requirements on [env_logger](https://github.com/sebasmagri/env_logger) to permit the latest version.
- [Release notes](https://github.com/sebasmagri/env_logger/releases)
- [Commits](https://github.com/sebasmagri/env_logger/commits/v0.6.0)

Signed-off-by: dependabot[bot] <support@dependabot.com>
2018-11-12 08:17:10 +00:00
Alex Crichton
406581da7e
Merge pull request #1024 from alexcrichton/remove-names
Add a flag to remove the wasm name section
2018-11-09 16:15:14 -06:00
Alex Crichton
37889d9fcd Fix --no-modules passing in WebAssembly.Module
This fixes a mistake in allowing a `WebAssembly.Module` to be passed to
the initialization function in `--no-modules` mode by ensuring that it
resolves to a map of an instance/module instead of just resolving to an
instance.
2018-11-09 12:15:36 -08:00
Alex Crichton
cb246e38fb Rename host_binding to final 2018-11-09 08:00:41 -08:00
Alex Crichton
2c9084d0e2 Update web-sys test to only test compilation 2018-11-09 07:56:48 -08:00
Alex Crichton
12fc09a124 Add a flag to remove the wasm name section
This commit adds a `--remove-name-section` flag to the `wasm-bindgen`
command which will remove the `name` section of the wasm file, used to
indicate the names of functions typically used in debugging. This flag
is off-by-default and will primarily be controlled by wasm-pack,
typically being passed by default with `wasm-pack build --release`.

Closes #1021
2018-11-09 07:45:19 -08:00
Alex Crichton
8ba467e905 Disallow both structural and host_binding 2018-11-08 16:47:46 -08:00
Nick Fitzgerald
75c18f0916 Only emit JS glue assertions for move arguments in debug mode 2018-11-08 15:08:46 -08:00
Alex Crichton
4c42aba007 Switch all imports to structural by default
This commit switches all imports of JS methods to `structural` by
default. Proposed in [RFC 5] this should increase the performance of
bindings today while also providing future-proofing for possible
confusion with the recent addition of the `Deref` trait for all imported
types by default as well.

A new attribute, `host_binding`, is introduced in this PR as well to
recover the old behavior of binding directly to an imported function
which will one day be the precise function on the prototype. Eventually
`web-sys` will switcsh over entirely to being driven via `host_binding`
methods, but for now it's been measured to be not quite as fast so we're
not making that switch yet.

Note that `host_binding` differs from the proposed name of `final` due
to the controversy, and its hoped that `host_binding` is a good
middle-ground!

[RFC 5]: https://rustwasm.github.io/rfcs/005-structural-and-deref.html
2018-11-08 13:52:18 -08:00
Alex Crichton
6093fd29d1 Don't convert boolean arguments going to wasm
The wasm spec defines boolean conversion when crossing to the wasm type
i32 as 1 for `true` and 0 for `false`, so no need for us to do it
ourselves!
2018-11-08 13:06:03 -08:00
Alex Crichton
a16b4dd9a4 Optimize shim generation for structural items
This commit removes shims, where possible, for `structural` items.
Instead of generating code that looks like:

    const target = function() { this.foo(); };
    exports.__wbg_thing = function(a) { target.call(getObject(a)); };

we now instead generate:

    exports.__wbg_thing = function(a) { getObject(a).foo(); };

Note that this only applies to `structural` bindings, all default
bindings (as of this commit) are still using imported targets to ensure
that their binding can't change after instantiation.

This change was [detailed in RFC #5][link] as an important optimization
for `structural` bindings to ensure they've got performance parity with
today's non-`structural` default bindings.

[link]: https://rustwasm.github.io/rfcs/005-structural-and-deref.html#why-is-it-ok-to-make-structural-the-default
2018-11-08 13:04:38 -08:00
Alex Crichton
64a6241960 Fix tagging static methods as structural 2018-11-08 11:50:34 -08:00
Alex Crichton
b013ec6288 Use splat instead of arguments in tests
Previously `arguments` was used to pass around an array of arguments,
but this wasn't actually a `js_sys::Array` but rather a somewhat
esoteric internal object. When switching over `Array` methods to be
`structural` this caused issues because the inherent methods on an
`arguments` object were different than that of `js_sys::Array`.
2018-11-08 11:46:52 -08:00
Alex Crichton
5b76a6291e Implement Deref for all imported JS types
This commit implements the first half of [RFC #5] where the `Deref`
trait is implemented for all imported types. The target of `Deref` is
either the first entry of the list of `extends` attribute or `JsValue`.

All examples using `.as_ref()` with various `web-sys` types have been
updated to the more ergonomic deref casts now. Additionally the
`web-sys` generation of the `extends` array has been fixed slightly to
explicitly list implementatoins in the hierarchy order to ensure the
correct target for `Deref` is chosen.

[RFC #5]: https://github.com/rustwasm/rfcs/blob/master/text/005-structural-and-deref.md
2018-11-08 11:01:34 -08:00
Jake Riesterer
3d1f4263ad Change UnionType to IdlType::Union function to include any supported types instead of returning None when there is at least one unsupported type
For example, the constructor in Response.webidl accepts multiple types. However, one of those types is `ReadableStream` which isn't defined yet, and that causes all constructors for Response to be skipped even though the other argument types could be supported.
2018-11-07 10:39:22 -08:00
Alex Crichton
176eedc63b Make to_idl_type infallible
This commit makes the `to_idl_type` infallible, returning a new enum
variant, `UnknownInterface`, in the one location that we still return
`None`. By making this infallible we can ensure that expansion of unions
which have unknown types still generate methods for all the variants
which we actually have all the methods for!
2018-11-07 10:38:35 -08:00
Alex Crichton
16d5243362 Implement support for js_class on exported types
Allow defining types which have different names in Rust than they have
in JS! (just like can be done with imported types)

Closes #1010
2018-11-05 12:29:14 -08:00
Henrik Sjööh
d331b706c3 update typescript generation to reflect that Option<T> can be undefined 2018-11-04 10:26:20 +01:00
Alex Crichton
40b68c66d9 Fix a bug in coalescing types with GC
When a duplicate type is found is should no longer be considered used!
2018-10-30 15:45:21 -07:00
Alex Crichton
31c11f0781 GC passive segments
We statically know which passive segments are actually used, so let's be
sure to gc them!
2018-10-29 16:49:02 -07:00
Alex Crichton
91d68a0012 Add tests for the wasm-gc crate
This commit adds a test harness and the beginnings of a test suite for
the crate that performs GC over a wasm module. This crate historically
has had zero tests because it was thought that it would no longer be
used once LLD landed with `--gc-sections`, but `wasm-bindgen` has come
to rely more and more on `wasm-gc` for various purposes.

The last release of `wasm-bindgen` was also released with a bug in the
recently refactored support in the `wasm-gc` crate, providing a perfect
time and motivation to start writing some tests!

All tests added here are `*.wat` files which contain the expected output
after the gc pass is executed. Tests are automatically updated with
`BLESS_TESTS=1` in the environment, which is the expected way to
generate the output for each test.
2018-10-29 15:49:35 -07:00