Skip to content
New issue

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

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

Already on GitHub? # to your account

Import extern wast #194

Closed
lrettig opened this issue Jul 31, 2018 · 9 comments
Closed

Import extern wast #194

lrettig opened this issue Jul 31, 2018 · 9 comments

Comments

@lrettig
Copy link

lrettig commented Jul 31, 2018

Hello,

I'd like to be able to link against a function defined in another module -- either the wast or a precompiled wasm binary. Is this possible today? (If it's not possible at compile time I suppose I'd have to wait for dynamic linking to become possible per #84.)

Thanks

@dcodeIO
Copy link
Member

dcodeIO commented Jul 31, 2018

At this point, what you could do is declare the functionality of the other module in your AssemblyScript sources (like you'd to in TS with a .d.ts file), and then use Binaryen's wasm-merge or similar for static linking afterwards. wasm-merge basically resolves (missing) imports vs. exports, so that should theoretically work. If the other module is written in C, that could look like:

declare namespace env {
  export function some_function_in_c(); void;
}

Of course you could also use two WASM modules, with one of them being an import of the other one, using the same technique, but without linking them statically to a single binary.

@lrettig
Copy link
Author

lrettig commented Aug 1, 2018

Of course you could also use two WASM modules, with one of them being an import of the other one, using the same technique, but without linking them statically to a single binary.

Could you expand upon this a little bit? In particular the "without linking them statically to a single binary" part, since it seems like that's what you're recommending with wasm-merge. Thanks!

@dcodeIO
Copy link
Member

dcodeIO commented Aug 1, 2018

What I meant was to use the exports of one of the modules as the imports of the other module. For example, if you have two modules foo and bar:

var foo = new WebAssembly.Instance(fooModule, {});
var bar = new WebAssembly.Instance(barModule, { foo: foo.exports });

While declaring the structure of foo within bar:

declare namespace foo {
  ...
}

@lrettig
Copy link
Author

lrettig commented Aug 1, 2018

Got it, that makes sense, but I think that means it needs to be run in the browser/in a JS environment which we're not doing with Ewasm. Thanks! It looks like my only options here are some form of static linking using wasm-merge, rewriting the darn import in AS itself, or adding support for it in the runtime environment.

@lrettig
Copy link
Author

lrettig commented Aug 1, 2018

How hard would it be to add support for some form of "inline Wast" within AS?

@dcodeIO
Copy link
Member

dcodeIO commented Aug 4, 2018

There is some form already, but it doesn't cover everything yet and might need other improvements. In general, if Binaryen had an API to compile only an expression and integrate it into the IR, that'd also allow us to do something like:

function myFunc(): i32 {
  return <i32>`(i32.const 1)`!;
}

That's of course just a very basic example to get the idea, here misusing the ! non-null assertion postfix operator.

@dcodeIO
Copy link
Member

dcodeIO commented Aug 4, 2018

Another option would be to support something like a keccak256.wat alongside a keccak256.d.ts to be imported as if it was a normal source file, but that might be more complicated than it looks because there isn't really any .d.ts support currently. Would be great to have, though.

@lrettig lrettig changed the title Import extern Import extern wast Aug 4, 2018
@dcodeIO dcodeIO removed the question label Feb 7, 2019
@cdetrio
Copy link

cdetrio commented Jul 11, 2019

wasm-merge was deleted from binaryen recently, fyi WebAssembly/binaryen#2174

@dcodeIO
Copy link
Member

dcodeIO commented May 27, 2020

Closing this issue as part of 2020 vacuum because it has been lingering for quite a while and it seems unlikely that it will be picked up any time soon due to missing support on the Binaryen side. If this is important to someone who is interested in making this work, I suggest adding the relevant mechanism to Binaryen first, and once that's there open a new issue here.

@dcodeIO dcodeIO closed this as completed May 27, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants