Skip to content

Commit

Permalink
[Truffle] We shouldn't need any AOT substitutions in our own code.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Dec 10, 2016
1 parent ddc4db5 commit b3aee53
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 85 deletions.
Expand Up @@ -13,26 +13,13 @@
*/
package org.jruby.truffle.aot;

import com.oracle.truffle.api.source.Source;
import org.jcodings.exception.InternalException;
import org.jruby.truffle.RubyLanguage;
import org.jruby.truffle.language.loader.SourceLoader;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

// Checkstyle: stop

final class Target_org_jruby_truffle_core_TruffleSystemNodes_FullMemoryBarrierPrimitiveNode {
static void fullFence() {
throw new UnsupportedOperationException();
}
}

final class Target_org_jcodings_Encoding {
static org.jcodings.Encoding load(String name) {
JRubySupport.EncodingInstance instance = JRubySupport.allEncodings.get(name);
Expand All @@ -53,23 +40,6 @@ static DataInputStream openStream(String name) {
}
}

@SuppressWarnings("static-method")
final class Target_org_jruby_truffle_language_loader_SourceLoader {
Source loadResource(String path) throws IOException {
if (!(path.startsWith(SourceLoader.TRUFFLE_SCHEME) || path.startsWith(SourceLoader.JRUBY_SCHEME))) {
throw new UnsupportedOperationException();
}

final String canonicalPath = JRubySourceLoaderSupport.canonicalizeResourcePath(path);
final JRubySourceLoaderSupport.CoreLibraryFile coreFile = JRubySourceLoaderSupport.allCoreLibraryFiles.get(canonicalPath);
if (coreFile == null) {
throw new FileNotFoundException(path);
}

return Source.newBuilder(new InputStreamReader(new ByteArrayInputStream(coreFile.code), StandardCharsets.UTF_8)).name(path).mimeType(RubyLanguage.MIME_TYPE).build();
}
}

/** Dummy class to have a class with the file's name. */
public final class JRubySubstitutions {
}
Expand Up @@ -26,7 +26,7 @@
/**
* Visit files with knowledge of the root of the subtree the traversal started from.
*/
interface RootedFileVisitor<T> extends FileVisitor<T> {
public interface RootedFileVisitor<T> extends FileVisitor<T> {
void setRoot(T root);

T getRoot();
Expand Down
Expand Up @@ -15,7 +15,7 @@

import java.nio.file.SimpleFileVisitor;

class SimpleRootedFileVisitor<T> extends SimpleFileVisitor<T> implements RootedFileVisitor<T> {
public class SimpleRootedFileVisitor<T> extends SimpleFileVisitor<T> implements RootedFileVisitor<T> {
private T root;

@Override
Expand Down
20 changes: 0 additions & 20 deletions truffle/src/main/java/org/jruby/truffle/aot/substitutions.json
Expand Up @@ -19,24 +19,4 @@
}
]
},
{
"annotatedClass": "org.jruby.truffle.aot.Target_org_jruby_truffle_language_loader_SourceLoader",
"originalClass": "org.jruby.truffle.language.loader.SourceLoader",
"methods": [
{
"annotatedName": "loadResource",
"substitute": true
}
]
},
{
"annotatedClass": "org.jruby.truffle.aot.Target_org_jruby_truffle_core_TruffleSystemNodes_FullMemoryBarrierPrimitiveNode",
"originalClass": "org.jruby.truffle.core.TruffleSystemNodes.FullMemoryBarrierPrimitiveNode",
"methods": [
{
"annotatedName": "fullFence",
"substitute": true
}
]
},
]
Expand Up @@ -38,7 +38,10 @@
*/
package org.jruby.truffle.core;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleOptions;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
Expand Down Expand Up @@ -114,27 +117,32 @@ public abstract static class FullMemoryBarrierPrimitiveNode extends CoreMethodNo

@Specialization
public Object fullMemoryBarrier() {
fullFence();
if (TruffleOptions.AOT) {
throw new UnsupportedOperationException();
} else {
U.fullFence();
}

return nil();
}

private static final sun.misc.Unsafe U = loadUnsafe();

private static sun.misc.Unsafe loadUnsafe() {
try {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
Field f = unsafeClass.getDeclaredField("theUnsafe");
f.setAccessible(true);
return (sun.misc.Unsafe) f.get(null);
} catch (Throwable e) {
throw new UnsupportedOperationException(e);
if (TruffleOptions.AOT) {
return null;
} else {
try {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
Field f = unsafeClass.getDeclaredField("theUnsafe");
f.setAccessible(true);
return (sun.misc.Unsafe) f.get(null);
} catch (Throwable e) {
throw new UnsupportedOperationException(e);
}
}
}

private static void fullFence() {
U.fullFence();
}

}

}
Expand Up @@ -11,10 +11,11 @@
* Some of the code in this class is modified from org.jruby.util.StringSupport,
* licensed under the same EPL1.0/GPL 2.0/LGPL 2.1 used throughout.
*/
package org.jruby.truffle.aot;
package org.jruby.truffle.language.loader;

