Skip to content

Commit

Permalink
Overlays for Reference.refersTo
Browse files Browse the repository at this point in the history
  • Loading branch information
rakachan committed Sep 28, 2021
1 parent 0820ac2 commit 83464af
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 7 deletions.
33 changes: 33 additions & 0 deletions espresso/mx.espresso/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,39 @@
"checkPackagePrefix": False, # java.lang.ref.PublicFinalReference
},

"com.oracle.truffle.espresso.jdk8": {
"subDir": "src",
"sourceDirs": ["src"],
"overlayTarget": "com.oracle.truffle.espresso",
"checkPackagePrefix": "false",
"checkstyle": "com.oracle.truffle.espresso",
"javaCompliance": "8",
},

"com.oracle.truffle.espresso.jdk11": {
"subDir": "src",
"sourceDirs": ["src"],
"overlayTarget": "com.oracle.truffle.espresso",
"checkPackagePrefix": "false",
"multiReleaseJarVersion": "11",
"checkstyle": "com.oracle.truffle.espresso",
"javaCompliance": "11+",
},

"com.oracle.truffle.espresso.jdk17": {
"subDir": "src",
"sourceDirs": ["src"],
"overlayTarget": "com.oracle.truffle.espresso",
"dependencies": [
"com.oracle.truffle.espresso",
],
"checkPackagePrefix": "false",
"multiReleaseJarVersion": "17",
"checkstyle": "com.oracle.truffle.espresso",
"javaCompliance": "17+",
},


"com.oracle.truffle.espresso.processor": {
"subDir": "src",
"sourceDirs": ["src"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.oracle.truffle.espresso.overlay;

import java.lang.ref.Reference;

import com.oracle.truffle.espresso.runtime.StaticObject;

public class ReferenceSupport {
public static boolean phantomReferenceRefersTo(Reference<StaticObject> ref, StaticObject object) {
return ref.refersTo(object);
}

public static boolean referenceRefersTo(Reference<StaticObject> ref, StaticObject object) {
return ref.refersTo(object);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.oracle.truffle.espresso.overlay;

import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;

import com.oracle.truffle.espresso.runtime.StaticObject;

public class ReferenceSupport {
@SuppressWarnings("unused")
public static boolean phantomReferenceRefersTo(Reference<StaticObject> ref, StaticObject object) {
assert (ref instanceof PhantomReference);
return false;
}

public static boolean referenceRefersTo(Reference<StaticObject> ref, StaticObject object) {
assert !(ref instanceof PhantomReference);
StaticObject value = ref.get();
value = value == null ? StaticObject.NULL : value;
return value == object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
import com.oracle.truffle.espresso.nodes.EspressoRootNode;
import com.oracle.truffle.espresso.nodes.interop.ToEspressoNode;
import com.oracle.truffle.espresso.nodes.interop.ToEspressoNodeGen;
import com.oracle.truffle.espresso.overlay.ReferenceSupport;
import com.oracle.truffle.espresso.runtime.Attribute;
import com.oracle.truffle.espresso.runtime.Classpath;
import com.oracle.truffle.espresso.runtime.EspressoContext;
Expand All @@ -138,6 +139,7 @@
import com.oracle.truffle.espresso.runtime.MethodHandleIntrinsics;
import com.oracle.truffle.espresso.runtime.StaticObject;
import com.oracle.truffle.espresso.substitutions.CallableFromNative;
import com.oracle.truffle.espresso.substitutions.EspressoReference;
import com.oracle.truffle.espresso.substitutions.GenerateNativeEnv;
import com.oracle.truffle.espresso.substitutions.Inject;
import com.oracle.truffle.espresso.substitutions.JavaType;
Expand Down Expand Up @@ -3432,28 +3434,32 @@ public boolean JVM_HasReferencePendingList() {
return getContext().hasReferencePendingList();
}

@SuppressWarnings({"rawtypes", "unchecked"})
@VmImpl(isJni = true)
public boolean JVM_PhantomReferenceRefersTo(@JavaType(Reference.class) StaticObject ref, @SuppressWarnings("unused") @JavaType(Object.class) StaticObject object,
public boolean JVM_PhantomReferenceRefersTo(@JavaType(Reference.class) StaticObject ref, @JavaType(Object.class) StaticObject object,
@Inject SubstitutionProfiler profiler) {
if (StaticObject.isNull(ref)) {
profiler.profile(0);
getMeta().throwNullPointerException();
}
assert InterpreterToVM.instanceOf(ref, getMeta().java_lang_ref_PhantomReference) : "Cannot call Reference.get on PhantomReference";
// At this point, we would need to call the host's PhantomReference.refersTo() method, but
// it is not available in Java 8 or 11.
return false;
EspressoReference host = (EspressoReference) getMeta().HIDDEN_HOST_REFERENCE.getHiddenObject(ref);
assert host instanceof Reference;
// Call host's refersTo. Not available in 8 or 11.
return ReferenceSupport.phantomReferenceRefersTo((Reference) host, object);
}

@SuppressWarnings({"rawtypes", "unchecked"})
@VmImpl(isJni = true)
public boolean JVM_ReferenceRefersTo(@JavaType(Reference.class) StaticObject ref, @JavaType(Object.class) StaticObject object,
@Inject SubstitutionProfiler profiler) {
if (StaticObject.isNull(ref)) {
profiler.profile(0);
getMeta().throwNullPointerException();
}
assert !InterpreterToVM.instanceOf(ref, getMeta().java_lang_ref_PhantomReference) : "Cannot call Reference.get on PhantomReference";
return Target_java_lang_ref_Reference.get(ref, getMeta()) == object;
EspressoReference host = (EspressoReference) getMeta().HIDDEN_HOST_REFERENCE.getHiddenObject(ref);
assert host instanceof Reference;
// Call host's refersTo. Not available in 8 or 11.
return ReferenceSupport.referenceRefersTo((Reference) host, object);
}

@VmImpl(isJni = true)
Expand Down

0 comments on commit 83464af

Please sign in to comment.