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
**Note:** Make sure to reference the views as shown above as these will automatically be updated when the module's memory grows.
139
135
140
-
### Allocating/obtaining strings to/from memory
136
+
### Working with strings and arrays
137
+
138
+
Strings and arrays cannot yet flow in and out of WebAssembly naturally, hence it is necessary to create them in the module's memory using the `newString` and `newArray` helpers. Afterwards, instead of passing the string or array directly, the resulting reference (pointer) is provided instead:
141
139
142
140
```js
143
-
// Allocating a string, i.e. to be passed to an export expecting one
144
141
var str ="Hello world!";
145
142
var ptr =module.newString(str);
146
143
147
-
// Disposing a string that is no longer needed (requires memory.free to be exported)
148
-
module.memory.free(ptr);
144
+
// ... do something with ptr, i.e. call a WebAssembly export ...
145
+
```
146
+
147
+
Similarly, when a string or array is returned from a WebAssembly function, a reference (pointer) is received on the JS side and the `getString` and `getArray` helpers can be used to obtain their values:
149
148
150
-
// Obtaining a string, i.e. as returned by an export
149
+
```js
151
150
var ptrToString =...;
152
151
var str =module.getString(ptrToString);
152
+
153
+
// ... do something with str ...
153
154
```
154
155
155
156
### Usage with TypeScript definitions produced by the compiler
0 commit comments