From 8c12f5a3c971294343db053916420330b69b7665 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sat, 1 Feb 2020 16:49:52 +0100 Subject: [PATCH] implement "instanceof", WIP --- .../module/JavaMethodWasmCodeBuilder.java | 8 ++++++-- .../module/WasmStructInstruction.java | 3 +++ .../jwebassembly/wasm/StructOperator.java | 3 ++- .../jwebassembly/runtime/RuntimeErrors.java | 15 ++++++++++++++- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java index f69faec..15fec3d 100644 --- a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.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. @@ -654,7 +654,11 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { addBlockInstruction( WasmBlockOperator.THROW, null, codePos, lineNumber ); break; //TODO case 192: // checkcast - //TODO case 193: // instanceof + case 193: // instanceof + idx = byteCode.readUnsignedShort(); + name = ((ConstantClass)constantPool.get( idx )).getName(); + addStructInstruction( StructOperator.INSTANCEOF, name, null, codePos, lineNumber ); + break; case 194: // monitorenter addBlockInstruction( WasmBlockOperator.MONITOR_ENTER, null, codePos, lineNumber ); break; diff --git a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java index 4aa84a7..ec9a420 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java @@ -208,6 +208,8 @@ class WasmStructInstruction extends WasmInstruction { return fieldName.getType(); case SET: return null; + case INSTANCEOF: + return ValueType.i32; // a boolean value default: throw new WasmException( "Unknown array operation: " + op, -1 ); } @@ -220,6 +222,7 @@ class WasmStructInstruction extends WasmInstruction { int getPopCount() { switch( op ) { case GET: + case INSTANCEOF: return 1; case SET: return 2; diff --git a/src/de/inetsoftware/jwebassembly/wasm/StructOperator.java b/src/de/inetsoftware/jwebassembly/wasm/StructOperator.java index e20b680..d086e98 100644 --- a/src/de/inetsoftware/jwebassembly/wasm/StructOperator.java +++ b/src/de/inetsoftware/jwebassembly/wasm/StructOperator.java @@ -1,5 +1,5 @@ /* - Copyright 2018 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. @@ -27,4 +27,5 @@ public enum StructOperator { GET, SET, NULL, + INSTANCEOF, } \ No newline at end of file diff --git a/test/de/inetsoftware/jwebassembly/runtime/RuntimeErrors.java b/test/de/inetsoftware/jwebassembly/runtime/RuntimeErrors.java index bf666b1..c50d4ac 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/RuntimeErrors.java +++ b/test/de/inetsoftware/jwebassembly/runtime/RuntimeErrors.java @@ -114,7 +114,6 @@ public class RuntimeErrors { } } - @Test public void interfaceCall() throws IOException { compileErrorTest( "Interface calls are not supported.", InterfaceMethod.class ); @@ -127,4 +126,18 @@ public class RuntimeErrors { return list.size(); } } + + + @Test + public void instanceofCall() throws IOException { + compileErrorTest( "Unknown operator: INSTANCEOF", InstanceofMethod.class ); + } + + static class InstanceofMethod { + @Export + static boolean runnable() { + Object obj = new Object(); + return obj instanceof Integer; + } + } }