Skip to content
Permalink
Browse files
[Truffle] Implemented Kernel#eval with a String-coerced argument.
  • Loading branch information
nirvdrum committed Dec 5, 2014
1 parent aaed4a6 commit 3c98a0fef1f2aaf8e2cb4e6682fc5b5e9e62d07e
@@ -266,6 +266,10 @@ public static boolean isSecondFloat(Object a, Object b) {
return b instanceof Double;
}

public static boolean isString(Object o) {
return o instanceof RubyString;
}

public RubyBignum bignum(int value) {
return bignum((long) value);
}
@@ -479,12 +479,16 @@ public EqlNode(EqlNode prev) {
@CoreMethod(names = "eval", isModuleFunction = true, required = 1, optional = 1)
public abstract static class EvalNode extends CoreMethodNode {

@Child protected DispatchHeadNode toStr;

public EvalNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
toStr = new DispatchHeadNode(context);
}

public EvalNode(EvalNode prev) {
super(prev);
toStr = prev.toStr;
}

@Specialization
@@ -501,6 +505,14 @@ public Object eval(RubyString source, RubyBinding binding) {
return getContext().eval(source.toString(), binding, this);
}

@Specialization(guards = "!isString")
public Object eval(VirtualFrame frame, RubyBasicObject object, UndefinedPlaceholder binding) {
notDesignedForCompilation();

RubyString coerced = (RubyString) toStr.call(frame, object, "to_str", null);

return getContext().eval(coerced.toString(), this);
}
}

@CoreMethod(names = "exec", isModuleFunction = true, required = 1, argumentsAsArray = true)
@@ -1,7 +1,7 @@
fails:Kernel#eval is a private method
fails:Kernel#eval is a module function
fails:Kernel#eval evaluates the code within
fails:Kernel#eval coerces an object to string
passes:Kernel#eval coerces an object to string
fails:Kernel#eval evaluates within the scope of the eval
fails:Kernel#eval evaluates such that consts are scoped to the class of the eval
fails:Kernel#eval finds a local in an enclosing scope

0 comments on commit 3c98a0f

Please sign in to comment.