2017-03-19 11:54:29 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2017 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;
|
|
|
|
|
2017-03-20 17:48:05 +01:00
|
|
|
import java.io.BufferedInputStream;
|
2017-03-24 22:48:28 +01:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2017-03-19 11:54:29 +01:00
|
|
|
import java.io.File;
|
2017-03-20 17:48:05 +01:00
|
|
|
import java.io.FileInputStream;
|
2017-03-24 18:19:25 +01:00
|
|
|
import java.io.IOException;
|
2017-03-24 22:48:28 +01:00
|
|
|
import java.io.OutputStream;
|
2017-03-19 11:54:29 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2017-03-20 17:48:05 +01:00
|
|
|
import de.inetsoftware.classparser.ClassFile;
|
2017-03-24 22:48:28 +01:00
|
|
|
import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter;
|
2017-03-24 18:19:25 +01:00
|
|
|
import de.inetsoftware.jwebassembly.module.ModuleWriter;
|
2017-03-24 18:08:03 +01:00
|
|
|
import de.inetsoftware.jwebassembly.text.TextModuleWriter;
|
2017-03-20 17:48:05 +01:00
|
|
|
|
2017-03-19 11:54:29 +01:00
|
|
|
/**
|
|
|
|
* The main class of the compiler.
|
|
|
|
*
|
|
|
|
* @author Volker Berlin
|
|
|
|
*/
|
|
|
|
public class JWebAssembly {
|
|
|
|
|
|
|
|
private List<File> classFiles = new ArrayList<File>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a instance.
|
|
|
|
*/
|
|
|
|
public JWebAssembly() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a classFile to compile
|
|
|
|
*
|
|
|
|
* @param classFile
|
|
|
|
* the file
|
|
|
|
*/
|
|
|
|
public void addFile( @Nonnull File classFile ) {
|
|
|
|
classFiles.add( classFile );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the added files to a WebAssembly module in text representation.
|
|
|
|
*
|
|
|
|
* @return the module as string
|
2017-03-20 17:48:05 +01:00
|
|
|
* @throws WasmException
|
2017-03-19 11:54:29 +01:00
|
|
|
* if any conversion error occurs
|
|
|
|
*/
|
2017-03-20 17:48:05 +01:00
|
|
|
public String compileToText() throws WasmException {
|
|
|
|
StringBuilder output = new StringBuilder();
|
2017-03-24 18:19:25 +01:00
|
|
|
compileToText( output );
|
|
|
|
return output.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the added files to a WebAssembly module in text representation.
|
|
|
|
*
|
|
|
|
* @param output
|
|
|
|
* the target for the module data
|
|
|
|
* @throws WasmException
|
|
|
|
* if any conversion error occurs
|
|
|
|
*/
|
|
|
|
public void compileToText( Appendable output ) throws WasmException {
|
2017-03-24 18:08:03 +01:00
|
|
|
try (TextModuleWriter writer = new TextModuleWriter( output )) {
|
2017-03-24 18:19:25 +01:00
|
|
|
compile( writer );
|
2017-03-20 17:48:05 +01:00
|
|
|
} catch( Exception ex ) {
|
|
|
|
throw WasmException.create( ex );
|
|
|
|
}
|
2017-03-24 18:19:25 +01:00
|
|
|
}
|
|
|
|
|
2017-03-24 22:48:28 +01:00
|
|
|
/**
|
|
|
|
* Convert the added files to a WebAssembly module in binary representation.
|
|
|
|
*
|
|
|
|
* @return the module as string
|
|
|
|
* @throws WasmException
|
|
|
|
* if any conversion error occurs
|
|
|
|
*/
|
|
|
|
public byte[] compileToBinary() throws WasmException {
|
|
|
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
|
compileToBinary( output );
|
|
|
|
return output.toByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the added files to a WebAssembly module in binary representation.
|
|
|
|
*
|
|
|
|
* @param output
|
|
|
|
* the target for the module data
|
|
|
|
* @throws WasmException
|
|
|
|
* if any conversion error occurs
|
|
|
|
*/
|
|
|
|
public void compileToBinary( OutputStream output ) throws WasmException {
|
|
|
|
try (BinaryModuleWriter writer = new BinaryModuleWriter( output )) {
|
|
|
|
compile( writer );
|
|
|
|
} catch( Exception ex ) {
|
|
|
|
throw WasmException.create( ex );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 18:19:25 +01:00
|
|
|
/**
|
|
|
|
* Convert the added files to a WebAssembly module.
|
|
|
|
*
|
|
|
|
* @param writer
|
|
|
|
* the formatter
|
|
|
|
* @throws IOException
|
|
|
|
* if any I/O error occur
|
|
|
|
* @throws WasmException
|
|
|
|
* if any conversion error occurs
|
|
|
|
*/
|
|
|
|
private void compile( ModuleWriter writer ) throws IOException, WasmException {
|
|
|
|
for( File file : classFiles ) {
|
|
|
|
ClassFile classFile = new ClassFile( new BufferedInputStream( new FileInputStream( file ) ) );
|
|
|
|
writer.write( classFile );
|
|
|
|
}
|
2017-03-19 11:54:29 +01:00
|
|
|
}
|
|
|
|
}
|