From 43b41d4fad056f5c736fe9280d9775c56887a6d1 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sat, 25 Jan 2020 23:42:22 +0100 Subject: [PATCH] interface call, WIP --- .../module/JavaMethodWasmCodeBuilder.java | 5 +- .../module/WasmCallInterfaceInstruction.java | 63 +++++++++++++++++++ .../jwebassembly/module/WasmCodeBuilder.java | 15 +++++ .../jwebassembly/module/WasmInstruction.java | 4 +- 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/de/inetsoftware/jwebassembly/module/WasmCallInterfaceInstruction.java diff --git a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java index 097d45d..f69faec 100644 --- a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java @@ -577,6 +577,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { case 182: // invokevirtual case 183: // invokespecial, invoke a constructor case 184: // invokestatic + case 185: // invokeinterface idx = byteCode.readUnsignedShort(); ref = (ConstantRef)constantPool.get( idx ); FunctionName funcName = new FunctionName( ref ); @@ -591,9 +592,11 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { case 184: addCallInstruction( funcName, codePos, lineNumber ); break; + case 185: + addCallInterfaceInstruction( funcName, codePos, lineNumber ); + break; } break; - //TODO case 185: // invokeinterface case 186: // invokedynamic idx = byteCode.readUnsignedShort(); ConstantInvokeDynamic dynamic = (ConstantInvokeDynamic)constantPool.get( idx ); diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallInterfaceInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallInterfaceInstruction.java new file mode 100644 index 0000000..9e2d36e --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallInterfaceInstruction.java @@ -0,0 +1,63 @@ +/* + Copyright 2020 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.module; + +import java.io.IOException; + +import javax.annotation.Nonnull; + +import de.inetsoftware.jwebassembly.WasmException; + +/** + * WasmInstruction for a function call. + * + * @author Volker Berlin + * + */ +class WasmCallInterfaceInstruction extends WasmCallInstruction { + + /** + * Create an instance of a function call instruction + * + * @param name + * the function name that should be called + * @param javaCodePos + * the code position/offset in the Java method + * @param lineNumber + * the line number in the Java source code + * @param types + * the type manager + */ + WasmCallInterfaceInstruction( FunctionName name, int javaCodePos, int lineNumber, TypeManager types ) { + super( name, javaCodePos, lineNumber, types, true ); + } + + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.CallInterface; + } + + /** + * {@inheritDoc} + */ + public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { + throw new WasmException( "Interface calls not supported", getLineNumber() ); + } +} diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index b3aa0c3..363db25 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -502,6 +502,21 @@ public abstract class WasmCodeBuilder { } } + /** + * Add interface function call + * @param name + * the function name that should be called + * @param javaCodePos + * the code position/offset in the Java method + * @param lineNumber + * the line number in the Java source code + */ + protected void addCallInterfaceInstruction( FunctionName name, int javaCodePos, int lineNumber ) { + WasmCallInterfaceInstruction interfaceCall = new WasmCallInterfaceInstruction( name, javaCodePos, lineNumber, types ); + + instructions.add( interfaceCall ); + } + /** * Add a block operation. * diff --git a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java index a8cbdb3..f857d55 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java @@ -1,5 +1,5 @@ /* - Copyright 2018 - 2019 Volker Berlin (i-net software) + Copyright 2018 - 2020 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. @@ -35,7 +35,7 @@ abstract class WasmInstruction { * Type of instruction to faster differ as with instanceof. */ static enum Type { - Const, Convert, Local, Global, Table, Memory, Block, Numeric, Nop, Call, CallIndirect, Array, Struct, Dup, DupThis; + Const, Convert, Local, Global, Table, Memory, Block, Numeric, Nop, Call, CallIndirect, CallInterface, Array, Struct, Dup, DupThis; } private int javaCodePos;