Skip to content

Commit 2c6fb9a

Browse files
VolkerVolker
authored andcommitted
Add handling of global section.
1 parent 758557f commit 2c6fb9a

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
5555

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

58+
private Map<String, Global> globals = new LinkedHashMap<>();
59+
5860
private Map<String, String> exports = new LinkedHashMap<>();
5961

6062
private Map<String, ImportFunction> imports = new LinkedHashMap<>();
@@ -86,6 +88,7 @@ public void close() throws IOException {
8688
writeTypeSection();
8789
writeImportSection();
8890
writeFunctionSection();
91+
writeGlobalSection();
8992
writeExportSection();
9093
writeCodeSection();
9194

@@ -163,6 +166,27 @@ private void writeFunctionSection() throws IOException {
163166
}
164167
}
165168

169+
/**
170+
* Write the global section to the output. This section contains a declaring of global (static) variables.
171+
*
172+
* @throws IOException
173+
* if any I/O error occur
174+
*/
175+
private void writeGlobalSection() throws IOException {
176+
int count = globals.size();
177+
if( count > 0 ) {
178+
WasmOutputStream stream = new WasmOutputStream();
179+
stream.writeVaruint32( count );
180+
for( Global var : globals.values() ) {
181+
stream.write( var.type.getCode() );
182+
stream.write( var.mutability ? 1 : 0 );
183+
writeConst( stream, 0, var.type );
184+
stream.writeOpCode( END );
185+
}
186+
wasm.writeSection( SectionType.Global, stream, null );
187+
}
188+
}
189+
166190
/**
167191
* Write the export section to the output. This section contains a mapping from the external index to the type signature index.
168192
*
@@ -307,6 +331,22 @@ protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
307331
*/
308332
@Override
309333
protected void writeConst( Number value, ValueType valueType ) throws IOException {
334+
writeConst( codeStream, value, valueType );
335+
}
336+
337+
/**
338+
* Write a constant number value
339+
*
340+
* @param codeStream
341+
* the target stream
342+
* @param value
343+
* the value
344+
* @param valueType
345+
* the data type of the number
346+
* @throws IOException
347+
* if any I/O error occur
348+
*/
349+
private static void writeConst( WasmOutputStream codeStream, Number value, ValueType valueType ) throws IOException {
310350
switch( valueType ) {
311351
case i32:
312352
codeStream.writeOpCode( I32_CONST );
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2018 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.binary;
17+
18+
import de.inetsoftware.jwebassembly.module.ValueType;
19+
20+
/**
21+
* An entry in the global section of the WebAssembly.
22+
*
23+
* @author Volker Berlin
24+
*/
25+
class Global {
26+
27+
ValueType type;
28+
29+
boolean mutability;
30+
}

0 commit comments

Comments
 (0)