mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 02:44:47 +01:00
add TypeManager
This commit is contained in:
parent
6196648cc0
commit
bfc6cbb3f0
@ -26,6 +26,7 @@ import de.inetsoftware.classparser.ConstantClass;
|
||||
import de.inetsoftware.classparser.ConstantPool;
|
||||
import de.inetsoftware.classparser.ConstantRef;
|
||||
import de.inetsoftware.jwebassembly.WasmException;
|
||||
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
|
||||
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.StorageType;
|
||||
@ -545,7 +546,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
|
||||
//TODO case 186: // invokedynamic
|
||||
case 187: // new
|
||||
String name = ((ConstantClass)constantPool.get( byteCode.readUnsignedShort() )).getName();
|
||||
StorageType storageType = ValueType.anyref;
|
||||
StorageType storageType = new StructType(name);
|
||||
addStructInstruction( StructOperator.NEW_DEFAULT, storageType, codePos );
|
||||
break;
|
||||
case 188: // newarray
|
||||
|
@ -58,6 +58,8 @@ public class ModuleGenerator {
|
||||
|
||||
private FunctionManager functions = new FunctionManager();
|
||||
|
||||
private TypeManager types = new TypeManager();
|
||||
|
||||
/**
|
||||
* Create a new generator.
|
||||
*
|
||||
@ -237,6 +239,8 @@ public class ModuleGenerator {
|
||||
case Call:
|
||||
functions.functionCall( ((WasmCallInstruction)instruction).getFunctionName() );
|
||||
break;
|
||||
case Struct:
|
||||
types.useType( ((WasmStructInstruction)instruction).getStorageType() );
|
||||
default:
|
||||
}
|
||||
instruction.writeTo( writer );
|
||||
|
80
src/de/inetsoftware/jwebassembly/module/TypeManager.java
Normal file
80
src/de/inetsoftware/jwebassembly/module/TypeManager.java
Normal file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
Copyright 2018 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.util.HashMap;
|
||||
|
||||
import de.inetsoftware.jwebassembly.wasm.StorageType;
|
||||
|
||||
/**
|
||||
* Manage the written and to write types (classes)
|
||||
*
|
||||
* @author Volker Berlin
|
||||
*/
|
||||
class TypeManager {
|
||||
|
||||
private final HashMap<String, StructType> map = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Use the type in the output.
|
||||
*
|
||||
* @param storageType
|
||||
* the reference to a type
|
||||
*/
|
||||
void useType( StorageType storageType ) {
|
||||
if( storageType instanceof StructType ) {
|
||||
StructType type = (StructType)storageType;
|
||||
StructType existingType = map.get( type.name );
|
||||
if( existingType == null ) {
|
||||
type.code = map.size();
|
||||
map.put( type.name, type );
|
||||
} else {
|
||||
type.code = existingType.code;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A reference to a type.
|
||||
*
|
||||
* @author Volker Berlin
|
||||
*/
|
||||
static class StructType implements StorageType {
|
||||
|
||||
private final String name;
|
||||
|
||||
private int code;
|
||||
|
||||
/**
|
||||
* Create a reference to type
|
||||
*
|
||||
* @param name
|
||||
* the Java class name
|
||||
*/
|
||||
StructType( String name ) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
@ -22,8 +22,6 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import de.inetsoftware.jwebassembly.WasmException;
|
||||
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
|
||||
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.StorageType;
|
||||
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||
@ -56,6 +54,15 @@ class WasmStructInstruction extends WasmInstruction {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the storage type of this instruction.
|
||||
*
|
||||
* @return the type
|
||||
*/
|
||||
StorageType getStorageType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user