Skip to content

Commit

Permalink
Showing 7 changed files with 116 additions and 50 deletions.
21 changes: 20 additions & 1 deletion lib/ruby/truffle/cext/ruby.h
Original file line number Diff line number Diff line change
@@ -125,6 +125,10 @@ VALUE rb_hash_new(void);
VALUE rb_hash_aref(VALUE hash, VALUE key);
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE value);

// Class

const char* rb_class2name(VALUE module);

// Utilities

int rb_scan_args(int argc, VALUE *argv, const char *format, ...);
@@ -140,7 +144,16 @@ VALUE rb_iv_set(VALUE object, const char *name, VALUE value);

// Accessing constants

VALUE rb_const_get(VALUE object, ID name);
int rb_const_defined(VALUE module, ID name);
int rb_const_defined_at(VALUE module, ID name);

VALUE rb_const_get(VALUE module, ID name);
VALUE rb_const_get_at(VALUE module, ID name);
VALUE rb_const_get_from(VALUE module, ID name);

VALUE rb_const_set(VALUE module, ID name, VALUE value);
VALUE rb_define_const(VALUE module, const char *name, VALUE value);
void rb_define_global_const(const char *name, VALUE value);

// Raising exceptions

@@ -161,6 +174,12 @@ void rb_define_module_function(VALUE module, const char *name, void *function, i
void rb_define_global_function(const char *name, void *function, int argc);
void rb_define_singleton_method(VALUE object, const char *name, void *function, int argc);

void rb_define_alias(VALUE module, const char *new_name, const char *old_name);
void rb_alias(VALUE module, ID new_name, ID old_name);

void rb_undef_method(VALUE module, const char *name);
void rb_undef(VALUE module, ID name);

#if defined(__cplusplus)
}
#endif
Binary file modified lib/ruby/truffle/cext/ruby.su
Binary file not shown.
15 changes: 0 additions & 15 deletions spec/ruby/optional/capi/ext/jruby_truffle.h
Original file line number Diff line number Diff line change
@@ -53,7 +53,6 @@

/* Class */
#undef HAVE_RB_CALL_SUPER
#undef HAVE_RB_CLASS2NAME
#undef HAVE_RB_CLASS_NAME
#undef HAVE_RB_CLASS_NEW
#undef HAVE_RB_CLASS_NEW_INSTANCE
@@ -343,20 +342,6 @@
#undef HAVE_RB_MARSHAL_DUMP
#undef HAVE_RB_MARSHAL_LOAD

/* Module */
#undef HAVE_RB_ALIAS
#undef HAVE_RB_CONST_DEFINED
#undef HAVE_RB_CONST_DEFINED_AT
#undef HAVE_RB_CONST_GET
#undef HAVE_RB_CONST_GET_AT
#undef HAVE_RB_CONST_GET_FROM
#undef HAVE_RB_CONST_SET
#undef HAVE_RB_DEFINE_ALIAS
#undef HAVE_RB_DEFINE_CONST
#undef HAVE_RB_DEFINE_GLOBAL_CONST
#undef HAVE_RB_UNDEF
#undef HAVE_RB_UNDEF_METHOD

/* Numeric */
#undef HAVE_NUM2CHR
#undef HAVE_RB_CMPINT
32 changes: 0 additions & 32 deletions spec/truffle/tags/optional/capi/module_tags.txt

This file was deleted.

54 changes: 52 additions & 2 deletions truffle/src/main/c/cext/ruby.c
Original file line number Diff line number Diff line change
@@ -222,6 +222,12 @@ VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE value) {
return value;
}

// Class

const char* rb_class2name(VALUE module) {
return RSTRING_PTR(truffle_invoke(module, "name"));
}

// Utilities

