@tybys/wasm-util 0.8.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +193 -193
  2. package/dist/tsdoc-metadata.json +1 -1
  3. package/dist/wasm-util.esm-bundler.js +2650 -2638
  4. package/dist/wasm-util.esm.js +2650 -2638
  5. package/dist/wasm-util.esm.min.js +1 -1
  6. package/dist/wasm-util.js +2650 -2638
  7. package/dist/wasm-util.min.js +1 -1
  8. package/lib/cjs/asyncify.js +157 -157
  9. package/lib/cjs/index.js +12 -12
  10. package/lib/cjs/jspi.js +45 -45
  11. package/lib/cjs/load.js +96 -96
  12. package/lib/cjs/memory.js +34 -34
  13. package/lib/cjs/wasi/error.js +102 -102
  14. package/lib/cjs/wasi/fd.js +267 -267
  15. package/lib/cjs/wasi/fs.js +2 -2
  16. package/lib/cjs/wasi/index.js +193 -193
  17. package/lib/cjs/wasi/path.js +173 -173
  18. package/lib/cjs/wasi/preview1.js +1485 -1478
  19. package/lib/cjs/wasi/rights.js +139 -139
  20. package/lib/cjs/wasi/types.js +219 -219
  21. package/lib/cjs/wasi/util.js +127 -121
  22. package/lib/cjs/webassembly.js +12 -12
  23. package/lib/mjs/asyncify.mjs +153 -153
  24. package/lib/mjs/index.mjs +9 -9
  25. package/lib/mjs/jspi.mjs +39 -39
  26. package/lib/mjs/load.mjs +89 -89
  27. package/lib/mjs/memory.mjs +28 -28
  28. package/lib/mjs/wasi/error.mjs +97 -97
  29. package/lib/mjs/wasi/fd.mjs +256 -256
  30. package/lib/mjs/wasi/fs.mjs +1 -1
  31. package/lib/mjs/wasi/index.mjs +188 -188
  32. package/lib/mjs/wasi/path.mjs +168 -168
  33. package/lib/mjs/wasi/preview1.mjs +1481 -1474
  34. package/lib/mjs/wasi/rights.mjs +134 -134
  35. package/lib/mjs/wasi/types.mjs +216 -216
  36. package/lib/mjs/wasi/util.mjs +113 -108
  37. package/lib/mjs/webassembly.mjs +9 -9
  38. package/package.json +58 -58
  39. package/lib/cjs/memfs.js +0 -2689
  40. package/lib/mjs/memfs.mjs +0 -2688
