@@ -4,7 +4,8 @@ const { instantiateSync } = require("assemblyscript/lib/loader");
44const { main } = require ( "assemblyscript/cli/asc" ) ;
55const { parse } = require ( "assemblyscript/cli/util/options" ) ;
66const path = require ( "path" ) ;
7- const fs = require ( "fs" ) . promises ;
7+ const fssync = require ( "fs" ) ;
8+ const fs = fssync . promises ;
89const { WASI } = require ( "wasi" ) ;
910
1011const promises = [ ] ;
@@ -21,13 +22,29 @@ const options = parse(process.argv.slice(2), {
2122 "type" : "b" ,
2223 "alias" : "u"
2324 } ,
25+ "preopens" : {
26+ "description" : "A set of preopened directories for wasi. Ex. ./dir=./dir" ,
27+ "type" : "S" ,
28+ "alias" : "p" ,
29+ "default" : [ ] ,
30+ } ,
2431} ) ;
2532
2633if ( options . unknown . length > 1 ) {
2734 console . error ( "Unknown options arguments: " + options . unknown . join ( " " ) ) ;
2835 process . exit ( 1 ) ;
2936}
3037
38+ // calculate default preopens from cli
39+ const preopens = options . options . preopens . reduce (
40+ ( obj , value ) => {
41+ const [ _ , dir1 , dir2 ] = / ( [ ^ = ] ) * = ( .* ) / . exec ( value ) ;
42+ obj [ dir1 ] = dir2 ;
43+ return obj ;
44+ } ,
45+ { }
46+ ) ;
47+
3148const reporter = new VerboseReporter ( ) ;
3249reporter . stderr = process . stderr ;
3350reporter . stdout = process . stdout ;
@@ -40,7 +57,6 @@ const ascOptions = [
4057 relativeFromCwd ( require . resolve ( "@as-pect/assembly/assembly/index.ts" ) ) ,
4158 "--use" , "ASC_RTRACE=1" ,
4259 "--explicitStart" ,
43- "--validate" ,
4460 "--measure" ,
4561 "--lib" , "assembly" ,
4662 "--transform" , require . resolve ( "@as-pect/core/lib/transform/index.js" ) ,
@@ -117,29 +133,36 @@ function runTest(fileName, type, binary, wat) {
117133 const basename = path . basename ( fileName , ".ts" ) ;
118134 const fullName = path . join ( dirname , basename )
119135 const watPath = `${ fullName } .${ type } .wat` ;
136+ const wasiPath = `${ fullName } .wasi.js` ;
120137 const fileNamePath = `${ fullName } .${ type } .ts` ;
121138
139+ // use either a custom wasi configuration, or the default one
140+ const wasiOptions = fssync . existsSync ( wasiPath )
141+ ? require ( path . resolve ( wasiPath ) )
142+ : {
143+ args : process . argv ,
144+ env : process . env ,
145+ preopens,
146+ } ;
147+
122148 // should not block testing
123149 promises . push ( fs . writeFile ( watPath , wat ) ) ;
124150
151+ const wasi = new WASI ( wasiOptions ) ;
152+
125153 const context = new TestContext ( {
126154 fileName : fileNamePath , // set the fileName
127155 reporter, // use verbose reporter
128156 binary, // pass the binary to get function names
157+ wasi,
129158 } ) ;
130- const wasi = new WASI ( {
131- args : [ ] ,
132- env : { } ,
133- preopens : {
134- // '/sandbox': '/some/real/path/that/wasm/can/access'
135- }
136- } ) ;
159+
137160 const imports = context . createImports ( {
138161 wasi_snapshot_preview1 : wasi . wasiImport ,
139162 } ) ;
140163
141164 const instance = instantiateSync ( binary , imports ) ;
142- // TODO: wasi.start(instance);
165+
143166 process . stdout . write ( "\n" ) ;
144167 context . run ( instance ) ;
145168
0 commit comments