Skip to content

Commit

Permalink
Cext fixes:
Browse files Browse the repository at this point in the history
* rb_class_of should return immediate metaclass, not Ruby .class metaclass
* Better error message for multi-runtime collision
* Reorg jruby namespace fields a bit
  • Loading branch information
headius committed Sep 21, 2011
1 parent 6edec02 commit 48c6f02
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
3 changes: 2 additions & 1 deletion cext/src/class.cpp
Expand Up @@ -63,7 +63,8 @@ rb_class_new_instance(int argc, VALUE* argv, VALUE klass)
extern "C" VALUE
rb_class_of(VALUE obj)
{
return callMethodA(obj, "class", 0, NULL);
JLocalEnv env;
return (VALUE) env->CallStaticObjectMethod(JRuby_class, JRuby_getMetaClass, valueToObject(env, obj));
}

extern "C" VALUE
Expand Down
2 changes: 1 addition & 1 deletion cext/src/invoke.cpp
Expand Up @@ -92,7 +92,7 @@ Java_org_jruby_cext_Native_callInit(JNIEnv* env, jobject self, jobject jThreadCo
{
jobject runtime = env->CallObjectMethod(jThreadContext, jruby::ThreadContext_getRuntime_method);
if (!env->IsSameObject(runtime, jruby::runtime)) {
jruby::throwExceptionByName(env, jruby::RuntimeException, "invalid ruby runtime");
jruby::throwExceptionByName(env, jruby::RuntimeException, "C extension initialized against invalid ruby runtime");
return 0;
}

Expand Down
41 changes: 33 additions & 8 deletions cext/src/jruby-cext.cpp
Expand Up @@ -84,8 +84,12 @@ namespace jruby {
jmethodID JRuby_nativeBlockingRegion;
jmethodID JRuby_newThread;
jmethodID JRuby_newProc;
jmethodID JRuby_sysFail;
jmethodID JRuby_threadSleep;
jmethodID JRuby_getMetaClass;

jmethodID ThreadContext_getRuntime_method;

jmethodID Ruby_defineModule_method;
jmethodID Ruby_getNil_method;
jmethodID Ruby_getTrue_method;
Expand All @@ -94,44 +98,62 @@ namespace jruby {
jmethodID Ruby_getModule_method;
jmethodID Ruby_newSymbol_method;
jmethodID Ruby_newFixnum_method;
jmethodID Ruby_defineClass_method;
jmethodID Ruby_defineClassUnder_method;
jmethodID Ruby_getClassFromPath_method;
jmethodID Ruby_defineReadonlyVariable_method;

jmethodID RaiseException_constructor;

jmethodID RubyData_newRubyData_method;

jmethodID RubyObject_getNativeTypeIndex_method;

jmethodID RubyBignum_big2dbl_method;
jmethodID RubyBignum_big2long_method;
jmethodID RubyBignum_big2ulong_method;

jmethodID RubyNumeric_num2long_method;
jmethodID RubyNumeric_num2chr_method;
jmethodID RubyNumeric_num2dbl_method;
jmethodID RubyNumeric_int2fix_method;

jmethodID RubyString_newStringNoCopy;
jmethodID RubyString_view;
jmethodID RubyString_resize_method;

jmethodID RubySymbol_getSymbolLong;

jmethodID RubyStruct_newInstance;

jmethodID IRubyObject_callMethod;
jmethodID IRubyObject_asJavaString_method;
jmethodID IRubyObject_respondsTo_method;

jmethodID Handle_nativeHandle;

jmethodID Ruby_getCurrentContext_method;

jmethodID GC_trigger;

jmethodID RubyArray_toJavaArray_method;

jmethodID RubyClass_newClass_method;
jmethodID Ruby_defineClass_method;
jmethodID Ruby_defineClassUnder_method;
jmethodID RubyClass_setAllocator_method;
jmethodID Ruby_getClassFromPath_method;
jmethodID ObjectAllocator_allocate_method;
jmethodID RubyClass_getAllocator_method;

jmethodID RubyModule_undef_method;

jmethodID ObjectAllocator_allocate_method;

jmethodID RubyBasicObject_getInstanceVariable_method;
jmethodID RubyBasicObject_setInstanceVariable_method;
jmethodID RubyBasicObject_hasInstanceVariable_method;
jmethodID Ruby_defineReadonlyVariable_method;
jmethodID JRuby_sysFail;
jmethodID RubyString_resize_method;

jmethodID RubyArray_newArray;
jmethodID RubyArray_clear_method;
jmethodID RubyArray_append_method;
jmethodID JRuby_threadSleep;

jfieldID Handle_address_field;
jfieldID RubyString_value_field;
jfieldID RubyFloat_value_field;
Expand Down Expand Up @@ -325,6 +347,7 @@ loadIds(JNIEnv* env)
"(Lorg/jruby/Ruby;Lorg/jruby/RubyArray;)Lorg/jruby/runtime/builtin/IRubyObject;");
JRuby_blockGiven = getStaticMethodID(env, JRuby_class, "blockGiven", "(Lorg/jruby/Ruby;)I");
JRuby_getBlockProc = getStaticMethodID(env, JRuby_class, "getBlockProc", "(Lorg/jruby/Ruby;)Lorg/jruby/RubyProc;");
JRuby_getMetaClass = getStaticMethodID(env, JRuby_class, "getMetaClass", "(Lorg/jruby/runtime/builtin/IRubyObject;)J");
RubyArray_toJavaArray_method = getMethodID(env, RubyArray_class, "toJavaArray",
"()[Lorg/jruby/runtime/builtin/IRubyObject;");
RubyClass_newClass_method = getStaticMethodID(env, RubyClass_class, "newClass",
Expand All @@ -335,6 +358,8 @@ loadIds(JNIEnv* env)
"(Ljava/lang/String;Lorg/jruby/RubyClass;Lorg/jruby/runtime/ObjectAllocator;Lorg/jruby/RubyModule;)Lorg/jruby/RubyClass;");
RubyClass_setAllocator_method = getMethodID(env, RubyClass_class, "setAllocator",
"(Lorg/jruby/runtime/ObjectAllocator;)V");
RubyModule_undef_method = getMethodID(env, RubyModule_class, "undef",
"(Lorg/jruby/runtime/ThreadContext;Ljava/lang/String;)V");
Ruby_getClassFromPath_method = getMethodID(env, Ruby_class, "getClassFromPath",
"(Ljava/lang/String;)Lorg/jruby/RubyModule;");
ObjectAllocator_allocate_method = getMethodID(env, ObjectAllocator_class, "allocate",
Expand Down
3 changes: 3 additions & 0 deletions cext/src/jruby.h
Expand Up @@ -124,11 +124,14 @@ namespace jruby {
extern jmethodID JRuby_nativeBlockingRegion;
extern jmethodID JRuby_newThread;
extern jmethodID JRuby_newProc;
extern jmethodID JRuby_getMetaClass;

extern jmethodID RubyArray_toJavaArray_method;
extern jmethodID RubyClass_newClass_method;
extern jmethodID Ruby_defineClass_method;
extern jmethodID Ruby_defineClassUnder_method;
extern jmethodID RubyClass_setAllocator_method;
extern jmethodID RubyModule_undef_method;
extern jmethodID Ruby_getClassFromPath_method;
extern jmethodID ObjectAllocator_allocate_method;
extern jmethodID RubyClass_getAllocator_method;
Expand Down
4 changes: 1 addition & 3 deletions cext/src/module.cpp
Expand Up @@ -157,11 +157,9 @@ rb_undef_method(VALUE klass, const char* method)
{
JLocalEnv env;

jmethodID undef = getMethodID(env, RubyModule_class, "undef",
"(Lorg/jruby/runtime/ThreadContext;Ljava/lang/String;)V");
jobject ctxt = env->CallObjectMethod(getRuntime(), Ruby_getCurrentContext_method);
checkExceptions(env);
env->CallObjectMethod(valueToObject(env, klass), undef, ctxt, env->NewStringUTF(method));
env->CallObjectMethod(valueToObject(env, klass), RubyModule_undef_method, ctxt, env->NewStringUTF(method));
checkExceptions(env);
}

Expand Down
Binary file added lib/native/Darwin/libjffi-1.0.jnilib
Binary file not shown.
Binary file modified lib/native/Darwin/libjruby-cext.jnilib
Binary file not shown.
5 changes: 5 additions & 0 deletions src/org/jruby/cext/JRuby.java
Expand Up @@ -276,6 +276,11 @@ public static void threadSleep(Ruby runtime, int interval) {
// Thread wakeup, do nothing
}
}

public static long getMetaClass(IRubyObject object) {
RubyClass metaClass = object.getMetaClass();
return Handle.nativeHandle(metaClass);
}

public static final class NativeFunctionTask implements BlockingTask {

Expand Down

0 comments on commit 48c6f02

Please sign in to comment.