Commit Graph

207 Commits

Author SHA1 Message Date
Alex Crichton
53d6e00b70 Fix compilation of example in docs
Stray `match` keyword!

Closes #1109
2018-12-12 08:20:35 -08:00
Nick Fitzgerald
77ddf5ebcd examples: Add a requestAnimationFrame loop example 2018-12-10 12:50:19 -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
Danilo Bargen
fdf201a8f1
Fix typo in wasm-bindgen-test usage docs 2018-11-29 17:47:07 +01: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
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
Tim Ryan
90193eab51 Adds support for #[wasm_bindgen(typescript_custom_section)]. 2018-11-24 00:49:28 -05:00
Markus Stange
4c44f7d2db
Fix typo: wasm-bindgn -> wasm-bindgen 2018-11-22 10:34:31 -05:00
Garrett Steffen
92ce73d197
Revert js-sys link change, added web-sys link
Correction to Pull Request #1035

limira is correct, that particular paragraph is referencing another crate. Considering the wasm-bindgen tutorial links here for the web-sys crate I added another link to the correct cargo link.

Sorry for the mistake.
2018-11-17 09:57:43 -06:00
Garrett Steffen
0452fbc639
Fixed link to the wrong crate 2018-11-16 19:22:19 -06: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
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
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
Alex Crichton
ac6a230d83
Merge pull request #1012 from alexcrichton/rename-exported-type
Implement support for `js_class` on exported types
2018-11-05 17:44:21 -06: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
Alex Crichton
9478a65e3c Add a note about renaming types and js_class
When a type is renamed in Rust via `js_name` then all method imports
will also need a `js_class` annotation to hook them up correctly.
2018-11-05 11:10:09 -08:00
Jonathan Kingston
b322f46303 Adding in TODO MVC example using web-sys 2018-10-23 14:15:42 +01:00
Alex Crichton
25b26f41e7 Implement support for WebAssembly threads
... and add a parallel raytracing demo!

This commit adds enough support to `wasm-bindgen` to produce a workable
wasm binary *today* with the experimental WebAssembly threads support
implemented in Firefox Nightly. I've tried to comment what's going on in
the commits and such, but at a high level the changes made here are:

* A new transformation, living in a new `wasm-bindgen-threads-xform`
  crate, prepares a wasm module for parallel execution. This performs a
  number of mundane tasks which I hope to detail in a blog post later on.
* The `--no-modules` output is enhanced with more support for when
  shared memory is enabled, allowing passing in the module/memory to
  initialize the wasm instance on multiple threads (sharing both module
  and memory).
* The `wasm-bindgen` crate now offers the ability, in `--no-modules`
  mode, to get a handle on the `WebAssembly.Module` instance.
* The example itself requires Xargo to recompile the standard library
  with atomics and an experimental feature enabled. Afterwards it
  experimentally also enables threading support in wasm-bindgen.

I've also added hopefully enough CI support to compile this example in a
builder so we can upload it and poke around live online. I hope to
detail more about the technical details here in a blog post soon as
well!
2018-10-23 01:20:18 -07:00
Alex Crichton
34b75c35b2 Allow passing a WebAssembly.Module in --no-modules
I've noticed this in a few cases where it's sometimes easy to have a
`WebAssembly.Module` on-hand for the `--no-modules` mode where you don't
want to necessarily `fetch`. This commit changes the exported
initialization function in `--no-modules` mode to support both!
2018-10-16 10:48:07 -07:00
Alex Crichton
5957289ef2 Add caveat for BigInt and u64 in browser support
Closes #948
2018-10-10 10:30:32 -07:00
Nick Fitzgerald
8d195d07ee guide: clarify testing docs; prefer wasm-pack-based workflows 2018-10-09 09:31:01 -07:00
Daniele Esposti
882c6ef0ed Update installation of toolchain in Basic Usage page 2018-10-05 17:15:22 +01:00
Alex Crichton
32fd1227e4
Merge pull request #910 from alexcrichton/fix-text-encoder-edge
Fix polyfill of `TextEncoder` and `TextDecoder`
2018-10-03 00:07:30 -07:00
Alex Crichton
f75349262a Rename polyfill to vendor_prefix
cc #906
2018-10-01 14:45:30 -07:00
Alex Crichton
717cfa303d Fix polyfill of TextEncoder and TextDecoder
This commit does a few things, including:

* Fixing the generated JS of `wasm-bindgen` to allow polyfills to work.
  (a minor tweak of the generated JS)

* All examples are updated to include a Webpack-specific polyfill for
  these two types to get examples working in Edge.

* A new page has been added to the guide about supported browsers. This
  mentions known caveats like IE 11 requiring `wasm2js` as well as
  documenting some `TextEncoder` and `TextDecoder` workarounds for Edge.