package/README.md CHANGED
@@ -1,193 +1,193 @@
1
- # @tybys/wasm-util
2
-
3
- WebAssembly related utils for browser environment
4
-
5
- **The output code is ES2019**
6
-
7
- ## Features
8
-
9
- All example code below need to be bundled by ES module bundlers like `webpack` / `rollup`, or specify import map in browser native ES module runtime.
10
-
11
- ### WASI polyfill for browser
12
-
13
- The API is similar to the `require('wasi').WASI` in Node.js.
14
-
15
- You can use `memfs-browser` to provide filesystem capability.
16
-
17
- - Example: [https://github.com/toyobayashi/wasi-wabt](https://github.com/toyobayashi/wasi-wabt)
18
- - Demo: [https://toyobayashi.github.io/wasi-wabt/](https://toyobayashi.github.io/wasi-wabt/)
19
-
20
- ```js
21
- import { load, WASI } from '@tybys/wasm-util'
22
- import { Volumn, createFsFromVolume } from 'memfs-browser'
23
-
24
- const fs = createFsFromVolume(Volume.from({
25
- '/home/wasi': null
26
- }))
27
-
28
- const wasi = new WASI({
29
- args: ['chrome', 'file.wasm'],
30
- env: {
31
- NODE_ENV: 'development',
32
- WASI_SDK_PATH: '/opt/wasi-sdk'
33
- },
34
- preopens: {
35
- '/': '/'
36
- },
37
- fs,
38
-
39
- // redirect stdout / stderr
40
-
41
- // print (text) { console.log(text) },
42
- // printErr (text) { console.error(text) }
43
- })
44
-
45
- const imports = {
46
- wasi_snapshot_preview1: wasi.wasiImport
47
- }
48
-
49
- const { module, instance } = await load('/path/to/file.wasm', imports)
50
- wasi.start(instance)
51
- // wasi.initialize(instance)
52
- ```
53
-
54
- Implemented syscalls: [wasi_snapshot_preview1](#wasi_snapshot_preview1)
55
-
56
- ### `load` / `loadSync`
57
-
58
- `loadSync` has 4KB wasm size limit in browser.
59
-
60
- ```js
61
- // bundler
62
- import { load, loadSync } from '@tybys/wasm-util'
63
-
64
- const imports = { /* ... */ }
65
-
66
- // using path
67
- const { module, instance } = await load('/path/to/file.wasm', imports)
68
- const { module, instance } = loadSync('/path/to/file.wasm', imports)
69
-
70
- // using URL
71
- const { module, instance } = await load(new URL('./file.wasm', import.meta.url), imports)
72
- const { module, instance } = loadSync(new URL('./file.wasm', import.meta.url), imports)
73
-
74
- // using Uint8Array
75
- const buffer = new Uint8Array([
76
- 0x00, 0x61, 0x73, 0x6d,
77
- 0x01, 0x00, 0x00, 0x00
78
- ])
79
- const { module, instance } = await load(buffer, imports)
80
- const { module, instance } = loadSync(buffer, imports)
81
-
82
- // auto asyncify
83
- const {
84
- module,
85
- instance: asyncifiedInstance
86
- } = await load(buffer, imports, { /* asyncify options */})
87
- asyncifiedInstance.exports.fn() // => return Promise
88
- ```
89
-
90
- ### Extend Memory instance
91
-
92
- ```js
93
- import { Memory, extendMemory } from '@tybys/wasm-util'
94
-
95
- const memory = new WebAssembly.Memory({ initial: 256 })
96
- // const memory = instance.exports.memory
97
-
98
- extendMemory(memory)
99
- console.log(memory instanceof Memory)
100
- console.log(memory instanceof WebAssembly.Memory)
101
- // expose memory view getters like Emscripten
102
- const { HEAPU8, HEAPU32, view } = memory
103
- ```
104
-
105
- ### Asyncify wrap
106
-
107
- Build the C code using `clang`, `wasm-ld` and `wasm-opt`
108
-
109
- ```c
110
- void async_sleep(int ms);
111
-
112
- int main() {
113
- async_sleep(200);
114
- return 0;
115
- }
116
- ```
117
-
118
- ```js
119
- import { Asyncify } from '@tybys/wasm-util'
120
-
121
- const asyncify = new Asyncify()
122
-
123
- const imports = {
124
- env: {
125
- async_sleep: asyncify.wrapImportFunction(function (ms) {
126
- return new Promise((resolve) => {
127
- setTimeout(resolve, ms)
128
- })
129
- })
130
- }
131
- }
132
-
133
- // async_sleep(200)
134
- const bytes = await (await fetch('/asyncfied_by_wasm-opt.wasm')).arrayBuffer()
135
- const { instance } = await WebAssembly.instantiate(bytes, imports)
136
- const asyncifiedInstance = asyncify.init(instance.exports.memory, instance, {
137
- wrapExports: ['_start']
138
- })
139
-
140
- const p = asyncifedInstance._start()
141
- console.log(typeof p.then === 'function')
142
- const now = Date.now()
143
- await p
144
- console.log(Date.now() - now >= 200)
145
- ```
146
-
147
- ### wasi_snapshot_preview1
148
-
149
- - [x] args_get
150
- - [x] args_sizes_get
151
- - [x] environ_get
152
- - [x] environ_sizes_get
153
- - [x] clock_res_get
154
- - [x] clock_time_get
155
- - [ ] ~~fd_advise~~
156
- - [x] fd_allocate
157
- - [x] fd_close
158
- - [x] fd_datasync
159
- - [x] fd_fdstat_get
160
- - [ ] ~~fd_fdstat_set_flags~~
161
- - [x] fd_fdstat_set_rights
162
- - [x] fd_filestat_get
163
- - [x] fd_filestat_set_size
164
- - [x] fd_filestat_set_times
165
- - [x] fd_pread
166
- - [x] fd_prestat_get
167
- - [x] fd_prestat_dir_name
168
- - [x] fd_pwrite
169
- - [x] fd_read
170
- - [x] fd_readdir
171
- - [x] fd_renumber
172
- - [x] fd_seek
173
- - [x] fd_sync
174
- - [x] fd_tell
175
- - [x] fd_write
176
- - [x] path_create_directory
177
- - [x] path_filestat_get
178
- - [x] path_filestat_set_times
179
- - [x] path_link
180
- - [x] path_open
181
- - [x] path_readlink
182
- - [x] path_remove_directory
183
- - [x] path_rename
184
- - [x] path_symlink
185
- - [x] path_unlink_file
186
- - [x] poll_oneoff (timer only)
187
- - [x] proc_exit
188
- - [ ] ~~proc_raise~~
189
- - [x] sched_yield
190
- - [x] random_get
191
- - [ ] ~~sock_recv~~
192
- - [ ] ~~sock_send~~
193
- - [ ] ~~sock_shutdown~~
1
+ # @tybys/wasm-util
2
+
3
+ WebAssembly related utils for browser environment
4
+
5
+ **The output code is ES2019**
6
+
7
+ ## Features
8
+
9
+ All example code below need to be bundled by ES module bundlers like `webpack` / `rollup`, or specify import map in browser native ES module runtime.
10
+
11
+ ### WASI polyfill for browser
12
+
13
+ The API is similar to the `require('wasi').WASI` in Node.js.
14
+
15
+ You can use `memfs-browser` to provide filesystem capability.
16
+
17
+ - Example: [https://github.com/toyobayashi/wasi-wabt](https://github.com/toyobayashi/wasi-wabt)
18
+ - Demo: [https://toyobayashi.github.io/wasi-wabt/](https://toyobayashi.github.io/wasi-wabt/)
19
+
20
+ ```js
21
+ import { load, WASI } from '@tybys/wasm-util'
22
+ import { Volume, createFsFromVolume } from 'memfs-browser'
23
+
24
+ const fs = createFsFromVolume(Volume.fromJSON({
25
+ '/home/wasi': null
26
+ }))
27
+
28
+ const wasi = new WASI({
29
+ args: ['chrome', 'file.wasm'],
30
+ env: {
31
+ NODE_ENV: 'development',
32
+ WASI_SDK_PATH: '/opt/wasi-sdk'
33
+ },
34
+ preopens: {
35
+ '/': '/'
36
+ },
37
+ fs,
38
+
39
+ // redirect stdout / stderr
40
+
41
+ // print (text) { console.log(text) },
42
+ // printErr (text) { console.error(text) }
43
+ })
44
+
45
+ const imports = {
46
+ wasi_snapshot_preview1: wasi.wasiImport
47
+ }
48
+
49
+ const { module, instance } = await load('/path/to/file.wasm', imports)
50
+ wasi.start(instance)
51
+ // wasi.initialize(instance)
52
+ ```
53
+
54
+ Implemented syscalls: [wasi_snapshot_preview1](#wasi_snapshot_preview1)
55
+
56
+ ### `load` / `loadSync`
57
+
58
+ `loadSync` has 4KB wasm size limit in browser.
59
+
60
+ ```js
61
+ // bundler
62
+ import { load, loadSync } from '@tybys/wasm-util'
63
+
64
+ const imports = { /* ... */ }
65
+
66
+ // using path
67
+ const { module, instance } = await load('/path/to/file.wasm', imports)
68
+ const { module, instance } = loadSync('/path/to/file.wasm', imports)
69
+
70
+ // using URL
71
+ const { module, instance } = await load(new URL('./file.wasm', import.meta.url), imports)
72
+ const { module, instance } = loadSync(new URL('./file.wasm', import.meta.url), imports)
73
+
74
+ // using Uint8Array
75
+ const buffer = new Uint8Array([
76
+ 0x00, 0x61, 0x73, 0x6d,
77
+ 0x01, 0x00, 0x00, 0x00
78
+ ])
79
+ const { module, instance } = await load(buffer, imports)
80
+ const { module, instance } = loadSync(buffer, imports)
81
+
82
+ // auto asyncify
83
+ const {
84
+ module,
85
+ instance: asyncifiedInstance
86
+ } = await load(buffer, imports, { /* asyncify options */})
87
+ asyncifiedInstance.exports.fn() // => return Promise
88
+ ```
89
+
90
+ ### Extend Memory instance
91
+
92
+ ```js
93
+ import { Memory, extendMemory } from '@tybys/wasm-util'
94
+
95
+ const memory = new WebAssembly.Memory({ initial: 256 })
96
+ // const memory = instance.exports.memory
97
+
98
+ extendMemory(memory)
99
+ console.log(memory instanceof Memory)
100
+ console.log(memory instanceof WebAssembly.Memory)
101
+ // expose memory view getters like Emscripten
102
+ const { HEAPU8, HEAPU32, view } = memory
103
+ ```
104
+
105
+ ### Asyncify wrap
106
+
107
+ Build the C code using `clang`, `wasm-ld` and `wasm-opt`
108
+
109
+ ```c
110
+ void async_sleep(int ms);
111
+
112
+ int main() {
113
+ async_sleep(200);
114
+ return 0;
115
+ }
116
+ ```
117
+
118
+ ```js
119
+ import { Asyncify } from '@tybys/wasm-util'
120
+
121
+ const asyncify = new Asyncify()
122
+
123
+ const imports = {
124
+ env: {
125
+ async_sleep: asyncify.wrapImportFunction(function (ms) {
126
+ return new Promise((resolve) => {
127
+ setTimeout(resolve, ms)
128
+ })
129
+ })
130
+ }
131
+ }
132
+
133
+ // async_sleep(200)
134
+ const bytes = await (await fetch('/asyncfied_by_wasm-opt.wasm')).arrayBuffer()
135
+ const { instance } = await WebAssembly.instantiate(bytes, imports)
136
+ const asyncifiedInstance = asyncify.init(instance.exports.memory, instance, {
137
+ wrapExports: ['_start']
138
+ })
139
+
140
+ const p = asyncifedInstance._start()
141
+ console.log(typeof p.then === 'function')
142
+ const now = Date.now()
143
+ await p
144
+ console.log(Date.now() - now >= 200)
145
+ ```
146
+
147
+ ### wasi_snapshot_preview1
148
+
149
+ - [x] args_get
150
+ - [x] args_sizes_get
151
+ - [x] environ_get
152
+ - [x] environ_sizes_get
153
+ - [x] clock_res_get
154
+ - [x] clock_time_get
155
+ - [ ] ~~fd_advise~~
156
+ - [x] fd_allocate
157
+ - [x] fd_close
158
+ - [x] fd_datasync
159
+ - [x] fd_fdstat_get
160
+ - [ ] ~~fd_fdstat_set_flags~~
161
+ - [x] fd_fdstat_set_rights
162
+ - [x] fd_filestat_get
163
+ - [x] fd_filestat_set_size
164
+ - [x] fd_filestat_set_times
165
+ - [x] fd_pread
166
+ - [x] fd_prestat_get
167
+ - [x] fd_prestat_dir_name
168
+ - [x] fd_pwrite
169
+ - [x] fd_read
170
+ - [x] fd_readdir
171
+ - [x] fd_renumber
172
+ - [x] fd_seek
173
+ - [x] fd_sync
174
+ - [x] fd_tell
175
+ - [x] fd_write
176
+ - [x] path_create_directory
177
+ - [x] path_filestat_get
178
+ - [x] path_filestat_set_times
179
+ - [x] path_link
180
+ - [x] path_open
181
+ - [x] path_readlink
182
+ - [x] path_remove_directory
183
+ - [x] path_rename
184
+ - [x] path_symlink
185
+ - [x] path_unlink_file
186
+ - [x] poll_oneoff (timer only)
187
+ - [x] proc_exit
188
+ - [ ] ~~proc_raise~~
189
+ - [x] sched_yield
190
+ - [x] random_get
191
+ - [ ] ~~sock_recv~~
192
+ - [ ] ~~sock_send~~
193
+ - [ ] ~~sock_shutdown~~
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.33.4"
8
+ "packageVersion": "7.34.4"
9
9
  }
10
10
  ]
11
11
  }