Add handling of global section.

This commit is contained in:
Volker 2018-08-13 17:39:14 +02:00
parent 758557f76b
commit 2c6fb9aae0
2 changed files with 70 additions and 0 deletions

View File

@ -55,6 +55,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
private Map<String, Function> functions = new LinkedHashMap<>();
private Map<String, Global> globals = new LinkedHashMap<>();
private Map<String, String> exports = new LinkedHashMap<>();
private Map<String, ImportFunction> imports = new LinkedHashMap<>();
@ -86,6 +88,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
writeTypeSection();
writeImportSection();
writeFunctionSection();
writeGlobalSection();
writeExportSection();
writeCodeSection();
@ -163,6 +166,27 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
}
}
/**
* Write the global section to the output. This section contains a declaring of global (static) variables.
*
* @throws IOException
* if any I/O error occur
*/
private void writeGlobalSection() throws IOException {
int count = globals.size();
if( count > 0 ) {
WasmOutputStream stream = new WasmOutputStream();
stream.writeVaruint32( count );
for( Global var : globals.values() ) {
stream.write( var.type.getCode() );
stream.write( var.mutability ? 1 : 0 );
writeConst( stream, 0, var.type );
stream.writeOpCode( END );
}
wasm.writeSection( SectionType.Global, stream, null );
}
}
/**
* Write the export section to the output. This section contains a mapping from the external index to the type signature index.
*
@ -307,6 +331,22 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
*/
@Override
protected void writeConst( Number value, ValueType valueType ) throws IOException {
writeConst( codeStream, value, valueType );
}
/**
* Write a constant number value
*
* @param codeStream
* the target stream
* @param value
* the value
* @param valueType
* the data type of the number
* @throws IOException
* if any I/O error occur
*/
private static void writeConst( WasmOutputStream codeStream, Number value, ValueType valueType ) throws IOException {
switch( valueType ) {
case i32:
codeStream.writeOpCode( I32_CONST );

View File

@ -0,0 +1,30 @@
/*
* 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.binary;
import de.inetsoftware.jwebassembly.module.ValueType;
/**
* An entry in the global section of the WebAssembly.
*
* @author Volker Berlin
*/
class Global {
ValueType type;
boolean mutability;
}