extern "C" VALUE rb_struct_define(const char* name_cstr, ...) { JLocalEnv env; va_list varargs; std::vector<char *> args; va_start(varargs, name_cstr); char* cp; while ((cp = va_arg(varargs, char *)) != NULL) { args.push_back(cp); } va_end(varargs); jobjectArray argArray = env->NewObjectArray(args.size() + 1, IRubyObject_class, NULL); checkExceptions(env); env->SetObjectArrayElement(argArray, 0, name_cstr == NULL ? getNil() : valueToObject(env, rb_str_new_cstr(name_cstr))); checkExceptions(env); for (unsigned int i = 0; i < args.size(); i++) { env->SetObjectArrayElement(argArray, i + 1, valueToObject(env, rb_str_new_cstr(args[i]))); checkExceptions(env); } jmethodID mid = getCachedMethodID(env, Ruby_class, "getStructClass", "()Lorg/jruby/RubyClass;"); jobject structClass = env->CallObjectMethod(getRuntime(), mid); jobject newStructSubclass = env->CallStaticObjectMethod(RubyStruct_class, RubyStruct_newInstance, structClass, argArray, getNullBlock()); checkExceptions(env); return objectToValue(env, newStructSubclass); }
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" 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_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_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); }
Symbol* jruby::resolveSymbolById(ID id) { JLocalEnv env; jobject obj = env->CallStaticObjectMethod(Symbol_class, RubySymbol_getSymbolLong, jruby::getRuntime(), (jlong) id); if (env->IsSameObject(obj, NULL)) { rb_raise(rb_eRuntimeError, "could not resolve symbol ID %lld", (long long) id); } return addSymbol(env, id, obj); }
extern "C" void rb_define_method(VALUE klass, const char* meth, VALUE(*fn)(ANYARGS), int arity) { JLocalEnv env; jmethodID JRuby_newMethod = getStaticMethodID(env, JRuby_class, "newMethod", "(Lorg/jruby/RubyModule;JI)Lorg/jruby/internal/runtime/methods/DynamicMethod;"); jmethodID RubyModule_addMethod_method = getMethodID(env, RubyModule_class, "addMethod", "(Ljava/lang/String;Lorg/jruby/internal/runtime/methods/DynamicMethod;)V"); jobject module = valueToObject(env, klass); env->CallVoidMethod(module, RubyModule_addMethod_method, env->NewStringUTF(meth), env->CallStaticObjectMethod(JRuby_class, JRuby_newMethod, module, (jlong)(intptr_t) fn, arity)); checkExceptions(env); }
static VALUE newArray(long len) { if (len < 0) { rb_raise(rb_eArgError, "negative array size (or size too big)"); } JLocalEnv env; jobject ary = env->CallStaticObjectMethod(RubyArray_class, RubyArray_newArray, getRuntime(), (jlong)len); checkExceptions(env); VALUE ary_value = objectToValue(env, ary); return ary_value; }
extern "C" void rb_define_singleton_method(VALUE object, const char* meth, VALUE(*fn)(ANYARGS), int arity) { JLocalEnv env; jmethodID IRubyObject_getSingletonClass_method = getMethodID(env, IRubyObject_class, "getSingletonClass", "()Lorg/jruby/RubyClass;"); jobject singleton = env->CallObjectMethod(valueToObject(env, object), IRubyObject_getSingletonClass_method); jmethodID JRuby_newMethod = getStaticMethodID(env, JRuby_class, "newMethod", "(Lorg/jruby/RubyModule;JI)Lorg/jruby/internal/runtime/methods/DynamicMethod;"); jmethodID RubyModule_addMethod_method = getMethodID(env, RubyModule_class, "addMethod", "(Ljava/lang/String;Lorg/jruby/internal/runtime/methods/DynamicMethod;)V"); env->CallVoidMethod(singleton, RubyModule_addMethod_method, env->NewStringUTF(meth), env->CallStaticObjectMethod(JRuby_class, JRuby_newMethod, singleton, (jlong)(intptr_t) fn, arity)); checkExceptions(env); }
extern "C" VALUE rb_data_object_alloc(VALUE klass, void* data, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree) { Handle* h = new Handle; JLocalEnv env; h->data = data; h->dmark = dmark; h->dfree = dfree; h->finalize = rubydata_finalize; h->type = T_DATA; jobject obj = env->CallStaticObjectMethod(RubyData_class, RubyData_newRubyData_method, getRuntime(), valueToObject(env, klass), (jlong) h); checkExceptions(env); h->obj = env->NewWeakGlobalRef(obj); checkExceptions(env); return (VALUE) (uintptr_t) h; }
extern "C" VALUE rb_class_of(VALUE obj) { JLocalEnv env; return (VALUE) env->CallStaticObjectMethod(JRuby_class, JRuby_getMetaClass, valueToObject(env, obj)); }