Skip to content

Commit

Permalink
Registering RubyLanguage using @TruffleLanguage.Registration annotati…
Browse files Browse the repository at this point in the history
…on and providing base implementation of its methods.
  • Loading branch information
Jaroslav Tulach committed May 29, 2015
1 parent b221938 commit 3afa8a5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@
*/
package org.jruby.truffle.nodes.core;

import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.truffle.runtime.core.RubySymbol;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.FrameSlot;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.source.SourceSection;
Expand Down Expand Up @@ -172,7 +167,7 @@ public ReadPropertyNode(RubyContext context, SourceSection sourceSection) {
public Object executeForeign(VirtualFrame frame, TruffleObject receiver, int identifier) {
return node.executeForeign(frame, receiver, identifier);
}

@Specialization
public Object executeForeign(VirtualFrame frame, String receiver, int identifier) {
return receiver.charAt(identifier);
Expand Down Expand Up @@ -308,37 +303,24 @@ public Object executeForeign(VirtualFrame frame, TruffleObject receiver) {

}

/*
@CoreMethod(names = "export", isModuleFunction = true, needsSelf = false, required = 2)
public abstract static class ExportNode extends CoreMethodArrayArgumentsNode {

public ExportNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization(guards = "name == cachedName")
public Object export(VirtualFrame frame, RubyString name, TruffleObject object, @Cached("name") RubyString cachedName, @Cached("rubyStringToString(cachedName)") String stringName, @Cached("getGlobal()") TruffleGlobalScope global) {
global.exportTruffleObject(stringName, object);
return object;
}
@Specialization
public Object export(VirtualFrame frame, RubyString name, TruffleObject object) {
globalScope.exportTruffleObject(name.toString(), object);
getContext().exportObject(name, object);
return object;
}

protected static String rubyStringToString(RubyString rubyString) {
return rubyString.toString();
}
protected TruffleGlobalScope getGlobal() {
return globalScope;
}
}

@CoreMethod(names = "import", isModuleFunction = true, needsSelf = false, required = 1)
public abstract static class ImportNode extends CoreMethodArrayArgumentsNode {

Expand All @@ -347,25 +329,10 @@ public ImportNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization(guards = "name == cachedName")
public TruffleObject importObject(VirtualFrame frame, RubyString name, @Cached("name") RubyString cachedName, @Cached("getSlot(cachedName)") FrameSlot slot, @Cached("getGlobal()") TruffleGlobalScope global) {
return global.getTruffleObject(slot);
}
@Specialization
public Object importObject(VirtualFrame frame, RubyString name) {
return getGlobal().getTruffleObject(getSlot(name));
}
protected FrameSlot getSlot(RubyString rubyString) {
return globalScope.getFrameSlot(rubyString.toString());
}
protected TruffleGlobalScope getGlobal() {
return globalScope;
return getContext().importObject(name);
}

}
*/
}
22 changes: 22 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/runtime/RubyContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrument.Probe;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.BytesDecoder;
import com.oracle.truffle.api.source.Source;
Expand Down Expand Up @@ -107,10 +108,17 @@ public class RubyContext extends ExecutionContext implements TruffleContextInter

private final PrintStream debugStandardOut;

private Map<String,TruffleObject> exported;
private final TruffleLanguage.Env env;

public RubyContext(Ruby runtime) {
this(runtime, null);
}
public RubyContext(Ruby runtime, TruffleLanguage.Env env) {
latestInstance = this;

assert runtime != null;
this.env = env;

compilerOptions = Truffle.getRuntime().createCompilerOptions();

Expand Down Expand Up @@ -698,4 +706,18 @@ public PrintStream getDebugStandardOut() {
return debugStandardOut;
}

public void exportObject(RubyString name, TruffleObject object) {
if (exported == null) {
exported = new HashMap<>();
}
exported.put(name.toString(), object);
}

public Object findExportedObject(String name) {
return exported == null ? null : exported.get(name);
}

public Object importObject(RubyString name) {
return env.importSymbol(name.toString());
}
}
18 changes: 13 additions & 5 deletions truffle/src/main/java/org/jruby/truffle/runtime/RubyLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,32 @@
import com.oracle.truffle.api.source.Source;

import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.truffle.runtime.core.RubyBasicObject;

@TruffleLanguage.Registration(name = "Ruby", mimeType = "application/x-ruby")
public class RubyLanguage extends TruffleLanguage {

private final RubyContext context;

public RubyLanguage(Env env, RubyContext context) {
public RubyLanguage(Env env) {
super(env);
this.context = context;
Ruby r = Ruby.newInstance();
this.context = new RubyContext(r, env);
}

@Override
protected Object eval(Source source) throws IOException {
throw new UnsupportedOperationException();
try {
return context.eval(source);
} catch (Exception e) {
throw new IOException(e);
}
}

@Override
protected Object findExportedSymbol(String s) {
throw new UnsupportedOperationException();
return context.findExportedObject(s);
}

@Override
Expand All @@ -40,7 +48,7 @@ protected Object getLanguageGlobal() {

@Override
protected boolean isObjectOfLanguage(Object o) {
throw new UnsupportedOperationException();
return o instanceof RubyBasicObject;
}

}

0 comments on commit 3afa8a5

Please sign in to comment.