Skip to content

Commit 2ecec66

Browse files
committed
Mention exporting memory utilities more prominently in loader readme, fixes #318
1 parent 7cfc43c commit 2ecec66

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lib/loader/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,16 @@ Instances are automatically populated with useful utility:
6161
A 64-bit float view on the memory.
6262

6363
* **newString**(str: `string`): `number`<br />
64-
Allocates a new string in the module's memory and returns its pointer. Requires `memory.allocate` to be exported from your module's entry file, i.e.:
65-
66-
```js
67-
import "allocator/tlsf";
68-
export { memory };
69-
```
64+
Allocates a new string in the module's memory and returns its pointer.<sup>2</sup>
7065

7166
* **getString**(ptr: `number`): `string`<br />
7267
Gets a string from the module's memory by its pointer.
7368

7469
* **newArray**(view: `TypedArray`, length?: `number`, unsafe?: `boolean`): `number`<br />
75-
Copies a typed array into the module's memory and returns its pointer.
70+
Copies a typed array into the module's memory and returns its pointer.<sup>2</sup>
7671

7772
* **newArray**(ctor: `TypedArrayConstructor`, length: `number`, unsafe?: `boolean`): `number`<br />
78-
Creates a typed array in the module's memory and returns its pointer.
73+
Creates a typed array in the module's memory and returns its pointer.<sup>2</sup>
7974

8075
* **getArray**(ctor: `TypedArrayConstructor`, ptr: `number`): `TypedArray`<br />
8176
Gets a view on a typed array in the module's memory by its pointer.
@@ -90,7 +85,8 @@ Instances are automatically populated with useful utility:
9085
Creates a new function in the module's table and returns its pointer. Note that only actual
9186
WebAssembly functions, i.e. as exported by the module, are supported.
9287

93-
<sup>1</sup> This feature has not yet landed in any VM as of this writing.
88+
<sup>1</sup> This feature has not yet landed in any VM as of this writing.<br />
89+
<sup>2</sup> Requires that memory utilities have been exported through `export { memory }` within the entry file.
9490

9591
Examples
9692
--------
@@ -137,19 +133,24 @@ myModule.F64[ptrToFloat64 >>> 3] = newValue;
137133

138134
**Note:** Make sure to reference the views as shown above as these will automatically be updated when the module's memory grows.
139135

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:
141139

142140
```js
143-
// Allocating a string, i.e. to be passed to an export expecting one
144141
var str = "Hello world!";
145142
var ptr = module.newString(str);
146143

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:
149148

150-
// Obtaining a string, i.e. as returned by an export
149+
```js
151150
var ptrToString = ...;
152151
var str = module.getString(ptrToString);
152+
153+
// ... do something with str ...
153154
```
154155

155156
### Usage with TypeScript definitions produced by the compiler

0 commit comments

Comments
 (0)