|
| 1 | +# Frequently Asked Questions (FAQ) |
| 2 | + |
| 3 | +This page hosts a series of questions that are frequently asked or that might be confusing about |
| 4 | +WebAssembly (core), components, and the WebAssembly ecosystem as a whole. |
| 5 | + |
| 6 | +## Q: What is the difference between a WebAssembly component and a WebAssembly module? |
| 7 | + |
| 8 | +A WebAssembly module (more precisely referred to as a "WebAssembly core module") is a |
| 9 | +binary that conforms to the [WebAssembly Core Specification][wasm-core-spec]. |
| 10 | + |
| 11 | +A WebAssembly component refers to a WebAssembly core module that has been wrapped in |
| 12 | +the [Canonical ABI][cabi], i.e. the binary contract for the Component Model. By adhering to the |
| 13 | +canonical ABI, WebAssembly components can take advantage of all the features the Component Model |
| 14 | +has to offer. |
| 15 | + |
| 16 | +WebAssembly Components can (and often do) contain core modules, but generally WebAssembly core modules |
| 17 | +*cannot not* contain Components. One easy way to differentiate is by reading the WAT for a component: |
| 18 | + |
| 19 | +A WebAssembly core module generally starts with a `(module)` s-expression: |
| 20 | +```wat |
| 21 | +(module |
| 22 | + ... |
| 23 | +) |
| 24 | +``` |
| 25 | + |
| 26 | +A WebAssembly component starts with the `(component)` s-expression: |
| 27 | + |
| 28 | +```wat |
| 29 | +(component |
| 30 | + ... |
| 31 | +) |
| 32 | +``` |
| 33 | + |
| 34 | +One part that might cause confusion here is that a WebAssembly Preview 1 "component" is in fact a |
| 35 | +*core module*. More precisely, a Preview 1 "component" is a WebAssembly core module with a well-defined |
| 36 | +set of exports ([specified in WITX][wasi-p1]). |
| 37 | + |
| 38 | +[cabi]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md |
| 39 | +[wasi-p1]: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/witx/wasi_snapshot_preview1.witx |
| 40 | +[wasm-core-spec]: https://webassembly.github.io/spec/core/ |
| 41 | + |
| 42 | +## Q: How do WebAssembly Components and the WebAssembly System Interface (WASI) relate to each other? |
| 43 | + |
| 44 | +WebAssembly core components do not have any access to the host system (by design) -- they can only perform |
| 45 | +computations on a fixed set of types. The component model enables core components to interact with the "outside" |
| 46 | +world via a rich set of types ([WebAssembly Interface Types][wit]). |
| 47 | + |
| 48 | +The WebAssembly System Interface (WASI) is a *standardization* of the interfaces, functions and types that |
| 49 | +a system or platform can expose to a WebAssembly component. At a glance, many parts of WASI are UNIX-like, |
| 50 | +in that they match traditional expectations for programs like STDIN, STDOUT, and writing to files. |
| 51 | + |
| 52 | +Some WASI system interfaces work at a much higher level than the command line however, like |
| 53 | +[`wasi:http`][wasi-http]. `wasi:http` is included as a standardized platform due to the ubiquity |
| 54 | +of the internet and the common use case of WebAssembly components with "the web" as a platform. |
| 55 | + |
| 56 | +With WIT, platform builders can define *any* interface to be *your* system interfaces that WebAssembly components can |
| 57 | +expect to access -- WASI is a standardized set which enables to build on a shared base set of abstractions. |
| 58 | + |
| 59 | +[wit]: https://component-model.bytecodealliance.org/design/wit.html |
| 60 | +[wasi-http]: https://github.com/WebAssembly/wasi-http |
| 61 | + |
| 62 | +## Q: I see the terms Preview 1 and Preview 2 frequently. What do those refer to? |
| 63 | + |
| 64 | +Preview 1 refers to the first iteration of the Component Model which was based on WITX and is now deprecated: |
| 65 | + |
| 66 | +https://github.com/WebAssembly/WASI/tree/main/legacy |
| 67 | + |
| 68 | +Preview 2 refers to a newer iteration of the Component Model which uses WebAssembly Interface Types (WIT): |
| 69 | + |
| 70 | +https://github.com/WebAssembly/WASI/tree/main/wasip2 |
| 71 | + |
| 72 | +Many programming language toolchains may only support Preview 1 components natively, but this isn't a problem |
| 73 | +in practice as Preview 1 components can be *adapted* into Preview 2 components automatically. |
| 74 | + |
| 75 | +## Q: What are component imports? |
| 76 | + |
| 77 | +WebAssembly components represent a new kind of binary that can *describe* its interface or expected usage natively. |
| 78 | +This means that WebAssembly components have functionality that they might *import* (e.g. `wasi:cli/environment`). |
| 79 | + |
| 80 | +Imports are easiest illustrated with WIT: |
| 81 | + |
| 82 | +```wit |
| 83 | +package example-namespace:example-package; |
| 84 | +
|
| 85 | +world example-world { |
| 86 | + import wasi:cli/environment@0.2.4; |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +The [`environment` interface in `wasi:cli`][wasi-cli-env] provides various types and functions for interacting with |
| 91 | +environment variables. |
| 92 | + |
| 93 | +The component is said to "import" the `wasi:cli/environment` interface, using the available functions and types therein. |
| 94 | + |
| 95 | +[wasi-cli-env]: https://github.com/WebAssembly/wasi-cli/blob/main/wit/environment.wit |
| 96 | + |
| 97 | +## Q: What are component exports? |
| 98 | + |
| 99 | +WebAssembly components represent a new kind of binary that can *describe* it's expected usage natively. This means that |
| 100 | +WebAssembly components have functionality that they *export*, which users of the component (e.g. another component, or |
| 101 | +a WebAssembly host) can use. |
| 102 | + |
| 103 | +Exports are easiest illustrated with WIT: |
| 104 | + |
| 105 | +```wit |
| 106 | +package example-namespace:example-package; |
| 107 | +
|
| 108 | +interface example-interface { |
| 109 | + say-hello: func(name: string) -> string; |
| 110 | +} |
| 111 | +
|
| 112 | +world example-world { |
| 113 | + export example-interface; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +For the component that inhabits the `example-world` defined above, the outside world can expect the WebAssembly binary to |
| 118 | +have a `say-hello` function that is callable via the `example-namespace:example-package/example-interface` interface. |
| 119 | + |
| 120 | +The component is said to "export" the `example-interface` interface, making available the functions and types therein. |
| 121 | + |
| 122 | +## Still have questions? |
| 123 | + |
| 124 | +Please contribute to the Component Book by filing your question (or one that you think should be covered here) as |
| 125 | +[an issue on GitHub][gh-issues]. |
| 126 | + |
| 127 | +[gh-issues-new]: https://github.com/bytecodealliance/component-docs/issues/new |
0 commit comments