From 2c6fb9aae09af48e0a35e37ce7ba0fc07d042712 Mon Sep 17 00:00:00 2001 From: Volker Date: Mon, 13 Aug 2018 17:39:14 +0200 Subject: [PATCH] Add handling of global section. --- .../binary/BinaryModuleWriter.java | 40 +++++++++++++++++++ .../jwebassembly/binary/Global.java | 30 ++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/de/inetsoftware/jwebassembly/binary/Global.java diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 8326545..4c4cc7a 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -55,6 +55,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod private Map functions = new LinkedHashMap<>(); + private Map globals = new LinkedHashMap<>(); + private Map exports = new LinkedHashMap<>(); private Map 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 ); diff --git a/src/de/inetsoftware/jwebassembly/binary/Global.java b/src/de/inetsoftware/jwebassembly/binary/Global.java new file mode 100644 index 0000000..4835bd2 --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/binary/Global.java @@ -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; +}