Skip to content

Add external function pointers - #19

Merged
jfbastien merged 1 commit into
masterfrom
add-fps
May 1, 2015
Merged

jfbastien merged 1 commit into
masterfrom
add-fps

Conversation

@kripken

@kripken kripken commented May 1, 2015

Copy link
Copy Markdown
Member

It is useful to add function pointers at runtime, that arrive from outside WebAssembly. This is related to dynamic linking, but distinct. For example, qsort might be compiled into WebAssembly, and outside JavaScript might pass in a function at runtime to sort by.

This is very useful for the C++ <-> JS interop story, specifically, when extending a C++ class using JavaScript (the same could be true of C++ <-> other language interop, if not on the web). Specifically, for extension done at runtime, i.e., we don't whether or how many times the class will be extended by JavaScript. Emscripten currently supports this in 3 ways: By "reserving" function pointer space for JS functions to be added; by adding an extra dynamic lookup table; and by "emulating" function tables (i.e., having them outside of the asm module, so that new JS methods can be added directly). Supporting a direct way to add function pointers to outside JS methods would be far superior to all of those.

This is also related to other future features like accessing GC things. Note that a JS function would be just a GC thing, in a sense; the proposal here is to be able to add such GC things as function pointers.

Thoughts?

@jfbastien

Copy link
Copy Markdown
Member

This is effectively saying that we should spec an FFI interface, right?

@kripken

kripken commented May 1, 2015

Copy link
Copy Markdown
Member Author

I don't think this adds that - we can already import methods from outside, as an ES6 module that has some methods. And we can call them (like asm.js ffi calls). So the call has to contain all the type info for the call (what type is returned, etc.).

The proposal here just says that we can add such external methods to function tables dynamically, at runtime.

@jfbastien

Copy link
Copy Markdown
Member

My thinking was that wasm code could say "there's this external thing that I'll get later. Let me use it." In the browser it could be JS that provides the impl, and in a shell it could be C++ code or something. I think both should work.

That's pretty much dynamic linking / modules?

@lukewagner

Copy link
Copy Markdown
Member

Actually, there's a pretty interesting underlying asymmetry that I think Alon's point hits on:

  • for wasm (if we assume dynamic linking) we can either:
    1. declare all the imported functions a priori and they are imported at load time and have pretty fast calls
    2. pull in new wasm code dynamically through dynamic loading (dlopen) which gives us function pointers
  • for JS, we can only do the first of these

But our ES6-Module-y version of dlopen will just take a URL (or byte stream for dynamic compilation), so that means we could pass a JS module which would logically return function pointers (what else?) that, in the case of a JS module, thunked into JS. So for the JS-extending-C++-class use case, you'd put the JS class in an ES6 module and dlopen() it. Would that work?

@kripken

kripken commented May 1, 2015

Copy link
Copy Markdown
Member Author

Yes, if the goal with dlopen was to support non-wasm modules as well - I can clarify that in the spec, if so? - then that would cover most of what the added line in this pull request suggests.

There is still another aspect, that a direct API to add a function pointer to a new external method provides, which is that you don't need to create and dlopen an entire ES6 module to get a function pointer to a new method. Might be an overhead issue there. But if that is the only issue, I think it is enough to change this pull to mention that dlopen can load non-wasm modules too. Sound ok?

@jfbastien

Copy link
Copy Markdown
Member

dlopen being able to load non-wasm modules is what I had in mind, so clarifying it sgtm.

@lukewagner

Copy link
Copy Markdown
Member

sgtm too

@kripken

kripken commented May 1, 2015

Copy link
Copy Markdown
Member Author

Updated.

@jfbastien

Copy link
Copy Markdown
Member

I'd make it less JS-centric, since we want this to work outside. They key is that dlopen can be fulfilled by wasm, JS, or whatever else is in the current env.

@kripken

kripken commented May 1, 2015

Copy link
Copy Markdown
Member Author

How, though? I have to mention ES6 modules here, since that's the underlying model I think we have in mind? I'm not sure how to avoid mentioning JS. What kind of change do you suggest?

@jfbastien

Copy link
Copy Markdown
Member

run-time dynamic linking can be fulfilled by WebAssembly or JavaScript when in-browser, and by other means outside the browser such as loading C++ compiled to a .so file on Linux.

@kripken

kripken commented May 1, 2015

Copy link
Copy Markdown
Member Author

Updated.

jfbastien added a commit that referenced this pull request May 1, 2015
Add external function pointers
@jfbastien
jfbastien merged commit 739e307 into master May 1, 2015
@jfbastien
jfbastien deleted the add-fps branch May 1, 2015 20:15
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

Successfully merging this pull request may close these issues.

3 participants