Skip to content

Commit 3494c2a

Browse files
VolkerVolker
authored andcommitted
Add unit tests for the text output.
1 parent 41e66c8 commit 3494c2a

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

test/de/inetsoftware/jwebassembly/ScriptEngine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
*/
2424
public enum ScriptEngine {
2525
NodeJS,
26-
SpiderMonkey;
26+
SpiderMonkey,
27+
NodeWast;
2728

2829
public static Collection<ScriptEngine[]> testParams() {
29-
ScriptEngine[][] val = {{ScriptEngine.SpiderMonkey},{ScriptEngine.NodeJS}};
30+
ScriptEngine[][] val = {{ScriptEngine.SpiderMonkey},{ScriptEngine.NodeJS},{ScriptEngine.NodeWast}};
3031
return Arrays.asList(val);
3132
}
3233
}

test/de/inetsoftware/jwebassembly/WasmRule.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package de.inetsoftware.jwebassembly;
1717

1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.fail;
1920

2021
import java.io.File;
2122
import java.io.FileOutputStream;
@@ -36,16 +37,22 @@
3637
*/
3738
public 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
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env node
2+
3+
var nodeFS = require('fs');
4+
var wabt = require("wabt");
5+
6+
var filename = '{test.wast}';
7+
var text = nodeFS['readFileSync'](filename, "utf8");
8+
9+
var ret = wabt.parseWat(filename, text);
10+
ret = ret.toBinary({}).buffer;
11+
12+
function instantiate(bytes, imports) {
13+
return WebAssembly.compile(bytes).then(
14+
m => new WebAssembly.Instance(m, imports), reason => console.log(reason) );
15+
}
16+
17+
var dependencies = {
18+
"global": {},
19+
"env": {}
20+
};
21+
dependencies["global.Math"] = Math;
22+
23+
function callExport(instance) {
24+
try{
25+
console.log( instance.exports[process.argv[2]]( ...process.argv.slice(3) ) );
26+
}catch(err){
27+
console.log(err)
28+
}
29+
}
30+
31+
instantiate( ret, dependencies ).then(
32+
instance => callExport(instance),
33+
reason => console.log(reason)
34+
);

0 commit comments

Comments
 (0)