static duk_double_t duk__push_this_number_plain(duk_context *ctx) { duk_hobject *h; /* Number built-in accepts a plain number or a Number object (whose * internal value is operated on). Other types cause TypeError. */ duk_push_this(ctx); if (duk_is_number(ctx, -1)) { DUK_DDD(DUK_DDDPRINT("plain number value: %!T", (duk_tval *) duk_get_tval(ctx, -1))); goto done; } h = duk_get_hobject(ctx, -1); if (!h || (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_NUMBER)) { DUK_DDD(DUK_DDDPRINT("unacceptable this value: %!T", (duk_tval *) duk_get_tval(ctx, -1))); DUK_ERROR((duk_hthread *) ctx, DUK_ERR_TYPE_ERROR, "expected a number"); } duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE); DUK_ASSERT(duk_is_number(ctx, -1)); DUK_DDD(DUK_DDDPRINT("number object: %!T, internal value: %!T", (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1))); duk_remove(ctx, -2); done: return duk_get_number(ctx, -1); }
static duk_ret_t Line_ToLineSegment_Selector(duk_context* ctx) { int numArgs = duk_get_top(ctx); if (numArgs == 2 && duk_is_number(ctx, 0) && duk_is_number(ctx, 1)) return Line_ToLineSegment_float_float(ctx); if (numArgs == 1 && duk_is_number(ctx, 0)) return Line_ToLineSegment_float(ctx); duk_error(ctx, DUK_ERR_ERROR, "Could not select function overload"); }
static duk_ret_t Line_ClosestPoint_Selector(duk_context* ctx) { int numArgs = duk_get_top(ctx); if (numArgs == 3 && GetValueObject<Ray>(ctx, 0, Ray_ID) && duk_is_number(ctx, 1) && duk_is_number(ctx, 2)) return Line_ClosestPoint_Ray_float_float(ctx); if (numArgs == 3 && GetValueObject<Line>(ctx, 0, Line_ID) && duk_is_number(ctx, 1) && duk_is_number(ctx, 2)) return Line_ClosestPoint_Line_float_float(ctx); if (numArgs == 3 && GetValueObject<LineSegment>(ctx, 0, LineSegment_ID) && duk_is_number(ctx, 1) && duk_is_number(ctx, 2)) return Line_ClosestPoint_LineSegment_float_float(ctx); if (numArgs == 3 && GetValueObject<Triangle>(ctx, 0, Triangle_ID) && duk_is_number(ctx, 1) && GetValueObject<float2>(ctx, 2, float2_ID)) return Line_ClosestPoint_Triangle_float_float2(ctx); if (numArgs == 2 && GetValueObject<LineSegment>(ctx, 0, LineSegment_ID) && duk_is_number(ctx, 1)) return Line_ClosestPoint_LineSegment_float(ctx); if (numArgs == 2 && GetValueObject<Triangle>(ctx, 0, Triangle_ID) && duk_is_number(ctx, 1)) return Line_ClosestPoint_Triangle_float(ctx); if (numArgs == 2 && GetValueObject<Line>(ctx, 0, Line_ID) && duk_is_number(ctx, 1)) return Line_ClosestPoint_Line_float(ctx); if (numArgs == 2 && GetValueObject<Ray>(ctx, 0, Ray_ID) && duk_is_number(ctx, 1)) return Line_ClosestPoint_Ray_float(ctx); if (numArgs == 2 && GetValueObject<float3>(ctx, 0, float3_ID) && duk_is_number(ctx, 1)) return Line_ClosestPoint_float3_float(ctx); if (numArgs == 1 && GetValueObject<Triangle>(ctx, 0, Triangle_ID)) return Line_ClosestPoint_Triangle(ctx); if (numArgs == 1 && GetValueObject<float3>(ctx, 0, float3_ID)) return Line_ClosestPoint_float3(ctx); if (numArgs == 1 && GetValueObject<Line>(ctx, 0, Line_ID)) return Line_ClosestPoint_Line(ctx); if (numArgs == 1 && GetValueObject<LineSegment>(ctx, 0, LineSegment_ID)) return Line_ClosestPoint_LineSegment(ctx); if (numArgs == 1 && GetValueObject<Ray>(ctx, 0, Ray_ID)) return Line_ClosestPoint_Ray(ctx); duk_error(ctx, DUK_ERR_ERROR, "Could not select function overload"); }
static int FileSystem_ScanDir(duk_context* ctx) { duk_push_this(ctx); FileSystem* fs = js_to_class_instance<FileSystem>(ctx, -1, 0); if ( !duk_is_string(ctx, 0) || !duk_is_string(ctx, 1) || !duk_is_number(ctx, 2) || !duk_is_boolean(ctx, 3)) { duk_push_string(ctx, "FileSystem::ScanDir bad args"); duk_throw(ctx); } const char* pathName = duk_to_string(ctx, 0); const char* filter = duk_to_string(ctx, 1); unsigned flags = duk_to_number(ctx, 2); bool recursive = duk_to_boolean(ctx, 3) ? true : false; Vector<String> result; fs->ScanDir(result, pathName, filter, flags, recursive); duk_push_array(ctx); for (unsigned i = 0; i < result.Size(); i++) { duk_push_string(ctx, result[i].CString()); duk_put_prop_index(ctx, -2, i); } return 1; }
static duk_ret_t dukzip_zip_newfile(duk_context *ctx) { zip_fileinfo zi = {0}; int res = ZIP_OK; zipFile archive = dukzip_zip_from_this(ctx); const char *filename = ""; duk_int_t level = Z_DEFAULT_COMPRESSION; duk_int_t method = Z_DEFLATED; const char *comment = ""; if (duk_is_object(ctx, 0)) { dukzip_zip_checkoptions(ctx, 0, &filename, &level, &method, &comment); } else { filename = duk_require_string(ctx, 0); if (duk_is_number(ctx, 1)) { level = duk_get_int(ctx, 1); } } res = zipOpenNewFileInZip64(archive, filename, &zi, NULL, 0, NULL, 0, comment, method, level, 1); if (res == ZIP_OK) { duk_push_true(ctx); } else { duk_push_false(ctx); } return 1; }
static duk_ret_t nsp_texture_constructor(duk_context *ctx) { int width = duk_require_int(ctx, 0); int height = duk_require_int(ctx, 1); if (width < 1 || height < 1) { duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "Width and height must be positive"); duk_throw(ctx); } bool has_transparency; uint16_t transparent_color; if ((has_transparency = duk_is_number(ctx, 2))) { transparent_color = (uint16_t)duk_get_int(ctx, 2); } duk_push_this(ctx); duk_push_fixed_buffer(ctx, width * height * 2); duk_put_prop_string(ctx, -2, "bitmap"); duk_push_int(ctx, width); duk_put_prop_string(ctx, -2, "width"); duk_push_int(ctx, height); duk_put_prop_string(ctx, -2, "height"); if (has_transparency) { duk_push_int(ctx, transparent_color); } else { duk_push_null(ctx); } duk_put_prop_string(ctx, -2, "transparentColor"); return 0; }
static duk_ret_t Line_Intersects_Selector(duk_context* ctx) { int numArgs = duk_get_top(ctx); if (numArgs == 3 && GetValueObject<OBB>(ctx, 0, OBB_ID) && duk_is_number(ctx, 1) && duk_is_number(ctx, 2)) return Line_Intersects_OBB_float_float(ctx); if (numArgs == 3 && GetValueObject<AABB>(ctx, 0, AABB_ID) && duk_is_number(ctx, 1) && duk_is_number(ctx, 2)) return Line_Intersects_AABB_float_float(ctx); if (numArgs == 1 && GetValueObject<Capsule>(ctx, 0, Capsule_ID)) return Line_Intersects_Capsule(ctx); if (numArgs == 1 && GetValueObject<Frustum>(ctx, 0, Frustum_ID)) return Line_Intersects_Frustum(ctx); if (numArgs == 1 && GetValueObject<AABB>(ctx, 0, AABB_ID)) return Line_Intersects_AABB(ctx); if (numArgs == 1 && GetValueObject<OBB>(ctx, 0, OBB_ID)) return Line_Intersects_OBB(ctx); duk_error(ctx, DUK_ERR_ERROR, "Could not select function overload"); }
static duk_ret_t Line_Distance_Selector(duk_context* ctx) { int numArgs = duk_get_top(ctx); if (numArgs == 3 && GetValueObject<Line>(ctx, 0, Line_ID) && duk_is_number(ctx, 1) && duk_is_number(ctx, 2)) return Line_Distance_Line_float_float(ctx); if (numArgs == 3 && GetValueObject<LineSegment>(ctx, 0, LineSegment_ID) && duk_is_number(ctx, 1) && duk_is_number(ctx, 2)) return Line_Distance_LineSegment_float_float(ctx); if (numArgs == 3 && GetValueObject<Ray>(ctx, 0, Ray_ID) && duk_is_number(ctx, 1) && duk_is_number(ctx, 2)) return Line_Distance_Ray_float_float(ctx); if (numArgs == 2 && GetValueObject<LineSegment>(ctx, 0, LineSegment_ID) && duk_is_number(ctx, 1)) return Line_Distance_LineSegment_float(ctx); if (numArgs == 2 && GetValueObject<Line>(ctx, 0, Line_ID) && duk_is_number(ctx, 1)) return Line_Distance_Line_float(ctx); if (numArgs == 2 && GetValueObject<float3>(ctx, 0, float3_ID) && duk_is_number(ctx, 1)) return Line_Distance_float3_float(ctx); if (numArgs == 2 && GetValueObject<Ray>(ctx, 0, Ray_ID) && duk_is_number(ctx, 1)) return Line_Distance_Ray_float(ctx); if (numArgs == 1 && GetValueObject<Sphere>(ctx, 0, Sphere_ID)) return Line_Distance_Sphere(ctx); if (numArgs == 1 && GetValueObject<Capsule>(ctx, 0, Capsule_ID)) return Line_Distance_Capsule(ctx); if (numArgs == 1 && GetValueObject<Line>(ctx, 0, Line_ID)) return Line_Distance_Line(ctx); if (numArgs == 1 && GetValueObject<Ray>(ctx, 0, Ray_ID)) return Line_Distance_Ray(ctx); if (numArgs == 1 && GetValueObject<LineSegment>(ctx, 0, LineSegment_ID)) return Line_Distance_LineSegment(ctx); if (numArgs == 1 && GetValueObject<float3>(ctx, 0, float3_ID)) return Line_Distance_float3(ctx); duk_error(ctx, DUK_ERR_ERROR, "Could not select function overload"); }
int Get( duk_context * ctx, const int index, const int * ) { dukbind_assert( duk_is_number( ctx, index ), "No conversion is allowed in Get methods" ); duk_double_t double_result = duk_to_number( ctx, index ); int int_result = static_cast<int>( double_result ); dukbind_assert( double_result == (duk_double_t)int_result, "Number is not a valid integer" ); return int_result; }
static duk_ret_t EntityReference_Ctor_Selector(duk_context* ctx) { int numArgs = duk_get_top(ctx); if (numArgs == 1 && duk_is_number(ctx, 0)) return EntityReference_Ctor_entity_id_t(ctx); if (numArgs == 1 && duk_is_string(ctx, 0)) return EntityReference_Ctor_String(ctx); if (numArgs == 0) return EntityReference_Ctor(ctx); duk_error(ctx, DUK_ERR_ERROR, "Could not select function overload"); }
static int NativePinSetTrigger(duk_context* ctx) { uint32_t debounce = 0; AJS_IO_PinTriggerCondition condition = (AJS_IO_PinTriggerCondition)duk_require_int(ctx, 0); if (condition & AJS_IO_PIN_TRIGGER_ON_BOTH) { if (duk_is_number(ctx, 2)) { debounce = duk_get_int(ctx, 2); debounce = min(255, debounce); } } return SetTriggerCallback(ctx, AJS_IO_FUNCTION_DIGITAL_IN, debounce); }
float Get( duk_context * ctx, const int index, const float * ) { dukbind_assert( duk_is_number( ctx, index ), "No conversion is allowed in Get methods" ); duk_double_t double_result = duk_to_number( ctx, index ); float float_result = static_cast<float>( double_result ); #if DUKBIND_NO_FLOAT_CONVERSION dukbind_assert( double_result == (duk_double_t)float_result, "Number is not a valid integer" ); #endif return float_result; }
gboolean _gum_duk_parse_bytes (duk_context * ctx, duk_idx_t index, GBytes ** bytes) { gpointer data; duk_size_t size; data = duk_get_buffer_data (ctx, index, &size); if (data != NULL) { *bytes = g_bytes_new (data, size); return TRUE; } else if (duk_is_array (ctx, index)) { duk_size_t i; duk_get_prop_string (ctx, index, "length"); size = duk_get_uint (ctx, -1); duk_pop (ctx); if (size >= GUM_MAX_JS_BYTE_ARRAY_LENGTH) return FALSE; data = g_malloc (size); for (i = 0; i != size; i++) { duk_get_prop_index (ctx, index, (duk_uarridx_t) i); ((guint8 *) data)[i] = duk_get_uint (ctx, -1) & 0xff; duk_pop (ctx); } *bytes = g_bytes_new_take (data, size); return TRUE; } else if (duk_is_null_or_undefined (ctx, index) || duk_is_boolean (ctx, index) || duk_is_number (ctx, index) || duk_is_nan (ctx, index) || duk_is_string (ctx, index) || duk_is_function (ctx, index)) { return FALSE; } *bytes = g_bytes_new (NULL, 0); return TRUE; }
static int es_sqlite_query(duk_context *ctx) { int argc = duk_get_top(ctx); if(argc < 2) return DUK_RET_TYPE_ERROR; es_sqlite_t *es = es_resource_get(ctx, 0, &es_resource_sqlite); const char *query = duk_safe_to_string(ctx, 1); if(es->es_stmt) { sqlite3_finalize(es->es_stmt); es->es_stmt = NULL; } int rc = db_prepare(es->es_db, &es->es_stmt, query); if(rc != SQLITE_OK) { if(es->es_transaction && rc == SQLITE_LOCKED) duk_error(ctx, ST_ERROR_SQLITE_BASE | rc , "Deadlock"); duk_error(ctx, ST_ERROR_SQLITE_BASE | rc, "Sqlite error 0x%x -- %s", rc, sqlite3_errmsg(es->es_db)); } sqlite3_stmt *stmt = es->es_stmt; for(int i = 2; i < argc; i++) { int sqlite_arg = i - 1; if(duk_is_null_or_undefined(ctx, i)) { sqlite3_bind_null(stmt, sqlite_arg); } else if(duk_is_number(ctx, i)) { sqlite3_bind_double(stmt, sqlite_arg, duk_get_number(ctx, i)); } else if(duk_is_boolean(ctx, i)) { sqlite3_bind_int(stmt, sqlite_arg, duk_get_boolean(ctx, i)); } else { sqlite3_bind_text(stmt, sqlite_arg, duk_safe_to_string(ctx, i), -1, SQLITE_STATIC); } } es_sqlite_stmt_step(ctx, es); return 0; }
gboolean _gum_duk_get_uint (duk_context * ctx, duk_idx_t index, guint * u) { duk_double_t number; if (!duk_is_number (ctx, index)) return FALSE; number = duk_require_number (ctx, index); if (number < 0) return FALSE; *u = (guint) number; return TRUE; }
gboolean _gum_duk_get_uint64 (duk_context * ctx, duk_idx_t index, GumDukCore * core, guint64 * u) { if (duk_is_pointer (ctx, index)) { *u = *((const guint64 *) duk_require_pointer (ctx, index)); return TRUE; } else if (duk_is_number (ctx, index)) { duk_double_t number; number = duk_require_number (ctx, index); if (number < 0) return FALSE; *u = (guint64) number; return TRUE; } else { gboolean success = FALSE; duk_dup (ctx, index); duk_push_heapptr (ctx, core->uint64); if (duk_instanceof (ctx, -2, -1)) { GumDukUInt64 * object; object = _gum_duk_require_data (ctx, -2); *u = object->value; success = TRUE; } duk_pop_2 (ctx); return success; } }
static void dukzip_zip_checkoptions(duk_context *ctx, duk_idx_t idx, const char **filename, duk_int_t *level, duk_int_t *method, const char **comment) { duk_get_prop_string(ctx, idx, "filename"); if (duk_is_string(ctx, -1)) { *filename = duk_get_string(ctx, -1); duk_pop(ctx); } else { duk_pop(ctx); } duk_get_prop_string(ctx, idx, "level"); if (duk_is_number(ctx, -1)) { *level = duk_get_int(ctx, -1); duk_pop(ctx); } else { duk_pop(ctx); } duk_get_prop_string(ctx, idx, "method"); if (duk_is_string(ctx, -1)) { duk_push_string(ctx, "deflate"); if (duk_equals(ctx, -1, -2)) { *method = Z_DEFLATED; } else { duk_pop(ctx); duk_push_string(ctx, "store"); if (duk_equals(ctx, -1, -2)) { *method = 0; } } duk_pop_2(ctx); } else { duk_pop(ctx); } duk_get_prop_string(ctx, idx, "comment"); if (duk_is_string(ctx, -1)) { *comment = duk_get_string(ctx, -1); duk_pop(ctx); } else { duk_pop(ctx); } }
/* * Configures a pin as a digital output pin */ static int NativeIoDigitalOut(duk_context* ctx) { AJ_Status status; int idx; void* pinCtx; uint32_t pin = GetPinId(ctx, 0, AJS_IO_FUNCTION_DIGITAL_OUT); /* * Target specific I/O pin initialization */ status = AJS_TargetIO_PinOpen(pin, AJS_IO_PIN_OUTPUT, &pinCtx); if (status != AJ_OK) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Failed to configure digital output pin: %s", AJ_StatusText(status)); } idx = NewIOObject(ctx, pinCtx, AJS_IO_FUNCTION_DIGITAL_OUT, NativePinFinalizer); /* * Functions to set/get the pin level */ AJS_SetPropertyAccessors(ctx, idx, "level", NativeLevelSetter, NativeLevelGetter); /* * Check for and set initial value */ if (duk_is_number(ctx, 1)) { duk_dup(ctx, 1); duk_put_prop_string(ctx, idx, "level"); } /* * Toggle function */ duk_push_c_lightfunc(ctx, NativeTogglePin, 0, 0, 0); duk_put_prop_string(ctx, idx, "toggle"); /* * Only allow if the PWM functions is supported */ if (AJS_TargetIO_GetInfo(pin)->functions & AJS_IO_FUNCTION_PWM) { duk_push_c_lightfunc(ctx, NativePWM, 2, 0, 0); duk_put_prop_string(ctx, idx, "pwm"); } /* * Return the digital output pin object */ return 1; }
int my_adder(duk_context *ctx) { int i, n; double res = 0.0; duk_push_this(ctx); printf("this binding: '%s'\n", duk_to_string(ctx, -1)); duk_pop(ctx); n = duk_get_top(ctx); for (i = 0; i < n; i++) { if (!duk_is_number(ctx, i)) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "argument %d is not a number", i); } res += duk_get_number(ctx, i); } duk_push_number(ctx, res); return 1; }
gboolean _gum_duk_parse_pointer (duk_context * ctx, duk_idx_t index, GumDukCore * core, gpointer * ptr) { if (duk_is_string (ctx, index)) { const gchar * ptr_as_string, * end; gboolean valid; ptr_as_string = duk_require_string (ctx, index); if (g_str_has_prefix (ptr_as_string, "0x")) { *ptr = GSIZE_TO_POINTER ( g_ascii_strtoull (ptr_as_string + 2, (gchar **) &end, 16)); valid = end != ptr_as_string + 2; } else { *ptr = GSIZE_TO_POINTER ( g_ascii_strtoull (ptr_as_string, (gchar **) &end, 10)); valid = end != ptr_as_string; } return valid; } else if (duk_is_number (ctx, index)) { duk_double_t number; number = duk_require_number (ctx, index); if (number < 0) return FALSE; *ptr = GSIZE_TO_POINTER ((gsize) number); return TRUE; } return _gum_duk_get_pointer (ctx, index, core, ptr); }
static int duk_disasm(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { int res = 0, res2 = 0; const char *opstr = NULL; ut8 *b = a->cur->user; duk_push_global_stash (ctx); duk_dup (ctx, 0); /* timer callback */ duk_get_prop_string (ctx, -2, "disfun"); b = a->cur->user = duk_require_tval (ctx, -1); // pushBuffer (buf, len); if (duk_is_callable(ctx, -1)) { int i; // duk_push_string (ctx, "TODO 2"); pushBuffer (buf, len); duk_call (ctx, 1); // [ size, str ] for (i = 0; i<3; i++) { duk_dup_top (ctx); duk_get_prop_index (ctx, -1, i); if (duk_is_number (ctx, -1)) { if (res) res2 = duk_to_number (ctx, -1); else res2 = res = duk_to_number (ctx, -1); } else if (duk_is_string (ctx, -1)) { if (!opstr) { opstr = duk_to_string (ctx, -1); } } duk_pop (ctx); } } else { eprintf ("[:(] Is not a function %02x %02x\n", b[0],b[1]); } // fill op struct op->size = res; if (!opstr) opstr = "invalid"; strncpy (op->buf_asm, opstr, sizeof (op->buf_asm)); r_hex_bin2str (buf, op->size, op->buf_hex); return res2; }
static int es_htsmsg_get_value_duk(duk_context *ctx) { htsmsg_t *m = es_get_native_obj(ctx, 0, &es_native_htsmsg); htsmsg_field_t *f; int want_attr = 0; if(duk_is_number(ctx, 1)) { int i = duk_require_int(ctx, 1); HTSMSG_FOREACH(f, m) { if(f->hmf_flags & HMF_XML_ATTRIBUTE) continue; if(!i--) break; } if(f == NULL) return 0; } else {
/* * Configures a pin as a digital input pin */ static int NativeIoDigitalIn(duk_context* ctx) { AJ_Status status; int idx; void* pinCtx; int config = -1; uint32_t pin = GetPinId(ctx, 0, AJS_IO_FUNCTION_DIGITAL_IN); if (duk_is_undefined(ctx, 1)) { config = AJS_IO_PIN_PULL_UP; } else if (duk_is_number(ctx, 1)) { config = (AJS_IO_PinConfig)duk_get_int(ctx, 1); } if ((config != AJS_IO_PIN_OPEN_DRAIN) && (config != AJS_IO_PIN_PULL_UP) && (config != AJS_IO_PIN_PULL_DOWN)) { duk_error(ctx, DUK_ERR_RANGE_ERROR, "Configuration must be pullUp, pullDown, or openDrain"); } /* * Target specific I/O pin initialization */ status = AJS_TargetIO_PinOpen(pin, (AJS_IO_PinConfig)config, &pinCtx); if (status != AJ_OK) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Failed to open digital input pin: %s", AJ_StatusText(status)); } idx = NewIOObject(ctx, pinCtx, AJS_IO_FUNCTION_DIGITAL_IN, NativePinFinalizer); /* * Function to get the pin level */ AJS_SetPropertyAccessors(ctx, idx, "level", NULL, NativeLevelGetter); /* * Function to set and clear a trigger */ duk_push_c_lightfunc(ctx, NativePinSetTrigger, 3, 0, 0); duk_put_prop_string(ctx, idx, "setTrigger"); duk_push_c_lightfunc(ctx, NativePinClearTrigger, 1, 0, 0); duk_put_prop_string(ctx, idx, "clearTrigger"); /* * Return the digital input pin object */ return 1; }
static duk_ret_t js_ByteArray_getProp(duk_context* ctx) { bytearray_t* array; int index; int size; duk_get_prop_string(ctx, 0, "\xFF" "ptr"); array = duk_get_pointer(ctx, -1); duk_pop(ctx); if (duk_is_number(ctx, 1)) { index = duk_to_int(ctx, 1); size = get_bytearray_size(array); if (index < 0 || index >= size) duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "ByteArray[]: Index is out of bounds (%i - size: %i)", index, size); duk_push_uint(ctx, get_byte(array, index)); return 1; } else { duk_dup(ctx, 1); duk_get_prop(ctx, 0); return 1; } }
/* * Read data from a file. Args: * - 0: FILE * - 1: nread (a number or a Buffer-ish object) */ static duk_ret_t io_fread(duk_context* ctx) { FILE* f; size_t nread, r; char* buf; int create_buf = 0; f = duk_require_pointer(ctx, 0); if (duk_is_number(ctx, 1)) { nread = duk_require_int(ctx, 1); buf = duk_push_dynamic_buffer(ctx, nread); create_buf = 1; } else { buf = duk_require_buffer_data(ctx, 1, &nread); if (buf == NULL || nread == 0) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "invalid buffer"); return -42; /* control never returns here */ } } r = fread(buf, 1, nread, f); if (ferror(f)) { if (create_buf) { duk_pop(ctx); } clearerr(f); SJS_THROW_ERRNO_ERROR2(EIO); return -42; /* control never returns here */ } if (create_buf) { /* return the string we read */ duk_resize_buffer(ctx, -1, r); } else { /* the data was written to the buffer, return how much we read */ duk_push_int(ctx, r); } return 1; }
/* * Read a line from a file. Args: * - 0: FILE * - 1: nread (a number or a Buffer-ish object) */ static duk_ret_t io_fgets(duk_context* ctx) { FILE* f; size_t nread; char* r; char* buf; char* alloc_buf = NULL; f = duk_require_pointer(ctx, 0); if (duk_is_number(ctx, 1)) { nread = duk_require_int(ctx, 1); alloc_buf = malloc(nread); if (!alloc_buf) { SJS_THROW_ERRNO_ERROR2(ENOMEM); return -42; /* control never returns here */ } buf = alloc_buf; } else { buf = duk_require_buffer_data(ctx, 1, &nread); if (buf == NULL || nread == 0) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "invalid buffer"); return -42; /* control never returns here */ } } r = fgets(buf, nread, f); if (r == NULL) { duk_push_string(ctx, ""); } else { if (alloc_buf) { /* return the string we read */ duk_push_string(ctx, r); } else { /* the data was written to the buffer, return how much we read */ duk_push_int(ctx, strlen(r)); } } free(alloc_buf); return 1; }
DUK_INTERNAL duk_ret_t duk_bi_array_constructor(duk_context *ctx) { duk_idx_t nargs; duk_double_t d; duk_uint32_t len; duk_idx_t i; nargs = duk_get_top(ctx); duk_push_array(ctx); if (nargs == 1 && duk_is_number(ctx, 0)) { /* XXX: expensive check (also shared elsewhere - so add a shared internal API call?) */ d = duk_get_number(ctx, 0); len = duk_to_uint32(ctx, 0); if (((duk_double_t) len) != d) { return DUK_RET_RANGE_ERROR; } /* XXX: if 'len' is low, may want to ensure array part is kept: * the caller is likely to want a dense array. */ duk_push_u32(ctx, len); duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); /* [ ToUint32(len) array ToUint32(len) ] -> [ ToUint32(len) array ] */ return 1; } /* XXX: optimize by creating array into correct size directly, and * operating on the array part directly; values can be memcpy()'d from * value stack directly as long as refcounts are increased. */ for (i = 0; i < nargs; i++) { duk_dup(ctx, i); duk_xdef_prop_index_wec(ctx, -2, (duk_uarridx_t) i); } duk_push_u32(ctx, (duk_uint32_t) nargs); duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); return 1; }
gboolean _gum_duk_get_int64 (duk_context * ctx, duk_idx_t index, GumDukCore * core, gint64 * i) { if (duk_is_pointer (ctx, index)) { *i = *((const gint64 *) duk_require_pointer (ctx, index)); return TRUE; } else if (duk_is_number (ctx, index)) { *i = (gint64) duk_require_number (ctx, index); return TRUE; } else { gboolean success = FALSE; duk_dup (ctx, index); duk_push_heapptr (ctx, core->int64); if (duk_instanceof (ctx, -2, -1)) { GumDukInt64 * object; object = _gum_duk_require_data (ctx, -2); *i = object->value; success = TRUE; } duk_pop_2 (ctx); return success; } }
CEJsValue js_to_cejs_value(duk_context* ctx, int index) { if (duk_is_number(ctx, index)) return JS_NUMBER(duk_to_number(ctx, index)); if (duk_is_boolean(ctx, index)) return JS_BOOL(duk_to_boolean(ctx, index)); if (duk_is_string(ctx, index)) return JS_STRING(duk_to_string(ctx, index)); if (duk_is_array(ctx, index)) return JS_STRING(duk_json_encode(ctx, index)); if (duk_is_object(ctx, index)) return JS_STRING(duk_json_encode(ctx, index)); if (duk_is_undefined(ctx, index)) { return JS_UNDEFINED; } return JS_NULL; }
guint _gum_duk_require_index (duk_context * ctx, duk_idx_t index) { if (duk_is_number (ctx, index)) { guint value; if (_gum_duk_get_uint (ctx, index, &value)) { return value; } else { _gum_duk_throw (ctx, "invalid index"); return 0; /* unreachable */ } } else { const gchar * str; gchar * endptr; glong value; gboolean valid; str = duk_require_string (ctx, index); value = strtol (str, &endptr, 10); valid = *str != '\0' && *endptr == '\0' && value >= 0; if (!valid) _gum_duk_throw (ctx, "invalid index"); return value; } }