Example #1
0
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);
}
Example #2
0
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);
}
Example #3
0
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);
}
Example #4
0
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);
}
Example #5
0
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);
}
Example #6
0
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);
}
Example #7
0
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);
}
Example #8
0
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);
}
Example #9
0
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);
    }
}
Example #10
0
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);
}
Example #11
0
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);
}
Example #12
0
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);
}
Example #13
0
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);
}
Example #14
0
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);
}
Example #15
0
extern "C" VALUE
rb_iv_set(VALUE obj, const char* name, VALUE value)
{
    JLocalEnv env;

    char var_name[strlen(name) + 2];
    (name[0] != '@') ? strcpy(var_name, "@")[0] : var_name[0] = '\0';
    strcat(var_name, name);

    jobject retval = env->CallObjectMethod(valueToObject(env, obj), RubyBasicObject_setInstanceVariable_method,
            env->NewStringUTF(var_name), valueToObject(env, value));
    checkExceptions(env);
    return objectToValue(env, retval);
}
Example #16
0
extern "C" VALUE
rb_define_module_under(VALUE module, const char* name)
{
    JLocalEnv env;

    jmethodID Ruby_defineModuleUnder_method = getMethodID(env, Ruby_class, "defineModuleUnder",
            "(Ljava/lang/String;Lorg/jruby/RubyModule;)Lorg/jruby/RubyModule;");
    jobject mod = env->CallObjectMethod(getRuntime(), Ruby_defineModuleUnder_method,
            env->NewStringUTF(name), valueToObject(env, module));
    checkExceptions(env);

    return objectToValue(env, mod);

}
Example #17
0
extern "C" ID
rb_intern2(const char* name, long len)
{
    std::map<StringKey, ID>::iterator it = nonConstSymbolMap.find(StringKey(name, len));
    if (it != nonConstSymbolMap.end()) {
        return it->second;
    }

    JLocalEnv env;
    jobject result = env->CallObjectMethod(getRuntime(), Ruby_newSymbol_method, env->NewStringUTF(name));
    checkExceptions(env);

    Symbol* sym = addSymbol(env, env->GetIntField(result, RubySymbol_id_field), result);
    nonConstSymbolMap.insert(std::map<StringKey, ID>::value_type(StringKey(sym->cstr, (long) len), sym->id));

    return sym->id;
}
Example #18
0
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);
}
Example #19
0
extern "C" VALUE
rb_struct_new(VALUE klass, ...)
{
    JLocalEnv env;
    jmethodID mid = getCachedMethodID(env, RubyBasicObject_class, "getInternalVariable", 
        "(Ljava/lang/String;)Ljava/lang/Object;");

    int size = NUM2INT(objectToValue(env, env->CallObjectMethod(valueToObject(env, klass), mid, env->NewStringUTF("__size__"))));

    VALUE* values = (VALUE *) alloca(sizeof(VALUE) * size);

    va_list args;
    va_start(args, klass);
    for (int i = 0; i < size; ++i) {
        values[i] = va_arg(args, VALUE);
    }

    va_end(args);

    return callMethodA(klass, "new", size, values);
}