Closes #895
2018-09-30 10:16:20 -07:00
Alex Crichton
3c14f7a6eb Implement a polyfill attribute for imports
Allow using imported APIs under alternative names, such as prefixed
names, for web APIs when the exact API differs across browsers.
2018-09-28 13:43:00 -07:00
Alex Crichton
114176f8b8 Add documentation about supported targets
* Main target is wasm32-unknown-unknown
* All other targets work ok, but imports panic
* Emscripten explicitly not supported at this time

Closes #892
2018-09-27 13:39:23 -07:00
Alex Crichton
a3e160744e Update Window example in web-sys 2018-09-26 08:14:14 -07:00
Alex Crichton
7a2f55d2d1 Update another git url 2018-09-26 08:11:55 -07:00
Alex Crichton
3b2448cce1 Update web-sys docs now that it's published 2018-09-26 08:10:18 -07:00
Cameron Taggart
052b41b84c update to "webpack-cli": "^3.1.1" 2018-09-25 18:50:27 -07:00
Nick Fitzgerald
a920656e09 guide: Update untyped JS values section to handle fallibility of Reflect::* APIs 2018-09-25 14:30:26 -07:00
Nick Fitzgerald
dfd0f534f9 guide: Add section about iterating over JS values 2018-09-25 14:30:26 -07:00
Nick Fitzgerald
8a2b2cb6e1
Merge pull request #876 from alexcrichton/contrib-js-sys
Update contributing docs for js-sys
2018-09-24 17:35:28 -07:00
Nick Fitzgerald
f9f8d2c214
Merge pull request #877 from alexcrichton/opt-tests
Add some blurbs about optimizing for size
2018-09-24 17:30:41 -07:00
Alex Crichton
b256b98e38 Improve link_mem_intrinsics hack
Previously the `link_mem_intrinsics` hack actually had a runtime
overhead by storing a value into a global location, but it turns out we
can actually use a non-inlined function call as part of the *descriptor*
which requires this to be in the final binary, but we'll end up snip'ing
the value at the end.

All in all this should mean that it's not a zero-overhead solution for
linking these intrinsics! The `#[wasm_bindgen]` attribute already has
other problems if the descriptors don't show up, so that's the least of
our issues!
2018-09-24 15:43:04 -07:00
Alex Crichton
0ba7e8107c Add some blurbs about optimizing for size
The main thing we mention is to explicitly not measure the output of the
compiler, but only the ouput of `wasm-bindgen` itself.

Closes #826
2018-09-24 11:28:02 -07:00
Alex Crichton
752ad99cbd Update contributing docs for js-sys
Tweak a few links here and there and otherwise mention that we should be
complete and stage 4 proposals can be added in the future

Closes #859
2018-09-24 10:51:05 -07:00
Alex Crichton
d10ca579e4 Yarn is no longer needed for tests 2018-09-24 10:44:14 -07:00
Alex Crichton
e353c1cf6f Build examples and deploy to gh-pages
Rejigger Travis slightly to take advantage of build stages to build the
`gh-pages` branch amongst a set of builders, and then when they're all
done we synchronize and deploy the site. For now use S3 as a backing
store for data between jobs.
2018-09-20 20:55:02 -07:00
Alex Crichton
f871cf6354 Update design doc link
Closes #851
2018-09-20 17:38:19 -07:00
Alex Crichton
3efe51eb8b Reorganize and rewrite examples
This commit is a large-ish scale reorganization of our examples. The
main goal here is to have a dedicated section of the guide for example,
and all examples will be listed there. Each example's `README` is now
just boilerplate pointing at the guide along with a blurb about how to
run it.

Some examples like `math` and `smorgasboard` have been deleted as they
didn't really serve much purpose, and others like `closures` have been
rewritten with `web-sys` instead of hand-bound bindings.

Overall it's hoped that this puts us in a good and consistent state for
our examples, with all of them being described in the guide, excerpts
are in the guide, and they're all relatively idiomatically using
`web-sys`.
2018-09-20 16:45:30 -07:00
Alex Crichton
4ca187cc2b
Merge pull request #835 from blm768/webgl-example
Create basic WebGL example
2018-09-19 11:56:43 -07:00
Alex Crichton
7cf4213283 Allow returning Result from functions
This commit adds support for exporting a function defined in Rust that returns a
`Result`, translating the `Ok` variant to the actual return value and the `Err`
variant to an exception that's thrown in JS.

The support for return types and descriptors was rejiggered a bit to be a bit
more abstract and more well suited for this purpose. We no longer distinguish
between functions with a return value and those without a return value.
Additionally a new trait, `ReturnWasmAbi`, is used for converting return values.
This trait is an internal implementation detail, however, and shouldn't surface
itself to users much (if at all).

