extern "C" long long rb_num2ll(VALUE v) { if (FIXNUM_P(v)) { return FIX2LONG(v); } Handle* h = Handle::valueOf(v); if (h->getType() == T_FIXNUM) { return ((RubyFixnum *) h)->longValue(); } JLocalEnv env; jsync(env); jvalue params[1]; params[0].l = valueToObject(env, v); jlong result = env->CallStaticLongMethodA(RubyNumeric_class, RubyNumeric_num2long_method, params); checkExceptions(env); return (long long) result; }
extern "C" VALUE rb_data_object_alloc(VALUE klass, void* data, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree) { JLocalEnv env; RubyData* h = new RubyData; TAILQ_INSERT_TAIL(&dataHandles, h, dataList); h->toRData()->data = data; h->toRData()->dmark = dmark; h->toRData()->dfree = dfree; h->setType(T_DATA); jvalue params[3]; params[0].l = getRuntime(); params[1].l = valueToObject(env, klass); params[2].j = p2j(h); jobject obj = env->CallStaticObjectMethodA(RubyData_class, RubyData_newRubyData_method, params); checkExceptions(env); h->obj = env->NewGlobalRef(obj); checkExceptions(env); return (VALUE) (uintptr_t) h; }
extern "C" VALUE rb_rescue(VALUE (*b_proc)(ANYARGS), VALUE data1, VALUE (*r_proc)(ANYARGS), VALUE data2) { try { return (*b_proc)(data1); } catch (jruby::JavaException& ex) { JLocalEnv env; jthrowable t = ex.getCause(env); if (!env->IsInstanceOf(t, RaiseException_class)) { // Not a ruby exception, just propagate throw; } jobject rubyException = env->GetObjectField(t, RaiseException_exception_field); checkExceptions(env); VALUE exc = objectToValue(env, rubyException); if (rb_obj_is_kind_of(exc, rb_eStandardError)) { VALUE result = (*r_proc)(data2); env->CallStaticVoidMethod(JRuby_class, JRuby_clearErrorInfo, jruby::getRuntime()); return result; } rb_raise(rb_eTypeError, "wrong exception type raised (expected StandardError subclass)"); return Qnil; } }
extern "C" int rb_respond_to(VALUE obj, ID id) { JLocalEnv env; jboolean ret = env->CallBooleanMethod(valueToObject(env, obj), IRubyObject_respondsTo_method, idToString(env, id)); checkExceptions(env); return ret != JNI_FALSE; }
extern "C" char rb_num2chr(VALUE v) { JLocalEnv env; jbyte result = env->CallStaticByteMethod(RubyNumeric_class, RubyNumeric_num2chr_method, valueToObject(env, v)); checkExceptions(env); return (char) result; }
extern "C" int rb_ary_size(VALUE ary) { JLocalEnv env; int length = (int)(env->GetIntField(valueToObject(env, ary), RubyArray_length_field)); checkExceptions(env); return length; }
extern "C" VALUE rb_define_module(const char* name) { JLocalEnv env; jobject mod = env->CallObjectMethod(getRuntime(), Ruby_defineModule_method, env->NewStringUTF(name)); checkExceptions(env); return objectToValue(env, mod); }
extern "C" void rb_define_const(VALUE module, const char* name, VALUE obj) { JLocalEnv env; jmethodID mid = getMethodID(env, RubyModule_class, "defineConstant", "(Ljava/lang/String;Lorg/jruby/runtime/builtin/IRubyObject;)V"); env->CallVoidMethod(valueToObject(env, module), mid, env->NewStringUTF(name), valueToObject(env, obj)); checkExceptions(env); }
extern "C" VALUE rb_block_proc() { JLocalEnv env; jobject proc = env->CallStaticObjectMethod(JRuby_class, JRuby_getBlockProc, getRuntime()); checkExceptions(env); return objectToValue(env, proc); }
extern "C" VALUE rb_thread_create(VALUE (*fn)(ANYARGS), void* arg) { JLocalEnv env; jobject ret = env->CallStaticObjectMethod(JRuby_class, JRuby_newThread, getRuntime(), p2j((void*)fn), valueToObject(env, (arg == NULL ? rb_ary_new() : (VALUE)arg))); checkExceptions(env); return objectToValue(env, ret); }
extern "C" VALUE rb_class_new(VALUE klass) { JLocalEnv env; jobject jklass = env->CallStaticObjectMethod(RubyClass_class, RubyClass_newClass_method, getRuntime(), valueToObject(env, klass)); checkExceptions(env); return objectToValue(env, jklass); }
extern "C" VALUE rb_path2class(const char* path) { JLocalEnv env; jobject klass = env->CallObjectMethod(getRuntime(), Ruby_getClassFromPath_method, env->NewStringUTF(path)); checkExceptions(env); return objectToValue(env, klass); }
extern "C" VALUE rb_path_to_class(VALUE pathname) { JLocalEnv env; jobject klass = env->CallObjectMethod(getRuntime(), Ruby_getClassFromPath_method, env->NewStringUTF(rb_str_ptr_readonly(pathname))); checkExceptions(env); return objectToValue(env, klass); }
extern "C" VALUE rb_yield_splat(VALUE array) { JLocalEnv env; jobject retval = env->CallStaticObjectMethod(JRuby_class, JRuby_yield, getRuntime(), valueToObject(env, array)); checkExceptions(env); return objectToValue(env, retval); }
extern "C" void rb_str_modify(VALUE str) { JLocalEnv env; jmethodID mid = getCachedMethodID(env, RubyString_class, "modify", "()V"); env->CallVoidMethod(valueToObject(env, str), mid); checkExceptions(env); }
VALUE jruby::getSymbol(const char* name) { JLocalEnv env; jobject result = env->CallObjectMethod(getRuntime(), Ruby_newSymbol_method, env->NewStringUTF(name)); checkExceptions(env); return objectToValue(env, result); }
extern "C" void rb_const_set(VALUE parent, ID name, VALUE object) { JLocalEnv env; jmethodID mid = getMethodID(env, RubyModule_class, "setConstant", "(Ljava/lang/String;Lorg/jruby/runtime/builtin/IRubyObject;)Lorg/jruby/runtime/builtin/IRubyObject;"); env->CallObjectMethod(valueToObject(env, parent), mid, idToString(env, name), valueToObject(env, object)); checkExceptions(env); }
extern "C" void rb_define_alias(VALUE klass, const char* new_name, const char* old_name) { JLocalEnv env; jmethodID mid = getCachedMethodID(env, RubyModule_class, "defineAlias", "(Ljava/lang/String;Ljava/lang/String;)V"); env->CallVoidMethod(valueToObject(env, klass), mid, env->NewStringUTF(new_name), env->NewStringUTF(old_name)); checkExceptions(env); }
extern "C" void rb_undef_method(VALUE klass, const char* method) { JLocalEnv env; jobject ctxt = env->CallObjectMethod(getRuntime(), Ruby_getCurrentContext_method); checkExceptions(env); env->CallObjectMethod(valueToObject(env, klass), RubyModule_undef_method, ctxt, env->NewStringUTF(method)); checkExceptions(env); }
extern "C" VALUE rb_obj_alloc(VALUE klass) { JLocalEnv env; jobject allocator = getDefaultAllocator(env, klass); jobject instance = env->CallObjectMethod(allocator, ObjectAllocator_allocate_method, getRuntime(), valueToObject(env, klass)); checkExceptions(env); return objectToValue(env, instance); }
extern "C" VALUE rb_const_get_from(VALUE module, ID symbol) { JLocalEnv env; jmethodID mid = getMethodID(env, RubyModule_class, "getConstantFrom", "(Ljava/lang/String;)Lorg/jruby/runtime/builtin/IRubyObject;"); jobject c = env->CallObjectMethod(valueToObject(env, module), mid, idToString(env, symbol)); checkExceptions(env); return objectToValue(env, c); }
extern "C" void jruby_infect(VALUE object1, VALUE object2) { if (OBJ_TAINTED(object1)) { JLocalEnv env; jmethodID mid = getCachedMethodID(env, IRubyObject_class, "infectBy", "(Lorg/jruby/runtime/builtin/IRubyObject;)Lorg/jruby/runtime/builtin/IRubyObject;"); env->CallObjectMethod(valueToObject(env, object2), mid, object1); checkExceptions(env); } }
extern "C" VALUE rb_str_resize(VALUE str, long size) { long length = (long) jruby_str_length(str); if (size != length) { JLocalEnv env; env->CallVoidMethod(valueToObject(env, str), RubyString_resize_method, (jint)size); checkExceptions(env); jruby_rstring(str)->len = size; } return str; }
extern "C" VALUE rb_thread_blocking_region(rb_blocking_function_t func, void* data, rb_unblock_function_t ub_func, void* data2) { JLocalEnv env; jlong jret = env->CallStaticLongMethod(JRuby_class, JRuby_nativeBlockingRegion, getRuntime(), p2j((void *)func), p2j(data), p2j((void *)ub_func), p2j(data2)); checkExceptions(env); return (VALUE)(j2p(jret)); }
extern "C" void 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)); checkExceptions(env); }
extern "C" VALUE rb_singleton_class(VALUE obj) { JLocalEnv env; jmethodID IRubyObject_getSingletonClass_method = getCachedMethodID(env, IRubyObject_class, "getSingletonClass", "()Lorg/jruby/RubyClass;"); jobject singleton = env->CallObjectMethod(valueToObject(env, obj), IRubyObject_getSingletonClass_method); checkExceptions(env); return objectToValue(env, singleton); }
extern "C" double rb_big2dbl(VALUE obj) { JLocalEnv env; jvalue params[1]; params[0].l = valueToObject(env, obj); jdouble result = env->CallStaticDoubleMethodA(RubyBignum_class, RubyBignum_big2dbl_method, params); checkExceptions(env); return result; }
extern "C" VALUE rb_define_class(const char* name, VALUE parent) { JLocalEnv env; VALUE super = parent ? parent : rb_cObject; jobject result = env->CallObjectMethod(getRuntime(), Ruby_defineClass_method, env->NewStringUTF(name), valueToObject(env, super), getDefaultAllocator(env, super)); checkExceptions(env); return objectToValue(env, result); }
extern "C" void rb_define_alloc_func(VALUE klass, VALUE (*fn)(VALUE)) { JLocalEnv env; jobject allocator = env->NewObject(NativeObjectAllocator_class, getMethodID(env, NativeObjectAllocator_class, "<init>", "(J)V"), p2j((void *) fn)); checkExceptions(env); env->CallVoidMethod(valueToObject(env, klass), RubyClass_setAllocator_method, allocator); }
extern "C" VALUE rb_const_get_at(VALUE module, ID symbol) { JLocalEnv env; jmethodID mid = getMethodID(env, RubyModule_class, "getConstantAt", "(Ljava/lang/String;)Lorg/jruby/runtime/builtin/IRubyObject;"); jobject c = env->CallObjectMethod(valueToObject(env, module), mid, idToString(env, symbol)); checkExceptions(env); // Check for null return and call const_missing, if neccessary return c == NULL ? callMethod(module, "const_missing", 1, ID2SYM(symbol)) : objectToValue(env, c); }