int rb_scan_args(int argc, VALUE *argv, const char *format, ...) {
@@ -247,8 +253,36 @@ VALUE rb_iv_set(VALUE object, const char *name, VALUE value) {

// Accessing constants

VALUE rb_const_get(VALUE object, ID name) {
return truffle_invoke(object, "const_get", name);
int rb_const_defined(VALUE module, ID name) {
return truffle_invoke_b(module, "const_defined?", name);
}

int rb_const_defined_at(VALUE module, ID name) {
return truffle_invoke_b(module, "const_defined?", name, Qfalse);
}

VALUE rb_const_get(VALUE module, ID name) {
return truffle_invoke(module, "const_get", name);
}

VALUE rb_const_get_at(VALUE module, ID name) {
return truffle_invoke(module, "const_get", name, Qfalse);
}

VALUE rb_const_get_from(VALUE module, ID name) {
return truffle_invoke(RUBY_CEXT, "rb_const_get_from", module, name);
}

VALUE rb_const_set(VALUE module, ID name, VALUE value) {
return truffle_invoke(module, "const_set", name, value);
}

VALUE rb_define_const(VALUE module, const char *name, VALUE value) {
return rb_const_set(module, rb_str_new_cstr(name), value);
}

void rb_define_global_const(const char *name, VALUE value) {
rb_define_const(rb_cObject, name, value);
}

// Raising exceptions
@@ -302,3 +336,19 @@ void rb_define_global_function(const char *name, void *function, int argc) {
void rb_define_singleton_method(VALUE object, const char *name, void *function, int argc) {
truffle_invoke(RUBY_CEXT, "rb_define_singleton_method", object, rb_str_new_cstr(name), truffle_address_to_function(function), argc);
}

void rb_define_alias(VALUE module, const char *new_name, const char *old_name) {
rb_alias(module, rb_str_new_cstr(new_name), rb_str_new_cstr(old_name));
}

void rb_alias(VALUE module, ID new_name, ID old_name) {
truffle_invoke(RUBY_CEXT, "rb_alias", module, new_name, old_name);
}

void rb_undef_method(VALUE module, const char *name) {
rb_undef(module, rb_str_new_cstr(name));
}

void rb_undef(VALUE module, ID name) {
truffle_invoke(RUBY_CEXT, "rb_undef", module, name);
}
34 changes: 34 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/interop/CExtNodes.java
Original file line number Diff line number Diff line change
@@ -9,10 +9,21 @@
*/
package org.jruby.truffle.interop;

import com.oracle.truffle.api.dsl.CreateCast;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
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.NameToJavaStringNodeGen;
import org.jruby.truffle.language.RubyConstant;
import org.jruby.truffle.language.RubyNode;
import org.jruby.truffle.language.constants.GetConstantNode;
import org.jruby.truffle.language.constants.LookupConstantNode;

@CoreClass("Truffle::CExt")
public class CExtNodes {
@@ -130,4 +141,27 @@ public int long2fix(int num) {

}

@NodeChildren({
@NodeChild(type = RubyNode.class, value = "module"),
@NodeChild(type = RubyNode.class, value = "name")
})
@CoreMethod(names = "rb_const_get_from", isModuleFunction = true, required = 2)
public abstract static class ConstGetFromNode extends CoreMethodNode {

@CreateCast("name")
public RubyNode coerceToString(RubyNode name) {
return NameToJavaStringNodeGen.create(null, null, name);
}

@Child LookupConstantNode lookupConstantNode = LookupConstantNode.create(true, false);
@Child GetConstantNode getConstantNode = GetConstantNode.create();

@Specialization
public Object constGetFrom(VirtualFrame frame, DynamicObject module, String name) {
final RubyConstant constant = lookupConstantNode.lookupConstant(frame, module, name);
return getConstantNode.executeGetConstant(frame, module, name, constant, lookupConstantNode);
}

}

}
10 changes: 10 additions & 0 deletions truffle/src/main/ruby/core/truffle/cext.rb
Original file line number Diff line number Diff line change
@@ -158,5 +158,15 @@ def rb_define_module_function(mod, name, function, argc)
def rb_define_singleton_method(object, name, function, argc)
rb_define_method(object.singleton_class, name, function, argc)
end

def rb_alias(mod, new_name, old_name)
mod.send(:alias_method, new_name, old_name)

This comment has been minimized.

Copy link
@chrisseaton

chrisseaton May 30, 2016

Contributor

Could this one be written directly in C using interop?

This comment has been minimized.

Copy link
@eregon

eregon May 30, 2016

Author Member

Probably, indeed, I am not sure why I did it this way. Maybe I was wondering if calling private methods is allowed from interop?

This comment has been minimized.

Copy link
@chrisseaton

chrisseaton May 30, 2016

Contributor

I've documented that it currently ignores visibility entirely, but I don't think I have any tests to ensure that.

https://github.com/jruby/jruby/wiki/JRuby-and-Truffle-Interop

end

def rb_undef(mod, name)
if mod.frozen? or mod.method_defined?(name)
mod.send(:undef_method, name)
end
end
end
end

0 comments on commit f70b512

Please sign in to comment.