static VALUE rb_gsl_pow_int(VALUE obj, VALUE xx, VALUE nn) { VALUE x, ary, argv[2]; size_t i, j, size; int n; gsl_vector *v = NULL, *vnew = NULL; gsl_matrix *m = NULL, *mnew = NULL; if (CLASS_OF(xx) == rb_cRange) xx = rb_gsl_range2ary(xx); switch (TYPE(xx)) { case T_FIXNUM: case T_BIGNUM: case T_FLOAT: return rb_float_new(gsl_pow_int(NUM2DBL(xx), FIX2INT(nn))); break; case T_ARRAY: CHECK_FIXNUM(nn); n = FIX2INT(nn); size = RARRAY_LEN(xx); ary = rb_ary_new2(size); for (i = 0; i < size; i++) { x = rb_ary_entry(xx, i); Need_Float(x); // rb_ary_store(ary, i, rb_float_new(gsl_pow_int(RFLOAT(x)->value, n))); rb_ary_store(ary, i, rb_float_new(gsl_pow_int(NUM2DBL(x), n))); } return ary; break; default: #ifdef HAVE_NARRAY_H if (NA_IsNArray(xx)) { struct NARRAY *na; double *ptr1, *ptr2; CHECK_FIXNUM(nn); n = FIX2INT(nn); GetNArray(xx, na); ptr1 = (double*) na->ptr; size = na->total; ary = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(xx)); ptr2 = NA_PTR_TYPE(ary, double*); for (i = 0; i < size; i++) ptr2[i] = gsl_pow_int(ptr1[i], n); return ary; } #endif if (VECTOR_P(xx)) { CHECK_FIXNUM(nn); n = FIX2INT(nn); Data_Get_Struct(xx, gsl_vector, v); vnew = gsl_vector_alloc(v->size); for (i = 0; i < v->size; i++) { gsl_vector_set(vnew, i, gsl_pow_int(gsl_vector_get(v, i), n)); } return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew); } else if (MATRIX_P(xx)) { CHECK_FIXNUM(nn); n = FIX2INT(nn); Data_Get_Struct(xx, gsl_matrix, m); mnew = gsl_matrix_alloc(m->size1, m->size2); for (i = 0; i < m->size1; i++) { for (j = 0; j < m->size2; j++) { gsl_matrix_set(mnew, i, j, gsl_pow_int(gsl_matrix_get(m, i, j), n)); } } return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew); } else if (COMPLEX_P(xx) || VECTOR_COMPLEX_P(xx) || MATRIX_COMPLEX_P(xx)) { argv[0] = xx; argv[1] = nn; return rb_gsl_complex_pow_real(2, argv, obj); } else { rb_raise(rb_eTypeError, "wrong argument type %s (Array or Vector or Matrix expected)", rb_class2name(CLASS_OF(xx))); } break; } /* never reach here */ return Qnil; }
/** @section Instance methods @method left @method left=( left ) @method right @method right=( right ) @method top @method top=( top ) @method bottom @method bottom=( bottom ) These can be set and read at will. Differences with x, y, w and h are: * @right and @bottom are screen coordinates. @right is @x + @w and @bottom is @y + @h . */ static VALUE rb_array_set_right(VALUE self, VALUE new_right) { SET_X(NUM2DBL(new_right)-array_get_w(self)); return self; }
__inline__ static double array_get_h(VALUE array) {return NUM2DBL(rb_ary_entry(array, 3));}
static void* get_parameter(const ool_conmin_minimizer_type *T, ool_conmin_pgrad_parameters *Pp, ool_conmin_spg_parameters *Ps, ool_conmin_gencan_parameters *Pg, VALUE ary) { if (T == ool_conmin_minimizer_pgrad) { if (ary == Qnil) { ool_conmin_parameters_default(T, (void*) Pp); } else { Pp->fmin = NUM2DBL(rb_ary_entry(ary, 0)); Pp->tol = NUM2DBL(rb_ary_entry(ary, 1)); Pp->alpha = NUM2DBL(rb_ary_entry(ary, 2)); Pp->sigma1 = NUM2DBL(rb_ary_entry(ary, 3)); Pp->sigma2 = NUM2DBL(rb_ary_entry(ary, 4)); } return (void*) Pp; } else if (T == ool_conmin_minimizer_spg) { if (ary == Qnil) { ool_conmin_parameters_default(T, (void*) Ps); } else { Ps->fmin = NUM2DBL(rb_ary_entry(ary, 0)); Ps->tol = NUM2DBL(rb_ary_entry(ary, 1)); Ps->M = NUM2DBL(rb_ary_entry(ary, 2)); Ps->alphamin = NUM2DBL(rb_ary_entry(ary, 3)); Ps->alphamax = NUM2DBL(rb_ary_entry(ary, 4)); Ps->gamma = NUM2DBL(rb_ary_entry(ary, 5)); Ps->sigma2 = NUM2DBL(rb_ary_entry(ary, 6)); Ps->sigma2 = NUM2DBL(rb_ary_entry(ary, 7)); } return (void*) Ps; } else { if (ary == Qnil) { ool_conmin_parameters_default(T, (void*) Pg); } else { Pg->epsgpen = NUM2DBL(rb_ary_entry(ary, 0)); Pg->epsgpsn = NUM2DBL(rb_ary_entry(ary, 1)); Pg->fmin = NUM2DBL(rb_ary_entry(ary, 2)); Pg->udelta0 = NUM2DBL(rb_ary_entry(ary, 3)); Pg->ucgmia = NUM2DBL(rb_ary_entry(ary, 4)); Pg->ucgmib = NUM2DBL(rb_ary_entry(ary, 5)); Pg->cg_scre = FIX2INT(rb_ary_entry(ary, 6)); Pg->cg_gpnf = NUM2DBL(rb_ary_entry(ary, 7)); Pg->cg_epsi = NUM2DBL(rb_ary_entry(ary, 8)); Pg->cg_epsf = NUM2DBL(rb_ary_entry(ary, 9)); Pg->cg_epsnqmp = NUM2DBL(rb_ary_entry(ary, 10)); Pg->cg_maxitnqmp = (size_t) FIX2INT(rb_ary_entry(ary, 11)); Pg->nearlyq = FIX2INT(rb_ary_entry(ary, 12)); Pg->nint = NUM2DBL(rb_ary_entry(ary, 13)); Pg->next = NUM2DBL(rb_ary_entry(ary, 14)); Pg->mininterp = (size_t) FIX2INT(rb_ary_entry(ary, 15)); Pg->maxextrap = (size_t) FIX2INT(rb_ary_entry(ary, 16)); Pg->trtype = FIX2INT(rb_ary_entry(ary, 17)); Pg->eta = NUM2DBL(rb_ary_entry(ary, 18)); Pg->delmin = NUM2DBL(rb_ary_entry(ary, 19)); Pg->lspgmi = NUM2DBL(rb_ary_entry(ary, 20)); Pg->lspgma = NUM2DBL(rb_ary_entry(ary, 21)); Pg->theta = NUM2DBL(rb_ary_entry(ary, 22)); Pg->gamma = NUM2DBL(rb_ary_entry(ary, 23)); Pg->beta = NUM2DBL(rb_ary_entry(ary, 24)); Pg->sigma1 = NUM2DBL(rb_ary_entry(ary, 25)); Pg->sigma2 = NUM2DBL(rb_ary_entry(ary, 26)); Pg->epsrel = NUM2DBL(rb_ary_entry(ary, 27)); Pg->epsabs = NUM2DBL(rb_ary_entry(ary, 28)); Pg->infrel = NUM2DBL(rb_ary_entry(ary, 29)); Pg->infabs = NUM2DBL(rb_ary_entry(ary, 30)); } return (void*) Pg; } }
static VALUE rg_set_increments(VALUE self, VALUE step, VALUE page) { gtk_spin_button_set_increments(_SELF(self), NUM2DBL(step), NUM2DBL(page)); return self; }
/* Method: Draw#composite(x,y,width,height,img,operator=OverCompositeOp) Purpose: Implement the "image" drawing primitive Notes: The "img" argument can be either an ImageList object or an Image argument. */ VALUE Draw_composite(int argc, VALUE *argv, VALUE self) { Draw *draw; const char *op = "Over"; double x, y, width, height; CompositeOperator cop = OverCompositeOp; volatile VALUE image; Image *comp_img; struct TmpFile_Name *tmpfile_name; char name[MaxTextExtent]; // Buffer for "image" primitive char primitive[MaxTextExtent]; if (argc < 5 || argc > 6) { rb_raise(rb_eArgError, "wrong number of arguments (%d for 5 or 6)", argc); } // Retrieve the image to composite image = rm_cur_image(argv[4]); (void) rm_check_destroyed(image); x = NUM2DBL(argv[0]); y = NUM2DBL(argv[1]); width = NUM2DBL(argv[2]); height = NUM2DBL(argv[3]); // The default composition operator is "Over". if (argc == 6) { VALUE_TO_ENUM(argv[5], cop, CompositeOperator); switch (cop) { case AddCompositeOp: op = "Add"; break; case AtopCompositeOp: op = "Atop"; break; case BumpmapCompositeOp: op = "Bumpmap"; break; case ClearCompositeOp: op = "Clear"; break; case CopyBlueCompositeOp: op = "CopyBlue"; break; case CopyGreenCompositeOp: op = "CopyGreen"; break; case CopyOpacityCompositeOp: op = "CopyOpacity"; break; case CopyRedCompositeOp: op = "CopyRed"; break; case CopyCompositeOp: op = "Copy"; break; case DifferenceCompositeOp: op = "Difference"; break; case InCompositeOp: op = "In"; break; case MinusCompositeOp: op = "Minus"; break; case MultiplyCompositeOp: op = "Multiply"; break; case OutCompositeOp: op = "Out"; break; case OverCompositeOp: op = "Over"; break; case PlusCompositeOp: op = "Plus"; break; case SubtractCompositeOp: op = "Subtract"; break; case XorCompositeOp: op = "Xor"; break; default: rb_raise(rb_eArgError, "unknown composite operator (%d)", cop); break; } } Data_Get_Struct(self, Draw, draw); // Create a temp copy of the composite image Data_Get_Struct(image, Image, comp_img); rm_write_temp_image(comp_img, name); // Add the temp filename to the filename array. // Use Magick storage since we need to keep the list around // until destroy_Draw is called. tmpfile_name = magick_malloc(sizeof(struct TmpFile_Name)+strlen(name)); strcpy(tmpfile_name->name, name); tmpfile_name->next = draw->tmpfile_ary; draw->tmpfile_ary = tmpfile_name; // Form the drawing primitive (void) sprintf(primitive, "image %s %g,%g,%g,%g '%s'", op, x, y, width, height, name); // Send "primitive" to self. (void) rb_funcall(self, rb_intern("primitive"), 1, rb_str_new2(primitive)); return self; }
/* call-seq: stmt.bind_param(key, value) * * Binds value to the named (or positional) placeholder. If +param+ is a * Fixnum, it is treated as an index for a positional placeholder. * Otherwise it is used as the name of the placeholder to bind to. * * See also #bind_params. */ static VALUE bind_param(VALUE self, VALUE key, VALUE value) { sqlite3StmtRubyPtr ctx; int status; int index; Data_Get_Struct(self, sqlite3StmtRuby, ctx); REQUIRE_OPEN_STMT(ctx); switch(TYPE(key)) { case T_SYMBOL: key = rb_funcall(key, rb_intern("to_s"), 0); case T_STRING: if(RSTRING_PTR(key)[0] != ':') key = rb_str_plus(rb_str_new2(":"), key); index = sqlite3_bind_parameter_index(ctx->st, StringValuePtr(key)); break; default: index = (int)NUM2INT(key); } if(index == 0) rb_raise(rb_path2class("SQLite3::Exception"), "no such bind parameter"); switch(TYPE(value)) { case T_STRING: if(CLASS_OF(value) == cSqlite3Blob || rb_enc_get_index(value) == rb_ascii8bit_encindex() ) { status = sqlite3_bind_blob( ctx->st, index, (const char *)StringValuePtr(value), (int)RSTRING_LEN(value), SQLITE_TRANSIENT ); } else { if (UTF16_LE_P(value) || UTF16_BE_P(value)) { status = sqlite3_bind_text16( ctx->st, index, (const char *)StringValuePtr(value), (int)RSTRING_LEN(value), SQLITE_TRANSIENT ); } else { if (!UTF8_P(value) || !USASCII_P(value)) { value = rb_str_encode(value, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil); } status = sqlite3_bind_text( ctx->st, index, (const char *)StringValuePtr(value), (int)RSTRING_LEN(value), SQLITE_TRANSIENT ); } } break; case T_BIGNUM: { sqlite3_int64 num64; if (bignum_to_int64(value, &num64)) { status = sqlite3_bind_int64(ctx->st, index, num64); break; } } case T_FLOAT: status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value)); break; case T_FIXNUM: status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)FIX2LONG(value)); break; case T_NIL: status = sqlite3_bind_null(ctx->st, index); break; default: rb_raise(rb_eRuntimeError, "can't prepare %s", rb_class2name(CLASS_OF(value))); break; } CHECK(sqlite3_db_handle(ctx->st), status); return self; }
static VALUE rb_gsl_finite2(VALUE obj, VALUE x) { Need_Float(x); if (gsl_finite(NUM2DBL(x))) return Qtrue; else return Qfalse; }
static void method_caller(xmlXPathParserContextPtr ctxt, int nargs) { const xmlChar * function; const xmlChar * functionURI; size_t i, count; xsltTransformContextPtr transform; xmlXPathObjectPtr xpath; VALUE obj; VALUE *args; VALUE result; transform = xsltXPathGetTransformContext(ctxt); function = ctxt->context->function; functionURI = ctxt->context->functionURI; obj = (VALUE)xsltGetExtData(transform, functionURI); count = (size_t)ctxt->valueNr; args = calloc(count, sizeof(VALUE *)); for(i = 0; i < count; i++) { VALUE thing; xpath = valuePop(ctxt); switch(xpath->type) { case XPATH_STRING: thing = NOKOGIRI_STR_NEW2(xpath->stringval); break; case XPATH_NODESET: if(NULL == xpath->nodesetval) { thing = Nokogiri_wrap_xml_node_set( xmlXPathNodeSetCreate(NULL), DOC_RUBY_OBJECT(ctxt->context->doc)); } else { thing = Nokogiri_wrap_xml_node_set(xpath->nodesetval, DOC_RUBY_OBJECT(ctxt->context->doc)); } break; default: rb_raise(rb_eRuntimeError, "do not handle type: %d", xpath->type); } args[i] = thing; xmlFree(xpath); } result = rb_funcall3(obj, rb_intern((const char *)function), (int)count, args); free(args); switch(TYPE(result)) { case T_FLOAT: case T_BIGNUM: case T_FIXNUM: xmlXPathReturnNumber(ctxt, NUM2DBL(result)); break; case T_STRING: xmlXPathReturnString( ctxt, xmlStrdup((xmlChar *)StringValuePtr(result)) ); break; case T_TRUE: xmlXPathReturnTrue(ctxt); break; case T_FALSE: xmlXPathReturnFalse(ctxt); break; case T_NIL: break; default: rb_raise(rb_eRuntimeError, "Invalid return type"); } }
static VALUE rb_gsl_isinf2(VALUE obj, VALUE x) { // Need_Float(x); if (gsl_isinf(NUM2DBL(x))) return Qtrue; else return Qfalse; }
static VALUE rb_gsl_finite(VALUE obj, VALUE x) { Need_Float(x); return INT2FIX(gsl_finite(NUM2DBL(x))); }
static VALUE rb_gsl_ldexp(VALUE obj, VALUE x, VALUE e) { return rb_float_new(gsl_ldexp(NUM2DBL(x), FIX2INT(e))); }
static VALUE rb_GSL_MIN_DBL(VALUE obj, VALUE aa, VALUE bb) { Need_Float(aa); Need_Float(bb); return rb_float_new(GSL_MAX_DBL(NUM2DBL(aa), NUM2DBL(bb))); }
static VALUE rb_GSL_SIGN(VALUE obj, VALUE x) { return INT2FIX(GSL_SIGN(NUM2DBL(x))); }
static VALUE adj_clamp_page(VALUE self, VALUE lower, VALUE upper) { gtk_adjustment_clamp_page(_SELF(self), NUM2DBL(lower), NUM2DBL(upper)); return self; }
static VALUE rb_gsl_interp_evaluate(VALUE obj, VALUE xxa, VALUE yya, VALUE xx, double (*eval)(const gsl_interp *, const double [], const double [], double, gsl_interp_accel *)) { rb_gsl_interp *rgi = NULL; double *ptrx = NULL, *ptry = NULL; gsl_vector *v = NULL, *vnew = NULL; gsl_matrix *m = NULL, *mnew = NULL; VALUE ary, x; double val; size_t n, i, j, size, stridex, stridey; #ifdef HAVE_NARRAY_H struct NARRAY *na = NULL; double *ptrz = NULL, *ptr = NULL; #endif Data_Get_Struct(obj, rb_gsl_interp, rgi); ptrx = get_vector_ptr(xxa, &stridex, &size); if (size != rgi->p->size ){ rb_raise(rb_eTypeError, "size mismatch (xa:%d != %d)", (int) size, (int) rgi->p->size); } ptry = get_vector_ptr(yya, &stridey, &size); if (size != rgi->p->size ){ rb_raise(rb_eTypeError, "size mismatch (ya:%d != %d)", (int) size, (int) rgi->p->size); } if (CLASS_OF(xx) == rb_cRange) xx = rb_gsl_range2ary(xx); switch (TYPE(xx)) { case T_FIXNUM: case T_BIGNUM: case T_FLOAT: Need_Float(xx); return rb_float_new((*eval)(rgi->p, ptrx, ptry, NUM2DBL(xx), rgi->a)); break; case T_ARRAY: n = RARRAY(xx)->len; ary = rb_ary_new2(n); for (i = 0; i < n; i++) { x = rb_ary_entry(xx, i); Need_Float(x); val = (*eval)(rgi->p, ptrx, ptry, NUM2DBL(x), rgi->a); rb_ary_store(ary, i, rb_float_new(val)); } return ary; break; default: #ifdef HAVE_NARRAY_H if (NA_IsNArray(xx)) { GetNArray(xx, na); ptrz = (double*) na->ptr; ary = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(xx)); ptr = NA_PTR_TYPE(ary, double*); for (i = 0; i < na->total; i++) ptr[i] = (*eval)(rgi->p, ptrx, ptry, ptrz[i], rgi->a); return ary; } #endif if (VECTOR_P(xx)) { Data_Get_Struct(xx, gsl_vector, v); vnew = gsl_vector_alloc(v->size); for (i = 0; i < v->size; i++) { val = (*eval)(rgi->p, ptrx, ptry, gsl_vector_get(v, i), rgi->a); gsl_vector_set(vnew, i, val); } return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew); } else if (MATRIX_P(xx)) { Data_Get_Struct(xx, gsl_matrix, m); mnew = gsl_matrix_alloc(m->size1, m->size2); for (i = 0; i < m->size1; i++) { for (j = 0; j < m->size2; j++) { val = (*eval)(rgi->p, ptrx, ptry, gsl_matrix_get(m, i, j), rgi->a); gsl_matrix_set(mnew, i, j, val); } } return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew); } else { rb_raise(rb_eTypeError, "wrong argument type %s", rb_class2name(CLASS_OF(xx))); } break; } /* never reach here */ return Qnil; }
/* Method: Draw#marshal_load Purpose: Support Marsal.load Notes: On entry all fields are all-bits-0 */ VALUE Draw_marshal_load(VALUE self, VALUE ddraw) { Draw *draw; Pixel *pixel; volatile VALUE val; Data_Get_Struct(self, Draw, draw); draw->info = magick_malloc(sizeof(DrawInfo)); if (!draw->info) { rb_raise(rb_eNoMemError, "not enough memory to continue"); } GetDrawInfo(NULL, draw->info); OBJ_TO_MAGICK_STRING(draw->info->geometry, rb_hash_aref(ddraw, CSTR2SYM("geometry"))); //val = rb_hash_aref(ddraw, CSTR2SYM("viewbox")); //Export_RectangleInfo(&draw->info->viewbox, val); val = rb_hash_aref(ddraw, CSTR2SYM("affine")); Export_AffineMatrix(&draw->info->affine, val); draw->info->gravity = (GravityType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("gravity"))); val = rb_hash_aref(ddraw, CSTR2SYM("fill")); Data_Get_Struct(val, Pixel, pixel); draw->info->fill = *pixel; val = rb_hash_aref(ddraw, CSTR2SYM("stroke")); Data_Get_Struct(val, Pixel, pixel); draw->info->stroke = *pixel; draw->info->stroke_width = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("stroke_width"))); draw->info->fill_pattern = str_to_image(rb_hash_aref(ddraw, CSTR2SYM("fill_pattern"))); draw->info->stroke_pattern = str_to_image(rb_hash_aref(ddraw, CSTR2SYM("stroke_pattern"))); draw->info->stroke_antialias = RTEST(rb_hash_aref(ddraw, CSTR2SYM("stroke_antialias"))); draw->info->text_antialias = RTEST(rb_hash_aref(ddraw, CSTR2SYM("text_antialias"))); draw->info->decorate = (DecorationType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("decorate"))); OBJ_TO_MAGICK_STRING(draw->info->font, rb_hash_aref(ddraw, CSTR2SYM("font"))); OBJ_TO_MAGICK_STRING(draw->info->family, rb_hash_aref(ddraw, CSTR2SYM("family"))); draw->info->style = (StyleType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("style"))); draw->info->stretch = (StretchType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("stretch"))); draw->info->weight = NUM2ULONG(rb_hash_aref(ddraw, CSTR2SYM("weight"))); OBJ_TO_MAGICK_STRING(draw->info->encoding, rb_hash_aref(ddraw, CSTR2SYM("encoding"))); draw->info->pointsize = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("pointsize"))); OBJ_TO_MAGICK_STRING(draw->info->density, rb_hash_aref(ddraw, CSTR2SYM("density"))); draw->info->align = (AlignType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("align"))); val = rb_hash_aref(ddraw, CSTR2SYM("undercolor")); Data_Get_Struct(val, Pixel, pixel); draw->info->undercolor = *pixel; draw->info->clip_units = FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("clip_units"))); draw->info->opacity = NUM2QUANTUM(rb_hash_aref(ddraw, CSTR2SYM("opacity"))); #if defined(HAVE_ST_KERNING) draw->info->kerning = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("kerning"))); #endif #if defined(HAVE_ST_INTERWORD_SPACING) draw->info->interword_spacing = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("interword_spacing"))); #endif draw->primitives = rb_hash_aref(ddraw, CSTR2SYM("primitives")); return self; }
VALUE ph_phsensor_set_correction_temperature(VALUE self, VALUE temperature) { ph_raise(PhidgetPHSensor_setCorrectionTemperature((PhidgetPHSensorHandle)get_ph_handle(self), NUM2DBL(temperature))); return Qnil; }
static VALUE rg_set_value(VALUE self, VALUE value) { gtk_range_set_value(_SELF(self), NUM2DBL(value)); return self; }
VALUE ph_phsensor_set_ph_change_trigger(VALUE self, VALUE trigger) { ph_raise(PhidgetPHSensor_setPHChangeTrigger((PhidgetPHSensorHandle)get_ph_handle(self), NUM2DBL(trigger))); return Qnil; }
static void callback_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data) { Closure* closure = (Closure *) user_data; Function* fn = (Function *) closure->info; FunctionType *cbInfo = fn->info; VALUE* rbParams; VALUE rbReturnValue; int i; rbParams = ALLOCA_N(VALUE, cbInfo->parameterCount); for (i = 0; i < cbInfo->parameterCount; ++i) { VALUE param; switch (cbInfo->parameterTypes[i]->nativeType) { case NATIVE_INT8: param = INT2NUM(*(int8_t *) parameters[i]); break; case NATIVE_UINT8: param = UINT2NUM(*(uint8_t *) parameters[i]); break; case NATIVE_INT16: param = INT2NUM(*(int16_t *) parameters[i]); break; case NATIVE_UINT16: param = UINT2NUM(*(uint16_t *) parameters[i]); break; case NATIVE_INT32: param = INT2NUM(*(int32_t *) parameters[i]); break; case NATIVE_UINT32: param = UINT2NUM(*(uint32_t *) parameters[i]); break; case NATIVE_INT64: param = LL2NUM(*(int64_t *) parameters[i]); break; case NATIVE_UINT64: param = ULL2NUM(*(uint64_t *) parameters[i]); break; case NATIVE_LONG: param = LONG2NUM(*(long *) parameters[i]); break; case NATIVE_ULONG: param = ULONG2NUM(*(unsigned long *) parameters[i]); break; case NATIVE_FLOAT32: param = rb_float_new(*(float *) parameters[i]); break; case NATIVE_FLOAT64: param = rb_float_new(*(double *) parameters[i]); break; case NATIVE_STRING: param = (*(void **) parameters[i] != NULL) ? rb_tainted_str_new2(*(char **) parameters[i]) : Qnil; break; case NATIVE_POINTER: param = rbffi_Pointer_NewInstance(*(void **) parameters[i]); break; case NATIVE_BOOL: param = (*(uint8_t *) parameters[i]) ? Qtrue : Qfalse; break; case NATIVE_FUNCTION: case NATIVE_CALLBACK: param = rbffi_NativeValue_ToRuby(cbInfo->parameterTypes[i], rb_ary_entry(cbInfo->rbParameterTypes, i), parameters[i], Qnil); break; default: param = Qnil; break; } rbParams[i] = param; } rbReturnValue = rb_funcall2(fn->rbProc, id_call, cbInfo->parameterCount, rbParams); if (rbReturnValue == Qnil || TYPE(rbReturnValue) == T_NIL) { memset(retval, 0, cbInfo->ffiReturnType->size); } else switch (cbInfo->returnType->nativeType) { case NATIVE_INT8: case NATIVE_INT16: case NATIVE_INT32: *((ffi_sarg *) retval) = NUM2INT(rbReturnValue); break; case NATIVE_UINT8: case NATIVE_UINT16: case NATIVE_UINT32: *((ffi_arg *) retval) = NUM2UINT(rbReturnValue); break; case NATIVE_INT64: *((int64_t *) retval) = NUM2LL(rbReturnValue); break; case NATIVE_UINT64: *((uint64_t *) retval) = NUM2ULL(rbReturnValue); break; case NATIVE_LONG: *((ffi_sarg *) retval) = NUM2LONG(rbReturnValue); break; case NATIVE_ULONG: *((ffi_arg *) retval) = NUM2ULONG(rbReturnValue); break; case NATIVE_FLOAT32: *((float *) retval) = (float) NUM2DBL(rbReturnValue); break; case NATIVE_FLOAT64: *((double *) retval) = NUM2DBL(rbReturnValue); break; case NATIVE_POINTER: if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_PointerClass)) { *((void **) retval) = ((AbstractMemory *) DATA_PTR(rbReturnValue))->address; } else { // Default to returning NULL if not a value pointer object. handles nil case as well *((void **) retval) = NULL; } break; case NATIVE_BOOL: *((ffi_arg *) retval) = rbReturnValue == Qtrue; break; case NATIVE_FUNCTION: case NATIVE_CALLBACK: if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_PointerClass)) { *((void **) retval) = ((AbstractMemory *) DATA_PTR(rbReturnValue))->address; } else if (rb_obj_is_kind_of(rbReturnValue, rb_cProc) || rb_respond_to(rbReturnValue, id_call)) { VALUE function; function = rbffi_Function_ForProc(cbInfo->rbReturnType, rbReturnValue); *((void **) retval) = ((AbstractMemory *) DATA_PTR(function))->address; } else { *((void **) retval) = NULL; } break; default: *((ffi_arg *) retval) = 0; break; } }
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)); break; } default: rb_raise(rb_eRuntimeError, "Invalid return type"); } }
VALUE OSimplex_Generator_get_4d (const VALUE self, const VALUE x, const VALUE y, const VALUE z, const VALUE w) { SELF2GENERATOR(); double ret = open_simplex_noise4( gen->context, NUM2DBL(x) / gen->scale, NUM2DBL(y) / gen->scale, NUM2DBL(z) / gen->scale, NUM2DBL(w) / gen->scale ); return rb_float_new( normalize(ret, gen ) ); }
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; if (TYPE(value) == T_STRING) { value = rb_funcall(value, rb_intern("to_sym"), 0); } else if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) { rb_raise(cTypeError, "Expected number or symbol type for enum field '%s'.", name); } if (TYPE(value) == T_SYMBOL) { // Ensure that the given symbol exists in the enum module. VALUE lookup = rb_funcall(type_class, rb_intern("resolve"), 1, value); if (lookup == Qnil) { rb_raise(rb_eRangeError, "Unknown symbol value for enum field '%s'.", name); } else { int_val = NUM2INT(lookup); } } else { native_slot_check_int_range_precision(name, UPB_TYPE_INT32, value); int_val = NUM2INT(value); } DEREF(memory, int32_t) = int_val; break; } case UPB_TYPE_INT32: case UPB_TYPE_INT64: case UPB_TYPE_UINT32: case UPB_TYPE_UINT64: native_slot_check_int_range_precision(name, type, value); switch (type) { case UPB_TYPE_INT32: DEREF(memory, int32_t) = NUM2INT(value); break; case UPB_TYPE_INT64: DEREF(memory, int64_t) = NUM2LL(value); break; case UPB_TYPE_UINT32: DEREF(memory, uint32_t) = NUM2UINT(value); break; case UPB_TYPE_UINT64: DEREF(memory, uint64_t) = NUM2ULL(value); break; default: break; } break; default: break; } if (case_memory != NULL) { *case_memory = case_number; } }
static VALUE rg_set_range(VALUE self, VALUE min, VALUE max) { gtk_spin_button_set_range(_SELF(self), NUM2DBL(min), NUM2DBL(max)); return self; }
static VALUE rg_scroll_to(VALUE self, VALUE left, VALUE top) { goo_canvas_scroll_to(SELF(self), NUM2DBL(left), NUM2DBL(top)); return self; }
static VALUE rb_array_set_bottom(VALUE self, VALUE new_bottom) { SET_Y(NUM2DBL(new_bottom)-array_get_h(self)); return self; }
/* call-seq: volume=(volume_float) Sets the volume to the given value (0.0 to 1.0) */ static VALUE track_set_volume(VALUE obj, VALUE volume_obj) { SetTrackVolume(TRACK(obj), (short)(0x0100*NUM2DBL(volume_obj))); return Qnil; }
/* * Set orientation of the feature: 0..360 degrees. * * @overload dir=(value) * @param [Number] Value to set. */ VALUE rb_set_dir(VALUE self, VALUE value) { CVSURFPOINT(self)->dir = (float)NUM2DBL(value); return self; }
static VALUE rb_gsl_math_eval2(double (*func)(const double, const double), VALUE xx, VALUE yy) { VALUE x, y, ary; size_t i, j, size; gsl_vector *v = NULL, *v2 = NULL, *vnew = NULL; gsl_matrix *m = NULL, *m2 = NULL, *mnew = NULL; if (CLASS_OF(xx) == rb_cRange) xx = rb_gsl_range2ary(xx); switch (TYPE(xx)) { case T_FIXNUM: case T_BIGNUM: case T_FLOAT: Need_Float(yy); return rb_float_new((*func)(NUM2DBL(xx), NUM2DBL(yy))); break; case T_ARRAY: Check_Type(yy, T_ARRAY); size = RARRAY_LEN(xx); // if (size != RARRAY(yy)->len) rb_raise(rb_eRuntimeError, "array sizes are different."); if ((int) size != RARRAY_LEN(yy)) rb_raise(rb_eRuntimeError, "array sizes are different."); ary = rb_ary_new2(size); for (i = 0; i < size; i++) { x = rb_ary_entry(xx, i); y = rb_ary_entry(yy, i); Need_Float(x); Need_Float(y); // rb_ary_store(ary, i, rb_float_new((*func)(RFLOAT(x)->value, RFLOAT(y)->value))); rb_ary_store(ary, i, rb_float_new((*func)(NUM2DBL(x), NUM2DBL(y)))); } return ary; break; default: #ifdef HAVE_NARRAY_H if (NA_IsNArray(xx)) { struct NARRAY *nax, *nay; double *ptr1, *ptr2, *ptr3; GetNArray(xx, nax); GetNArray(yy, nay); ptr1 = (double*) nax->ptr; ptr2 = (double*) nay->ptr; size = nax->total; ary = na_make_object(NA_DFLOAT, nax->rank, nax->shape, CLASS_OF(xx)); ptr3 = NA_PTR_TYPE(ary, double*); for (i = 0; i < size; i++) ptr3[i] = (*func)(ptr1[i], ptr2[i]); return ary; } #endif if (VECTOR_P(xx)) { CHECK_VECTOR(yy); Data_Get_Struct(xx, gsl_vector, v); Data_Get_Struct(yy, gsl_vector, v2); vnew = gsl_vector_alloc(v->size); for (i = 0; i < v->size; i++) { gsl_vector_set(vnew, i, (*func)(gsl_vector_get(v, i), gsl_vector_get(v2, i))); } return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew); } else if (MATRIX_P(xx)) { CHECK_MATRIX(yy); Data_Get_Struct(xx, gsl_matrix, m); Data_Get_Struct(yy, gsl_matrix, m2); mnew = gsl_matrix_alloc(m->size1, m->size2); for (i = 0; i < m->size1; i++) { for (j = 0; j < m->size2; j++) { gsl_matrix_set(mnew, i, j, (*func)(gsl_matrix_get(m, i, j), gsl_matrix_get(m2, i, j))); } } return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew); } else { rb_raise(rb_eTypeError, "wrong argument type %s " "(Array or Vector or Matrix expected)", rb_class2name(CLASS_OF(xx))); } break; } /* never reach here */ return Qnil; }