Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coordinate with WebAssembly WG #10

Open
jethrogb opened this issue Mar 7, 2018 · 11 comments
Open

Coordinate with WebAssembly WG #10

jethrogb opened this issue Mar 7, 2018 · 11 comments

Comments

@jethrogb
Copy link
Collaborator

jethrogb commented Mar 7, 2018

Lots of issues touch both WebAssembly and general portability. Reference this
issue in the WebAssembly WG issue tracker to create a link to the portability
WG.

@mgattozzi
Copy link

A few things based off what we've seen from the wasm WG is that we have three distinct use cases:

  1. wasm that's called as part of JS or calls into it
  2. Pure wasm on the web
  3. wasm run not in a web browser

This and just how the binary is structured leads to some problems. We did talk a lot in this issue about what we can assume with the host environment but we're unsure right now what the best course is. This makes using libstd kind of possible maybe depending on the route taken but we have no satisfactory answer right now.

@Pauan
Copy link

Pauan commented Mar 12, 2018

Portability with wasm is more than just about wasm itself, it also has to do with the executing environment.

The two biggest executing environments are the browser and Node.js, and both environments have completely different APIs and behavior.

And it's not just the APIs either: the way that WebAssembly modules are loaded is different in the browser and Node.js. The way that libraries are created is different. The package managers are different. The tools are different. They really are completely separate in many deep ways.

So we actually have these use cases:

  1. Wasm library/application that wants to use JavaScript APIs that exist in the browser and Node.js.

  2. Wasm library/application that wants to use Node.js APIs.

  3. Wasm library/application that wants to use browser APIs (e.g. DOM).

  4. Wasm library/application that wants to use non-JavaScript APIs (there are already multiple wasm environments which don't include JavaScript at all, e.g. they are built on top of C++, C#, etc.)

  5. Wasm library that wants to be embedded within a larger JavaScript application.

  6. Wasm library that wants to be embedded within a larger Node.js application.

  7. Wasm library that wants to be embedded within a larger browser application.

  8. Wasm library that wants to be embedded within a larger non-JavaScript application (e.g. a wasm plugin embedded within a game engine, similar to what Lua is used for right now).

Essentially, we need to be able to differentiate (at a minimum) between Node.js APIs, browser APIs, general JavaScript APIs, and non-JavaScript APIs.

And the situation is complicated even further because there are things like Electron which allows for an application to use both Node.js and browser APIs at the same time. So it needs to be flexible enough to accommodate things like that.

@mgattozzi
Copy link

Thanks for the clarification @Pauan!

@Ericson2314
Copy link
Collaborator

Yikes. Does the standard just talk about the no-API case?

@mgattozzi
Copy link

No WebAssembly only defines the JS and Web APIs as well as the core functionality. A lot of the good stuff that will improve this probably won't be for some time. The standard made it so that we can have a Minimum Viable Product release, but it's pretty spartan in what it is capable of. We might be blocked on this for some time unfortunately.

@Ericson2314
Copy link
Collaborator

Ericson2314 commented Mar 13, 2018

@mgattozzi Actually I see that page as great news. The layering of the space gives / will give us a nice hierarchy of platforms. Since it looks like no web-assembly APIs are standardized at this moment, we'd probably want something user-definable, like the portability lint working over Cargo features, as a stop gap.

@mgattozzi
Copy link

Yeah! I'm more saying it's whatever we define right now and not something defined in the standard, which is fine, but if there is one eventually it'll break possibly the work we've done which isn't as good.

@Ericson2314
Copy link
Collaborator

Agreed, which is why I don't want to define any non-standard cfg tokens in the compiler. By using features, some library takes the fall :D.

@Pauan
Copy link

Pauan commented Mar 14, 2018

Right now the only standard way to interop with the executing environment is to use wasm import/export, and when doing so you can only import/export these types. In other words:

  • Global variables of type i32, i64, f32, or f64 (they're actually top-level module-local constants, but the spec calls them globals).

    Note: i64 is not supported in JavaScript, and you can only import/export immutable globals (this may change in the future).

  • Functions with zero or more arguments (each argument can be of type i32, i64, f32, or f64, and you can mix-and-match different types), and it either returns nothing, or it returns a single value (of type i32, i64, f32, or f64).

    Note: these are not first-class or higher-order functions, they can only be defined at the top-level of a wasm module.

  • Memory, which is just a raw vector of bytes. You can get and set i32, i64, f32, or f64 values into various indexes in the memory, and you can mix-and-match different types in the same memory.

    Currently there can only be a single Memory per wasm module, which is a quite heavy restriction. But that restriction may be relaxed in the future.

  • Tables, which is a vector of function pointers (you can mix-and-match different function types in the same table).

    Currently there can only be a single Table per wasm module, which is a quite heavy restriction. But that restriction may be relaxed in the future.

Beyond that, nothing else is currently standardized. In other words, no standard APIs, no API conventions, no type conversions, nothing at all.

There is work being done on creating standardized WebAssembly bindings for JavaScript and the browser APIs, but it will be quite some time before it's standardized. And it will be even more time before Node.js APIs are standardized (if they are ever standardized).

So it's up to us (Rust programmers and library authors) to decide on the APIs, and how to squeeze Rust types into the restricted wasm types, etc. There is ongoing work on that, with wasm-bindgen and stdweb, but I think those are outside the scope of the Portability WG.

So something like Cargo features sounds great. That should also handle things like Electron which allow both browser and Node.js APIs (the programmer would just use the Node.js and browser features at the same time).

@jethrogb
Copy link
Collaborator Author

@Ericson2314
Copy link
Collaborator

Alright, standard FFI. Yeah, that should definitely be done with Cargo features. Heck, even post stabilization it's always best to bake fewer things in the compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants