From 481bffe31ac78842cd17f2b766afd83e7e4c872e Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Wed, 17 Apr 2019 18:26:52 +0200 Subject: [PATCH] Fix the type of array.get operation. Before it was ever i32 from array index. --- .../module/JavaMethodWasmCodeBuilder.java | 14 ++++++++------ .../jwebassembly/module/LocaleVariableManager.java | 1 - 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java index fe8e876..d8636b0 100644 --- a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java @@ -186,7 +186,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { addArrayInstruction( ArrayOperator.GET, ValueType.f64, codePos, lineNumber ); break; case 50: // aaload - AnyType storeType = findPreviousPushInstructionPushValueType(); + AnyType storeType = findValueTypeFromStack( 2 ); addArrayInstruction( ArrayOperator.GET, storeType, codePos, lineNumber ); break; case 51: // baload @@ -244,7 +244,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { if( branchManager.isCatch( codePos ) ) { storeType = ValueType.anyref; // for the catch there are no previous instructions } else { - storeType = findPreviousPushInstructionPushValueType(); + storeType = findValueTypeFromStack( 1 ); } addLoadStoreInstruction( storeType, false, op - 75, codePos, lineNumber ); break; @@ -261,7 +261,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { addArrayInstruction( ArrayOperator.SET, ValueType.f64, codePos, lineNumber ); break; case 83: // aastore - storeType = findPreviousPushInstructionPushValueType(); + storeType = findValueTypeFromStack( 1 ); addArrayInstruction( ArrayOperator.SET, storeType, codePos, lineNumber ); break; case 84: // bastore @@ -279,7 +279,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { break; case 89: // dup: duplicate the value on top of the stack case 92: // dup2 - storeType = findPreviousPushInstructionPushValueType(); + storeType = findValueTypeFromStack( 1 ); addCallInstruction( new SyntheticFunctionName( "dup" + storeType, "local.get 0 local.get 0 return", storeType, null, storeType, storeType ), codePos, lineNumber ); break; @@ -768,17 +768,19 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { /** * We need the value type from the stack. * + * @param count + * the count of values on the stack back. 1 means the last value. 2 means the penultimate value. * @return the type of the last push value */ @Nonnull - private AnyType findPreviousPushInstructionPushValueType() { + private AnyType findValueTypeFromStack( int count ) { int valueCount = 0; List instructions = getInstructions(); for( int i = instructions.size() - 1; i >= 0; i-- ) { WasmInstruction instr = instructions.get( i ); AnyType valueType = instr.getPushValueType(); if( valueType != null ) { - if( ++valueCount == 1 ) { + if( ++valueCount == count ) { return valueType; } } diff --git a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java index 4c8b090..91122ea 100644 --- a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java +++ b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java @@ -133,7 +133,6 @@ class LocaleVariableManager { names.clear(); for( int i = 0; i < size; i++ ) { Variable var = variables[i]; - var.valueType = null; // TODO temporary hack var.name = findUniqueVarName( var.name ); }