Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 894e37251d10
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d37a55a48aef
Choose a head ref
  • 3 commits
  • 5 files changed
  • 1 contributor

Commits on Jul 1, 2016

  1. Copy the full SHA
    d5e1524 View commit details
  2. 1
    Copy the full SHA
    1aa432a View commit details
  3. Copy the full SHA
    d37a55a View commit details
4 changes: 2 additions & 2 deletions samples/truffle/interop/weather/weather.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Interop.eval('application/x-ruby', 'require "weather"');

temperature_in_city = Interop.import('temperature_in_city')
Weather = Interop.import('weather')

console.log('Temperature in New York now: ' + temperature_in_city.call('New York') + '℃');
console.log('Temperature in New York now: ' + Weather.temperature_in_city('New York') + '℃');
12 changes: 7 additions & 5 deletions samples/truffle/interop/weather/weather.rb
Original file line number Diff line number Diff line change
@@ -6,10 +6,12 @@
config.apikey = 'dd7073d18e3085d0300b6678615d904d'
end

def temperature_in_city(name)
name = Truffle::Interop.from_java_string(name)
weather = Openweather2.get_weather(city: name, units: 'metric')
weather.temperature
module Weather
def self.temperature_in_city(name)
name = Truffle::Interop.from_java_string(name)
weather = Openweather2.get_weather(city: name, units: 'metric')
weather.temperature
end
end

Truffle::Interop.export_method :temperature_in_city
Truffle::Interop.export :weather, Weather
Original file line number Diff line number Diff line change
@@ -472,6 +472,11 @@ public DynamicObject nameErrorClassVariableNotDefined(String name, DynamicObject
return nameError(String.format("class variable `%s' not defined for %s", name, Layouts.MODULE.getFields(module).getName()), name, currentNode);
}

@TruffleBoundary
public DynamicObject nameErrorImportNotFound(String name, Node currentNode) {
return nameError(String.format("import '%s' not found", name), name, currentNode);
}

@TruffleBoundary
public DynamicObject nameError(String message, String name, Node currentNode) {
final DynamicObject nameString = StringOperations.createString(context, StringOperations.encodeRope(message, UTF8Encoding.INSTANCE));
31 changes: 27 additions & 4 deletions truffle/src/main/java/org/jruby/truffle/interop/InteropNodes.java
Original file line number Diff line number Diff line change
@@ -13,8 +13,10 @@
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.CreateCast;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.ArityException;
@@ -34,11 +36,15 @@
import org.jruby.truffle.builtins.CoreClass;
import org.jruby.truffle.builtins.CoreMethod;
import org.jruby.truffle.builtins.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.builtins.CoreMethodNode;
import org.jruby.truffle.core.cast.NameToJavaStringNode;
import org.jruby.truffle.core.cast.NameToJavaStringNodeGen;
import org.jruby.truffle.core.rope.Rope;
import org.jruby.truffle.core.string.StringCachingGuards;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.language.RubyNode;
import org.jruby.truffle.language.control.JavaException;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.util.ByteList;
import java.io.IOException;

@@ -547,12 +553,29 @@ public Object export(DynamicObject name, TruffleObject object) {

}

@CoreMethod(names = "import", isModuleFunction = true, needsSelf = false, required = 1)
public abstract static class ImportNode extends CoreMethodArrayArgumentsNode {
@CoreMethod(names = "import", isModuleFunction = true, required = 1)
@NodeChild(value = "name", type = RubyNode.class)
public abstract static class ImportNode extends CoreMethodNode {

@CreateCast("name")
public RubyNode coercetNameToString(RubyNode newName) {
return NameToJavaStringNodeGen.create(newName);
}

@Specialization
public Object importObject(String name,
@Cached("create()") BranchProfile errorProfile) {
final Object value = doImport(name);
if (value != null) {
return value;
} else {
errorProfile.enter();
throw new RaiseException(coreExceptions().nameErrorImportNotFound(name, this));
}
}

@TruffleBoundary
@Specialization(guards = "isRubyString(name) || isRubySymbol(name)")
public Object importObject(DynamicObject name) {
private Object doImport(String name) {
return getContext().getInteropManager().importObject(name.toString());
}

Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@

import com.oracle.truffle.api.nodes.ControlFlowException;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.Layouts;

public class RaiseException extends ControlFlowException {

@@ -24,4 +25,14 @@ public DynamicObject getException() {
return exception;
}

@Override
public String getMessage() {
Object message = Layouts.EXCEPTION.getMessage(exception);
if (message != null) {
return message.toString();
} else {
return null;
}
}

}