static void CheckOSStatusOrRaise(OSStatus err){ if(err != 0){ CFStringRef description = SecCopyErrorMessageString(err, NULL); CFIndex bufferSize = CFStringGetMaximumSizeForEncoding(CFStringGetLength(description), kCFStringEncodingUTF8); char *buffer = malloc(bufferSize + 1); CFStringGetCString(description, buffer, bufferSize + 1, kCFStringEncodingUTF8); CFRelease(description); VALUE exceptionString = rb_enc_str_new(buffer, strlen(buffer), rb_utf8_encoding()); free(buffer); VALUE exception = Qnil; switch(err){ case errSecAuthFailed: exception = rb_obj_alloc(rb_eKeychainAuthFailedError); break; case errSecNoSuchKeychain: exception = rb_obj_alloc(rb_eKeychainNoSuchKeychainError); break; case errSecDuplicateItem: exception = rb_obj_alloc(rb_eKeychainDuplicateItemError); break; default: exception = rb_obj_alloc(rb_eKeychainError); } rb_funcall(exception, rb_intern("initialize"), 2,exceptionString, INT2FIX(err)); rb_exc_raise(exception); } }
// Define a method and attach data to it. // The method looks to ruby like a normal aliased CFUNC, with a modified // origin class. // // How this works: // // To store method data and have it registered with the GC, we need a // "slot" to put it in. This "slot" must be recognized and marked by // the garbage collector. There happens to be such a place we can put // data, and it has to do with aliased methods. When Ruby creates an // alias for a method, it stores a reference to the original class in // the method entry. The form of the method entry differs from ruby // version to ruby version, but the concept is the same across all of // them. // // In Rice, we make use of this by attach the data to a dummy object // (store) in the class variables table. // // When Ruby makes a method call, it stores the class Object and method // ID in the current stack frame. When Ruby calls into Rice, we grab // the class and method ID from the stack frame, then pull the data out // of the class. The data item is then used to determine how to convert // arguments and return type, how to handle exceptions, etc. // VALUE Rice::detail:: define_method_with_data( VALUE klass, ID id, VALUE (*cfunc)(ANYARGS), int arity, VALUE data) { VALUE store = rb_attr_get(klass, RICE_ID); if (store == Qnil) { store = rb_obj_alloc(rb_cObject); // store is stored in the instance variables table with // name "__rice__". // since "__rice__" does not have the @ prefix, // so it can never be read at the Ruby level. rb_ivar_set(klass, RICE_ID, store); } rb_ivar_set(store, id, data); // Create the aliased method on the origin class rb_define_method( klass, rb_id2name(id), cfunc, arity); return Qnil; }
VALUE dnssd_tr_new(long len, const char *buf) { VALUE self = rb_obj_alloc(cDNSSDTextRecord); dnssd_tr_decode_buffer(self, len, buf); return self; }
static void oci8_svcctx_init(oci8_base_t *base) { oci8_svcctx_t *svcctx = (oci8_svcctx_t *)base; svcctx->executing_thread = Qnil; svcctx->session = DATA_PTR(rb_obj_alloc(cSession)); svcctx->server = DATA_PTR(rb_obj_alloc(cServer)); ((oci8_svcctx_associate_t *)svcctx->session)->svcctx = svcctx; ((oci8_svcctx_associate_t *)svcctx->server)->svcctx = svcctx; svcctx->pid = getpid(); svcctx->is_autocommit = 0; #ifdef HAVE_TYPE_RB_BLOCKING_FUNCTION_T svcctx->non_blocking = 1; #endif svcctx->long_read_len = INT2FIX(65535); }
static void ossl_sslctx_session_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) { VALUE ary, sslctx_obj, sess_obj, ret_obj; void *ptr; int state = 0; OSSL_Debug("SSL SESSION remove callback entered"); if ((ptr = SSL_CTX_get_ex_data(ctx, ossl_ssl_ex_ptr_idx)) == NULL) return; sslctx_obj = (VALUE)ptr; sess_obj = rb_obj_alloc(cSSLSession); CRYPTO_add(&sess->references, 1, CRYPTO_LOCK_SSL_SESSION); DATA_PTR(sess_obj) = sess; ary = rb_ary_new2(2); rb_ary_push(ary, sslctx_obj); rb_ary_push(ary, sess_obj); ret_obj = rb_protect((VALUE(*)_((VALUE)))ossl_call_session_new_cb, ary, &state); if (state) { /* the SSL_CTX is frozen, nowhere to save state. there is no common accessor method to check it either. rb_ivar_set(sslctx_obj, ID_callback_state, INT2NUM(state)); */ } }
static VALUE t_get_subprocess_status (VALUE self, VALUE signature) { VALUE proc_status = Qnil; int status; pid_t pid; if (evma_get_subprocess_status (NUM2ULONG (signature), &status)) { if (evma_get_subprocess_pid (NUM2ULONG (signature), &pid)) { proc_status = rb_obj_alloc(rb_cProcStatus); /* MRI Ruby uses hidden instance vars */ rb_iv_set(proc_status, "status", INT2FIX(status)); rb_iv_set(proc_status, "pid", INT2FIX(pid)); #ifdef RUBINIUS /* Rubinius uses standard instance vars */ rb_iv_set(proc_status, "@pid", INT2FIX(pid)); if (WIFEXITED(status)) { rb_iv_set(proc_status, "@status", INT2FIX(WEXITSTATUS(status))); } else if(WIFSIGNALED(status)) { rb_iv_set(proc_status, "@termsig", INT2FIX(WTERMSIG(status))); } else if(WIFSTOPPED(status)) { rb_iv_set(proc_status, "@stopsig", INT2FIX(WSTOPSIG(status))); } #endif } } return proc_status; }
static VALUE fileIntForPath(const char *path, bool rubyExc) { SDL_RWops *ops = SDL_AllocRW(); try { shState->fileSystem().openRead(*ops, path); } catch (const Exception &e) { SDL_FreeRW(ops); if (rubyExc) raiseRbExc(e); else throw e; } VALUE klass = rb_const_get(rb_cObject, rb_intern("FileInt")); VALUE obj = rb_obj_alloc(klass); setPrivateData(obj, ops); return obj; }
static VALUE rb_f_catch(int argc, VALUE *argv) { VALUE tag; int state; VALUE val = Qnil; /* OK */ rb_thread_t *th = GET_THREAD(); rb_control_frame_t *saved_cfp = th->cfp; if (argc == 0) { tag = rb_obj_alloc(rb_cObject); } else { rb_scan_args(argc, argv, "01", &tag); } PUSH_TAG(); th->tag->tag = tag; if ((state = EXEC_TAG()) == 0) { val = rb_yield_0(1, &tag); } else if (state == TAG_THROW && RNODE(th->errinfo)->u1.value == tag) { th->cfp = saved_cfp; val = th->tag->retval; th->errinfo = Qnil; state = 0; } POP_TAG(); if (state) JUMP_TAG(state); return val; }
/* * call-seq: * key.group => group * * Returns a constant <code>OpenSSL::EC::Group</code> that is tied to the key. * Modifying the returned group can make the key invalid. */ static VALUE ossl_ec_key_get_group(VALUE self) { VALUE group_v; EC_KEY *ec; ossl_ec_group *ec_group; EC_GROUP *group; Require_EC_KEY(self, ec); group_v = rb_iv_get(self, "@group"); if (!NIL_P(group_v)) return group_v; if ((group = (EC_GROUP *)EC_KEY_get0_group(ec)) != NULL) { group_v = rb_obj_alloc(cEC_GROUP); SafeGet_ec_group(group_v, ec_group); ec_group->group = group; ec_group->dont_free = 1; rb_iv_set(group_v, "@key", self); rb_iv_set(self, "@group", group_v); return group_v; } return Qnil; }
/* return 1 normal. return 0 removes the session */ static int ossl_sslctx_session_new_cb(SSL *ssl, SSL_SESSION *sess) { VALUE ary, ssl_obj, sess_obj, ret_obj; void *ptr; int state = 0; OSSL_Debug("SSL SESSION new callback entered"); if ((ptr = SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx)) == NULL) return 1; ssl_obj = (VALUE)ptr; sess_obj = rb_obj_alloc(cSSLSession); CRYPTO_add(&sess->references, 1, CRYPTO_LOCK_SSL_SESSION); DATA_PTR(sess_obj) = sess; ary = rb_ary_new2(2); rb_ary_push(ary, ssl_obj); rb_ary_push(ary, sess_obj); ret_obj = rb_protect((VALUE(*)_((VALUE)))ossl_call_session_new_cb, ary, &state); if (state) { rb_ivar_set(ssl_obj, ID_callback_state, INT2NUM(state)); return 0; /* what should be returned here??? */ } return RTEST(ret_obj) ? 1 : 0; }
static VALUE nary_s_step( int argc, VALUE *argv, VALUE mod ) { VALUE self = rb_obj_alloc(na_cStep); step_initialize(argc, argv, self); return self; }
VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len) { int fd2; int retry = 0; struct accept_arg arg; rb_secure(3); arg.fd = fd; arg.sockaddr = sockaddr; arg.len = len; retry: rsock_maybe_wait_fd(fd); fd2 = (int)BLOCKING_REGION_FD(accept_blocking, &arg); if (fd2 < 0) { switch (errno) { case EMFILE: case ENFILE: if (retry) break; rb_gc(); retry = 1; goto retry; default: if (!rb_io_wait_readable(fd)) break; retry = 0; goto retry; } rb_sys_fail("accept(2)"); } rb_update_max_fd(fd2); if (!klass) return INT2NUM(fd2); return rsock_init_sock(rb_obj_alloc(klass), fd2); }
VALUE rsock_s_accept_nonblock(VALUE klass, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len) { int fd2; rb_secure(3); rb_io_set_nonblock(fptr); fd2 = cloexec_accept(fptr->fd, (struct sockaddr*)sockaddr, len); if (fd2 < 0) { switch (errno) { case EAGAIN: #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN case EWOULDBLOCK: #endif case ECONNABORTED: #if defined EPROTO case EPROTO: #endif rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "accept(2) would block"); } rb_sys_fail("accept(2)"); } rb_update_max_fd(fd2); make_fd_nonblock(fd2); return rsock_init_sock(rb_obj_alloc(klass), fd2); }
static void create_prolog_doc(PInfo pi, const char *target, Attr attrs) { VALUE doc; VALUE ah; VALUE nodes; if (0 != pi->h) { // top level object rb_raise(rb_eSyntaxError, "Prolog must be the first element in an XML document.\n"); } pi->h = pi->helpers; doc = rb_obj_alloc(ox_document_clas); ah = rb_hash_new(); for (; 0 != attrs->name; attrs++) { rb_hash_aset(ah, ID2SYM(rb_intern(attrs->name)), rb_str_new2(attrs->value)); #ifdef HAVE_RUBY_ENCODING_H if (0 == strcmp("encoding", attrs->name)) { pi->encoding = rb_enc_find(attrs->value); } #endif } nodes = rb_ary_new(); rb_ivar_set(doc, attributes_id, ah); rb_ivar_set(doc, nodes_id, nodes); pi->h->obj = nodes; pi->obj = doc; }
VALUE rbgobj_gtype_new(GType gtype) { VALUE result = rb_obj_alloc(rbgobj_cType); VALUE arg = ULONG2NUM(gtype); rb_obj_call_init(result, 1, &arg); return result; }
VALUE rbgobj_gtype_new(GType gtype) { VALUE result = rb_obj_alloc(RG_TARGET_NAMESPACE); VALUE arg = ULONG2NUM(gtype); rb_obj_call_init(result, 1, &arg); return result; }
VALUE rb_range_new(VALUE beg, VALUE end, int exclude_end) { VALUE range = rb_obj_alloc(rb_cRange); range_init(range, beg, end, exclude_end); return range; }
static VALUE dnssd_tr_decode(VALUE klass, VALUE str) { /* self needs to be on the stack - we add (allocate) * lots of key, value pairs when decoding and this could * cause the gc to run. */ volatile VALUE self = rb_obj_alloc(klass); dnssd_tr_decode_str(self, str); return self; }
/* * call-seq: * BasicSocket.for_fd(fd) => basicsocket * * Returns a socket object which contains the file descriptor, _fd_. * * # If invoked by inetd, STDIN/STDOUT/STDERR is a socket. * STDIN_SOCK = Socket.for_fd(STDIN.fileno) * p STDIN_SOCK.remote_address * */ static VALUE bsock_s_for_fd(VALUE klass, VALUE fd) { rb_io_t *fptr; VALUE sock = rsock_init_sock(rb_obj_alloc(klass), NUM2INT(fd)); GetOpenFile(sock, fptr); return sock; }
static VALUE rb_f_catch(VALUE rcv, SEL sel, int argc, VALUE *argv) { VALUE tag; if (argc == 0) { tag = rb_obj_alloc(rb_cObject); } else { rb_scan_args(argc, argv, "01", &tag); } return rb_vm_catch(tag); }
/* call-seq: * c = Xmms::Collection.universe * * Returns a collection referencing the "All Media" set. */ static VALUE c_coll_universe (VALUE klass) { VALUE obj = rb_obj_alloc (klass); RbCollection *coll = NULL; Data_Get_Struct (obj, RbCollection, coll); coll->real = xmmsc_coll_universe (); return obj; }
/* call-seq: * c = Xmms::Collection.universe * * Returns a collection referencing the "All Media" set. */ static VALUE c_coll_universe (VALUE klass) { VALUE obj = rb_obj_alloc (klass); RbCollection *coll = NULL; Data_Get_Struct (obj, RbCollection, coll); coll->real = xmmsv_new_coll (XMMS_COLLECTION_TYPE_UNIVERSE); return obj; }
void bitmapInitProps(Bitmap *b, VALUE self) { /* Wrap properties */ VALUE fontKlass = rb_const_get(rb_cObject, rb_intern("Font")); VALUE fontObj = rb_obj_alloc(fontKlass); rb_obj_call_init(fontObj, 0, 0); Font *font = getPrivateData<Font>(fontObj); b->setInitFont(font); rb_iv_set(self, "font", fontObj); }
/* * 既存のデータベースを開く。ブロックを指定した場合はブロッ * クに開いたデータベースを渡し、ブロックを抜けるときに閉じ * る。 _options_ にはハッシュでオプションを指定する。 * * @overload open(path, options=nil) * @return [Groonga::Database] * @param options [::Hash] The name and value * pairs. Omitted names are initialized as the default value. * @!macro [new] database.open.context * @option options :context (Groonga::Context.default) The context * データベースを結びつけるコンテキスト。 * @!macro database.open.context * @overload open(path, options=nil) * @param options [::Hash] The name and value * pairs. Omitted names are initialized as the default value. * @!macro database.open.context * @yield [database] * @yieldparam [Groonga::Database] database 開いたデータベース */ static VALUE rb_grn_database_s_open (int argc, VALUE *argv, VALUE klass) { VALUE database; database = rb_obj_alloc(klass); rb_grn_database_initialize(argc, argv, database); if (rb_block_given_p()) return rb_ensure(rb_yield, database, rb_grn_database_close, database); else return database; }
VALUE RubyEvaluator::GetContext(KObjectRef global) { std::string theid = this->GetContextId(global); VALUE ctx = rb_gv_get(theid.c_str()); if (ctx == Qnil) { VALUE ctx_class = rb_define_class("KrollRubyContext", rb_cObject); rb_define_method(ctx_class, "method_missing", VALUEFUNC(m_missing), -1); ctx = rb_obj_alloc(ctx_class); rb_gv_set(theid.c_str(), ctx); } return ctx; }
static void oci8_svcctx_init(oci8_base_t *base) { oci8_svcctx_t *svcctx = (oci8_svcctx_t *)base; VALUE obj; svcctx->executing_thread = Qnil; /* set session handle */ obj = rb_obj_alloc(cSession); rb_ivar_set(base->self, id_at_session_handle, obj); oci8_link_to_parent(DATA_PTR(obj), base); /* set server handle */ obj = rb_obj_alloc(cServer); rb_ivar_set(base->self, id_at_server_handle, obj); oci8_link_to_parent(DATA_PTR(obj), base); svcctx->pid = getpid(); svcctx->is_autocommit = 0; #ifdef HAVE_RB_THREAD_BLOCKING_REGION svcctx->non_blocking = 1; #endif svcctx->long_read_len = INT2FIX(65535); }
static void add_doctype(PInfo pi, const char *docType) { VALUE n = rb_obj_alloc(ox_doctype_clas); VALUE s = rb_str_new2(docType); #ifdef HAVE_RUBY_ENCODING_H if (0 != pi->encoding) { rb_enc_associate(s, pi->encoding); } #endif rb_ivar_set(n, value_id, s); rb_ary_push(pi->h->obj, n); }
static void add_cdata(PInfo pi, const char *cdata, size_t len) { VALUE n = rb_obj_alloc(ox_cdata_clas); VALUE s = rb_str_new2(cdata); #ifdef HAVE_RUBY_ENCODING_H if (0 != pi->encoding) { rb_enc_associate(s, pi->encoding); } #endif rb_ivar_set(n, value_id, s); rb_ary_push(pi->h->obj, n); }
void Init_ENV(void) { origenviron = GET_ENVIRON(); envtbl = rb_obj_alloc(rb_cObject); rb_extend_object(envtbl, rb_mEnumerable); VALUE klass = rb_singleton_class(envtbl); rb_objc_define_method(klass, "[]", rb_f_getenv, 1); rb_objc_define_method(klass, "fetch", env_fetch, -1); rb_objc_define_method(klass, "[]=", env_aset, 2); rb_objc_define_method(klass, "store", env_aset, 2); rb_objc_define_method(klass, "each", env_each_pair, 0); rb_objc_define_method(klass, "each_pair", env_each_pair, 0); rb_objc_define_method(klass, "each_key", env_each_key, 0); rb_objc_define_method(klass, "each_value", env_each_value, 0); rb_objc_define_method(klass, "delete", env_delete_m, 1); rb_objc_define_method(klass, "delete_if", env_delete_if, 0); rb_objc_define_method(klass, "clear", rb_env_clear_imp, 0); rb_objc_define_method(klass, "reject", env_reject, 0); rb_objc_define_method(klass, "reject!", env_reject_bang, 0); rb_objc_define_method(klass, "select", env_select, 0); rb_objc_define_method(klass, "shift", env_shift, 0); rb_objc_define_method(klass, "invert", env_invert, 0); rb_objc_define_method(klass, "replace", env_replace, 1); rb_objc_define_method(klass, "update", env_update, 1); rb_objc_define_method(klass, "inspect", env_inspect, 0); rb_objc_define_method(klass, "rehash", env_none, 0); rb_objc_define_method(klass, "to_a", env_to_a, 0); rb_objc_define_method(klass, "to_s", env_to_s, 0); rb_objc_define_method(klass, "key", env_key, 1); rb_objc_define_method(klass, "index", env_index, 1); rb_objc_define_method(klass, "size", env_size, 0); rb_objc_define_method(klass, "length", env_size, 0); rb_objc_define_method(klass, "empty?", env_empty_p, 0); rb_objc_define_method(klass, "keys", env_keys, 0); rb_objc_define_method(klass, "values", env_values, 0); rb_objc_define_method(klass, "values_at", env_values_at, -1); rb_objc_define_method(klass, "include?", env_has_key, 1); rb_objc_define_method(klass, "member?", env_has_key, 1); rb_objc_define_method(klass, "has_key?", env_has_key, 1); rb_objc_define_method(klass, "has_value?", env_has_value, 1); rb_objc_define_method(klass, "key?", env_has_key, 1); rb_objc_define_method(klass, "value?", env_has_value, 1); rb_objc_define_method(klass, "to_hash", env_to_hash, 0); rb_objc_define_method(klass, "assoc", env_assoc, 1); rb_objc_define_method(klass, "rassoc", env_rassoc, 1); rb_define_global_const("ENV", envtbl); }
VALUE nary_step_new( VALUE beg, VALUE end, VALUE step, VALUE len, VALUE excl ) { VALUE self = rb_obj_alloc(na_cStep); step_init(self, beg, end, step, len, excl); return self; }