import com.oracle.truffle.api.source.Source;
import org.jruby.truffle.language.loader.SourceLoader;
import org.jruby.truffle.aot.RootedFileVisitor;
import org.jruby.truffle.aot.SimpleRootedFileVisitor;

import java.io.IOException;
import java.io.Reader;
Expand Down
Expand Up @@ -10,11 +10,13 @@
package org.jruby.truffle.language.loader;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleOptions;
import com.oracle.truffle.api.source.Source;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.RubyLanguage;
import org.jruby.truffle.core.string.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -75,31 +77,45 @@ public Source loadFragment(String fragment, String name) {

@TruffleBoundary
private Source loadResource(String path) throws IOException {
if (!path.toLowerCase(Locale.ENGLISH).endsWith(".rb")) {
throw new FileNotFoundException(path);
}
if (TruffleOptions.AOT) {
if (!(path.startsWith(SourceLoader.TRUFFLE_SCHEME) || path.startsWith(SourceLoader.JRUBY_SCHEME))) {
throw new UnsupportedOperationException();
}

final Class<?> relativeClass;
final Path relativePath;
final String canonicalPath = JRubySourceLoaderSupport.canonicalizeResourcePath(path);
final JRubySourceLoaderSupport.CoreLibraryFile coreFile = JRubySourceLoaderSupport.allCoreLibraryFiles.get(canonicalPath);
if (coreFile == null) {
throw new FileNotFoundException(path);
}

if (path.startsWith(TRUFFLE_SCHEME)) {
relativeClass = RubyContext.class;
relativePath = FileSystems.getDefault().getPath(path.substring(TRUFFLE_SCHEME.length()));
} else if (path.startsWith(JRUBY_SCHEME)) {
relativeClass = jrubySchemeRelativeClass();
relativePath = FileSystems.getDefault().getPath(path.substring(JRUBY_SCHEME.length()));
return Source.newBuilder(new InputStreamReader(new ByteArrayInputStream(coreFile.code), StandardCharsets.UTF_8)).name(path).mimeType(RubyLanguage.MIME_TYPE).build();
} else {
throw new UnsupportedOperationException();
}
if (!path.toLowerCase(Locale.ENGLISH).endsWith(".rb")) {
throw new FileNotFoundException(path);
}

final Path normalizedPath = relativePath.normalize();
final InputStream stream = relativeClass.getResourceAsStream(StringUtils.replace(normalizedPath.toString(), '\\', '/'));
final Class<?> relativeClass;
final Path relativePath;

if (stream == null) {
throw new FileNotFoundException(path);
}
if (path.startsWith(TRUFFLE_SCHEME)) {
relativeClass = RubyContext.class;
relativePath = FileSystems.getDefault().getPath(path.substring(TRUFFLE_SCHEME.length()));
} else if (path.startsWith(JRUBY_SCHEME)) {
relativeClass = jrubySchemeRelativeClass();
relativePath = FileSystems.getDefault().getPath(path.substring(JRUBY_SCHEME.length()));
} else {
throw new UnsupportedOperationException();
}

final Path normalizedPath = relativePath.normalize();
final InputStream stream = relativeClass.getResourceAsStream(StringUtils.replace(normalizedPath.toString(), '\\', '/'));

return Source.newBuilder(new InputStreamReader(stream, StandardCharsets.UTF_8)).name(path).mimeType(RubyLanguage.MIME_TYPE).build();
if (stream == null) {
throw new FileNotFoundException(path);
}

return Source.newBuilder(new InputStreamReader(stream, StandardCharsets.UTF_8)).name(path).mimeType(RubyLanguage.MIME_TYPE).build();
}
}

private static Class<?> jrubySchemeRelativeClass() {
Expand Down

0 comments on commit b3aee53

Please sign in to comment.