mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 10:44:47 +01:00
reduce the preparing and write more on the fly
This commit is contained in:
parent
bf6bf44252
commit
01dcb85b36
@ -58,8 +58,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
|
||||
private WasmOutputStream codeStream = new WasmOutputStream();
|
||||
|
||||
private WasmOutputStream functionsStream = new WasmOutputStream();
|
||||
|
||||
private List<FunctionType> functionTypes = new ArrayList<>();
|
||||
|
||||
private Map<String, Function> functions = new LinkedHashMap<>();
|
||||
@ -175,7 +173,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
}
|
||||
WasmOutputStream stream = new WasmOutputStream();
|
||||
stream.writeVaruint32( size );
|
||||
functionsStream.writeTo( stream );
|
||||
for( Function func : functions.values() ) {
|
||||
func.functionsStream.writeTo( stream );
|
||||
}
|
||||
wasm.writeSection( SectionType.Code, stream );
|
||||
}
|
||||
|
||||
@ -237,14 +237,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
functionType = new FunctionType();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void prepareFunction( FunctionName name ) {
|
||||
functions.put( name.signatureName, new Function() );
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@ -274,7 +266,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
*/
|
||||
@Override
|
||||
protected void writeMethodStart( FunctionName name ) throws IOException {
|
||||
function = functions.get( name.signatureName );
|
||||
function = getFunction( name );
|
||||
functionType = new FunctionType();
|
||||
codeStream.reset();
|
||||
locals.clear();
|
||||
@ -328,6 +320,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
localsStream.writeVaruint32( 1 ); // TODO optimize, write the count of same types.
|
||||
localsStream.writeValueType( valueType );
|
||||
}
|
||||
WasmOutputStream functionsStream = function.functionsStream = new WasmOutputStream();
|
||||
functionsStream.writeVaruint32( localsStream.size() + codeStream.size() + 1 );
|
||||
localsStream.writeTo( functionsStream );
|
||||
codeStream.writeTo( functionsStream );
|
||||
@ -702,23 +695,31 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
*/
|
||||
@Override
|
||||
protected void writeFunctionCall( FunctionName name ) throws IOException {
|
||||
Function func = getFunction( name );
|
||||
codeStream.writeOpCode( CALL );
|
||||
codeStream.writeVaruint32( func.id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the function object for the name. If not exists then it will be created.
|
||||
*
|
||||
* @param name
|
||||
* the function name
|
||||
* @return the function object
|
||||
*/
|
||||
@Nonnull
|
||||
private Function getFunction( FunctionName name ) {
|
||||
String signatureName = name.signatureName;
|
||||
int id;
|
||||
Function func = functions.get( signatureName );
|
||||
if( func != null ) {
|
||||
id = func.id;
|
||||
} else {
|
||||
ImportFunction entry = imports.get( signatureName );
|
||||
if( entry != null ) {
|
||||
id = entry.id;
|
||||
} else {
|
||||
if( func == null ) {
|
||||
func = imports.get( signatureName );
|
||||
if( func == null ) {
|
||||
func = new Function();
|
||||
id = func.id = functions.size() + imports.size();
|
||||
func.id = functions.size() + imports.size();
|
||||
functions.put( signatureName, func );
|
||||
}
|
||||
}
|
||||
codeStream.writeOpCode( CALL );
|
||||
codeStream.writeVaruint32( id );
|
||||
return func;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,6 +31,8 @@ class Function extends SectionEntry {
|
||||
|
||||
List<String> paramNames;
|
||||
|
||||
WasmOutputStream functionsStream;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -192,8 +192,6 @@ public class ModuleGenerator {
|
||||
String importName = (String)annotationValues.get( "name" );
|
||||
writer.prepareImport( name, impoarModule, importName );
|
||||
writeMethodSignature( name, null, null );
|
||||
} else {
|
||||
writer.prepareFunction( name );
|
||||
}
|
||||
} catch( Exception ioex ) {
|
||||
throw WasmException.create( ioex, sourceFile, className, -1 );
|
||||
|
@ -58,14 +58,6 @@ public abstract class ModuleWriter implements Closeable {
|
||||
*/
|
||||
protected abstract void prepareImport( FunctionName name, String importModule, String importName ) throws IOException;
|
||||
|
||||
/**
|
||||
* Prepare a single function in the prepare phase.
|
||||
*
|
||||
* @param name
|
||||
* the function name
|
||||
*/
|
||||
protected void prepareFunction( FunctionName name ) {}
|
||||
|
||||
/**
|
||||
* Write an export directive
|
||||
* @param name
|
||||
|
Loading…
x
Reference in New Issue
Block a user