2
2
const WASI_ESUCCESS = 0 ;
3
3
const WASI_EBADF = 8 ;
4
4
const WASI_ENOENT = 44 ;
5
- const WASI_ENOTTY = 59 ;
6
5
7
6
const IOVEC_SIZE = 8 ;
8
7
9
8
const WASI_WHENCE_CUR = 0 ;
10
9
const WASI_WHENCE_END = 1 ;
11
10
const WASI_WHENCE_SET = 2 ;
12
11
13
- const input_file = {
14
- data : Buffer . from ( `nav
15
- ul
16
- margin: 0
17
- padding: 0
18
- list-style: none
19
-
20
- li
21
- display: inline-block
22
-
23
- a
24
- display: block
25
- padding: 6px 12px
26
- text-decoration: none
27
-
28
- ` , 'utf-8' ) ,
29
- pos : 0 ,
30
- closed : true ,
31
- } ;
12
+ const Utils = ( ( ) => {
13
+ const { randomFillSync } = require ( 'crypto' ) ;
14
+ const {
15
+ openSync,
16
+ readSync,
17
+ writeSync,
18
+ closeSync,
19
+ statSync,
20
+ fstatSync
21
+ } = require ( 'fs' ) ;
22
+ const { join } = require ( 'path' ) ;
23
+ return {
24
+ randomFillSync,
25
+ openSync,
26
+ readSync,
27
+ writeSync,
28
+ closeSync,
29
+ statSync,
30
+ fstatSync,
31
+ join,
32
+ } ;
33
+ } ) ( ) ;
32
34
33
35
function read_iovec ( buffer , p ) {
34
36
const dv = new DataView ( buffer , p , IOVEC_SIZE ) ;
@@ -155,17 +157,14 @@ class DirFD {
155
157
constructor ( path ) {
156
158
this . path = path ;
157
159
this . dirpath = Buffer . from ( path , 'utf-8' ) ;
158
- const { statSync } = require ( 'fs' ) ;
159
- this . stat = to_fdstat ( statSync ( path ) ) ;
160
+ this . stat = to_fdstat ( Utils . statSync ( path ) ) ;
160
161
}
161
162
getFullPath ( path ) {
162
- const { join } = require ( 'path' ) ;
163
- return join ( this . path , path ) ;
163
+ return Utils . join ( this . path , path ) ;
164
164
}
165
165
getFilestat ( path ) {
166
- const { statSync } = require ( 'fs' ) ;
167
166
try {
168
- const stat = statSync ( this . getFullPath ( path ) ) ;
167
+ const stat = Utils . statSync ( this . getFullPath ( path ) ) ;
169
168
return { stat : to_fdstat ( stat ) , result : WASI_ESUCCESS } ;
170
169
} catch ( _ ) {
171
170
return { stat : null , result : WASI_ENOENT } ;
@@ -190,9 +189,8 @@ class DirFD {
190
189
} else {
191
190
flags = "r+" ;
192
191
}
193
- const { openSync, readFileSync } = require ( 'fs' ) ;
194
192
try {
195
- const fd = openSync ( this . getFullPath ( path ) , flags ) ;
193
+ const fd = Utils . openSync ( this . getFullPath ( path ) , flags ) ;
196
194
return {
197
195
entity : new FileFD ( fd ) ,
198
196
result : WASI_ESUCCESS ,
@@ -207,8 +205,7 @@ class DirFD {
207
205
class FileFD {
208
206
constructor ( native ) {
209
207
this . native = native ;
210
- const { fstatSync } = require ( 'fs' ) ;
211
- const stat = fstatSync ( this . native ) ;
208
+ const stat = Utils . fstatSync ( this . native ) ;
212
209
this . stat = to_fdstat ( stat ) ;
213
210
this . pos = 0 ;
214
211
this . size = stat . size ;
@@ -217,8 +214,7 @@ class FileFD {
217
214
let read = 0 ;
218
215
for ( let i = 0 ; i < iovs_len ; i ++ ) {
219
216
const { buf_p, buf_len } = read_iovec ( buffer , iovs_p + i * IOVEC_SIZE ) ;
220
- const { readSync } = require ( 'fs' ) ;
221
- let chunk_read = readSync (
217
+ let chunk_read = Utils . readSync (
222
218
this . native ,
223
219
new Uint8Array ( buffer , buf_p , buf_len ) ,
224
220
0 ,
@@ -237,8 +233,7 @@ class FileFD {
237
233
let written = 0 ;
238
234
for ( let i = 0 ; i < iovs_len ; i ++ ) {
239
235
const { buf_p, buf_len } = read_iovec ( buffer , iovs_p + i * IOVEC_SIZE ) ;
240
- const { writeSync } = require ( 'fs' ) ;
241
- let chunk_written = writeSync (
236
+ let chunk_written = Utils . writeSync (
242
237
this . native ,
243
238
new Uint8Array ( buffer , buf_p , buf_len ) ,
244
239
0 ,
@@ -254,8 +249,7 @@ class FileFD {
254
249
return WASI_ESUCCESS ;
255
250
}
256
251
close ( ) {
257
- const { closeSync } = require ( 'fs' ) ;
258
- closeSync ( this . native ) ;
252
+ Utils . closeSync ( this . native ) ;
259
253
return WASI_ESUCCESS ;
260
254
}
261
255
seek ( buffer , offset_low , offset_high , whence , newoffset_p ) {
@@ -410,10 +404,9 @@ function createWasiHost() {
410
404
return result ;
411
405
} ,
412
406
random_get ( buf_p , buf_len ) {
413
- const { randomFillSync } = require ( 'crypto' )
414
407
const { buffer } = host . memory ;
415
408
const arr = new Uint8Array ( buffer ) ;
416
- randomFillSync ( arr , buf_p , buf_len ) ;
409
+ Utils . randomFillSync ( arr , buf_p , buf_len ) ;
417
410
return WASI_ESUCCESS ;
418
411
} ,
419
412
fd_fdstat_set_flags ( fd , falgs ) {
0 commit comments