mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
simplify the internal API
This commit is contained in:
parent
f71747a87f
commit
c6897d8bf4
@ -97,7 +97,7 @@ public class Code {
|
|||||||
}
|
}
|
||||||
AttributeInfo data = attributes.get( "LocalVariableTable" );
|
AttributeInfo data = attributes.get( "LocalVariableTable" );
|
||||||
if( data != null ) {
|
if( data != null ) {
|
||||||
localVariableTable = new LocalVariableTable( maxLocals );
|
localVariableTable = new LocalVariableTable( maxLocals, constantPool );
|
||||||
localVariableTable.read( data.getDataInputStream(), true );
|
localVariableTable.read( data.getDataInputStream(), true );
|
||||||
data = attributes.get( "LocalVariableTypeTable" );
|
data = attributes.get( "LocalVariableTypeTable" );
|
||||||
if( data != null ) {
|
if( data != null ) {
|
||||||
|
@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with 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 {
|
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;
|
private boolean declared;
|
||||||
|
|
||||||
@ -44,16 +46,21 @@ public class LocalVariable {
|
|||||||
*
|
*
|
||||||
* @param input
|
* @param input
|
||||||
* the stream of the class
|
* the stream of the class
|
||||||
|
* @param position
|
||||||
|
* the position in the LocalVariableTable
|
||||||
|
* @param constantPool
|
||||||
|
* Reference to the current ConstantPool
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if any I/O error occurs.
|
* 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();
|
start_pc = input.readUnsignedShort();
|
||||||
length = input.readUnsignedShort();
|
length = input.readUnsignedShort();
|
||||||
name_index = input.readUnsignedShort();
|
name_index = input.readUnsignedShort();
|
||||||
descriptor_index = input.readUnsignedShort();
|
descriptor_index = input.readUnsignedShort();
|
||||||
index = input.readUnsignedShort();
|
index = input.readUnsignedShort();
|
||||||
this.position = position;
|
this.position = position;
|
||||||
|
this.constantPool = constantPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -76,9 +83,10 @@ public class LocalVariable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the name of the variable
|
* 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 );
|
return (String)constantPool.get( name_index );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,6 +27,8 @@ import javax.annotation.Nullable;
|
|||||||
*/
|
*/
|
||||||
public class LocalVariableTable {
|
public class LocalVariableTable {
|
||||||
|
|
||||||
|
private final ConstantPool constantPool;
|
||||||
|
|
||||||
private LocalVariable[] tablePosition;
|
private LocalVariable[] tablePosition;
|
||||||
|
|
||||||
private LocalVariable[] table;
|
private LocalVariable[] table;
|
||||||
@ -34,11 +36,17 @@ public class LocalVariableTable {
|
|||||||
private int count;
|
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];
|
table = new LocalVariable[maxLocals];
|
||||||
tablePosition = new LocalVariable[maxLocals];
|
tablePosition = new LocalVariable[maxLocals];
|
||||||
|
this.constantPool = constantPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,7 +68,7 @@ public class LocalVariableTable {
|
|||||||
}
|
}
|
||||||
boolean[] wasSet = new boolean[table.length];
|
boolean[] wasSet = new boolean[table.length];
|
||||||
for( int i = 0; i < count; i++ ) {
|
for( int i = 0; i < count; i++ ) {
|
||||||
LocalVariable var = new LocalVariable( input, i );
|
LocalVariable var = new LocalVariable( input, i, constantPool );
|
||||||
int idx = var.getIndex();
|
int idx = var.getIndex();
|
||||||
if( withPositions ) {
|
if( withPositions ) {
|
||||||
tablePosition[i] = var;
|
tablePosition[i] = var;
|
||||||
@ -105,6 +113,7 @@ public class LocalVariableTable {
|
|||||||
*
|
*
|
||||||
* @param idx
|
* @param idx
|
||||||
* the index in the memory
|
* the index in the memory
|
||||||
|
* @return the LocalVariable
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public LocalVariable get( int idx ) {
|
public LocalVariable get( int idx ) {
|
||||||
@ -116,14 +125,12 @@ public class LocalVariableTable {
|
|||||||
*
|
*
|
||||||
* @param name
|
* @param name
|
||||||
* needed for evaluate the name.
|
* needed for evaluate the name.
|
||||||
* @param constantPool
|
|
||||||
* current constant pool
|
|
||||||
* @return the LocalVariable or null
|
* @return the LocalVariable or null
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public LocalVariable get( String name, ConstantPool constantPool ) {
|
public LocalVariable get( String name ) {
|
||||||
for( int i=0; i<table.length; i++ ){
|
for( int i=0; i<table.length; i++ ){
|
||||||
if( name.equals( table[i].getName( constantPool ) )) {
|
if( name.equals( table[i].getName() )) {
|
||||||
return table[i];
|
return table[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -127,7 +127,7 @@ public class ModuleGenerator {
|
|||||||
String impoarModule = (String)annotationValues.get( "module" );
|
String impoarModule = (String)annotationValues.get( "module" );
|
||||||
String importName = (String)annotationValues.get( "name" );
|
String importName = (String)annotationValues.get( "name" );
|
||||||
writer.prepareImport( name, impoarModule, importName );
|
writer.prepareImport( name, impoarModule, importName );
|
||||||
writeMethodSignature( method.getType(), method, null, null );
|
writeMethodSignature( method.getType(), null, null );
|
||||||
} else {
|
} else {
|
||||||
writer.prepareFunction( name );
|
writer.prepareFunction( name );
|
||||||
}
|
}
|
||||||
@ -173,7 +173,7 @@ public class ModuleGenerator {
|
|||||||
writeExport( name, method );
|
writeExport( name, method );
|
||||||
writer.writeMethodStart( name );
|
writer.writeMethodStart( name );
|
||||||
|
|
||||||
writeMethodSignature( signature, method, code.getLocalVariableTable(), codeBuilder );
|
writeMethodSignature( signature, code.getLocalVariableTable(), codeBuilder );
|
||||||
|
|
||||||
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
|
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
|
||||||
instruction.writeTo( writer );
|
instruction.writeTo( writer );
|
||||||
@ -212,8 +212,6 @@ public class ModuleGenerator {
|
|||||||
*
|
*
|
||||||
* @param signature
|
* @param signature
|
||||||
* the Java signature, typical method.getType();
|
* the Java signature, typical method.getType();
|
||||||
* @param method
|
|
||||||
* the method
|
|
||||||
* @param variables
|
* @param variables
|
||||||
* Java variable table with names of the variables for debugging
|
* Java variable table with names of the variables for debugging
|
||||||
* @param codeBuilder
|
* @param codeBuilder
|
||||||
@ -223,7 +221,7 @@ public class ModuleGenerator {
|
|||||||
* @throws WasmException
|
* @throws WasmException
|
||||||
* if some Java code can't converted
|
* 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";
|
String kind = "param";
|
||||||
int paramCount = 0;
|
int paramCount = 0;
|
||||||
ValueType type = null;
|
ValueType type = null;
|
||||||
@ -235,7 +233,7 @@ public class ModuleGenerator {
|
|||||||
String name = null;
|
String name = null;
|
||||||
if( kind == "param" ) {
|
if( kind == "param" ) {
|
||||||
if( variables != null ) {
|
if( variables != null ) {
|
||||||
name = variables.getPosition( paramCount ).getName( method.getConstantPool() );
|
name = variables.getPosition( paramCount ).getName();
|
||||||
}
|
}
|
||||||
paramCount++;
|
paramCount++;
|
||||||
}
|
}
|
||||||
@ -250,7 +248,7 @@ public class ModuleGenerator {
|
|||||||
type = localTypes.get( i );
|
type = localTypes.get( i );
|
||||||
String name = null;
|
String name = null;
|
||||||
if( variables != null ) {
|
if( variables != null ) {
|
||||||
name = variables.getPosition( paramCount ).getName( method.getConstantPool() );
|
name = variables.getPosition( paramCount ).getName();
|
||||||
}
|
}
|
||||||
writer.writeMethodParam( "local", type, name );
|
writer.writeMethodParam( "local", type, name );
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user