Add unit tests for the text output.

This commit is contained in:
Volker 2018-08-14 22:36:43 +02:00
parent 41e66c8a14
commit 3494c2a57a
3 changed files with 72 additions and 9 deletions

View File

@ -23,10 +23,11 @@ import java.util.Collection;
*/ */
public enum ScriptEngine { public enum ScriptEngine {
NodeJS, NodeJS,
SpiderMonkey; SpiderMonkey,
NodeWast;
public static Collection<ScriptEngine[]> testParams() { public static Collection<ScriptEngine[]> testParams() {
ScriptEngine[][] val = {{ScriptEngine.SpiderMonkey},{ScriptEngine.NodeJS}}; ScriptEngine[][] val = {{ScriptEngine.SpiderMonkey},{ScriptEngine.NodeJS},{ScriptEngine.NodeWast}};
return Arrays.asList(val); return Arrays.asList(val);
} }
} }

View File

@ -16,6 +16,7 @@
package de.inetsoftware.jwebassembly; package de.inetsoftware.jwebassembly;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -36,16 +37,22 @@ import org.junit.rules.TemporaryFolder;
*/ */
public class WasmRule extends TemporaryFolder { public class WasmRule extends TemporaryFolder {
private static final boolean IS_WINDOWS = System.getProperty( "os.name" ).toLowerCase().indexOf( "win" ) >= 0;
private static final SpiderMonkey spiderMonkey = new SpiderMonkey(); private static final SpiderMonkey spiderMonkey = new SpiderMonkey();
private final Class<?>[] classes; private final Class<?>[] classes;
private File wasmFile; private File wasmFile;
private File wastFile;
private File nodeScript; private File nodeScript;
private File spiderMonkeyScript; private File spiderMonkeyScript;
private File nodeWastScript;
private boolean failed; private boolean failed;
private String textCompiled; private String textCompiled;
@ -88,14 +95,29 @@ public class WasmRule extends TemporaryFolder {
URL url = clazz.getResource( '/' + clazz.getName().replace( '.', '/' ) + ".class" ); URL url = clazz.getResource( '/' + clazz.getName().replace( '.', '/' ) + ".class" );
wasm.addFile( url ); wasm.addFile( url );
} }
textCompiled = wasm.compileToText(); // smoke test textCompiled = wasm.compileToText();
try { try {
create(); create();
wastFile = newFile( "test.wast" );
try( FileOutputStream stream = new FileOutputStream( wastFile ) ) {
stream.write( textCompiled.getBytes( StandardCharsets.UTF_8 ) );
}
wasmFile = newFile( "test.wasm" ); wasmFile = newFile( "test.wasm" );
wasm.compileToBinary( wasmFile ); wasm.compileToBinary( wasmFile );
nodeScript = createScript( "nodetest.js" ); nodeScript = createScript( "nodetest.js" );
spiderMonkeyScript = createScript( "SpiderMonkeyTest.js" ); spiderMonkeyScript = createScript( "SpiderMonkeyTest.js" );
nodeWastScript = createScript( "WastTest.js" );
ProcessBuilder processBuilder = IS_WINDOWS ? new ProcessBuilder( "cmd", "/C", "npm", "install", "wabt@nightly" ) : new ProcessBuilder( "cmd", "/C", "npm", "install", "wabt@nightly" );
processBuilder.directory( getRoot() );
Process process = processBuilder.start();
int exitCode = process.waitFor();
if( exitCode != 0 ) {
fail( readStream( process.getErrorStream() ) );
}
} catch( Throwable ex ) { } catch( Throwable ex ) {
System.out.println( textCompiled ); System.out.println( textCompiled );
throwException( ex ); throwException( ex );
@ -116,6 +138,7 @@ public class WasmRule extends TemporaryFolder {
URL scriptUrl = getClass().getResource( name ); URL scriptUrl = getClass().getResource( name );
String expected = readStream( scriptUrl.openStream() ); String expected = readStream( scriptUrl.openStream() );
expected = expected.replace( "{test.wasm}", wasmFile.getName() ); expected = expected.replace( "{test.wasm}", wasmFile.getName() );
expected = expected.replace( "{test.wast}", wastFile.getName() );
try (FileOutputStream scriptStream = new FileOutputStream( file )) { try (FileOutputStream scriptStream = new FileOutputStream( file )) {
scriptStream.write( expected.getBytes( StandardCharsets.UTF_8 ) ); scriptStream.write( expected.getBytes( StandardCharsets.UTF_8 ) );
} }
@ -214,7 +237,10 @@ public class WasmRule extends TemporaryFolder {
processBuilder = spiderMonkeyCommand(); processBuilder = spiderMonkeyCommand();
break; break;
case NodeJS: case NodeJS:
processBuilder = nodeJsCommand(); processBuilder = nodeJsCommand( nodeScript );
break;
case NodeWast:
processBuilder = nodeJsCommand( nodeWastScript );
break; break;
default: default:
throw new IllegalStateException( script.toString() ); throw new IllegalStateException( script.toString() );
@ -266,20 +292,22 @@ public class WasmRule extends TemporaryFolder {
/** /**
* Create a ProcessBuilder for node.js * Create a ProcessBuilder for node.js
* *
* @param script
* the path to the script that should be executed
* @return the value from the script * @return the value from the script
*/ */
private ProcessBuilder nodeJsCommand() { private static ProcessBuilder nodeJsCommand( File script ) {
String command = System.getProperty( "node.dir" ); String command = System.getProperty( "node.dir" );
if( command == null ) { if( command == null ) {
command = "node"; command = "node";
} else { } else {
if( System.getProperty("os.name").toLowerCase().indexOf( "win" ) >= 0 ) { if( IS_WINDOWS ) {
command += "/node"; command += "/node";
} else { } else {
command += "/bin/node"; command += "/bin/node";
} }
} }
return new ProcessBuilder( command, "--experimental-wasm-se", "--experimental-wasm-sat-f2i-conversions", nodeScript.getAbsolutePath() ); return new ProcessBuilder( command, "--experimental-wasm-se", "--experimental-wasm-sat-f2i-conversions", script.getAbsolutePath() );
} }
/** /**

View File

@ -0,0 +1,34 @@
#!/usr/bin/env node
var nodeFS = require('fs');
var wabt = require("wabt");
var filename = '{test.wast}';
var text = nodeFS['readFileSync'](filename, "utf8");
var ret = wabt.parseWat(filename, text);
ret = ret.toBinary({}).buffer;
function instantiate(bytes, imports) {
return WebAssembly.compile(bytes).then(
m => new WebAssembly.Instance(m, imports), reason => console.log(reason) );
}
var dependencies = {
"global": {},
"env": {}
};
dependencies["global.Math"] = Math;
function callExport(instance) {
try{
console.log( instance.exports[process.argv[2]]( ...process.argv.slice(3) ) );
}catch(err){
console.log(err)
}
}
instantiate( ret, dependencies ).then(
instance => callExport(instance),
reason => console.log(reason)
);