From d9e8b70d0678040abdcd0346e0e3ebf08d74baff Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Wed, 3 Jul 2019 20:09:22 +0200 Subject: [PATCH] Use JavaScript imports generated from the imports declarations. --- .../jwebassembly/JWebAssembly.java | 12 +-- .../javascript/JavaScriptWriter.java | 2 +- .../jwebassembly/module/ModuleGenerator.java | 10 ++- .../jwebassembly/SpiderMonkeyTest.js | 11 +-- .../jwebassembly/SpiderMonkeyWatTest.js | 11 +-- test/de/inetsoftware/jwebassembly/WatTest.js | 9 +-- .../javascript/JavaScriptWriterTest.java | 16 ++-- test/de/inetsoftware/jwebassembly/nodetest.js | 9 +-- .../jwebassembly/runtime/CallFunctions.java | 2 +- .../jwebassembly/runtime/MathAPI.java | 76 +++++++++++++++++++ 10 files changed, 110 insertions(+), 48 deletions(-) create mode 100644 test/de/inetsoftware/jwebassembly/runtime/MathAPI.java diff --git a/src/de/inetsoftware/jwebassembly/JWebAssembly.java b/src/de/inetsoftware/jwebassembly/JWebAssembly.java index 4fb0699..48adba3 100644 --- a/src/de/inetsoftware/jwebassembly/JWebAssembly.java +++ b/src/de/inetsoftware/jwebassembly/JWebAssembly.java @@ -203,14 +203,14 @@ public class JWebAssembly { /** * Convert the added files to a WebAssembly module in text representation. * - * @param output + * @param target * the target for the module data * @throws WasmException * if any conversion error occurs */ private void compileToText( WasmTarget target ) throws WasmException { try (TextModuleWriter writer = new TextModuleWriter( target, properties )) { - compile( writer ); + compile( writer, target ); } catch( Exception ex ) { throw WasmException.create( ex ); } @@ -271,7 +271,7 @@ public class JWebAssembly { */ private void compileToBinary( WasmTarget target ) throws WasmException { try (BinaryModuleWriter writer = new BinaryModuleWriter( target, properties )) { - compile( writer ); + compile( writer, target ); } catch( Exception ex ) { throw WasmException.create( ex ); } @@ -282,13 +282,15 @@ public class JWebAssembly { * * @param writer * the formatter + * @param target + * the target for the module data * @throws IOException * if any I/O error occur * @throws WasmException * if any conversion error occurs */ - private void compile( ModuleWriter writer ) throws IOException, WasmException { - ModuleGenerator generator = new ModuleGenerator( writer, libraries ); + private void compile( ModuleWriter writer, WasmTarget target ) throws IOException, WasmException { + ModuleGenerator generator = new ModuleGenerator( writer, target, libraries ); for( URL url : classFiles ) { ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) ); generator.prepare( classFile ); diff --git a/src/de/inetsoftware/jwebassembly/javascript/JavaScriptWriter.java b/src/de/inetsoftware/jwebassembly/javascript/JavaScriptWriter.java index 04021cf..93d5ccf 100644 --- a/src/de/inetsoftware/jwebassembly/javascript/JavaScriptWriter.java +++ b/src/de/inetsoftware/jwebassembly/javascript/JavaScriptWriter.java @@ -129,6 +129,6 @@ public class JavaScriptWriter { } isFirst = false; } - out.append( "\n}\n" ); + out.append( "\n};\nif (typeof module !== \"undefined\") module.exports = wasmImports;" ); } } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 57a0dfa..70fccfe 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -38,6 +38,7 @@ import de.inetsoftware.classparser.ConstantClass; import de.inetsoftware.classparser.MethodInfo; import de.inetsoftware.jwebassembly.JWebAssembly; import de.inetsoftware.jwebassembly.WasmException; +import de.inetsoftware.jwebassembly.javascript.JavaScriptWriter; import de.inetsoftware.jwebassembly.module.TypeManager.StructType; import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.NamedStorageType; @@ -54,6 +55,8 @@ public class ModuleGenerator { private final ModuleWriter writer; + private final JavaScriptWriter javaScript; + private final URLClassLoader libraries; private final JavaMethodWasmCodeBuilder javaCodeBuilder = new JavaMethodWasmCodeBuilder(); @@ -75,11 +78,14 @@ public class ModuleGenerator { * * @param writer * the target writer + * @param target + * the target for the module data * @param libraries * libraries */ - public ModuleGenerator( @Nonnull ModuleWriter writer, @Nonnull List libraries ) { + public ModuleGenerator( @Nonnull ModuleWriter writer, WasmTarget target, @Nonnull List libraries ) { this.writer = writer; + this.javaScript = new JavaScriptWriter( target ); this.libraries = new URLClassLoader( libraries.toArray( new URL[libraries.size()] ) ); javaCodeBuilder.init( types ); ((WasmCodeBuilder)watParser).init( types ); @@ -211,6 +217,7 @@ public class ModuleGenerator { String importName = (String)importAnannotation.get( "name" ); writer.prepareImport( name, impoarModule, importName ); writeMethodSignature( name, true, null ); + javaScript.addImport( impoarModule, importName, importAnannotation ); } // init/write the function types @@ -295,6 +302,7 @@ public class ModuleGenerator { throw new WasmException( "Missing function: " + next.signatureName, -1 ); } } + javaScript.finish(); } /** diff --git a/test/de/inetsoftware/jwebassembly/SpiderMonkeyTest.js b/test/de/inetsoftware/jwebassembly/SpiderMonkeyTest.js index b4940e4..3ad307e 100644 --- a/test/de/inetsoftware/jwebassembly/SpiderMonkeyTest.js +++ b/test/de/inetsoftware/jwebassembly/SpiderMonkeyTest.js @@ -1,14 +1,7 @@ -#!/usr/bin/env node - +load( "spiderMonkey.wasm.js" ); var wasm = read( "spiderMonkey.wasm", "binary" ); // https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Shell_global_objects var testData = JSON.parse( read( "testdata.json" ) ); -var dependencies = { - "global": {}, - "env": {} -}; -dependencies["global.Math"] = Math; - function callExport(instance) { var result = {}; for (var method in testData) { @@ -24,7 +17,7 @@ function callExport(instance) { redirect( original ); } -WebAssembly.instantiate( wasm, dependencies ).then( +WebAssembly.instantiate( wasm, wasmImports ).then( obj => callExport(obj.instance), reason => console.log(reason) ); diff --git a/test/de/inetsoftware/jwebassembly/SpiderMonkeyWatTest.js b/test/de/inetsoftware/jwebassembly/SpiderMonkeyWatTest.js index 154dac9..d504829 100644 --- a/test/de/inetsoftware/jwebassembly/SpiderMonkeyWatTest.js +++ b/test/de/inetsoftware/jwebassembly/SpiderMonkeyWatTest.js @@ -1,14 +1,7 @@ -#!/usr/bin/env node - +load( "spiderMonkey.wasm.js" ); var wasm = wasmTextToBinary( read( "spiderMonkey.wat" ) ); var testData = JSON.parse( read( "testdata.json" ) ); -var dependencies = { - "global": {}, - "env": {} -}; -dependencies["global.Math"] = Math; - function callExport(instance) { var result = {}; for (var method in testData) { @@ -24,7 +17,7 @@ function callExport(instance) { redirect( original ); } -WebAssembly.instantiate( wasm, dependencies ).then( +WebAssembly.instantiate( wasm, wasmImports ).then( obj => callExport(obj.instance), reason => console.log(reason) ); diff --git a/test/de/inetsoftware/jwebassembly/WatTest.js b/test/de/inetsoftware/jwebassembly/WatTest.js index 1ee300c..02dccd4 100644 --- a/test/de/inetsoftware/jwebassembly/WatTest.js +++ b/test/de/inetsoftware/jwebassembly/WatTest.js @@ -3,6 +3,7 @@ var fs = require('fs'); var wabt = require("wabt")(); +const wasmImports = require( "./test.wasm.js" ); var filename = '{test.wat}'; var text = fs.readFileSync(filename, "utf8"); var testData = JSON.parse( fs.readFileSync( "testdata.json", "utf8" ) ); @@ -11,12 +12,6 @@ var features = {'sat_float_to_int':true, 'sign_extension':true, 'exceptions':tru var wasm = wabt.parseWat(filename, text, features); wasm = wasm.toBinary({}).buffer; -var dependencies = { - "global": {}, - "env": {} -}; -dependencies["global.Math"] = Math; - function callExport(instance) { var result = {}; for (var method in testData) { @@ -30,7 +25,7 @@ function callExport(instance) { fs.writeFileSync( "testresult.json", JSON.stringify(result) ); } -WebAssembly.instantiate( wasm, dependencies ).then( +WebAssembly.instantiate( wasm, wasmImports ).then( obj => callExport(obj.instance), reason => console.log(reason) ); diff --git a/test/de/inetsoftware/jwebassembly/javascript/JavaScriptWriterTest.java b/test/de/inetsoftware/jwebassembly/javascript/JavaScriptWriterTest.java index a143495..cc89655 100644 --- a/test/de/inetsoftware/jwebassembly/javascript/JavaScriptWriterTest.java +++ b/test/de/inetsoftware/jwebassembly/javascript/JavaScriptWriterTest.java @@ -26,8 +26,8 @@ public class JavaScriptWriterTest { "Foo:{\n" + "bar:1 + 1\n" + "}\n" + - "}\n" + - "", builder.toString() ); + "};\n" + + "if (typeof module !== \"undefined\") module.exports = wasmImports;", builder.toString() ); } @Test @@ -42,8 +42,8 @@ public class JavaScriptWriterTest { "bar:1 + 1,\n" + "xyz:3\n" + "}\n" + - "}\n" + - "", builder.toString() ); + "};\n" + + "if (typeof module !== \"undefined\") module.exports = wasmImports;", builder.toString() ); } @Test @@ -60,8 +60,8 @@ public class JavaScriptWriterTest { "Foo:{\n" + "foo:1 + 1\n" + "}\n" + - "}\n" + - "", builder.toString() ); + "};\n" + + "if (typeof module !== \"undefined\") module.exports = wasmImports;", builder.toString() ); } @Test @@ -73,7 +73,7 @@ public class JavaScriptWriterTest { writer.finish( builder ); assertEquals( "var wasmImports = {\n" + "Foo:Foo\n" + - "}\n" + - "", builder.toString() ); + "};\n" + + "if (typeof module !== \"undefined\") module.exports = wasmImports;", builder.toString() ); } } diff --git a/test/de/inetsoftware/jwebassembly/nodetest.js b/test/de/inetsoftware/jwebassembly/nodetest.js index 09eebca..b93c0e4 100644 --- a/test/de/inetsoftware/jwebassembly/nodetest.js +++ b/test/de/inetsoftware/jwebassembly/nodetest.js @@ -2,16 +2,11 @@ var fs = require('fs'); +const wasmImports = require( "./test.wasm.js" ); var filename = '{test.wasm}'; var wasm = fs.readFileSync(filename); var testData = JSON.parse( fs.readFileSync( "testdata.json", "utf8" ) ); -var dependencies = { - "global": {}, - "env": {} -}; -dependencies["global.Math"] = Math; - function callExport(instance) { var result = {}; for (var method in testData) { @@ -25,7 +20,7 @@ function callExport(instance) { fs.writeFileSync( "testresult.json", JSON.stringify(result) ); } -WebAssembly.instantiate( wasm, dependencies ).then( +WebAssembly.instantiate( wasm, wasmImports ).then( obj => callExport(obj.instance), reason => console.log(reason) ); diff --git a/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java b/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java index 612009e..e5db495 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java +++ b/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java @@ -73,7 +73,7 @@ public class CallFunctions extends AbstractBaseTest { } - @Import( module = "global.Math", name = "max" ) + @Import( module = "Math", name = "max" ) static int abc( int a, int b) { return Math.max( a, b ); } diff --git a/test/de/inetsoftware/jwebassembly/runtime/MathAPI.java b/test/de/inetsoftware/jwebassembly/runtime/MathAPI.java new file mode 100644 index 0000000..7ad7ae7 --- /dev/null +++ b/test/de/inetsoftware/jwebassembly/runtime/MathAPI.java @@ -0,0 +1,76 @@ +/* + * Copyright 2019 Volker Berlin (i-net software) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.inetsoftware.jwebassembly.runtime; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.ClassRule; +import org.junit.runners.Parameterized.Parameters; + +import de.inetsoftware.jwebassembly.ScriptEngine; +import de.inetsoftware.jwebassembly.WasmRule; +import de.inetsoftware.jwebassembly.api.annotation.Export; + +/** + * @author Volker Berlin + */ +public class MathAPI extends AbstractBaseTest { + + @ClassRule + public static WasmRule rule = new WasmRule( TestClass.class ); + + public MathAPI( ScriptEngine script, String method, Object[] params ) { + super( rule, script, method, params ); + } + + @Parameters(name="{0}-{1}") + public static Collection data() { + ArrayList list = new ArrayList<>(); + for( ScriptEngine[] val : ScriptEngine.testParams() ) { + ScriptEngine script = val[0]; + addParam( list, script, "sin0" ); + addParam( list, script, "sinPI" ); + addParam( list, script, "cos0" ); + addParam( list, script, "tan0" ); + } + rule.setTestParameters( list ); + return list; + } + + static class TestClass { + + @Export + static double sin0() { + return Math.sin( 0 ); + } + + @Export + static double sinPI() { + return Math.sin( Math.PI / 2 ); + } + + @Export + static double cos0() { + return Math.cos( 0 ); + } + + @Export + static double tan0() { + return Math.tan( 0 ); + } + } +}