diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java index f715944e9..28e48f318 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java @@ -23,6 +23,7 @@ import org.teavm.interop.Rename; import org.teavm.interop.Structure; import org.teavm.interop.Superclass; import org.teavm.interop.Sync; +import org.teavm.interop.Unmanaged; import org.teavm.jso.browser.TimerHandler; import org.teavm.platform.Platform; import org.teavm.platform.PlatformObject; @@ -392,6 +393,7 @@ public class TObject { protected void finalize() throws TThrowable { } + @Unmanaged public static TObject wrap(Object obj) { return (TObject) obj; } diff --git a/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java b/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java index 83400693b..67de85609 100644 --- a/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java +++ b/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java @@ -50,6 +50,7 @@ import org.teavm.backend.wasm.intrinsics.FloatIntrinsic; import org.teavm.backend.wasm.intrinsics.FunctionIntrinsic; import org.teavm.backend.wasm.intrinsics.GCIntrinsic; import org.teavm.backend.wasm.intrinsics.MutatorIntrinsic; +import org.teavm.backend.wasm.intrinsics.ObjectIntrinsic; import org.teavm.backend.wasm.intrinsics.PlatformClassIntrinsic; import org.teavm.backend.wasm.intrinsics.PlatformClassMetadataIntrinsic; import org.teavm.backend.wasm.intrinsics.PlatformIntrinsic; @@ -338,6 +339,7 @@ public class WasmTarget implements TeaVMTarget, TeaVMWasmHost { context.addIntrinsic(new RuntimeClassIntrinsic()); context.addIntrinsic(new FloatIntrinsic()); context.addIntrinsic(new DoubleIntrinsic()); + context.addIntrinsic(new ObjectIntrinsic()); context.addGenerator(new ArrayGenerator()); IntrinsicFactoryContext intrinsicFactoryContext = new IntrinsicFactoryContext(); diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java b/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java index 340385a70..975fa98ac 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java @@ -120,7 +120,6 @@ import org.teavm.runtime.RuntimeClass; import org.teavm.runtime.ShadowStack; class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { - private static FieldReference tagField = new FieldReference(RuntimeClass.class.getName(), "tag"); private static final int SWITCH_TABLE_THRESHOLD = 256; private WasmGenerationContext context; private WasmClassGenerator classGenerator; @@ -1267,6 +1266,8 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { @Override public void visit(InstanceOfExpr expr) { + classGenerator.getClassPointer(expr.getType()); + accept(expr.getExpr()); WasmBlock block = new WasmBlock(false); @@ -1279,14 +1280,15 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { WasmBranch ifNull = new WasmBranch(new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.EQ, new WasmGetLocal(objectVar), new WasmInt32Constant(0)), block); ifNull.setResult(new WasmInt32Constant(0)); - block.getBody().add(ifNull); + block.getBody().add(new WasmDrop(ifNull)); WasmCall supertypeCall = new WasmCall(context.names.forSupertypeFunction(expr.getType())); - int tagOffset = classGenerator.getFieldOffset(tagField); - WasmExpression classRef = new WasmLoadInt32(4, result, WasmInt32Subtype.INT32, tagOffset); + WasmExpression classRef = new WasmLoadInt32(4, result, WasmInt32Subtype.INT32); classRef = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.SHL, classRef, new WasmInt32Constant(3)); supertypeCall.getArguments().add(classRef); block.getBody().add(supertypeCall); + + result = block; } @Override diff --git a/core/src/main/java/org/teavm/backend/wasm/generators/ArrayGenerator.java b/core/src/main/java/org/teavm/backend/wasm/generators/ArrayGenerator.java index 3c255cf8b..ca5b4ac7f 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generators/ArrayGenerator.java +++ b/core/src/main/java/org/teavm/backend/wasm/generators/ArrayGenerator.java @@ -32,6 +32,7 @@ import org.teavm.backend.wasm.model.expression.WasmIntBinary; import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation; import org.teavm.backend.wasm.model.expression.WasmIntType; import org.teavm.backend.wasm.model.expression.WasmLoadFloat32; +import org.teavm.backend.wasm.model.expression.WasmLoadFloat64; import org.teavm.backend.wasm.model.expression.WasmLoadInt32; import org.teavm.backend.wasm.model.expression.WasmLoadInt64; import org.teavm.backend.wasm.model.expression.WasmReturn; @@ -147,7 +148,7 @@ public class ArrayGenerator implements WasmMethodGenerator { call.getArguments().add(new WasmLoadFloat32(2, offset, baseAddr)); break; case DOUBLE: - call.getArguments().add(new WasmLoadFloat32(3, offset, baseAddr)); + call.getArguments().add(new WasmLoadFloat64(3, offset, baseAddr)); break; } diff --git a/core/src/main/java/org/teavm/backend/wasm/intrinsics/ObjectIntrinsic.java b/core/src/main/java/org/teavm/backend/wasm/intrinsics/ObjectIntrinsic.java new file mode 100644 index 000000000..bbe11b78f --- /dev/null +++ b/core/src/main/java/org/teavm/backend/wasm/intrinsics/ObjectIntrinsic.java @@ -0,0 +1,46 @@ +/* + * Copyright 2018 Alexey Andreev. + * + * 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 org.teavm.backend.wasm.intrinsics; + +import org.teavm.ast.InvocationExpr; +import org.teavm.backend.wasm.model.expression.WasmExpression; +import org.teavm.model.MethodReference; + +public class ObjectIntrinsic implements WasmIntrinsic { + @Override + public boolean isApplicable(MethodReference methodReference) { + if (!methodReference.getClassName().equals("java.lang.Object")) { + return false; + } + + switch (methodReference.getName()) { + case "wrap": + return true; + default: + return false; + } + } + + @Override + public WasmExpression apply(InvocationExpr invocation, WasmIntrinsicManager manager) { + switch (invocation.getMethod().getName()) { + case "wrap": + return manager.generate(invocation.getArguments().get(0)); + default: + throw new AssertionError(); + } + } +} diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java index f8e9fc918..ecc482c95 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java @@ -658,7 +658,7 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor { ? functionIndexes.get(expression.getFunctionName()) : importedIndexes.get(expression.getFunctionName()); if (functionIndex == null) { - writer.writeByte(0x0A); + writer.writeByte(0x00); return; }