From cdaae53dc098de760b2784e45a99aae07e424e37 Mon Sep 17 00:00:00 2001 From: Luke Wagner Date: Thu, 16 Jul 2015 15:42:16 -1000 Subject: Consolidate explanation of modules into a new Modules.md and improve explanation --- Modules.md | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 Modules.md (limited to 'Modules.md') diff --git a/Modules.md b/Modules.md new file mode 100644 index 0000000..6ccc29a --- /dev/null +++ b/Modules.md @@ -0,0 +1,112 @@ +# Modules + +The distributable, loadable, and executable unit of code in WebAssembly +is called a **module**. A module contains: +* a set of [imports and exports](Modules.md#imports-and-exports); +* a section defining the [initial state of linear memory](Modules.md#initial-state-of-linear-memory); +* a section containing [code](Modules.md#code-section); +* after the MVP, sections containing [debugging/symbol information](Tooling.md); and +* possibly other sections in the future. + +While WebAssembly modules are designed to interoperate with ES6 modules +in a Web environment (more details [below](Modules.md#integration-with-es6-modules)), +WebAssembly modules are defined independently of JavaScript and do not require +the host environment to include a JavaScript VM. + +## Imports and Exports + +A module defines a set of functions in its +[code section](Modules.md#code-section) and can declare and name a subset of +these functions to be **exports**. The meaning of exports (how and when they are +called) is defined by the host environment. For example, a minimal shell +environment might only probe for and call a `_start` export when given a module +to execute. + +A module can declare a set of **imports**. An import is a tuple containing a +module name, the name of an exported function to import from the named module, +and the signature to use for that import within the importing module. Within a +module, the import can be [directly called](AstSemantics.md#calls) like a +function (according to its locally-declared signature). + +The WebAssembly spec does not define how imports are interpreted: +* the host environment can interpret the module name as a file path, a URL, + a key in a fixed set of builtin modules or the host environment may invoke a + user-defined hook to resolve the module name to one of these; +* the module name does not need to resolve to a WebAssembly module; it + could resolve to a builtin module (implemented by the host environment) or a + module written in another, compatible language; and +* the meaning of calling an imported function is host-defined. + +The open-ended nature of module imports allow them to be used to expose +arbitrary host environment functionality to WebAssembly code, similar to a +native `syscall`. For example, a shell environment could define a builtin +`stdio` module with an export `puts`. + +In C/C++, an undefined `extern` declaration could be compiled to an import and +C/C++ calls to this `extern` would then be compiled to calls to this import. This +is one way low-level C/C++ libraries could call out of WebAssembly in order to +implement portable source-level interfaces (e.g., POSIX, OpenGL or SDL) in +terms of host-specific functionality. + +### Integration with ES6 modules + +While ES6 defines how to parse, link and execute a module, ES6 does not +define when this parsing/linking/execution occurs. An additional extension +to the HTML spec is required to say when a script is parsed as a module instead +of normal global code. This work is [ongoing](TODO). Currently, the following +entry points for modules are being considered: +* `