Closes #841
2018-09-18 13:13:59 -07:00
Ben Merritt
426671d83c Create basic WebGL example 2018-09-17 20:59:26 -07:00
Alex Crichton
87176b15bc Fix dependencies sections for web-sys in guide
Closes #829
2018-09-14 14:05:04 -07:00
Alex Crichton
05a0a5c6a1
Merge pull request #825 from fitzgen/paint-table-of-contents
guide: Add paint example to the table of contents
2018-09-13 18:21:03 -07:00
Nick Fitzgerald
fa5551d91f guide: Add paint example to the table of contents 2018-09-13 16:36:57 -07:00
Chinedu Francis Nwafili
17db217fe9
Change CI example to use a releaase binary 2018-09-13 19:06:31 -04:00
Anton Danilkin
97fc9b64f8 Add a section to the guide about the Paint example 2018-09-14 00:46:53 +03:00
Nick Fitzgerald
c6ede65856
Merge pull request #818 from fitzgen/guide-wasm-bindgen-test
guide: Add documentation for testing with `wasm-bindgen-test`
2018-09-12 15:40:31 -07:00
Nick Fitzgerald
e2e815a477 guide: Add documentation for testing with wasm-bindgen-test 2018-09-12 15:40:09 -07:00
Nick Fitzgerald
4e86ecd2c5
Merge pull request #813 from fitzgen/duck-typed-interfaces
Duck typed interfaces
2018-09-12 11:20:26 -07:00
Alex Crichton
73e6191b60
Merge pull request #703 from lnicola/guide-publish-app
Guide: explain how to deploy the applicaton to a web server
2018-09-12 09:33:39 -07:00
Nick Fitzgerald
1872e84a8a guide: Add section on working with duck-typed interfaces 2018-09-11 16:40:32 -07:00
Nick Fitzgerald
27a7008764 guide: Add section on accessing properties of untyped values
Part of #616
2018-09-11 15:26:51 -07:00
Alex Crichton
1d2d397f55
Merge pull request #808 from fitzgen/web-sys-in-the-guide
web-sys gets its own section in the guide
2018-09-11 10:08:06 -07:00
Nick Fitzgerald
3f92607be2
Merge pull request #810 from Tarnadas/doc/js-sys-function
doc: Add number suffix hint for JS function calls
2018-09-11 10:04:20 -07:00
Mario Reder
84a477d78d doc: Add number suffix hint for JS function calls 2018-09-11 07:25:08 +02:00
Mario Reder
e6fe9cf353 doc: Add nightly feature hint for closures
resolves #767
2018-09-11 07:06:21 +02:00
Laurentiu Nicola
8ffa24bfe5 Guide: explain how to deploy the application 2018-09-11 07:33:24 +03:00
Nick Fitzgerald
86796f8a03 guide: Add a user-facing web-sys section 2018-09-10 17:51:44 -07:00
Nick Fitzgerald
e730ee9a62 guide: update publishing new releases page 2018-09-10 15:37:12 -07:00
Nick Fitzgerald
1a39e4e737 guide: Fix nesting of contributing pages 2018-09-10 15:32:39 -07:00
Alex Crichton
457efc0f31 Implement support for WebIDL Callback types
This commit adds support for the WebIDL `Callback` type by translating all
callbacks to the `js_sys::Function` type. This will enable passing raw JS values
into callbacks as well as Rust valus using the `Closure` type.

