1616package de .inetsoftware .jwebassembly ;
1717
1818import static org .junit .Assert .assertEquals ;
19+ import static org .junit .Assert .fail ;
1920
2021import java .io .File ;
2122import java .io .FileOutputStream ;
3637 */
3738public class WasmRule extends TemporaryFolder {
3839
40+ private static final boolean IS_WINDOWS = System .getProperty ( "os.name" ).toLowerCase ().indexOf ( "win" ) >= 0 ;
41+
3942 private static final SpiderMonkey spiderMonkey = new SpiderMonkey ();
4043
4144 private final Class <?>[] classes ;
4245
4346 private File wasmFile ;
4447
48+ private File wastFile ;
49+
4550 private File nodeScript ;
4651
4752 private File spiderMonkeyScript ;
4853
54+ private File nodeWastScript ;
55+
4956 private boolean failed ;
5057
5158 private String textCompiled ;
@@ -88,14 +95,29 @@ public void compile() throws IOException, WasmException {
8895 URL url = clazz .getResource ( '/' + clazz .getName ().replace ( '.' , '/' ) + ".class" );
8996 wasm .addFile ( url );
9097 }
91- textCompiled = wasm .compileToText (); // smoke test
98+ textCompiled = wasm .compileToText ();
9299 try {
93- create ();
100+ create ();
101+
102+ wastFile = newFile ( "test.wast" );
103+ try ( FileOutputStream stream = new FileOutputStream ( wastFile ) ) {
104+ stream .write ( textCompiled .getBytes ( StandardCharsets .UTF_8 ) );
105+ }
106+
94107 wasmFile = newFile ( "test.wasm" );
95108 wasm .compileToBinary ( wasmFile );
96-
109+
97110 nodeScript = createScript ( "nodetest.js" );
98111 spiderMonkeyScript = createScript ( "SpiderMonkeyTest.js" );
112+ nodeWastScript = createScript ( "WastTest.js" );
113+
114+ ProcessBuilder processBuilder = IS_WINDOWS ? new ProcessBuilder ( "cmd" , "/C" , "npm" , "install" , "wabt@nightly" ) : new ProcessBuilder ( "cmd" , "/C" , "npm" , "install" , "wabt@nightly" );
115+ processBuilder .directory ( getRoot () );
116+ Process process = processBuilder .start ();
117+ int exitCode = process .waitFor ();
118+ if ( exitCode != 0 ) {
119+ fail ( readStream ( process .getErrorStream () ) );
120+ }
99121 } catch ( Throwable ex ) {
100122 System .out .println ( textCompiled );
101123 throwException ( ex );
@@ -116,6 +138,7 @@ private File createScript( String name ) throws IOException {
116138 URL scriptUrl = getClass ().getResource ( name );
117139 String expected = readStream ( scriptUrl .openStream () );
118140 expected = expected .replace ( "{test.wasm}" , wasmFile .getName () );
141+ expected = expected .replace ( "{test.wast}" , wastFile .getName () );
119142 try (FileOutputStream scriptStream = new FileOutputStream ( file )) {
120143 scriptStream .write ( expected .getBytes ( StandardCharsets .UTF_8 ) );
121144 }
@@ -214,7 +237,10 @@ public String evalWasm( ScriptEngine script, String methodName, Object... params
214237 processBuilder = spiderMonkeyCommand ();
215238 break ;
216239 case NodeJS :
217- processBuilder = nodeJsCommand ();
240+ processBuilder = nodeJsCommand ( nodeScript );
241+ break ;
242+ case NodeWast :
243+ processBuilder = nodeJsCommand ( nodeWastScript );
218244 break ;
219245 default :
220246 throw new IllegalStateException ( script .toString () );
@@ -266,20 +292,22 @@ private ProcessBuilder spiderMonkeyCommand() throws IOException {
266292 /**
267293 * Create a ProcessBuilder for node.js
268294 *
295+ * @param script
296+ * the path to the script that should be executed
269297 * @return the value from the script
270298 */
271- private ProcessBuilder nodeJsCommand () {
299+ private static ProcessBuilder nodeJsCommand ( File script ) {
272300 String command = System .getProperty ( "node.dir" );
273301 if ( command == null ) {
274302 command = "node" ;
275303 } else {
276- if ( System . getProperty ( "os.name" ). toLowerCase (). indexOf ( "win" ) >= 0 ) {
304+ if ( IS_WINDOWS ) {
277305 command += "/node" ;
278306 } else {
279307 command += "/bin/node" ;
280308 }
281309 }
282- return new ProcessBuilder ( command , "--experimental-wasm-se" , "--experimental-wasm-sat-f2i-conversions" , nodeScript .getAbsolutePath () );
310+ return new ProcessBuilder ( command , "--experimental-wasm-se" , "--experimental-wasm-sat-f2i-conversions" , script .getAbsolutePath () );
283311 }
284312
285313 /**
0 commit comments