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

Optional imports #1281

Open
sbc100 opened this issue May 30, 2019 · 6 comments
Open

Optional imports #1281

sbc100 opened this issue May 30, 2019 · 6 comments

Comments

@sbc100
Copy link
Member

sbc100 commented May 30, 2019

In WASI we are discussing adding optional imports (also known as weak imports in the ELF spec) on top of the existing import system: WebAssembly/WASI#36.

For a more integrated solution I was thinking about a possible spec proposal to add the concept of a weak import. @binji and I discussed how this might work, at least for Function and Global imports:

  1. New import types for weak_global and weak_function (or some bit to indicate the weakness of an import). These would exist in the same index space that strongly imported functions and globals.
  2. A call to an undefined weak function would result in a trap.
  3. A global.get or global.set on an undefined weak global would result in a trap.
  4. New instructions func.check and global.check (taking function and global immediates respectively) could be used to check for the presence of the weak import at runtime.

One downside of this approach is that it is specific to functions and globals and doesn't necessarily to all possible types of imports.

@awtcode
Copy link

awtcode commented May 31, 2019

@sbc100 , does this mean that we can get rid of the JS import thunks like the following?
function _sidey(){ Module["_sidey"].apply(null,arguments) }

@sbc100
Copy link
Member Author

sbc100 commented May 31, 2019

@sbc100 , does this mean that we can get rid of the JS import thunks like the following?
function _sidey(){ Module["_sidey"].apply(null,arguments) }

Its not clear to me exactly what the purpose of that wrapper in particular is.

But in general with dynamic linking in emscripten we are going to continue to need to have JS thunks for at least some functions to work around the problem of circular dependencies. i.e. if two modules mutually depend on each other there is no way to simultaneously initialize them without providing some kind of thunk functions for thier mutual dependencies that then act and thunks into the real WebAssembly functions which become available only after instantiation.

In short, I think the problem of mutual imports is independent of that of weak imports.

@awtcode
Copy link

awtcode commented May 31, 2019

Thanks for your reply @sbc100 . Just to clarify, these wrappers are generated by Emscripten and not by me. They are used as import functions into the main module thru asmLibraryArg.

Perhaps, I don't understand about the use of weak imports in this context. Are weak imports a mechanism that we use to instantiate a Wasm module while the required import function is not defined?

@sbc100
Copy link
Member Author

sbc100 commented May 31, 2019

Yes, a weak import is one that can be undefined, but not just at initialization time, also at runtime. A weak import remains undefifined for the duration of the execution of the program.

Not to be confused with lazy loading or on-demand loading of symbols, which is a separate issue related to dynamic linking.

@guybedford
Copy link

This got me thinking if such a feature might be similarly useful on the JS side for a import { weak x as y } from 'module' which would allow a shared foundation in the ESM integration. Sample spec text at https://github.com/guybedford/proposal-weak-imports.

@erights
Copy link

erights commented Aug 17, 2019

At the WASI meeting on 15 August 2019, we decided to rename "weak imports" to "optional imports".

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

No branches or pull requests

5 participants