This commit doesn't currently implement "callback interfaces" in WebIDL, that's
left for a follow-up commit.
2018-09-06 19:50:46 -07:00
Alex Crichton
269c491380
Gate web-sys APIs on activated features (#790)
* Gate `web-sys` APIs on activated features

Currently the compile times of `web-sys` are unfortunately prohibitive,
increasing the barrier to using it. This commit updates the crate to instead
have all APIs gated by a set of Cargo features which affect what bindings are
generated at compile time (and which are then compiled by rustc). It's
significantly faster to activate only a handful of features vs all thousand of
them!

A magical env var is added to print the list of all features that should be
generated, and then necessary logic is added to ferry features from the build
script to the webidl crate which then uses that as a filter to remove items
after parsing. Currently parsing is pretty speedy so we'll unconditionally parse
all WebIDL files, but this may change in the future!

For now this will make the `web-sys` crate a bit less ergonomic to use as lots
of features will need to be specified, but it should make it much more
approachable in terms of first-user experience with compile times.

* Fix AppVeyor testing web-sys

* FIx a typo

* Udpate feature listings from rebase conflicts

* Add some crate docs and such
2018-09-05 12:55:30 -07:00
Richard Dodd
7d5d845608 Add docs and remove typecheck from variadic attribute 2018-09-01 13:55:35 +01:00
Nick Fitzgerald
e290c75c32 Add @afdw to the team! \o/ 2018-08-30 12:59:34 -07:00
Alex Crichton
69a831423b Update publishing instructions
We've got a publish script now!
2018-08-27 13:51:47 -07:00
Alex Crichton
d4297ad2d3 Remove use_extern_macros features
This has now been stabilized!
2018-08-19 14:33:01 -07:00
Nick Fitzgerald
a5a8fd747d guide: Split supported types section up into many sub sections 2018-08-14 18:01:17 -07:00
Nick Fitzgerald
b1e3101fd4 guide: Add examples for number slices 2018-08-14 17:42:47 -07:00
Nick Fitzgerald
fea41b4a87 guide: also allow Option<bool> 2018-08-14 17:34:16 -07:00
Nick Fitzgerald
9c9e53485a guide: Add examples of boxed number slices 2018-08-14 17:15:01 -07:00
Nick Fitzgerald
8043baac69 guide: Add examples for working with numbers to types section 2018-08-14 15:45:25 -07:00
Nick Fitzgerald
602b63c1b0 guide: Add working with pointers example to types section 2018-08-14 15:24:43 -07:00
Alex Crichton
46f1719524
Merge pull request #696 from fitzgen/more-examples-for-guide
More examples for guide
2018-08-13 18:34:26 -06:00
Nick Fitzgerald
d1b2299340 guide: Add Box<[JsValue]> example to supported types section 2018-08-13 17:08:18 -07:00
Nick Fitzgerald
60307e81f9 guide: Add JsValue example to supported types section 2018-08-13 16:57:29 -07:00
Nick Fitzgerald
fedd1a5440 guide: Add bool example to supported types section 2018-08-13 16:55:05 -07:00
Nick Fitzgerald
975a122d6d guide: Add a char example to the supported types section 2018-08-13 16:24:39 -07:00
Nick Fitzgerald
74dc8874e1 guide: add String example usage to supported types 2018-08-13 16:20:25 -07:00
Nick Fitzgerald
fa72afe286 guide: Add str examples to supported types section 2018-08-13 16:12:58 -07:00
Nick Fitzgerald
8e19645006 guide: Add exported rust type examples to reference 2018-08-13 16:03:02 -07:00
Nick Fitzgerald
485d377594 guide: Add a reference section for Promises and Futures 2018-08-13 15:44:28 -07:00
R. Andrew Ohana
36fe4c23dc
Merge pull request #678 from derekdreery/webidl_namespace_support
Add support webidl namespaces.
2018-08-12 17:41:54 -07:00
Richard Dodd
ea05235985 Fix docs about testing webidl 2018-08-12 21:28:59 +01:00
Nick Fitzgerald
e87498e939 guide: Start adding example usage to "supported types" section 2018-08-10 16:56:40 -07:00
Mario Reder
cc76963bad
guide: typo in arbitrary data with serde 2018-08-09 17:46:38 +02:00
Nick Fitzgerald
2fcc74e226 guide: refactor and update testing instructions for contributing 2018-08-08 15:33:56 -07:00
Alex Crichton
5b935526ff
Merge pull request #640 from alexcrichton/jscast
Implement RFC #2 - casting hierarchy between JS values
2018-08-07 17:26:37 -05:00
Nick Fitzgerald
43636977ae
Merge pull request #664 from fitzgen/guide-serde-arbitrary-data
Guide serde arbitrary data
2018-08-07 14:48:45 -07:00
Nick Fitzgerald
1d92784e07 guide: add indexing_{getter,setter,deleter} attribute page 2018-08-07 14:42:13 -07:00
Nick Fitzgerald
2e7620e014 guide: Polish Serializing + Deserializing into/from JsValue with Serde section 2018-08-07 14:25:30 -07:00
Nick Fitzgerald
676611020e guide: Delete "Feature Reference" page
We now have a rather large section for feature reference, and I don't think
there is anything in this page that isn't covered elsewhere and in more detail
anymore.
2018-08-07 14:25:30 -07:00
Alex Crichton
bd15db40a0 Rebase fallout and review comments 2018-08-07 13:24:48 -07:00
Alex Crichton
37db88ebfa Implement #[wasm_bindgen(extends = ...)]
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.
2018-08-07 13:04:11 -07:00
Nick Fitzgerald
9d291187c5 guide: Fix alphabetizing of attributes 2018-08-07 11:14:00 -07:00
Nick Fitzgerald
451a2a8118 guide: Add small intro to Rust export attributes section 2018-08-07 11:13:41 -07:00