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
var x =newFloat64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0 ] );
176
-
177
-
// Allocate space on the heap:
178
-
var bytes =wasm.malloc( x.length*x.BYTES_PER_ELEMENT );
179
-
180
-
// Copy data to the heap:
181
-
bytes.set( newUint8Array( x.buffer ) );
182
-
183
-
// Compute the sum:
184
-
var s =wasm( x.length, bytes, 1 );
185
-
// returns 15.0
186
-
187
-
// Free the memory:
188
-
wasm.free( bytes );
189
-
```
190
-
191
-
The method accepts the following `options`:
192
-
193
-
-**memory**: total memory. If not provided a `buffer`, setting the `memory` option instructs the returned function to allocate an internal memory store of the specified size.
194
-
-**stack**: total stack size. Must be less than the `memory` option and large enough for a program's needs. Default: `1024` bytes.
195
-
-**buffer**: `ArrayBuffer` serving as the underlying memory store. If not provided, each returned function will allocate and manage its own memory. If provided a `memory` option, the buffer `byteLength` must equal the specified total memory.
196
-
197
-
To create a function using an externally defined memory buffer, set the `buffer` option.
198
-
199
-
<!-- FIXME: glue code seems to require more than just setting a `buffer` property: https://github.com/kripken/emscripten/blob/655cec1975e784985d25bc80c5438d715ac5ec13/src/preamble.js#L2155 and https://github.com/kripken/emscripten/blob/655cec1975e784985d25bc80c5438d715ac5ec13/src/preamble.js#L1141 -->
200
-
201
-
<!-- run-disable -->
202
-
203
-
```javascript
204
-
varArrayBuffer=require( '@stdlib/array/buffer' );
205
-
var buffer =newArrayBuffer( 16777216 ); // ~16MB
206
-
207
-
var wasm =dasum.wasm({
208
-
'buffer': buffer
209
-
});
210
-
```
211
-
212
-
Providing external memory can be advantageous when wanting to a) centrally manage memory allocation, b) share memory between multiple memory managed functions, and/or c) limit the total amount of allocated memory within an application or library.
213
-
214
-
##### wasm.malloc( nbytes )
215
-
216
-
Allocates space on the heap and returns a bytes-wise [`typed array`][mdn-typed-array] view ([`Uint8Array`][mdn-uint8array]).
217
-
218
-
```javascript
219
-
var wasm =dasum.wasm();
220
-
221
-
// Allocate 64 bytes:
222
-
var bytes =wasm.malloc( 64 );
223
-
```
224
-
225
-
##### bytes.getValue( ptr\[, type] )
226
-
227
-
Returns a value at a specific memory address (represented by a byte index). By default, the function returns a `double`. Possible types include: `'i8'`, `'i16'`, `'i32'`, `'i64'`, `'float'`, and `'double'`.
var view =newFloat64Array( bytes.buffer, bytes.byteOffset, N );
238
-
239
-
view.set( [ 1.0, -2.0, 3.0 ] );
240
-
241
-
var ptr =1*8; // 8 bytes per double
242
-
var y =bytes.getValue( ptr );
243
-
// returns -2.0
244
-
245
-
wasm.free( bytes );
246
-
```
247
-
248
-
While this method may be convenient when interacting with the bytes view directly, using a [`typed array`][mdn-typed-array] view is likely to be more performant.
var view =newFloat64Array( bytes.buffer, bytes.byteOffset, N );
259
-
260
-
view.set( [ 1.0, -2.0, 3.0 ] );
261
-
262
-
var y = view[ 1 ];
263
-
// returns -2.0
264
-
265
-
wasm.free( bytes );
266
-
```
267
-
268
-
##### bytes.setValue( ptr, value\[, type] )
269
-
270
-
Sets a value at a specific memory address (represented by a byte index). By default, the function sets a `double`. Possible types include: `'i8'`, `'i16'`, `'i32'`, `'i64'`, `'float'`, and `'double'`.
var view =newFloat64Array( bytes.buffer, bytes.byteOffset, N );
281
-
282
-
view.set( [ 1.0, -2.0, 3.0 ] );
283
-
284
-
var ptr =1*8; // 8 bytes per double
285
-
var y =bytes.getValue( ptr );
286
-
// returns -2.0
287
-
288
-
bytes.setValue( ptr, -10.0 );
289
-
290
-
y =bytes.getValue( ptr );
291
-
// returns -10.0
292
-
293
-
wasm.free( bytes );
294
-
```
295
-
296
-
While this method may be convenient when interacting with the bytes view directly, using a [`typed array`][mdn-typed-array] view is likely to be more performant.
0 commit comments