mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 18:44:47 +01:00
Add a target handler.
This commit is contained in:
parent
7d8424a2b0
commit
ed4fac4cbd
@ -36,6 +36,7 @@ import de.inetsoftware.classparser.ClassFile;
|
||||
import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter;
|
||||
import de.inetsoftware.jwebassembly.module.ModuleGenerator;
|
||||
import de.inetsoftware.jwebassembly.module.ModuleWriter;
|
||||
import de.inetsoftware.jwebassembly.module.WasmTarget;
|
||||
import de.inetsoftware.jwebassembly.text.TextModuleWriter;
|
||||
|
||||
/**
|
||||
@ -225,8 +226,8 @@ public class JWebAssembly {
|
||||
* if any conversion error occurs
|
||||
*/
|
||||
public void compileToBinary( File file ) throws WasmException {
|
||||
try (OutputStream output = new BufferedOutputStream( new FileOutputStream( file ) )) {
|
||||
compileToBinary( output );
|
||||
try (WasmTarget target = new WasmTarget( file ) ) {
|
||||
compileToBinary( target );
|
||||
} catch( Exception ex ) {
|
||||
throw WasmException.create( ex );
|
||||
}
|
||||
@ -241,7 +242,23 @@ public class JWebAssembly {
|
||||
* if any conversion error occurs
|
||||
*/
|
||||
public void compileToBinary( OutputStream output ) throws WasmException {
|
||||
try (BinaryModuleWriter writer = new BinaryModuleWriter( output, properties )) {
|
||||
try (WasmTarget target = new WasmTarget( output )) {
|
||||
compileToBinary( target );
|
||||
} catch( Exception ex ) {
|
||||
throw WasmException.create( ex );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the added files to a WebAssembly module in binary representation.
|
||||
*
|
||||
* @param target
|
||||
* the target for the module data
|
||||
* @throws WasmException
|
||||
* if any conversion error occurs
|
||||
*/
|
||||
private void compileToBinary( WasmTarget target ) throws WasmException {
|
||||
try (BinaryModuleWriter writer = new BinaryModuleWriter( target, properties )) {
|
||||
compile( writer );
|
||||
} catch( Exception ex ) {
|
||||
throw WasmException.create( ex );
|
||||
|
@ -16,7 +16,6 @@
|
||||
package de.inetsoftware.jwebassembly.binary;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@ -34,10 +33,11 @@ import de.inetsoftware.jwebassembly.JWebAssembly;
|
||||
import de.inetsoftware.jwebassembly.module.FunctionName;
|
||||
import de.inetsoftware.jwebassembly.module.ModuleWriter;
|
||||
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
|
||||
import de.inetsoftware.jwebassembly.module.WasmTarget;
|
||||
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
||||
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||
import de.inetsoftware.jwebassembly.wasm.VariableOperator;
|
||||
@ -54,6 +54,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
|
||||
private static final int WASM_BINARY_VERSION = 1;
|
||||
|
||||
private WasmTarget target;
|
||||
|
||||
private WasmOutputStream wasm;
|
||||
|
||||
private final boolean debugNames;
|
||||
@ -90,8 +92,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
* @throws IOException
|
||||
* if any I/O error occur
|
||||
*/
|
||||
public BinaryModuleWriter( OutputStream output, HashMap<String, String> properties ) throws IOException {
|
||||
wasm = new WasmOutputStream( output );
|
||||
public BinaryModuleWriter( WasmTarget target, HashMap<String, String> properties ) throws IOException {
|
||||
this.target = target;
|
||||
// for now we build the source map together with debug names
|
||||
debugNames = createSourceMap = Boolean.parseBoolean( properties.get( JWebAssembly.DEBUG_NAMES ) );
|
||||
}
|
||||
@ -101,6 +103,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
wasm = new WasmOutputStream( target.getWasmOutput() );
|
||||
wasm.write( WASM_BINARY_MAGIC );
|
||||
wasm.writeInt32( WASM_BINARY_VERSION );
|
||||
|
||||
|
81
src/de/inetsoftware/jwebassembly/module/WasmTarget.java
Normal file
81
src/de/inetsoftware/jwebassembly/module/WasmTarget.java
Normal file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.module;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* The target for the different outputs
|
||||
*/
|
||||
public class WasmTarget implements Closeable {
|
||||
|
||||
private File file;
|
||||
|
||||
private OutputStream output;
|
||||
|
||||
/**
|
||||
* Create a target with a file.
|
||||
*
|
||||
* @param file
|
||||
* the wasm file
|
||||
*/
|
||||
public WasmTarget( File file ) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a target with an OutputStream
|
||||
*
|
||||
* @param output
|
||||
* the stream for the wasm file
|
||||
*/
|
||||
public WasmTarget( OutputStream output ) {
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the wasm OutputStream
|
||||
*
|
||||
* @return the stream
|
||||
* @throws IOException
|
||||
* if any I/O error occur
|
||||
*/
|
||||
@Nonnull
|
||||
public OutputStream getWasmOutput() throws IOException {
|
||||
if( output == null ) {
|
||||
output = new BufferedOutputStream( new FileOutputStream( file ) );
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close all streams
|
||||
*
|
||||
* @throws IOException
|
||||
* if any I/O error occur
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
output.close();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user