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: 178d928a6362
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: efb1615d6815
Choose a head ref
  • 4 commits
  • 8 files changed
  • 1 contributor

Commits on May 29, 2016

  1. Copy the full SHA
    b75f3d0 View commit details
  2. Copy the full SHA
    4a00b5d View commit details
  3. Copy the full SHA
    2ff9d7c View commit details
  4. [Truffle] Implement rb_define_class_{,id_}under.

    * Convert Java Strings to Ruby before calling to Ruby.
    eregon committed May 29, 2016
    Copy the full SHA
    efb1615 View commit details
16 changes: 12 additions & 4 deletions lib/ruby/truffle/cext/ruby.h
Original file line number Diff line number Diff line change
@@ -26,8 +26,8 @@ extern "C" {
#define xfree free
#define ALLOC_N(type, n) malloc(sizeof(type) * n)

typedef void *ID;
typedef void *VALUE;
typedef void* ID;
typedef void* VALUE;

// Constants

@@ -72,8 +72,12 @@ VALUE UINT2NUM(unsigned int value);
VALUE LONG2NUM(long value);
VALUE LONG2FIX(long value);

ID SYM2ID(VALUE value);
VALUE ID2SYM(ID value);

// Type checks

int NIL_P(VALUE value);
int FIXNUM_P(VALUE value);

// Float
@@ -85,13 +89,15 @@ VALUE rb_float_new(double value);
char *RSTRING_PTR(VALUE string);
int RSTRING_LEN(VALUE string);
VALUE rb_intern_str(VALUE string);
VALUE rb_str_new2(const char *string);
VALUE rb_str_new_cstr(const char *string);
#define rb_str_new2 rb_str_new_cstr
void rb_str_cat(VALUE string, const char *to_concat, long length);

// Symbol

ID rb_intern(const char *string);
VALUE ID2SYM(ID id);
ID rb_intern2(const char *string, long length);
#define rb_intern_const(str) rb_intern2((str), strlen(str))

// Array

@@ -141,6 +147,8 @@ void rb_raise(VALUE exception, const char *format, ...);
// Defining classes, modules and methods

VALUE rb_define_class(const char *name, VALUE superclass);
VALUE rb_define_class_under(VALUE module, const char *name, VALUE superclass);
VALUE rb_define_class_id_under(VALUE module, ID name, VALUE superclass);
VALUE rb_define_module(const char *name);
VALUE rb_define_module_under(VALUE module, const char *name);

Binary file modified lib/ruby/truffle/cext/ruby.su
Binary file not shown.
4 changes: 2 additions & 2 deletions spec/ruby/optional/capi/class_spec.rb
Original file line number Diff line number Diff line change
@@ -169,7 +169,7 @@
it "raises a NameError if the class variable is not defined" do
lambda {
@s.rb_cv_get(CApiClassSpecs::CVars, "@@no_cvar")
}.should raise_error(NameError)
}.should raise_error(NameError, /class variable @@no_cvar/)
end
end

@@ -331,7 +331,7 @@
it "raises a NameError if the class variable is not defined" do
lambda {
@s.rb_cvar_get(CApiClassSpecs::CVars, "@@no_cvar")
}.should raise_error(NameError)
}.should raise_error(NameError, /class variable @@no_cvar/)
end
end

