README.md (3273B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Read WebAssembly 22 23 > Read a file as [WebAssembly][webassembly]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var readWASM = require( '@stdlib/fs/read-wasm' ); 31 ``` 32 33 <a name="read-wasm"></a> 34 35 #### readWASM( file\[, options], clbk ) 36 37 Asynchronously reads a file as [WebAssembly][webassembly]. 38 39 ```javascript 40 var join = require( 'path' ).join; 41 42 var fpath = join( __dirname, 'examples', 'fixtures', 'file.wasm' ); 43 readWASM( fpath, onRead ); 44 45 function onRead( error, buf ) { 46 if ( error ) { 47 throw error; 48 } 49 console.log( buf ); 50 } 51 ``` 52 53 The function accepts the following `options`: 54 55 - **flag**: file status flag. 56 57 #### readWASM.sync( file\[, options] ) 58 59 Synchronously reads a file as [WebAssembly][webassembly]. 60 61 ```javascript 62 var join = require( 'path' ).join; 63 var instanceOf = require( '@stdlib/assert/instance-of' ); 64 65 var fpath = join( __dirname, 'examples', 'fixtures', 'file.wasm' ); 66 var out = readWASM.sync( fpath ); 67 if ( instanceOf( out, Error ) ) { 68 throw out; 69 } 70 console.log( out ); 71 ``` 72 73 The function accepts the same `options` as [`readWASM()`](#read-wasm) above. 74 75 </section> 76 77 <!-- /.usage --> 78 79 <section class="notes"> 80 81 ## Notes 82 83 - In contrast to [`readFile()`][@stdlib/fs/read-file], neither function accepts an `encoding` option. As [WebAssembly][webassembly] is a binary file format, if provided an `encoding` option, the function **overrides** the option, setting the option value to `null`. 84 - Both functions return [WebAssembly][webassembly] file content as a [`Uint8Array`][@stdlib/array/uint8]. 85 86 </section> 87 88 <!-- /.notes --> 89 90 <section class="examples"> 91 92 ## Examples 93 94 <!-- eslint-disable no-undef --> 95 96 <!-- eslint no-undef: "error" --> 97 98 ```javascript 99 var join = require( 'path' ).join; 100 var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' ); 101 var readWASM = require( '@stdlib/fs/read-wasm' ); 102 103 var fpath = join( __dirname, 'examples', 'fixtures', 'file.wasm' ); 104 readWASM( fpath, onRead ); 105 106 function onRead( error, wasm ) { 107 var bool; 108 if ( error ) { 109 throw error; 110 } 111 bool = hasWebAssemblySupport(); 112 113 // If WebAssembly is supported, create a WebAssembly module instance... 114 if ( bool ) { 115 wasm = new WebAssembly.Module( wasm ); 116 wasm = new WebAssembly.Instance( wasm, {} ); 117 console.log( wasm.exports.stdlib_hypot( 5.0, 12.0 ) ); 118 } else { 119 console.log( wasm ); 120 } 121 } 122 ``` 123 124 </section> 125 126 <!-- /.examples --> 127 128 <section class="links"> 129 130 [webassembly]: https://webassembly.org/ 131 132 [@stdlib/fs/read-file]: https://www.npmjs.com/package/@stdlib/fs/tree/main/read-file 133 134 [@stdlib/array/uint8]: https://www.npmjs.com/package/@stdlib/array-uint8 135 136 </section> 137 138 <!-- /.links -->