22const WASI_ESUCCESS = 0 ;
33const WASI_EBADF = 8 ;
44const WASI_ENOENT = 44 ;
5- const WASI_ENOTTY = 59 ;
65
76const IOVEC_SIZE = 8 ;
87
98const WASI_WHENCE_CUR = 0 ;
109const WASI_WHENCE_END = 1 ;
1110const WASI_WHENCE_SET = 2 ;
1211
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+ } ) ( ) ;
3234
3335function read_iovec ( buffer , p ) {
3436 const dv = new DataView ( buffer , p , IOVEC_SIZE ) ;
@@ -155,17 +157,14 @@ class DirFD {
155157 constructor ( path ) {
156158 this . path = path ;
157159 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 ) ) ;
160161 }
161162 getFullPath ( path ) {
162- const { join } = require ( 'path' ) ;
163- return join ( this . path , path ) ;
163+ return Utils . join ( this . path , path ) ;
164164 }
165165 getFilestat ( path ) {
166- const { statSync } = require ( 'fs' ) ;
167166 try {
168- const stat = statSync ( this . getFullPath ( path ) ) ;
167+ const stat = Utils . statSync ( this . getFullPath ( path ) ) ;
169168 return { stat : to_fdstat ( stat ) , result : WASI_ESUCCESS } ;
170169 } catch ( _ ) {
171170 return { stat : null , result : WASI_ENOENT } ;
@@ -190,9 +189,8 @@ class DirFD {
190189 } else {
191190 flags = "r+" ;
192191 }
193- const { openSync, readFileSync } = require ( 'fs' ) ;
194192 try {
195- const fd = openSync ( this . getFullPath ( path ) , flags ) ;
193+ const fd = Utils . openSync ( this . getFullPath ( path ) , flags ) ;
196194 return {
197195 entity : new FileFD ( fd ) ,
198196 result : WASI_ESUCCESS ,
@@ -207,8 +205,7 @@ class DirFD {
207205class FileFD {
208206 constructor ( native ) {
209207 this . native = native ;
210- const { fstatSync } = require ( 'fs' ) ;
211- const stat = fstatSync ( this . native ) ;
208+ const stat = Utils . fstatSync ( this . native ) ;
212209 this . stat = to_fdstat ( stat ) ;
213210 this . pos = 0 ;
214211 this . size = stat . size ;
@@ -217,8 +214,7 @@ class FileFD {
217214 let read = 0 ;
218215 for ( let i = 0 ; i < iovs_len ; i ++ ) {
219216 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 (
222218 this . native ,
223219 new Uint8Array ( buffer , buf_p , buf_len ) ,
224220 0 ,
@@ -237,8 +233,7 @@ class FileFD {
237233 let written = 0 ;
238234 for ( let i = 0 ; i < iovs_len ; i ++ ) {
239235 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 (
242237 this . native ,
243238 new Uint8Array ( buffer , buf_p , buf_len ) ,
244239 0 ,
@@ -254,8 +249,7 @@ class FileFD {
254249 return WASI_ESUCCESS ;
255250 }
256251 close ( ) {
257- const { closeSync } = require ( 'fs' ) ;
258- closeSync ( this . native ) ;
252+ Utils . closeSync ( this . native ) ;
259253 return WASI_ESUCCESS ;
260254 }
261255 seek ( buffer , offset_low , offset_high , whence , newoffset_p ) {
@@ -410,10 +404,9 @@ function createWasiHost() {
410404 return result ;
411405 } ,
412406 random_get ( buf_p , buf_len ) {
413- const { randomFillSync } = require ( 'crypto' )
414407 const { buffer } = host . memory ;
415408 const arr = new Uint8Array ( buffer ) ;
416- randomFillSync ( arr , buf_p , buf_len ) ;
409+ Utils . randomFillSync ( arr , buf_p , buf_len ) ;
417410 return WASI_ESUCCESS ;
418411 } ,
419412 fd_fdstat_set_flags ( fd , falgs ) {
0 commit comments