3 changes: 0 additions & 3 deletions spec/ruby/optional/capi/ext/jruby_truffle.h
Original file line number Diff line number Diff line change
@@ -352,8 +352,6 @@
#undef HAVE_RB_CONST_GET_FROM
#undef HAVE_RB_CONST_SET
#undef HAVE_RB_DEFINE_ALIAS
#undef HAVE_RB_DEFINE_CLASS_UNDER
#undef HAVE_RB_DEFINE_CLASS_ID_UNDER
#undef HAVE_RB_DEFINE_CONST
#undef HAVE_RB_DEFINE_GLOBAL_CONST
#undef HAVE_RB_DEFINE_GLOBAL_FUNCTION
@@ -521,7 +519,6 @@
#undef HAVE_RB_ID2NAME
#undef HAVE_RB_ID2STR
#undef HAVE_RB_INTERN_STR
#undef HAVE_RB_INTERN
#undef HAVE_RB_IS_CLASS_ID
#undef HAVE_RB_IS_CONST_ID
#undef HAVE_RB_IS_INSTANCE_ID
17 changes: 0 additions & 17 deletions spec/truffle/tags/optional/capi/class_tags.txt
Original file line number Diff line number Diff line change
@@ -30,17 +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_under creates a subclass of the superclass contained in a module
fails:C-API Class function rb_define_class_under sets the class name
fails:C-API Class function rb_define_class_under calls #inherited on the superclass
fails:C-API Class function rb_define_class_under raises a TypeError when given a non class object to superclass
fails:C-API Class function rb_define_class_under raises a TypeError when given a mismatched class to superclass
fails:C-API Class function rb_define_class_under defines a class for an existing Autoload
fails:C-API Class function rb_define_class_under raises a TypeError if class is defined and its superclass mismatches the given one
fails:C-API Class function rb_define_class_id_under creates a subclass of the superclass contained in a module
fails:C-API Class function rb_define_class_id_under sets the class name
fails:C-API Class function rb_define_class_id_under calls #inherited on the superclass
fails:C-API Class function rb_define_class_id_under defines a class for an existing Autoload
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
@@ -53,11 +42,5 @@ fails:C-API Class function rb_class_real returns the class of an object ignoring
fails:C-API Class function rb_class_real returns the class of an object ignoring included modules
fails:C-API Class function rb_class_real returns 0 if passed 0
fails:C-API Class function rb_cv_get raises a NameError if the class variable is not defined
fails:C-API Class function rb_define_class creates a subclass of the superclass
fails:C-API Class function rb_define_class sets the class name
fails:C-API Class function rb_define_class calls #inherited on the superclass
fails:C-API Class function rb_define_class raises a TypeError when given a non class object to superclass
fails:C-API Class function rb_define_class raises a TypeError when given a mismatched class to superclass
fails:C-API Class function rb_define_class raises a ArgumentError when given NULL as superclass
fails:C-API Class function rb_define_class_under raises a NameError when given a mismatched class to superclass
fails:C-API Class function rb_cvar_get raises a NameError if the class variable is not defined
48 changes: 32 additions & 16 deletions truffle/src/main/c/cext/ruby.c
Original file line number Diff line number Diff line change
@@ -90,10 +90,22 @@ VALUE LONG2FIX(long value) {
return (VALUE) truffle_invoke(RUBY_CEXT, "LONG2FIX", value);
}

ID SYM2ID(VALUE value) {
return (ID) value;
}

VALUE ID2SYM(ID value) {
return (VALUE) value;
}

// Type checks

int NIL_P(VALUE value) {
return truffle_invoke_b(RUBY_CEXT, "NIL_P", value);
}

int FIXNUM_P(VALUE value) {
return truffle_invoke_i(RUBY_CEXT, "FIXNUM_P", value);
return truffle_invoke_b(RUBY_CEXT, "FIXNUM_P", value);
}

// Float
@@ -113,26 +125,22 @@ int RSTRING_LEN(VALUE string) {
return truffle_get_size(string);
}

