mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 15:37:52 +01:00
add FunctionType to differ between real and abstract methods
This commit is contained in:
parent
dbf658ec41
commit
ae0f1e3410
@ -18,6 +18,7 @@ package de.inetsoftware.jwebassembly.binary;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -38,6 +39,7 @@ import de.inetsoftware.jwebassembly.sourcemap.SourceMapWriter;
|
|||||||
import de.inetsoftware.jwebassembly.sourcemap.SourceMapping;
|
import de.inetsoftware.jwebassembly.sourcemap.SourceMapping;
|
||||||
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
||||||
|
import de.inetsoftware.jwebassembly.wasm.FunctionType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
|
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
||||||
@ -77,6 +79,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
|
|
||||||
private Map<String, ImportFunction> imports = new LinkedHashMap<>();
|
private Map<String, ImportFunction> imports = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
private Map<String, Function> abstracts = new HashMap<>();
|
||||||
|
|
||||||
private Function function;
|
private Function function;
|
||||||
|
|
||||||
private FunctionTypeEntry functionType;
|
private FunctionTypeEntry functionType;
|
||||||
@ -513,8 +517,12 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeMethodParamStart( FunctionName name ) throws IOException {
|
protected void writeMethodParamStart( FunctionName name, FunctionType funcType ) throws IOException {
|
||||||
function = getFunction( name );
|
if( funcType == FunctionType.Abstract ) {
|
||||||
|
abstracts.put( name.signatureName, function = new Function() );
|
||||||
|
} else {
|
||||||
|
function = getFunction( name );
|
||||||
|
}
|
||||||
functionType = new FunctionTypeEntry();
|
functionType = new FunctionTypeEntry();
|
||||||
locals.clear();
|
locals.clear();
|
||||||
}
|
}
|
||||||
@ -1186,9 +1194,12 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
if( func == null ) {
|
if( func == null ) {
|
||||||
func = imports.get( signatureName );
|
func = imports.get( signatureName );
|
||||||
if( func == null ) {
|
if( func == null ) {
|
||||||
func = new Function();
|
func = abstracts.get( signatureName );
|
||||||
func.id = functions.size() + imports.size();
|
if( func == null ) {
|
||||||
functions.put( signatureName, func );
|
func = new Function();
|
||||||
|
func.id = functions.size() + imports.size();
|
||||||
|
functions.put( signatureName, func );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return func;
|
return func;
|
||||||
|
@ -43,6 +43,7 @@ import de.inetsoftware.jwebassembly.WasmException;
|
|||||||
import de.inetsoftware.jwebassembly.javascript.JavaScriptWriter;
|
import de.inetsoftware.jwebassembly.javascript.JavaScriptWriter;
|
||||||
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
|
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||||
|
import de.inetsoftware.jwebassembly.wasm.FunctionType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||||
@ -294,20 +295,20 @@ public class ModuleGenerator {
|
|||||||
importName = name.methodName;
|
importName = name.methodName;
|
||||||
}
|
}
|
||||||
writer.prepareImport( name, importModule, importName );
|
writer.prepareImport( name, importModule, importName );
|
||||||
writeMethodSignature( name, null );
|
writeMethodSignature( name, FunctionType.Import, null );
|
||||||
javaScript.addImport( importModule, importName, importAnannotation );
|
javaScript.addImport( importModule, importName, importAnannotation );
|
||||||
}
|
}
|
||||||
|
|
||||||
// init/write the function types
|
// init/write the function types
|
||||||
for( Iterator<FunctionName> iterator = functions.getWriteLater(); iterator.hasNext(); ) {
|
for( Iterator<FunctionName> iterator = functions.getWriteLater(); iterator.hasNext(); ) {
|
||||||
FunctionName name = iterator.next();
|
FunctionName name = iterator.next();
|
||||||
writeMethodSignature( name, null );
|
writeMethodSignature( name, FunctionType.Code, null );
|
||||||
}
|
}
|
||||||
|
|
||||||
// register types of abstract and interface methods
|
// register types of abstract and interface methods
|
||||||
for( Iterator<FunctionName> iterator = functions.getAbstractedFunctions(); iterator.hasNext(); ) {
|
for( Iterator<FunctionName> iterator = functions.getAbstractedFunctions(); iterator.hasNext(); ) {
|
||||||
FunctionName name = iterator.next();
|
FunctionName name = iterator.next();
|
||||||
//writeMethodSignature( name, null );
|
writeMethodSignature( name, FunctionType.Abstract, null );
|
||||||
}
|
}
|
||||||
|
|
||||||
JWebAssembly.LOGGER.fine( "scan finsih" );
|
JWebAssembly.LOGGER.fine( "scan finsih" );
|
||||||
@ -516,7 +517,7 @@ public class ModuleGenerator {
|
|||||||
private void writeMethodImpl( FunctionName name, WasmCodeBuilder codeBuilder ) throws WasmException, IOException {
|
private void writeMethodImpl( FunctionName name, WasmCodeBuilder codeBuilder ) throws WasmException, IOException {
|
||||||
writer.writeMethodStart( name, sourceFile );
|
writer.writeMethodStart( name, sourceFile );
|
||||||
functions.markAsWritten( name );
|
functions.markAsWritten( name );
|
||||||
writeMethodSignature( name, codeBuilder );
|
writeMethodSignature( name, FunctionType.Code, codeBuilder );
|
||||||
|
|
||||||
List<WasmInstruction> instructions = codeBuilder.getInstructions();
|
List<WasmInstruction> instructions = codeBuilder.getInstructions();
|
||||||
optimizer.optimze( instructions );
|
optimizer.optimze( instructions );
|
||||||
@ -600,6 +601,8 @@ public class ModuleGenerator {
|
|||||||
*
|
*
|
||||||
* @param name
|
* @param name
|
||||||
* the Java signature, typical method.getType();
|
* the Java signature, typical method.getType();
|
||||||
|
* @param funcType
|
||||||
|
* the type of function
|
||||||
* @param codeBuilder
|
* @param codeBuilder
|
||||||
* the calculated variables
|
* the calculated variables
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
@ -607,8 +610,8 @@ public class ModuleGenerator {
|
|||||||
* @throws WasmException
|
* @throws WasmException
|
||||||
* if some Java code can't converted
|
* if some Java code can't converted
|
||||||
*/
|
*/
|
||||||
private void writeMethodSignature( FunctionName name, WasmCodeBuilder codeBuilder ) throws IOException, WasmException {
|
private void writeMethodSignature( @Nonnull FunctionName name, @Nonnull FunctionType funcType, @Nullable WasmCodeBuilder codeBuilder ) throws IOException, WasmException {
|
||||||
writer.writeMethodParamStart( name );
|
writer.writeMethodParamStart( name, funcType );
|
||||||
int paramCount = 0;
|
int paramCount = 0;
|
||||||
if( functions.needThisParameter( name ) ) {
|
if( functions.needThisParameter( name ) ) {
|
||||||
StructType instanceType = types.valueOf( name.className );
|
StructType instanceType = types.valueOf( name.className );
|
||||||
|
@ -26,6 +26,7 @@ import javax.annotation.Nullable;
|
|||||||
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
|
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
||||||
|
import de.inetsoftware.jwebassembly.wasm.FunctionType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
|
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
||||||
@ -117,11 +118,13 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
*
|
*
|
||||||
* @param name
|
* @param name
|
||||||
* the function name
|
* the function name
|
||||||
|
* @param funcType
|
||||||
|
* the type of function
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if any I/O error occur
|
* if any I/O error occur
|
||||||
*/
|
*/
|
||||||
protected abstract void writeMethodParamStart( @Nonnull FunctionName name ) throws IOException;
|
protected abstract void writeMethodParamStart( @Nonnull FunctionName name, @Nonnull FunctionType funcType ) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a method parameter.
|
* Write a method parameter.
|
||||||
@ -149,13 +152,12 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
protected abstract void writeMethodParamFinish( @Nonnull FunctionName name ) throws IOException;
|
protected abstract void writeMethodParamFinish( @Nonnull FunctionName name ) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the method header.
|
* Start the writing of method/function code.
|
||||||
*
|
*
|
||||||
* @param name
|
* @param name
|
||||||
* the function name
|
* the function name
|
||||||
* @param sourceFile
|
* @param sourceFile
|
||||||
* the name of the source file
|
* the name of the source file
|
||||||
*
|
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if any I/O error occur
|
* if any I/O error occur
|
||||||
*/
|
*/
|
||||||
|
@ -37,6 +37,7 @@ import de.inetsoftware.jwebassembly.module.WasmOptions;
|
|||||||
import de.inetsoftware.jwebassembly.module.WasmTarget;
|
import de.inetsoftware.jwebassembly.module.WasmTarget;
|
||||||
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
||||||
|
import de.inetsoftware.jwebassembly.wasm.FunctionType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
|
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
||||||
@ -328,7 +329,7 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeMethodParamStart( @Nonnull FunctionName name ) throws IOException {
|
protected void writeMethodParamStart( @Nonnull FunctionName name, FunctionType funcType ) throws IOException {
|
||||||
typeOutput.setLength( 0 );
|
typeOutput.setLength( 0 );
|
||||||
methodParamNames.clear();
|
methodParamNames.clear();
|
||||||
}
|
}
|
||||||
|
30
src/de/inetsoftware/jwebassembly/wasm/FunctionType.java
Normal file
30
src/de/inetsoftware/jwebassembly/wasm/FunctionType.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 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.wasm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of function name.
|
||||||
|
*
|
||||||
|
* @author Volker Berlin
|
||||||
|
*/
|
||||||
|
public enum FunctionType {
|
||||||
|
/** imported function */
|
||||||
|
Import,
|
||||||
|
/** has real code */
|
||||||
|
Code,
|
||||||
|
/** abstract or interface, only used for indirrect call */
|
||||||
|
Abstract;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user