You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Rename section name Setup -> Installation
* Mention reactor exec model in library use
* Add troubleshooting section
* Update src/getting-started/troubleshooting.md
Co-authored-by: Max Desiatov <max@desiatov.com>
Co-authored-by: Max Desiatov <max@desiatov.com>
You need to compile the Swift code with linker option `--export`.
13
13
14
+
To call the exported function as a library multiple times, you need to:
15
+
16
+
1. Compile it as a [*WASI reactor* execution model](https://github.com/WebAssembly/WASI/blob/bac366c8aeb69cacfea6c4c04a503191bf1cede1/legacy/application-abi.md).
17
+
The default execution model is *command*, so you need to pass `-mexec-model=reactor` to linker.
18
+
2. Call `_initialize` function before interacting with the instance.
19
+
20
+
If your code has any top-level code, you need to export `main` function as well, and call it after `_initialize` function.
// Initialize the instance by following WASI reactor ABI
60
+
instance.exports._initialize();
61
+
// (Optional) Run the top-level code
62
+
instance.exports.main();
49
63
// Get the exported function
50
64
constaddFn=instance.exports.add;
51
65
console.log("2 + 3 = "+addFn(2, 3))
@@ -55,4 +69,4 @@ const main = async () => {
55
69
main()
56
70
```
57
71
58
-
If you use SwiftPM package, you can omit linker flag using clang's `__atribute__`. Please see [swiftwasm/JavaScriptKit#91](https://github.com/swiftwasm/JavaScriptKit/pull/91/files) for more detail info
72
+
If you use SwiftPM package, you can omit linker flag using clang's `__atribute__`. Please see [swiftwasm/JavaScriptKit#91](https://github.com/swiftwasm/JavaScriptKit/pull/91/files) for more detail info
These are some common issues you may run into while using SwiftWasm.
4
+
5
+
If you are having trouble that is not listed here, try searching for it in the [SwiftWasm issue tracker](https://github.com/swiftwasm/swift/issues).
6
+
If you are still having trouble, please file an issue or contact us at [the community Discord server](https://discord.gg/ashJW8T8yp).
7
+
8
+
9
+
## `RuntimeError: memory access out of bounds`
10
+
11
+
If you encounter this error, there are 3 possible causes:
12
+
13
+
### 1. You are trying to access invalid memory in your code
14
+
15
+
In this case, you need to make sure which memory operations are invalid in your code by `UnsafePointer` or C code.
16
+
17
+
### 2. You missed program initialization defined in [WASI Application ABI](https://github.com/WebAssembly/WASI/blob/bac366c8aeb69cacfea6c4c04a503191bf1cede1/legacy/application-abi.md).
18
+
19
+
If your application is used as a library, you need to follow [*WASI reactor ABI*](https://github.com/WebAssembly/WASI/blob/bac366c8aeb69cacfea6c4c04a503191bf1cede1/legacy/application-abi.md).
20
+
21
+
Please make sure that you followed it by reviewing the [Exporting function guide](../examples/exporting-function.md)
22
+
23
+
### 3. Stack overflow is occurring.
24
+
25
+
If you are using `--stack-first` linker option (carton uses it by default), you can face `RuntimeError: memory access out of bounds` error due to stack overflow.
26
+
27
+
You have two options to solve this issue:
28
+
1. Avoid recursive calls if possible.
29
+
2. Extend the stack size by linker option `-z stack-size=<size>`. [The default stack size is 64KB](https://github.com/llvm/llvm-project/blob/fabe915705472e2c06ed1aa9a90620462594e82f/llvm/include/llvm/BinaryFormat/Wasm.h#L32)
30
+
```
31
+
swift build --triple wasm32-unknown-wasi -Xlinker -z -Xlinker stack-size=131072
32
+
```
33
+
34
+
2. Identify which function consumes a lof of stack space by some tools like [wasm-stack-consumer](https://github.com/kateinoigakukun/wasm-stack-consumer)
35
+
36
+
37
+
See also: [LLVM Bugzilla – wasm32: Allow placing the stack before global data](https://bugs.llvm.org/show_bug.cgi?id=37181)
0 commit comments