本文整理汇总了C++中rb_obj_is_kind_of函数的典型用法代码示例。如果您正苦于以下问题:C++ rb_obj_is_kind_of函数的具体用法?C++ rb_obj_is_kind_of怎么用?C++ rb_obj_is_kind_of使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了rb_obj_is_kind_of函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: vm_call_method
//.........这里部分代码省略.........
rb_num_t i = num - 1;
VALUE sym;
if (num == 0) {
rb_raise(rb_eArgError, "no method name given");
}
sym = TOPN(i);
id = SYMBOL_P(sym) ? SYM2ID(sym) : rb_to_id(sym);
/* shift arguments */
if (i > 0) {
MEMMOVE(&TOPN(i), &TOPN(i-1), VALUE, i);
}
me = rb_method_entry(CLASS_OF(recv), id);
num -= 1;
DEC_SP(1);
flag |= VM_CALL_FCALL_BIT | VM_CALL_OPT_SEND_BIT;
goto start_method_dispatch;
}
case OPTIMIZED_METHOD_TYPE_CALL: {
rb_proc_t *proc;
int argc = num;
VALUE *argv = ALLOCA_N(VALUE, num);
GetProcPtr(recv, proc);
MEMCPY(argv, cfp->sp - num, VALUE, num);
cfp->sp -= num + 1;
val = rb_vm_invoke_proc(th, proc, proc->block.self, argc, argv, blockptr);
break;
}
default:
rb_bug("eval_invoke_method: unsupported optimized method type (%d)",
me->def->body.optimize_type);
}
break;
}
default:{
rb_bug("eval_invoke_method: unsupported method type (%d)", me->def->type);
break;
}
}
}
else {
int noex_safe;
if (!(flag & VM_CALL_FCALL_BIT) &&
(me->flag & NOEX_MASK) & NOEX_PRIVATE) {
int stat = NOEX_PRIVATE;
if (flag & VM_CALL_VCALL_BIT) {
stat |= NOEX_VCALL;
}
val = vm_method_missing(th, id, recv, num, blockptr, stat);
}
else if (!(flag & VM_CALL_OPT_SEND_BIT) && (me->flag & NOEX_MASK) & NOEX_PROTECTED) {
VALUE defined_class = me->klass;
if (RB_TYPE_P(defined_class, T_ICLASS)) {
defined_class = RBASIC(defined_class)->klass;
}
if (!rb_obj_is_kind_of(cfp->self, defined_class)) {
val = vm_method_missing(th, id, recv, num, blockptr, NOEX_PROTECTED);
}
else {
goto normal_method_dispatch;
}
}
else if ((noex_safe = NOEX_SAFE(me->flag)) > th->safe_level &&
(noex_safe > 2)) {
rb_raise(rb_eSecurityError, "calling insecure method: %s", rb_id2name(id));
}
else {
goto normal_method_dispatch;
}
}
}
else {
/* method missing */
int stat = 0;
if (flag & VM_CALL_VCALL_BIT) {
stat |= NOEX_VCALL;
}
if (flag & VM_CALL_SUPER_BIT) {
stat |= NOEX_SUPER;
}
if (id == idMethodMissing) {
VALUE *argv = ALLOCA_N(VALUE, num);
vm_method_missing_args(th, argv, num - 1, 0, stat);
rb_raise_method_missing(th, num, argv, recv, stat);
}
else {
val = vm_method_missing(th, id, recv, num, blockptr, stat);
}
}
RUBY_VM_CHECK_INTS();
return val;
}
示例2: rb_grn_view_sort
/* FIXME: DON'T WORK!!! */
static VALUE
rb_grn_view_sort (int argc, VALUE *argv, VALUE self)
{
VALUE rb_result = Qnil;
#ifdef WIN32
rb_raise(rb_eNotImpError, "grn_view_add() isn't available on Windows.");
#else
grn_ctx *context = NULL;
grn_obj *view;
grn_obj *result;
grn_table_sort_key *keys;
int i, n_keys;
int n_records, offset = 0, limit = -1;
VALUE rb_keys, options;
VALUE rb_offset, rb_limit;
VALUE *rb_sort_keys;
grn_table_cursor *cursor;
VALUE exception;
grn_obj id;
rb_grn_table_deconstruct(SELF(self), &view, &context,
NULL, NULL,
NULL, NULL, NULL,
NULL);
rb_scan_args(argc, argv, "11", &rb_keys, &options);
if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_keys, rb_cArray)))
rb_raise(rb_eArgError, "keys should be an array of key: <%s>",
rb_grn_inspect(rb_keys));
n_keys = RARRAY_LEN(rb_keys);
rb_sort_keys = RARRAY_PTR(rb_keys);
keys = ALLOCA_N(grn_table_sort_key, n_keys);
for (i = 0; i < n_keys; i++) {
VALUE rb_sort_options, rb_key, rb_resolved_key, rb_order;
if (RVAL2CBOOL(rb_obj_is_kind_of(rb_sort_keys[i], rb_cHash))) {
rb_sort_options = rb_sort_keys[i];
} else if (RVAL2CBOOL(rb_obj_is_kind_of(rb_sort_keys[i], rb_cArray))) {
rb_sort_options = rb_hash_new();
rb_hash_aset(rb_sort_options,
RB_GRN_INTERN("key"),
rb_ary_entry(rb_sort_keys[i], 0));
rb_hash_aset(rb_sort_options,
RB_GRN_INTERN("order"),
rb_ary_entry(rb_sort_keys[i], 1));
} else {
rb_sort_options = rb_hash_new();
rb_hash_aset(rb_sort_options,
RB_GRN_INTERN("key"),
rb_sort_keys[i]);
}
rb_grn_scan_options(rb_sort_options,
"key", &rb_key,
"order", &rb_order,
NULL);
if (RVAL2CBOOL(rb_obj_is_kind_of(rb_key, rb_cString))) {
rb_resolved_key = rb_grn_table_get_column(self, rb_key);
} else {
rb_resolved_key = rb_key;
}
keys[i].key = RVAL2GRNOBJECT(rb_resolved_key, &context);
if (!keys[i].key) {
rb_raise(rb_eGrnNoSuchColumn,
"no such column: <%s>: <%s>",
rb_grn_inspect(rb_key), rb_grn_inspect(self));
}
if (NIL_P(rb_order)) {
keys[i].flags = 0;
} else if (rb_grn_equal_option(rb_order, "desc") ||
rb_grn_equal_option(rb_order, "descending")) {
keys[i].flags = GRN_TABLE_SORT_DESC;
} else if (rb_grn_equal_option(rb_order, "asc") ||
rb_grn_equal_option(rb_order, "ascending")) {
keys[i].flags = GRN_TABLE_SORT_ASC;
} else {
rb_raise(rb_eArgError,
"order should be one of "
"[nil, :desc, :descending, :asc, :ascending]: %s",
rb_grn_inspect(rb_order));
}
}
rb_grn_scan_options(options,
"offset", &rb_offset,
"limit", &rb_limit,
NULL);
if (!NIL_P(rb_offset))
offset = NUM2INT(rb_offset);
if (!NIL_P(rb_limit))
limit = NUM2INT(rb_limit);
result = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_VIEW,
NULL, NULL);
grn_view_add(context, result,
grn_table_create(context, NULL, 0, NULL, GRN_TABLE_NO_KEY,
//.........这里部分代码省略.........
示例3: write_doc
static void write_doc(bson_buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_id) {
bson_buffer_position start_position = bson_buffer_get_position(buffer);
bson_buffer_position length_location = bson_buffer_save_space(buffer, 4);
bson_buffer_position length;
int allow_id;
int max_size;
int (*write_function)(VALUE, VALUE, VALUE) = NULL;
VALUE id_str = rb_str_new2("_id");
VALUE id_sym = ID2SYM(rb_intern("_id"));
if (length_location == -1) {
rb_raise(rb_eNoMemError, "failed to allocate memory in buffer.c");
}
// write '_id' first if move_id is true. then don't allow an id to be written.
if(move_id == Qtrue) {
allow_id = 0;
if (rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) {
VALUE id = rb_hash_aref(hash, id_str);
write_element_with_id(id_str, id, pack_extra(buffer, check_keys));
} else if (rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue) {
VALUE id = rb_hash_aref(hash, id_sym);
write_element_with_id(id_sym, id, pack_extra(buffer, check_keys));
}
}
else {
allow_id = 1;
// Ensure that hash doesn't contain both '_id' and :_id
if ((rb_obj_classname(hash), "Hash") == 0) {
if ((rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) &&
(rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue)) {
VALUE oid_sym = rb_hash_delete(hash, id_sym);
rb_funcall(hash, rb_intern("[]="), 2, id_str, oid_sym);
}
}
}
if(allow_id == 1) {
write_function = write_element_with_id;
}
else {
write_function = write_element_without_id;
}
// we have to check for an OrderedHash and handle that specially
if (strcmp(rb_obj_classname(hash), "BSON::OrderedHash") == 0) {
VALUE keys = rb_funcall(hash, rb_intern("keys"), 0);
int i;
for(i = 0; i < RARRAY_LEN(keys); i++) {
VALUE key = rb_ary_entry(keys, i);
VALUE value = rb_hash_aref(hash, key);
write_function(key, value, pack_extra(buffer, check_keys));
}
} else if (rb_obj_is_kind_of(hash, RB_HASH) == Qtrue) {
rb_hash_foreach(hash, write_function, pack_extra(buffer, check_keys));
} else {
bson_buffer_free(buffer);
rb_raise(InvalidDocument, "BSON.serialize takes a Hash but got a %s", rb_obj_classname(hash));
}
// write null byte and fill in length
SAFE_WRITE(buffer, &zero, 1);
length = bson_buffer_get_position(buffer) - start_position;
// make sure that length doesn't exceed the max size (determined by server, defaults to 4mb)
max_size = bson_buffer_get_max_size(buffer);
if (length > max_size) {
bson_buffer_free(buffer);
rb_raise(InvalidDocument,
"Document too large: This BSON document is limited to %d bytes.",
max_size);
}
SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&length, 4);
}
示例4: nstruct_add_type
static VALUE
nstruct_add_type(VALUE type, int argc, VALUE *argv, VALUE nst)
{
VALUE ofs, size;
ID id;
int i;
VALUE name=Qnil;
size_t *shape=NULL;
int ndim=0;
ssize_t stride;
narray_view_t *nt;
int j;
for (i=0; i<argc; i++) {
switch(TYPE(argv[i])) {
case T_STRING:
case T_SYMBOL:
if (NIL_P(name)) {
name = argv[i];
break;
}
rb_raise(rb_eArgError,"multiple name in struct definition");
case T_ARRAY:
if (shape) {
rb_raise(rb_eArgError,"multiple shape in struct definition");
}
ndim = RARRAY_LEN(argv[i]);
if (ndim > NA_MAX_DIMENSION) {
rb_raise(rb_eArgError,"too large number of dimensions");
}
if (ndim == 0) {
rb_raise(rb_eArgError,"array is empty");
}
shape = ALLOCA_N(size_t, ndim);
na_array_to_internal_shape(Qnil, argv[i], shape);
break;
}
}
id = rb_to_id(name);
name = ID2SYM(id);
if (rb_obj_is_kind_of(type,cNArray)) {
narray_t *na;
GetNArray(type,na);
type = CLASS_OF(type);
ndim = na->ndim;
shape = na->shape;
}
type = rb_narray_view_new(type,ndim,shape);
GetNArrayView(type,nt);
nt->stridx = ALLOC_N(stridx_t,ndim);
stride = na_dtype_elmsz(CLASS_OF(type));
for (j=ndim; j--; ) {
SDX_SET_STRIDE(nt->stridx[j], stride);
stride *= shape[j];
}
ofs = rb_iv_get(nst, "__offset__");
nt->offset = NUM2SIZET(ofs);
size = rb_funcall(type, rb_intern("byte_size"), 0);
rb_iv_set(nst, "__offset__", rb_funcall(ofs,'+',1,size));
rb_ary_push(rb_iv_get(nst,"__members__"),
rb_ary_new3(4,name,type,ofs,size)); // <- field definition
return Qnil;
}
示例5: cb_get_callback
void
cb_get_callback(lcb_t handle, const void *cookie, lcb_error_t error, const lcb_get_resp_t *resp)
{
struct cb_context_st *ctx = (struct cb_context_st *)cookie;
struct cb_bucket_st *bucket = ctx->bucket;
VALUE key, val, flags, cas, exc = Qnil, res, raw;
ctx->nqueries--;
key = STR_NEW((const char*)resp->v.v0.key, resp->v.v0.nkey);
cb_strip_key_prefix(bucket, key);
if (error != LCB_KEY_ENOENT || !ctx->quiet) {
exc = cb_check_error(error, "failed to get value", key);
if (exc != Qnil) {
rb_ivar_set(exc, cb_id_iv_operation, cb_sym_get);
ctx->exception = exc;
}
}
flags = ULONG2NUM(resp->v.v0.flags);
cas = ULL2NUM(resp->v.v0.cas);
raw = STR_NEW((const char*)resp->v.v0.bytes, resp->v.v0.nbytes);
val = cb_decode_value(ctx->transcoder, raw, resp->v.v0.flags, ctx->transcoder_opts);
if (rb_obj_is_kind_of(val, rb_eStandardError)) {
VALUE exc_str = rb_funcall(val, cb_id_to_s, 0);
VALUE msg = rb_funcall(rb_mKernel, cb_id_sprintf, 3,
rb_str_new2("unable to convert value for key \"%s\": %s"), key, exc_str);
ctx->exception = rb_exc_new3(cb_eValueFormatError, msg);
rb_ivar_set(ctx->exception, cb_id_iv_operation, cb_sym_get);
rb_ivar_set(ctx->exception, cb_id_iv_key, key);
rb_ivar_set(ctx->exception, cb_id_iv_inner_exception, val);
val = Qnil;
}
if (bucket->async) { /* asynchronous */
if (ctx->proc != Qnil) {
res = rb_class_new_instance(0, NULL, cb_cResult);
rb_ivar_set(res, cb_id_iv_error, exc);
rb_ivar_set(res, cb_id_iv_operation, cb_sym_get);
rb_ivar_set(res, cb_id_iv_key, key);
rb_ivar_set(res, cb_id_iv_value, val);
rb_ivar_set(res, cb_id_iv_flags, flags);
rb_ivar_set(res, cb_id_iv_cas, cas);
cb_proc_call(bucket, ctx->proc, 1, res);
}
} else { /* synchronous */
if (NIL_P(exc) && error != LCB_KEY_ENOENT) {
if (ctx->extended) {
val = rb_ary_new3(3, val, flags, cas);
}
if (ctx->all_replicas) {
VALUE ary = rb_hash_aref(ctx->rv, key);
if (NIL_P(ary)) {
ary = rb_ary_new();
rb_hash_aset(ctx->rv, key, ary);
}
rb_ary_push(ary, val);
} else {
rb_hash_aset(ctx->rv, key, val);
}
}
}
if (ctx->nqueries == 0) {
ctx->proc = Qnil;
if (bucket->async) {
cb_context_free(ctx);
}
}
(void)handle;
}
示例6: rb_rtype_valid
VALUE
rb_rtype_valid(VALUE self, VALUE expected, VALUE value) {
long i;
VALUE e_keys;
VALUE v_keys;
switch(TYPE(expected)) {
case T_MODULE:
case T_CLASS:
return rb_obj_is_kind_of(value, expected) ? Qtrue : Qfalse;
case T_SYMBOL:
return rb_respond_to(value, rb_to_id(expected)) ? Qtrue : Qfalse;
case T_REGEXP:
return rb_reg_match( expected, rb_funcall(value, id_to_s, 0) ) != Qnil ? Qtrue : Qfalse;
case T_HASH:
if( !RB_TYPE_P(value, T_HASH) ) {
return Qfalse;
}
e_keys = rb_funcall(expected, id_keys, 0);
v_keys = rb_funcall(value, id_keys, 0);
if( !RTEST(rb_funcall(e_keys, id_eqeq, 1, v_keys)) ) {
return Qfalse;
}
for(i = 0; i < RARRAY_LEN(e_keys); i++) {
VALUE e_k = rb_ary_entry(e_keys, i);
VALUE e_v = rb_hash_aref(expected, e_k);
if(rb_rtype_valid(self, e_v, rb_hash_aref(value, e_k)) == Qfalse) {
return Qfalse;
}
}
return Qtrue;
case T_ARRAY:
for(i = 0; i < RARRAY_LEN(expected); i++) {
VALUE e = rb_ary_entry(expected, i);
VALUE valid = rb_rtype_valid(self, e, value);
if(valid == Qtrue) {
return Qtrue;
}
}
return Qfalse;
case T_TRUE:
return RTEST(value) ? Qtrue : Qfalse;
case T_FALSE:
return !RTEST(value) ? Qtrue : Qfalse;
case T_NIL:
return value == Qnil;
default:
if(rb_obj_is_kind_of(expected, rb_cRange)) {
return rb_funcall(expected, id_include, 1, value);
}
else if(rb_obj_is_kind_of(expected, rb_cProc)) {
return RTEST(rb_funcall(expected, id_call, 1, value)) ? Qtrue : Qfalse;
}
else if( RTEST(rb_obj_is_kind_of(expected, rb_cRtypeBehaviorBase)) ) {
return rb_funcall(expected, id_valid, 1, value);
}
else {
VALUE str = rb_any_to_s(expected);
rb_raise(rb_eRtypeTypeSignatureError, "Invalid type signature: Unknown type behavior %s", StringValueCStr(str));
return Qfalse;
}
}
}
示例7: ossl_asn1_decode0
static VALUE
ossl_asn1_decode0(unsigned char **pp, long length, long *offset, long depth,
int once, int yield)
{
unsigned char *start, *p;
const unsigned char *p0;
long len, off = *offset;
int hlen, tag, tc, j;
VALUE ary, asn1data, value, tag_class;
ary = rb_ary_new();
p = *pp;
while(length > 0){
start = p;
p0 = p;
j = ASN1_get_object(&p0, &len, &tag, &tc, length);
p = (unsigned char *)p0;
if(j & 0x80) ossl_raise(eASN1Error, NULL);
hlen = p - start;
if(yield){
VALUE arg = rb_ary_new();
rb_ary_push(arg, LONG2NUM(depth));
rb_ary_push(arg, LONG2NUM(off));
rb_ary_push(arg, LONG2NUM(hlen));
rb_ary_push(arg, LONG2NUM(len));
rb_ary_push(arg, (j & V_ASN1_CONSTRUCTED) ? Qtrue : Qfalse);
rb_ary_push(arg, ossl_asn1_class2sym(tc));
rb_ary_push(arg, INT2NUM(tag));
rb_yield(arg);
}
length -= hlen;
off += hlen;
if(len > length) ossl_raise(eASN1Error, "value is too short");
if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
tag_class = sPRIVATE;
else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
tag_class = sCONTEXT_SPECIFIC;
else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
tag_class = sAPPLICATION;
else
tag_class = sUNIVERSAL;
if(j & V_ASN1_CONSTRUCTED){
/* TODO: if j == 0x21 it is indefinite length object. */
if((j == 0x21) && (len == 0)){
long lastoff = off;
value = ossl_asn1_decode0(&p, length, &off, depth+1, 0, yield);
len = off - lastoff;
}
else value = ossl_asn1_decode0(&p, len, &off, depth+1, 0, yield);
}
else{
value = rb_str_new((const char *)p, len);
p += len;
off += len;
}
if(tag_class == sUNIVERSAL &&
tag < ossl_asn1_info_size && ossl_asn1_info[tag].klass){
VALUE klass = *ossl_asn1_info[tag].klass;
long flag = 0;
if(!rb_obj_is_kind_of(value, rb_cArray)){
switch(tag){
case V_ASN1_BOOLEAN:
value = decode_bool(start, hlen+len);
break;
case V_ASN1_INTEGER:
value = decode_int(start, hlen+len);
break;
case V_ASN1_BIT_STRING:
value = decode_bstr(start, hlen+len, &flag);
break;
case V_ASN1_NULL:
value = decode_null(start, hlen+len);
break;
case V_ASN1_ENUMERATED:
value = decode_enum(start, hlen+len);
break;
case V_ASN1_OBJECT:
value = decode_obj(start, hlen+len);
break;
case V_ASN1_UTCTIME: /* FALLTHROUGH */
case V_ASN1_GENERALIZEDTIME:
value = decode_time(start, hlen+len);
break;
default:
/* use original value */
break;
}
}
asn1data = rb_funcall(klass, rb_intern("new"), 1, value);
if(tag == V_ASN1_BIT_STRING){
rb_iv_set(asn1data, "@unused_bits", LONG2NUM(flag));
}
}
else{
asn1data = rb_funcall(cASN1Data, rb_intern("new"), 3,
value, INT2NUM(tag), ID2SYM(tag_class));
}
rb_ary_push(ary, asn1data);
length -= len;
if(once) break;
//.........这里部分代码省略.........
示例8: native_slot_set_value_and_case
void native_slot_set_value_and_case(const char* name,
upb_fieldtype_t type, VALUE type_class,
void* memory, VALUE value,
uint32_t* case_memory,
uint32_t case_number) {
// Note that in order to atomically change the value in memory and the case
// value (w.r.t. Ruby VM calls), we must set the value at |memory| only after
// all Ruby VM calls are complete. The case is then set at the bottom of this
// function.
switch (type) {
case UPB_TYPE_FLOAT:
if (!is_ruby_num(value)) {
rb_raise(cTypeError, "Expected number type for float field '%s' (given %s).",
name, rb_class2name(CLASS_OF(value)));
}
DEREF(memory, float) = NUM2DBL(value);
break;
case UPB_TYPE_DOUBLE:
if (!is_ruby_num(value)) {
rb_raise(cTypeError, "Expected number type for double field '%s' (given %s).",
name, rb_class2name(CLASS_OF(value)));
}
DEREF(memory, double) = NUM2DBL(value);
break;
case UPB_TYPE_BOOL: {
int8_t val = -1;
if (value == Qtrue) {
val = 1;
} else if (value == Qfalse) {
val = 0;
} else {
rb_raise(cTypeError, "Invalid argument for boolean field '%s' (given %s).",
name, rb_class2name(CLASS_OF(value)));
}
DEREF(memory, int8_t) = val;
break;
}
case UPB_TYPE_STRING:
if (CLASS_OF(value) == rb_cSymbol) {
value = rb_funcall(value, rb_intern("to_s"), 0);
} else if (CLASS_OF(value) != rb_cString) {
rb_raise(cTypeError, "Invalid argument for string field '%s' (given %s).",
name, rb_class2name(CLASS_OF(value)));
}
DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value);
break;
case UPB_TYPE_BYTES: {
if (CLASS_OF(value) != rb_cString) {
rb_raise(cTypeError, "Invalid argument for bytes field '%s' (given %s).",
name, rb_class2name(CLASS_OF(value)));
}
DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value);
break;
}
case UPB_TYPE_MESSAGE: {
if (CLASS_OF(value) == CLASS_OF(Qnil)) {
value = Qnil;
} else if (CLASS_OF(value) != type_class) {
// check for possible implicit conversions
VALUE converted_value = NULL;
char* field_type_name = rb_class2name(type_class);
if (strcmp(field_type_name, "Google::Protobuf::Timestamp") == 0 &&
rb_obj_is_kind_of(value, rb_cTime)) {
// Time -> Google::Protobuf::Timestamp
VALUE hash = rb_hash_new();
rb_hash_aset(hash, rb_str_new2("seconds"), rb_funcall(value, rb_intern("to_i"), 0));
rb_hash_aset(hash, rb_str_new2("nanos"), rb_funcall(value, rb_intern("nsec"), 0));
VALUE args[1] = { hash };
converted_value = rb_class_new_instance(1, args, type_class);
} else if (strcmp(field_type_name, "Google::Protobuf::Duration") == 0 &&
rb_obj_is_kind_of(value, rb_cNumeric)) {
// Numeric -> Google::Protobuf::Duration
VALUE hash = rb_hash_new();
rb_hash_aset(hash, rb_str_new2("seconds"), rb_funcall(value, rb_intern("to_i"), 0));
VALUE n_value = rb_funcall(value, rb_intern("remainder"), 1, INT2NUM(1));
n_value = rb_funcall(n_value, rb_intern("*"), 1, INT2NUM(1000000000));
n_value = rb_funcall(n_value, rb_intern("round"), 0);
rb_hash_aset(hash, rb_str_new2("nanos"), n_value);
VALUE args[1] = { hash };
converted_value = rb_class_new_instance(1, args, type_class);
}
// raise if no suitable conversaion could be found
if (converted_value == NULL) {
rb_raise(cTypeError,
"Invalid type %s to assign to submessage field '%s'.",
rb_class2name(CLASS_OF(value)), name);
} else {
value = converted_value;
}
}
DEREF(memory, VALUE) = value;
break;
}
case UPB_TYPE_ENUM: {
int32_t int_val = 0;
//.........这里部分代码省略.........
示例9: f_kind_of_p
inline static VALUE
f_kind_of_p(VALUE x, VALUE c)
{
return rb_obj_is_kind_of(x, c);
}
示例10: ossl_ec_key_initialize
/* call-seq:
* OpenSSL::PKey::EC.new()
* OpenSSL::PKey::EC.new(ec_key)
* OpenSSL::PKey::EC.new(ec_group)
* OpenSSL::PKey::EC.new("secp112r1")
* OpenSSL::PKey::EC.new(pem_string)
* OpenSSL::PKey::EC.new(pem_string [, pwd])
* OpenSSL::PKey::EC.new(der_string)
*
* See the OpenSSL documentation for:
* EC_KEY_*
*/
static VALUE ossl_ec_key_initialize(int argc, VALUE *argv, VALUE self)
{
EVP_PKEY *pkey;
EC_KEY *ec = NULL;
VALUE arg, pass;
VALUE group = Qnil;
char *passwd = NULL;
GetPKey(self, pkey);
if (pkey->pkey.ec)
rb_raise(eECError, "EC_KEY already initialized");
rb_scan_args(argc, argv, "02", &arg, &pass);
if (NIL_P(arg)) {
ec = EC_KEY_new();
} else {
if (rb_obj_is_kind_of(arg, cEC)) {
EC_KEY *other_ec = NULL;
SafeRequire_EC_KEY(arg, other_ec);
ec = EC_KEY_dup(other_ec);
} else if (rb_obj_is_kind_of(arg, cEC_GROUP)) {
ec = EC_KEY_new();
group = arg;
} else {
BIO *in = ossl_obj2bio(arg);
if (!NIL_P(pass)) {
passwd = StringValuePtr(pass);
}
ec = PEM_read_bio_ECPrivateKey(in, NULL, ossl_pem_passwd_cb, passwd);
if (!ec) {
(void)BIO_reset(in);
(void)ERR_get_error();
ec = PEM_read_bio_EC_PUBKEY(in, NULL, ossl_pem_passwd_cb, passwd);
}
if (!ec) {
(void)BIO_reset(in);
(void)ERR_get_error();
ec = d2i_ECPrivateKey_bio(in, NULL);
}
if (!ec) {
(void)BIO_reset(in);
(void)ERR_get_error();
ec = d2i_EC_PUBKEY_bio(in, NULL);
}
BIO_free(in);
if (ec == NULL) {
const char *name = StringValueCStr(arg);
int nid = OBJ_sn2nid(name);
(void)ERR_get_error();
if (nid == NID_undef)
ossl_raise(eECError, "unknown curve name (%s)\n", name);
if ((ec = EC_KEY_new_by_curve_name(nid)) == NULL)
ossl_raise(eECError, "unable to create curve (%s)\n", name);
EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE);
EC_KEY_set_conv_form(ec, POINT_CONVERSION_UNCOMPRESSED);
}
}
}
if (ec == NULL)
ossl_raise(eECError, NULL);
if (!EVP_PKEY_assign_EC_KEY(pkey, ec)) {
EC_KEY_free(ec);
ossl_raise(eECError, "EVP_PKEY_assign_EC_KEY");
}
rb_iv_set(self, "@group", Qnil);
if (!NIL_P(group))
rb_funcall(self, rb_intern("group="), 1, arg);
return self;
}
示例11: ruby_funcall
static void ruby_funcall(xmlXPathParserContextPtr ctx, int nargs)
{
VALUE xpath_handler = Qnil;
VALUE result;
VALUE *argv;
VALUE doc;
VALUE node_set = Qnil;
xmlNodeSetPtr xml_node_set = NULL;
xmlXPathObjectPtr obj;
int i;
nokogiriNodeSetTuple *node_set_tuple;
assert(ctx);
assert(ctx->context);
assert(ctx->context->userData);
assert(ctx->context->doc);
assert(DOC_RUBY_OBJECT_TEST(ctx->context->doc));
xpath_handler = (VALUE)(ctx->context->userData);
argv = (VALUE *)calloc((size_t)nargs, sizeof(VALUE));
for (i = 0 ; i < nargs ; ++i) {
rb_gc_register_address(&argv[i]);
}
doc = DOC_RUBY_OBJECT(ctx->context->doc);
if (nargs > 0) {
i = nargs - 1;
do {
obj = valuePop(ctx);
switch(obj->type) {
case XPATH_STRING:
argv[i] = NOKOGIRI_STR_NEW2(obj->stringval);
break;
case XPATH_BOOLEAN:
argv[i] = obj->boolval == 1 ? Qtrue : Qfalse;
break;
case XPATH_NUMBER:
argv[i] = rb_float_new(obj->floatval);
break;
case XPATH_NODESET:
argv[i] = Nokogiri_wrap_xml_node_set(obj->nodesetval, doc);
break;
default:
argv[i] = NOKOGIRI_STR_NEW2(xmlXPathCastToString(obj));
}
xmlXPathFreeNodeSetList(obj);
} while(i-- > 0);
}
result = rb_funcall2(
xpath_handler,
rb_intern((const char *)ctx->context->function),
nargs,
argv
);
for (i = 0 ; i < nargs ; ++i) {
rb_gc_unregister_address(&argv[i]);
}
free(argv);
switch(TYPE(result)) {
case T_FLOAT:
case T_BIGNUM:
case T_FIXNUM:
xmlXPathReturnNumber(ctx, NUM2DBL(result));
break;
case T_STRING:
xmlXPathReturnString(
ctx,
(xmlChar *)xmlXPathWrapCString(StringValuePtr(result))
);
break;
case T_TRUE:
xmlXPathReturnTrue(ctx);
break;
case T_FALSE:
xmlXPathReturnFalse(ctx);
break;
case T_NIL:
break;
case T_ARRAY:
{
VALUE args[2];
args[0] = doc;
args[1] = result;
node_set = rb_class_new_instance(2, args, cNokogiriXmlNodeSet);
Data_Get_Struct(node_set, nokogiriNodeSetTuple, node_set_tuple);
xml_node_set = node_set_tuple->node_set;
xmlXPathReturnNodeSet(ctx, xmlXPathNodeSetMerge(NULL, xml_node_set));
}
break;
case T_DATA:
if(rb_obj_is_kind_of(result, cNokogiriXmlNodeSet)) {
Data_Get_Struct(result, nokogiriNodeSetTuple, node_set_tuple);
xml_node_set = node_set_tuple->node_set;
/* Copy the node set, otherwise it will get GC'd. */
xmlXPathReturnNodeSet(ctx, xmlXPathNodeSetMerge(NULL, xml_node_set));
//.........这里部分代码省略.........
示例12: rxml_dtd_initialize
/*
* call-seq:
* XML::Dtd.new("DTD string") -> dtd
* XML::Dtd.new("public", "system") -> dtd
* XML::Dtd.new("name", "public", "system", document) -> external subset dtd
* XML::Dtd.new("name", "public", "system", document, false) -> internal subset dtd
* XML::Dtd.new("name", "public", "system", document, true) -> internal subset dtd
*
* Create a new Dtd from the specified public and system
* identifiers.
*/
static VALUE rxml_dtd_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE external, system, dtd_string;
xmlParserInputBufferPtr buffer;
xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
xmlChar *new_string;
xmlDtdPtr xdtd;
// 1 argument -- string --> parsujeme jako dtd
// 2 arguments -- public, system --> bude se hledat
// 3 arguments -- public, system, name --> creates an external subset (any parameter may be nil)
// 4 arguments -- public, system, name, doc --> creates an external subset (any parameter may be nil)
// 5 arguments -- public, system, name, doc, true --> creates an internal subset (all but last parameter may be nil)
switch (argc)
{
case 3:
case 4:
case 5: {
VALUE name, doc, internal;
const xmlChar *xname = NULL, *xpublic = NULL, *xsystem = NULL;
xmlDocPtr xdoc = NULL;
rb_scan_args(argc, argv, "32", &external, &system, &name, &doc, &internal);
if (external != Qnil) {
Check_Type(external, T_STRING);
xpublic = (const xmlChar*) StringValuePtr(external);
}
if (system != Qnil) {
Check_Type(system, T_STRING);
xsystem = (const xmlChar*) StringValuePtr(system);
}
if (name != Qnil) {
Check_Type(name, T_STRING);
xname = (const xmlChar*) StringValuePtr(name);
}
if (doc != Qnil) {
if (rb_obj_is_kind_of(doc, cXMLDocument) == Qfalse)
rb_raise(rb_eTypeError, "Must pass an XML::Document object");
Data_Get_Struct(doc, xmlDoc, xdoc);
}
if (internal == Qnil || internal == Qfalse)
xdtd = xmlNewDtd(xdoc, xname, xpublic, xsystem);
else
xdtd = xmlCreateIntSubset(xdoc, xname, xpublic, xsystem);
if (xdtd == NULL)
rxml_raise(&xmlLastError);
/* Document will free this dtd now. */
RDATA(self)->dfree = NULL;
DATA_PTR(self) = xdtd;
xmlSetTreeDoc((xmlNodePtr) xdtd, xdoc);
}
break;
case 2:
rb_scan_args(argc, argv, "20", &external, &system);
Check_Type(external, T_STRING);
Check_Type(system, T_STRING);
xdtd = xmlParseDTD((xmlChar*) StringValuePtr(external),
(xmlChar*) StringValuePtr(system));
if (xdtd == NULL)
rxml_raise(&xmlLastError);
DATA_PTR(self) = xdtd;
xmlSetTreeDoc((xmlNodePtr) xdtd, NULL);
break;
case 1:
rb_scan_args(argc, argv, "10", &dtd_string);
Check_Type(dtd_string, T_STRING);
/* Note that buffer is freed by xmlParserInputBufferPush*/
buffer = xmlAllocParserInputBuffer(enc);
new_string = xmlStrdup((xmlChar*) StringValuePtr(dtd_string));
xmlParserInputBufferPush(buffer, xmlStrlen(new_string),
(const char*) new_string);
xdtd = xmlIOParseDTD(NULL, buffer, enc);
if (xdtd == NULL)
rxml_raise(&xmlLastError);
//.........这里部分代码省略.........
示例13: variadic_invoke
static VALUE
variadic_invoke(VALUE self, VALUE parameterTypes, VALUE parameterValues)
{
VariadicInvoker* invoker;
FFIStorage* params;
void* retval;
ffi_cif cif;
void** ffiValues;
ffi_type** ffiParamTypes;
ffi_type* ffiReturnType;
Type** paramTypes;
VALUE* argv;
int paramCount = 0, fixedCount = 0, i;
ffi_status ffiStatus;
rbffi_frame_t frame = { 0 };
Check_Type(parameterTypes, T_ARRAY);
Check_Type(parameterValues, T_ARRAY);
Data_Get_Struct(self, VariadicInvoker, invoker);
paramCount = (int) RARRAY_LEN(parameterTypes);
paramTypes = ALLOCA_N(Type *, paramCount);
ffiParamTypes = ALLOCA_N(ffi_type *, paramCount);
params = ALLOCA_N(FFIStorage, paramCount);
ffiValues = ALLOCA_N(void*, paramCount);
argv = ALLOCA_N(VALUE, paramCount);
retval = alloca(MAX(invoker->returnType->ffiType->size, FFI_SIZEOF_ARG));
for (i = 0; i < paramCount; ++i) {
VALUE rbType = rb_ary_entry(parameterTypes, i);
if (!rb_obj_is_kind_of(rbType, rbffi_TypeClass)) {
rb_raise(rb_eTypeError, "wrong type. Expected (FFI::Type)");
}
Data_Get_Struct(rbType, Type, paramTypes[i]);
switch (paramTypes[i]->nativeType) {
case NATIVE_INT8:
case NATIVE_INT16:
case NATIVE_INT32:
rbType = rb_const_get(rbffi_TypeClass, rb_intern("INT32"));
Data_Get_Struct(rbType, Type, paramTypes[i]);
break;
case NATIVE_UINT8:
case NATIVE_UINT16:
case NATIVE_UINT32:
rbType = rb_const_get(rbffi_TypeClass, rb_intern("UINT32"));
Data_Get_Struct(rbType, Type, paramTypes[i]);
break;
case NATIVE_FLOAT32:
rbType = rb_const_get(rbffi_TypeClass, rb_intern("DOUBLE"));
Data_Get_Struct(rbType, Type, paramTypes[i]);
break;
default:
break;
}
ffiParamTypes[i] = paramTypes[i]->ffiType;
if (ffiParamTypes[i] == NULL) {
rb_raise(rb_eArgError, "Invalid parameter type #%x", paramTypes[i]->nativeType);
}
argv[i] = rb_ary_entry(parameterValues, i);
}
ffiReturnType = invoker->returnType->ffiType;
if (ffiReturnType == NULL) {
rb_raise(rb_eArgError, "Invalid return type");
}
/*Get the number of fixed args from @fixed array*/
fixedCount = RARRAY_LEN(rb_iv_get(self, "@fixed"));
#ifdef HAVE_FFI_PREP_CIF_VAR
ffiStatus = ffi_prep_cif_var(&cif, invoker->abi, fixedCount, paramCount, ffiReturnType, ffiParamTypes);
#else
ffiStatus = ffi_prep_cif(&cif, invoker->abi, paramCount, ffiReturnType, ffiParamTypes);
#endif
switch (ffiStatus) {
case FFI_BAD_ABI:
rb_raise(rb_eArgError, "Invalid ABI specified");
case FFI_BAD_TYPEDEF:
rb_raise(rb_eArgError, "Invalid argument type specified");
case FFI_OK:
break;
default:
rb_raise(rb_eArgError, "Unknown FFI error");
}
rbffi_SetupCallParams(paramCount, argv, -1, paramTypes, params,
ffiValues, NULL, 0, invoker->rbEnums);
rbffi_frame_push(&frame);
ffi_call(&cif, FFI_FN(invoker->function), retval, ffiValues);
rbffi_frame_pop(&frame);
rbffi_save_errno();
//.........这里部分代码省略.........
示例14: rb_git_indexentry_toC
static void rb_git_indexentry_toC(git_index_entry *entry, VALUE rb_entry)
{
VALUE val;
Check_Type(rb_entry, T_HASH);
val = rb_hash_aref(rb_entry, CSTR2SYM("path"));
Check_Type(val, T_STRING);
entry->path = StringValueCStr(val);
val = rb_hash_aref(rb_entry, CSTR2SYM("oid"));
Check_Type(val, T_STRING);
rugged_exception_check(
git_oid_fromstr(&entry->id, StringValueCStr(val))
);
entry->dev = default_entry_value(rb_entry, "dev");
entry->ino = default_entry_value(rb_entry, "ino");
entry->mode = default_entry_value(rb_entry, "mode");
entry->gid = default_entry_value(rb_entry, "gid");
entry->uid = default_entry_value(rb_entry, "uid");
entry->file_size = default_entry_value(rb_entry, "file_size");
if ((val = rb_hash_aref(rb_entry, CSTR2SYM("mtime"))) != Qnil) {
if (!rb_obj_is_kind_of(val, rb_cTime))
rb_raise(rb_eTypeError, ":mtime must be a Time instance");
entry->mtime.seconds = NUM2INT(rb_funcall(val, rb_intern("to_i"), 0));
entry->mtime.nanoseconds = NUM2INT(rb_funcall(val, rb_intern("usec"), 0)) * 1000;
} else {
entry->mtime.seconds = entry->mtime.nanoseconds = 0;
}
if ((val = rb_hash_aref(rb_entry, CSTR2SYM("ctime"))) != Qnil) {
if (!rb_obj_is_kind_of(val, rb_cTime))
rb_raise(rb_eTypeError, ":ctime must be a Time instance");
entry->ctime.seconds = NUM2INT(rb_funcall(val, rb_intern("to_i"), 0));
entry->ctime.nanoseconds = NUM2INT(rb_funcall(val, rb_intern("usec"), 0)) * 1000;
} else {
entry->ctime.seconds = entry->ctime.nanoseconds = 0;
}
entry->flags = 0x0;
entry->flags_extended = 0x0;
val = rb_hash_aref(rb_entry, CSTR2SYM("stage"));
if (!NIL_P(val)) {
unsigned int stage = NUM2INT(val);
entry->flags &= ~GIT_IDXENTRY_STAGEMASK;
entry->flags |= (stage << GIT_IDXENTRY_STAGESHIFT) & GIT_IDXENTRY_STAGEMASK;
}
val = rb_hash_aref(rb_entry, CSTR2SYM("valid"));
if (!NIL_P(val)) {
entry->flags &= ~GIT_IDXENTRY_VALID;
if (rugged_parse_bool(val))
entry->flags |= GIT_IDXENTRY_VALID;
} else {
entry->flags |= GIT_IDXENTRY_VALID;
}
}
示例15: reparent_node_with
/* :nodoc: */
static VALUE reparent_node_with(VALUE pivot_obj, VALUE reparentee_obj, pivot_reparentee_func prf)
{
VALUE reparented_obj ;
xmlNodePtr reparentee, pivot, reparented, next_text, new_next_text ;
if(!rb_obj_is_kind_of(reparentee_obj, cNokogiriXmlNode))
rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
if(rb_obj_is_kind_of(reparentee_obj, cNokogiriXmlDocument))
rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
Data_Get_Struct(reparentee_obj, xmlNode, reparentee);
Data_Get_Struct(pivot_obj, xmlNode, pivot);
if(XML_DOCUMENT_NODE == reparentee->type || XML_HTML_DOCUMENT_NODE == reparentee->type)
rb_raise(rb_eArgError, "cannot reparent a document node");
xmlUnlinkNode(reparentee);
if (reparentee->doc != pivot->doc || reparentee->type == XML_TEXT_NODE) {
/*
* if the reparentee is a text node, there's a very good chance it will be
* merged with an adjacent text node after being reparented, and in that case
* libxml will free the underlying C struct.
*
* since we clearly have a ruby object which references the underlying
* memory, we can't let the C struct get freed. let's pickle the original
* reparentee by rooting it; and then we'll reparent a duplicate of the
* node that we don't care about preserving.
*
* alternatively, if the reparentee is from a different document than the
* pivot node, libxml2 is going to get confused about which document's
* "dictionary" the node's strings belong to (this is an otherwise
* uninteresting libxml2 implementation detail). as a result, we cannot
* reparent the actual reparentee, so we reparent a duplicate.
*/
nokogiri_root_node(reparentee);
if (!(reparentee = xmlDocCopyNode(reparentee, pivot->doc, 1))) {
rb_raise(rb_eRuntimeError, "Could not reparent node (xmlDocCopyNode)");
}
}
if (prf != xmlAddPrevSibling && prf != xmlAddNextSibling
&& reparentee->type == XML_TEXT_NODE && pivot->next && pivot->next->type == XML_TEXT_NODE) {
/*
* libxml merges text nodes in a right-to-left fashion, meaning that if
* there are two text nodes who would be adjacent, the right (or following,
* or next) node will be merged into the left (or preceding, or previous)
* node.
*
* and by "merged" I mean the string contents will be concatenated onto the
* left node's contents, and then the node will be freed.
*
* which means that if we have a ruby object wrapped around the right node,
* its memory would be freed out from under it.
*
* so, we detect this edge case and unlink-and-root the text node before it gets
* merged. then we dup the node and insert that duplicate back into the
* document where the real node was.
*
* yes, this is totally lame.
*/
next_text = pivot->next ;
new_next_text = xmlDocCopyNode(next_text, pivot->doc, 1) ;
xmlUnlinkNode(next_text);
nokogiri_root_node(next_text);
xmlAddNextSibling(pivot, new_next_text);
}
if(!(reparented = (*prf)(pivot, reparentee))) {
rb_raise(rb_eRuntimeError, "Could not reparent node");
}
/*
* make sure the ruby object is pointed at the just-reparented node, which
* might be a duplicate (see above) or might be the result of merging
* adjacent text nodes.
*/
DATA_PTR(reparentee_obj) = reparented ;
relink_namespace(reparented);
reparented_obj = Nokogiri_wrap_xml_node(Qnil, reparented);
rb_funcall(reparented_obj, decorate_bang, 0);
return reparented_obj ;
}