mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 02:44:47 +01:00
Use JavaScript imports generated from the imports declarations.
This commit is contained in:
parent
5133890a6b
commit
d9e8b70d06
@ -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 );
|
||||
|
@ -129,6 +129,6 @@ public class JavaScriptWriter {
|
||||
}
|
||||
isFirst = false;
|
||||
}
|
||||
out.append( "\n}\n" );
|
||||
out.append( "\n};\nif (typeof module !== \"undefined\") module.exports = wasmImports;" );
|
||||
}
|
||||
}
|
||||
|
@ -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<URL> libraries ) {
|
||||
public ModuleGenerator( @Nonnull ModuleWriter writer, WasmTarget target, @Nonnull List<URL> 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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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)
|
||||
);
|
||||
|
@ -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)
|
||||
);
|
||||
|
@ -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)
|
||||
);
|
||||
|
@ -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() );
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
);
|
||||
|
@ -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 );
|
||||
}
|
||||
|
76
test/de/inetsoftware/jwebassembly/runtime/MathAPI.java
Normal file
76
test/de/inetsoftware/jwebassembly/runtime/MathAPI.java
Normal file
@ -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<Object[]> data() {
|
||||
ArrayList<Object[]> 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 );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user