Skip to content

Commit

Permalink
[Truffle] Avoid automatic conversion Ruby String => Java String in rb…
Browse files Browse the repository at this point in the history
…_define_method.

* Otherwise it makes it impossible to get Ruby Strings passed to C-API functions.
* It is also less confusing by making conversions explicit.
eregon committed May 30, 2016
1 parent efb1615 commit bf946d7
Showing 4 changed files with 18 additions and 4 deletions.
Binary file modified lib/ruby/truffle/cext/ruby.su
Binary file not shown.
1 change: 0 additions & 1 deletion spec/truffle/tags/optional/capi/class_tags.txt
Original file line number Diff line number Diff line change
@@ -30,7 +30,6 @@ fails:C-API Class function rb_cvar_defined returns true if the class instance va
fails:C-API Class function rb_cv_set sets a class variable
fails:C-API Class function rb_cv_get returns the value of the class variable
fails:C-API Class function rb_cvar_set sets a class variable
fails:C-API Class function rb_define_class_id_under raises a TypeError if class is defined and its superclass mismatches the given one
fails:C-API Class function rb_define_class_variable sets a class variable
fails:C-API Class function rb_cvar_get returns the value of the class variable
fails:C-API Class function rb_class_new returns an new subclass of the superclass
Original file line number Diff line number Diff line change
@@ -38,7 +38,6 @@
import org.jruby.truffle.core.string.StringCachingGuards;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.util.ByteList;

import java.io.IOException;

@CoreClass("Truffle::Interop")
@@ -64,6 +63,9 @@ protected Node createIsExecutableNode() {
@CoreMethod(names = "execute", isModuleFunction = true, needsSelf = false, required = 1, rest = true)
public abstract static class ExecuteNode extends CoreMethodArrayArgumentsNode {

// NOTE (eregon, 30/05/2016): If you want to introduce automatic argument conversion here,
// look first at cext.rb #rb_define_method which wants no automatic conversion.

@Specialization(
guards = "args.length == cachedArgsLength",
limit = "getCacheLimit()"
@@ -610,4 +612,16 @@ protected int getCacheLimit() {

}

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

@Child ToJavaStringNode toJavaStringNode = ToJavaStringNode.create();

@Specialization
public Object toJavaString(Object value) {
return toJavaStringNode.executeToJavaString(value);
}

}

}
5 changes: 3 additions & 2 deletions truffle/src/main/ruby/core/truffle/cext.rb
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ def rb_float_new(value)
end

def RSTRING_PTR(string)
string
Truffle::Interop.to_java_string(string)
end

def rb_intern(str)
@@ -124,7 +124,8 @@ def rb_define_module_under(mod, name)

def rb_define_method(mod, name, function, argc)
mod.send(:define_method, name) do |*args|
function.call(self, *args)
# Using raw execute instead of #call here to avoid argument conversion
Truffle::Interop.execute(function, self, *args)
end
end

0 comments on commit bf946d7

Please sign in to comment.