VALUE rb_str_new2(const char *string) {
return (VALUE) truffle_invoke(RUBY_CEXT, "rb_str_new2", truffle_read_string(string));
VALUE rb_str_new_cstr(const char *string) {
return (VALUE) truffle_invoke(RUBY_CEXT, "rb_str_new_cstr", truffle_read_string(string));
}

VALUE rb_intern_str(VALUE string) {
return (VALUE) truffle_invoke(RUBY_CEXT, "rb_intern_str", string);
}

void rb_str_cat(VALUE string, const char *to_concat, long length) {
truffle_invoke(RUBY_CEXT, "rb_str_cat", string, truffle_read_string(to_concat), length);
truffle_invoke(RUBY_CEXT, "rb_str_cat", string, rb_str_new_cstr(to_concat), length);
}

// Symbol

ID rb_intern(const char *string) {
return (ID) truffle_invoke(RUBY_CEXT, "rb_intern", string);
}

VALUE ID2SYM(ID id) {
return truffle_invoke(RUBY_CEXT, "ID2SYM", id);
return (ID) truffle_invoke(RUBY_CEXT, "rb_intern", rb_str_new_cstr(string));
}

// Array
@@ -225,11 +233,11 @@ VALUE rb_funcall(VALUE object, ID name, int argc, ...) {
// Instance variables

VALUE rb_iv_get(VALUE object, const char *name) {
return truffle_read(object, truffle_read_string(name));
return truffle_read(object, rb_intern(name));
}

VALUE rb_iv_set(VALUE object, const char *name, VALUE value) {
truffle_write(object, truffle_read_string(name), value);
truffle_write(object, rb_intern(name), value);
return value;
}

@@ -248,25 +256,33 @@ void rb_raise(VALUE exception, const char *format, ...) {
// Defining classes, modules and methods

VALUE rb_define_class(const char *name, VALUE superclass) {
return truffle_invoke(RUBY_CEXT, "rb_define_class", truffle_read_string(name), superclass);
return rb_define_class_under(rb_cObject, name, superclass);
}

VALUE rb_define_class_under(VALUE module, const char *name, VALUE superclass) {
return rb_define_class_id_under(module, rb_str_new_cstr(name), superclass);
}

VALUE rb_define_class_id_under(VALUE module, ID name, VALUE superclass) {
return truffle_invoke(RUBY_CEXT, "rb_define_class_under", module, name, superclass);
}

VALUE rb_define_module(const char *name) {
return truffle_invoke(RUBY_CEXT, "rb_define_module", truffle_read_string(name));
return truffle_invoke(RUBY_CEXT, "rb_define_module", rb_str_new_cstr(name));
}

VALUE rb_define_module_under(VALUE module, const char *name) {
return truffle_invoke(RUBY_CEXT, "rb_define_module_under", module, name);
}

void rb_define_method(VALUE module, const char *name, void *function, int args) {
truffle_invoke(RUBY_CEXT, "rb_define_method", module, truffle_read_string(name), truffle_address_to_function(function), args);
truffle_invoke(RUBY_CEXT, "rb_define_method", module, rb_str_new_cstr(name), truffle_address_to_function(function), args);
}

void rb_define_private_method(VALUE module, const char *name, void *function, int args) {
truffle_invoke(RUBY_CEXT, "rb_define_private_method", module, truffle_read_string(name), truffle_address_to_function(function), args);
truffle_invoke(RUBY_CEXT, "rb_define_private_method", module, rb_str_new_cstr(name), truffle_address_to_function(function), args);
}

void rb_define_module_function(VALUE module, const char *name, void *function, int args) {
truffle_invoke(RUBY_CEXT, "rb_define_module_function", module, truffle_read_string(name), truffle_address_to_function(function), args);
truffle_invoke(RUBY_CEXT, "rb_define_module_function", module, rb_str_new_cstr(name), truffle_address_to_function(function), args);
}
Original file line number Diff line number Diff line change
@@ -143,13 +143,11 @@
import org.jruby.util.ConvertBytes;
import org.jruby.util.ConvertDouble;
import org.jruby.util.StringSupport;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.jruby.truffle.core.rope.RopeConstants.EMPTY_ASCII_8BIT_ROPE;
import static org.jruby.truffle.core.string.StringOperations.encoding;
import static org.jruby.truffle.core.string.StringOperations.rope;
@@ -1249,6 +1247,13 @@ public DynamicObject initialize(DynamicObject self, NotProvided from) {
return self;
}

@Specialization
public DynamicObject initializeJavaString(DynamicObject self, String from) {
raiseIfFrozen(self);
StringOperations.setRope(self, StringOperations.createRope(from, UTF8Encoding.INSTANCE));
return self;
}

@Specialization(guards = "isRubyString(from)")
public DynamicObject initialize(DynamicObject self, DynamicObject from) {
raiseIfFrozen(self);
38 changes: 23 additions & 15 deletions truffle/src/main/ruby/core/truffle/cext.rb
Original file line number Diff line number Diff line change
@@ -42,6 +42,10 @@ def rb_eRuntimeError
raise 'not implemented'
end

def NIL_P(value)
nil.equal?(value)
end

def FIXNUM_P(value)
value.is_a?(Fixnum)
end
@@ -54,22 +58,18 @@ def RSTRING_PTR(string)
string
end

def rb_intern
raise 'not implemented'
def rb_intern(str)
str.intern
end

def rb_str_new2(string)
string
def rb_str_new_cstr(java_string)
String.new(java_string)
end

def rb_intern_str(string)
string.intern
end

def ID2SYM(id)
id
end

def rb_str_cat(string, to_concat, length)
raise 'not implemented'
end
@@ -102,8 +102,16 @@ def rb_raise(object, name)
raise 'not implemented'
end

def rb_define_class(name, superclass)
Object.const_set(name, Class.new(superclass))
def rb_define_class_under(mod, name, superclass)
if mod.const_defined?(name)
klass = mod.const_get(name)
if superclass != klass.superclass
raise TypeError, "superclass mismatch for class #{name}"
end
klass
else
mod.const_set(name, Class.new(superclass))
end
end

def rb_define_module(name)
@@ -114,19 +122,19 @@ def rb_define_module_under(mod, name)
mod.const_set(name, Module.new)
end

def rb_define_method(mod, name, function, args)
def rb_define_method(mod, name, function, argc)
mod.send(:define_method, name) do |*args|
function.call(self, *args)
end
end

def rb_define_private_method(mod, name, function, args)
rb_define_method mod, name, function, args
def rb_define_private_method(mod, name, function, argc)
rb_define_method mod, name, function, argc
mod.send :private, name
end

def rb_define_module_function(mod, name, function, args)
rb_define_method mod, name, function, args
def rb_define_module_function(mod, name, function, argc)
rb_define_method mod, name, function, argc
mod.send :module_function, name
end