Skip to content

Commit 6ade59d

Browse files
VolkerVolker
authored andcommitted
fix structure problems with globals in text writer.
1 parent 658b859 commit 6ade59d

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
5656

5757
private Map<String, Function> functions = new LinkedHashMap<>();
5858

59+
private List<ValueType> locals;
60+
5961
private Map<String, Global> globals = new LinkedHashMap<>();
6062

6163
private Map<String, String> exports = new LinkedHashMap<>();
@@ -301,20 +303,21 @@ protected void writeMethodParam( String kind, ValueType valueType ) throws IOExc
301303
* {@inheritDoc}
302304
*/
303305
@Override
304-
protected void writeMethodParamFinish() throws IOException {
306+
protected void writeMethodParamFinish( List<ValueType> locals ) throws IOException {
305307
int typeId = functionTypes.indexOf( functionType );
306308
if( typeId < 0 ) {
307309
typeId = functionTypes.size();
308310
functionTypes.add( functionType );
309311
}
310312
function.typeId = typeId;
313+
this.locals = locals;
311314
}
312315

313316
/**
314317
* {@inheritDoc}
315318
*/
316319
@Override
317-
protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
320+
protected void writeMethodFinish() throws IOException {
318321
WasmOutputStream localsStream = new WasmOutputStream();
319322
localsStream.writeVaruint32( locals.size() );
320323
for( ValueType valueType : locals ) {

src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public class ModuleGenerator {
4141

4242
private final ModuleWriter writer;
4343

44-
private int paramCount;
45-
4644
private ValueType returnType;
4745

4846
private LocaleVariableManager localVariables = new LocaleVariableManager();
@@ -157,19 +155,19 @@ private void writeMethod( MethodInfo method ) throws WasmException {
157155
FunctionName name = new FunctionName( method );
158156
writeExport( name, method );
159157
writer.writeMethodStart( name );
160-
writeMethodSignature( method );
161158

162159
localVariables.reset();
163160
branchManager.reset();
164161

165162
byteCode = code.getByteCode();
166163
writeCode( byteCode, method.getConstantPool() );
167164
localVariables.calculate();
165+
writeMethodSignature( method );
168166

169167
for( WasmInstruction instruction : instructions ) {
170168
instruction.writeTo( writer );
171169
}
172-
writer.writeMethodFinish( localVariables.getLocalTypes( paramCount ) );
170+
writer.writeMethodFinish();
173171
}
174172
} catch( Exception ioex ) {
175173
int lineNumber = byteCode == null ? -1 : byteCode.getLineNumber();
@@ -215,19 +213,20 @@ private void writeMethodSignature( MethodInfo method ) throws IOException, WasmE
215213
int paramCount = 0;
216214
ValueType type = null;
217215
for( int i = 1; i < signature.length(); i++ ) {
218-
paramCount++;
219216
if( signature.charAt( i ) == ')' ) {
220-
this.paramCount = paramCount - 1;
221217
kind = "result";
222218
continue;
223219
}
220+
if( kind == "param" ) {
221+
paramCount++;
222+
}
224223
type = ValueType.getValueType( signature, i );
225224
if( type != null ) {
226225
writer.writeMethodParam( kind, type );
227226
}
228227
}
229228
this.returnType = type;
230-
writer.writeMethodParamFinish();
229+
writer.writeMethodParamFinish( localVariables.getLocalTypes( paramCount ) );
231230
}
232231

233232
/**

src/de/inetsoftware/jwebassembly/module/ModuleWriter.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,22 @@ protected void prepareFunction( FunctionName name ) {}
9898
/**
9999
* Finish the function parameter.
100100
*
101+
* @param locals
102+
* a list with types of local variables
103+
*
104+
*
101105
* @throws IOException
102106
* if any I/O error occur
103107
*/
104-
protected abstract void writeMethodParamFinish() throws IOException;
108+
protected abstract void writeMethodParamFinish( List<ValueType> locals ) throws IOException;
105109

106110
/**
107111
* Complete the method
108112
*
109-
* @param locals
110-
* a list with types of local variables
111-
*
112113
* @throws IOException
113114
* if any I/O error occur
114115
*/
115-
protected abstract void writeMethodFinish( List<ValueType> locals ) throws IOException;
116+
protected abstract void writeMethodFinish( ) throws IOException;
116117

117118
/**
118119
* Write a constant number value

src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,40 +98,41 @@ protected void writeExport( FunctionName name, String exportName ) throws IOExce
9898
*/
9999
@Override
100100
protected void writeMethodStart( FunctionName name ) throws IOException {
101-
newline( output );
102-
output.append( "(func $" );
103-
output.append( name.fullName );
104-
inset++;
105101
methodOutput.setLength( 0 );
102+
newline( methodOutput );
103+
methodOutput.append( "(func $" );
104+
methodOutput.append( name.fullName );
105+
inset++;
106106
}
107107

108108
/**
109109
* {@inheritDoc}
110110
*/
111111
@Override
112112
protected void writeMethodParam( String kind, ValueType valueType ) throws IOException {
113-
output.append( " (" ).append( kind ).append( ' ' ).append( valueType.toString() ).append( ')' );
113+
methodOutput.append( " (" ).append( kind ).append( ' ' ).append( valueType.toString() ).append( ')' );
114114
}
115115

116116
/**
117117
* {@inheritDoc}
118118
*/
119119
@Override
120-
protected void writeMethodParamFinish() throws IOException {
120+
protected void writeMethodParamFinish( List<ValueType> locals ) throws IOException {
121121
if( isImport ) {
122122
isImport = false;
123123
output.append( "))" );
124+
} else {
125+
for( ValueType valueType : locals ) {
126+
methodOutput.append( " (local " ).append( valueType.toString() ).append( ')' );
127+
}
124128
}
125129
}
126130

127131
/**
128132
* {@inheritDoc}
129133
*/
130134
@Override
131-
protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
132-
for( ValueType valueType : locals ) {
133-
output.append( " (local " ).append( valueType.toString() ).append( ')' );
134-
}
135+
protected void writeMethodFinish() throws IOException {
135136
output.append( methodOutput );
136137
inset--;
137138
newline( output );
@@ -174,11 +175,11 @@ protected void writeGlobalAccess( boolean load, FunctionName name, ConstantRef r
174175
// declare global variable if not already declared.
175176
output.append( "\n " );
176177
String type = ValueType.getValueType( ref.getType(), 0 ).toString();
177-
output.append( "(global $" ).append( name.fullName ).append( type ).append( ' ' ).append( type ).append( ".const 0)" );
178+
output.append( "(global $" ).append( name.fullName ).append( " (mut " ).append( type ).append( ") " ).append( type ).append( ".const 0)" );
178179
globals.add( name.fullName );
179180
}
180181
newline( methodOutput );
181-
methodOutput.append( load ? "get_local " : "set_local " ).append( name.fullName );
182+
methodOutput.append( load ? "get_global $" : "set_global $" ).append( name.fullName );
182183
}
183184

184185
/**

0 commit comments

Comments
 (0)