simplify the internal API

This commit is contained in:
Volker Berlin 2018-11-18 13:22:45 +01:00
parent f71747a87f
commit c6897d8bf4
4 changed files with 38 additions and 25 deletions

View File

@ -97,7 +97,7 @@ public class Code {
}
AttributeInfo data = attributes.get( "LocalVariableTable" );
if( data != null ) {
localVariableTable = new LocalVariableTable( maxLocals );
localVariableTable = new LocalVariableTable( maxLocals, constantPool );
localVariableTable.read( data.getDataInputStream(), true );
data = attributes.get( "LocalVariableTypeTable" );
if( data != null ) {

View File

@ -1,5 +1,5 @@
/*
Copyright 2011 - 2017 Volker Berlin (i-net software)
Copyright 2011 - 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.
@ -24,17 +24,19 @@ import java.io.IOException;
*/
public class LocalVariable {
private int start_pc;
private final int start_pc;
private int length;
private final int length;
private int name_index;
private final int name_index;
private int descriptor_index;
private final int descriptor_index;
private int index;
private final int index;
private int position;
private final int position;
private final ConstantPool constantPool;
private boolean declared;
@ -44,16 +46,21 @@ public class LocalVariable {
*
* @param input
* the stream of the class
* @param position
* the position in the LocalVariableTable
* @param constantPool
* Reference to the current ConstantPool
* @throws IOException
* if any I/O error occurs.
*/
LocalVariable( DataInputStream input, int position ) throws IOException {
LocalVariable( DataInputStream input, int position, ConstantPool constantPool ) throws IOException {
start_pc = input.readUnsignedShort();
length = input.readUnsignedShort();
name_index = input.readUnsignedShort();
descriptor_index = input.readUnsignedShort();
index = input.readUnsignedShort();
this.position = position;
this.constantPool = constantPool;
}
/**
@ -76,9 +83,10 @@ public class LocalVariable {
/**
* Get the name of the variable
* @param constantPool ConstantPool of the current class
*
* @return the name
*/
public String getName( ConstantPool constantPool ) {
public String getName() {
return (String)constantPool.get( name_index );
}

View File

@ -27,6 +27,8 @@ import javax.annotation.Nullable;
*/
public class LocalVariableTable {
private final ConstantPool constantPool;
private LocalVariable[] tablePosition;
private LocalVariable[] table;
@ -34,11 +36,17 @@ public class LocalVariableTable {
private int count;
/**
* @param maxLocals the count of local variables in the memory
* Create a new instance of the code attribute "LocalVariableTable".
*
* @param maxLocals
* the count of local variables in the memory
* @param constantPool
* Reference to the current ConstantPool
*/
LocalVariableTable( int maxLocals ) {
LocalVariableTable( int maxLocals, ConstantPool constantPool ) {
table = new LocalVariable[maxLocals];
tablePosition = new LocalVariable[maxLocals];
this.constantPool = constantPool;
}
/**
@ -60,7 +68,7 @@ public class LocalVariableTable {
}
boolean[] wasSet = new boolean[table.length];
for( int i = 0; i < count; i++ ) {
LocalVariable var = new LocalVariable( input, i );
LocalVariable var = new LocalVariable( input, i, constantPool );
int idx = var.getIndex();
if( withPositions ) {
tablePosition[i] = var;
@ -105,6 +113,7 @@ public class LocalVariableTable {
*
* @param idx
* the index in the memory
* @return the LocalVariable
*/
@Nonnull
public LocalVariable get( int idx ) {
@ -116,14 +125,12 @@ public class LocalVariableTable {
*
* @param name
* needed for evaluate the name.
* @param constantPool
* current constant pool
* @return the LocalVariable or null
*/
@Nullable
public LocalVariable get( String name, ConstantPool constantPool ) {
public LocalVariable get( String name ) {
for( int i=0; i<table.length; i++ ){
if( name.equals( table[i].getName( constantPool ) )) {
if( name.equals( table[i].getName() )) {
return table[i];
}
}

View File

@ -127,7 +127,7 @@ public class ModuleGenerator {
String impoarModule = (String)annotationValues.get( "module" );
String importName = (String)annotationValues.get( "name" );
writer.prepareImport( name, impoarModule, importName );
writeMethodSignature( method.getType(), method, null, null );
writeMethodSignature( method.getType(), null, null );
} else {
writer.prepareFunction( name );
}
@ -173,7 +173,7 @@ public class ModuleGenerator {
writeExport( name, method );
writer.writeMethodStart( name );
writeMethodSignature( signature, method, code.getLocalVariableTable(), codeBuilder );
writeMethodSignature( signature, code.getLocalVariableTable(), codeBuilder );
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
instruction.writeTo( writer );
@ -212,8 +212,6 @@ public class ModuleGenerator {
*
* @param signature
* the Java signature, typical method.getType();
* @param method
* the method
* @param variables
* Java variable table with names of the variables for debugging
* @param codeBuilder
@ -223,7 +221,7 @@ public class ModuleGenerator {
* @throws WasmException
* if some Java code can't converted
*/
private void writeMethodSignature( String signature, MethodInfo method, @Nullable LocalVariableTable variables, WasmCodeBuilder codeBuilder ) throws IOException, WasmException {
private void writeMethodSignature( String signature, @Nullable LocalVariableTable variables, WasmCodeBuilder codeBuilder ) throws IOException, WasmException {
String kind = "param";
int paramCount = 0;
ValueType type = null;
@ -235,7 +233,7 @@ public class ModuleGenerator {
String name = null;
if( kind == "param" ) {
if( variables != null ) {
name = variables.getPosition( paramCount ).getName( method.getConstantPool() );
name = variables.getPosition( paramCount ).getName();
}
paramCount++;
}
@ -250,7 +248,7 @@ public class ModuleGenerator {
type = localTypes.get( i );
String name = null;
if( variables != null ) {
name = variables.getPosition( paramCount ).getName( method.getConstantPool() );
name = variables.getPosition( paramCount ).getName();
}
writer.writeMethodParam( "local", type, name );
}