static int jsR_hasproperty(js_State *J, js_Object *obj, const char *name) { js_Property *ref; unsigned int k; if (obj->type == JS_CARRAY) { if (!strcmp(name, "length")) { js_pushnumber(J, obj->u.a.length); return 1; } } if (obj->type == JS_CSTRING) { if (!strcmp(name, "length")) { js_pushnumber(J, obj->u.s.length); return 1; } if (js_isarrayindex(J, name, &k)) { js_pushrune(J, js_runeat(J, obj->u.s.string, k)); return 1; } } if (obj->type == JS_CREGEXP) { if (!strcmp(name, "source")) { js_pushliteral(J, obj->u.r.source); return 1; } if (!strcmp(name, "global")) { js_pushboolean(J, obj->u.r.flags & JS_REGEXP_G); return 1; } if (!strcmp(name, "ignoreCase")) { js_pushboolean(J, obj->u.r.flags & JS_REGEXP_I); return 1; } if (!strcmp(name, "multiline")) { js_pushboolean(J, obj->u.r.flags & JS_REGEXP_M); return 1; } if (!strcmp(name, "lastIndex")) { js_pushnumber(J, obj->u.r.last); return 1; } } ref = jsV_getproperty(J, obj, name); if (ref) { if (ref->getter) { js_pushobject(J, ref->getter); js_pushobject(J, obj); js_call(J, 0); } else { js_pushvalue(J, ref->value); } return 1; } return 0; }
static void js_setvar(js_State *J, const char *name) { js_Environment *E = J->E; do { js_Property *ref = jsV_getproperty(J, E->variables, name); if (ref) { if (ref->setter) { js_pushobject(J, ref->setter); js_pushobject(J, E->variables); js_copy(J, -3); js_call(J, 1); js_pop(J, 1); return; } if (!(ref->atts & JS_READONLY)) ref->value = *stackidx(J, -1); else if (J->strict) js_typeerror(J, "'%s' is read-only", name); return; } E = E->outer; } while (E); if (J->strict) js_referenceerror(J, "assignment to undeclared variable '%s'", name); jsR_setproperty(J, J->G, name, stackidx(J, -1)); }
static void O_getOwnPropertyDescriptor(js_State *J) { js_Object *obj; js_Property *ref; if (!js_isobject(J, 1)) js_typeerror(J, "not an object"); obj = js_toobject(J, 1); ref = jsV_getproperty(J, obj, js_tostring(J, 2)); if (!ref) js_pushundefined(J); else { js_newobject(J); if (!ref->getter && !ref->setter) { js_pushvalue(J, ref->value); js_setproperty(J, -2, "value"); js_pushboolean(J, !(ref->atts & JS_READONLY)); js_setproperty(J, -2, "writable"); } else { if (ref->getter) js_pushobject(J, ref->getter); else js_pushundefined(J); js_setproperty(J, -2, "get"); if (ref->setter) js_pushobject(J, ref->setter); else js_pushundefined(J); js_setproperty(J, -2, "set"); } js_pushboolean(J, !(ref->atts & JS_DONTENUM)); js_setproperty(J, -2, "enumerable"); js_pushboolean(J, !(ref->atts & JS_DONTCONF)); js_setproperty(J, -2, "configurable"); } }
static void ToPropertyDescriptor(js_State *J, js_Object *obj, const char *name, js_Object *desc) { int haswritable = 0; int hasvalue = 0; int enumerable = 0; int configurable = 0; int writable = 0; int atts = 0; js_pushobject(J, obj); js_pushobject(J, desc); if (js_hasproperty(J, -1, "writable")) { haswritable = 1; writable = js_toboolean(J, -1); js_pop(J, 1); } if (js_hasproperty(J, -1, "enumerable")) { enumerable = js_toboolean(J, -1); js_pop(J, 1); } if (js_hasproperty(J, -1, "configurable")) { configurable = js_toboolean(J, -1); js_pop(J, 1); } if (js_hasproperty(J, -1, "value")) { hasvalue = 1; js_setproperty(J, -3, name); } if (!writable) atts |= JS_READONLY; if (!enumerable) atts |= JS_DONTENUM; if (!configurable) atts |= JS_DONTCONF; if (js_hasproperty(J, -1, "get")) { if (haswritable || hasvalue) js_typeerror(J, "value/writable and get/set attributes are exclusive"); } else { js_pushundefined(J); } if (js_hasproperty(J, -2, "set")) { if (haswritable || hasvalue) js_typeerror(J, "value/writable and get/set attributes are exclusive"); } else { js_pushundefined(J); } js_defaccessor(J, -4, name, atts); js_pop(J, 2); }
void js_construct(js_State *J, int n) { js_Object *obj; js_Object *prototype; js_Object *newobj; if (!js_iscallable(J, -n-1)) js_typeerror(J, "called object is not a function"); obj = js_toobject(J, -n-1); /* built-in constructors create their own objects, give them a 'null' this */ if (obj->type == JS_CCFUNCTION && obj->u.c.constructor) { int savebot = BOT; js_pushnull(J); if (n > 0) js_rot(J, n + 1); BOT = TOP - n - 1; jsR_pushtrace(J, obj->u.c.name, "[C]", 0); jsR_callcfunction(J, n, obj->u.c.length, obj->u.c.constructor); --J->tracetop; BOT = savebot; return; } /* extract the function object's prototype property */ js_getproperty(J, -n - 1, "prototype"); if (js_isobject(J, -1)) prototype = js_toobject(J, -1); else prototype = J->Object_prototype; js_pop(J, 1); /* create a new object with above prototype, and shift it into the 'this' slot */ newobj = jsV_newobject(J, JS_COBJECT, prototype); js_pushobject(J, newobj); if (n > 0) js_rot(J, n + 1); /* call the function */ js_call(J, n); /* if result is not an object, return the original object we created */ if (!js_isobject(J, -1)) { js_pop(J, 1); js_pushobject(J, newobj); } }
void jsB_initmath(js_State *J) { js_pushobject(J, jsV_newobject(J, JS_CMATH, J->Object_prototype)); { jsB_propn(J, "E", 2.7182818284590452354); jsB_propn(J, "LN10", 2.302585092994046); jsB_propn(J, "LN2", 0.6931471805599453); jsB_propn(J, "LOG2E", 1.4426950408889634); jsB_propn(J, "LOG10E", 0.4342944819032518); jsB_propn(J, "PI", 3.1415926535897932); jsB_propn(J, "SQRT1_2", 0.7071067811865476); jsB_propn(J, "SQRT2", 1.4142135623730951); jsB_propf(J, "abs", Math_abs, 1); jsB_propf(J, "acos", Math_acos, 1); jsB_propf(J, "asin", Math_asin, 1); jsB_propf(J, "atan", Math_atan, 1); jsB_propf(J, "atan2", Math_atan2, 2); jsB_propf(J, "ceil", Math_ceil, 1); jsB_propf(J, "cos", Math_cos, 1); jsB_propf(J, "exp", Math_exp, 1); jsB_propf(J, "floor", Math_floor, 1); jsB_propf(J, "log", Math_log, 1); jsB_propf(J, "max", Math_max, 0); jsB_propf(J, "min", Math_min, 0); jsB_propf(J, "pow", Math_pow, 2); jsB_propf(J, "random", Math_random, 0); jsB_propf(J, "round", Math_round, 1); jsB_propf(J, "sin", Math_sin, 1); jsB_propf(J, "sqrt", Math_sqrt, 1); jsB_propf(J, "tan", Math_tan, 1); } js_defglobal(J, "Math", JS_DONTENUM); }
void jsB_initarray(js_State *J) { js_pushobject(J, J->Array_prototype); { jsB_propf(J, "Array.prototype.toString", Ap_toString, 0); jsB_propf(J, "Array.prototype.concat", Ap_concat, 0); /* 1 */ jsB_propf(J, "Array.prototype.join", Ap_join, 1); jsB_propf(J, "Array.prototype.pop", Ap_pop, 0); jsB_propf(J, "Array.prototype.push", Ap_push, 0); /* 1 */ jsB_propf(J, "Array.prototype.reverse", Ap_reverse, 0); jsB_propf(J, "Array.prototype.shift", Ap_shift, 0); jsB_propf(J, "Array.prototype.slice", Ap_slice, 2); jsB_propf(J, "Array.prototype.sort", Ap_sort, 1); jsB_propf(J, "Array.prototype.splice", Ap_splice, 0); /* 2 */ jsB_propf(J, "Array.prototype.unshift", Ap_unshift, 0); /* 1 */ /* ES5 */ jsB_propf(J, "Array.prototype.indexOf", Ap_indexOf, 1); jsB_propf(J, "Array.prototype.lastIndexOf", Ap_lastIndexOf, 1); jsB_propf(J, "Array.prototype.every", Ap_every, 1); jsB_propf(J, "Array.prototype.some", Ap_some, 1); jsB_propf(J, "Array.prototype.forEach", Ap_forEach, 1); jsB_propf(J, "Array.prototype.map", Ap_map, 1); jsB_propf(J, "Array.prototype.filter", Ap_filter, 1); jsB_propf(J, "Array.prototype.reduce", Ap_reduce, 1); jsB_propf(J, "Array.prototype.reduceRight", Ap_reduceRight, 1); } js_newcconstructor(J, jsB_new_Array, jsB_new_Array, "Array", 0); /* 1 */ { /* ES5 */ jsB_propf(J, "Array.isArray", A_isArray, 1); } js_defglobal(J, "Array", JS_DONTENUM); }
void js_newscript(js_State *J, js_Function *fun, js_Environment *scope) { js_Object *obj = jsV_newobject(J, JS_CSCRIPT, NULL); obj->u.f.function = fun; obj->u.f.scope = scope; js_pushobject(J, obj); }
static void O_create(js_State *J) { js_Object *obj; js_Object *proto; js_Object *props; js_Property *ref; if (js_isobject(J, 1)) proto = js_toobject(J, 1); else if (js_isnull(J, 1)) proto = NULL; else js_typeerror(J, "not an object or null"); obj = jsV_newobject(J, JS_COBJECT, proto); js_pushobject(J, obj); if (js_isdefined(J, 2)) { if (!js_isobject(J, 2)) js_typeerror(J, "not an object"); props = js_toobject(J, 2); for (ref = props->head; ref; ref = ref->next) { if (!(ref->atts & JS_DONTENUM)) { if (ref->value.type != JS_TOBJECT) js_typeerror(J, "not an object"); ToPropertyDescriptor(J, obj, ref->name, ref->value.u.object); } } } }
static void jsB_new_Object(js_State *J) { if (js_isundefined(J, 1) || js_isnull(J, 1)) js_newobject(J); else js_pushobject(J, js_toobject(J, 1)); }
void js_pushundefinedthis(js_State *J) { if (J->strict) js_pushundefined(J); else js_pushobject(J, J->G); }
void jsB_initobject(js_State *J) { js_pushobject(J, J->Object_prototype); { jsB_propf(J, "Object.prototype.toString", Op_toString, 0); jsB_propf(J, "Object.prototype.toLocaleString", Op_toString, 0); jsB_propf(J, "Object.prototype.valueOf", Op_valueOf, 0); jsB_propf(J, "Object.prototype.hasOwnProperty", Op_hasOwnProperty, 1); jsB_propf(J, "Object.prototype.isPrototypeOf", Op_isPrototypeOf, 1); jsB_propf(J, "Object.prototype.propertyIsEnumerable", Op_propertyIsEnumerable, 1); } js_newcconstructor(J, jsB_Object, jsB_new_Object, "Object", 1); { /* ES5 */ jsB_propf(J, "Object.getPrototypeOf", O_getPrototypeOf, 1); jsB_propf(J, "Object.getOwnPropertyDescriptor", O_getOwnPropertyDescriptor, 2); jsB_propf(J, "Object.getOwnPropertyNames", O_getOwnPropertyNames, 1); jsB_propf(J, "Object.create", O_create, 2); jsB_propf(J, "Object.defineProperty", O_defineProperty, 3); jsB_propf(J, "Object.defineProperties", O_defineProperties, 2); jsB_propf(J, "Object.seal", O_seal, 1); jsB_propf(J, "Object.freeze", O_freeze, 1); jsB_propf(J, "Object.preventExtensions", O_preventExtensions, 1); jsB_propf(J, "Object.isSealed", O_isSealed, 1); jsB_propf(J, "Object.isFrozen", O_isFrozen, 1); jsB_propf(J, "Object.isExtensible", O_isExtensible, 1); jsB_propf(J, "Object.keys", O_keys, 1); } js_defglobal(J, "Object", JS_DONTENUM); }
void jsB_initarray(js_State *J) { js_pushobject(J, J->Array_prototype); { jsB_propf(J, "toString", Ap_toString, 0); jsB_propf(J, "concat", Ap_concat, 1); jsB_propf(J, "join", Ap_join, 1); jsB_propf(J, "pop", Ap_pop, 0); jsB_propf(J, "push", Ap_push, 1); jsB_propf(J, "reverse", Ap_reverse, 0); jsB_propf(J, "shift", Ap_shift, 0); jsB_propf(J, "slice", Ap_slice, 2); jsB_propf(J, "sort", Ap_sort, 1); jsB_propf(J, "splice", Ap_splice, 2); jsB_propf(J, "unshift", Ap_unshift, 1); /* ES5 */ jsB_propf(J, "indexOf", Ap_indexOf, 1); jsB_propf(J, "lastIndexOf", Ap_lastIndexOf, 1); jsB_propf(J, "every", Ap_every, 1); jsB_propf(J, "some", Ap_some, 1); jsB_propf(J, "forEach", Ap_forEach, 1); jsB_propf(J, "map", Ap_map, 1); jsB_propf(J, "filter", Ap_filter, 1); jsB_propf(J, "reduce", Ap_reduce, 1); jsB_propf(J, "reduceRight", Ap_reduceRight, 1); } js_newcconstructor(J, jsB_new_Array, jsB_new_Array, "Array", 1); { /* ES5 */ jsB_propf(J, "isArray", A_isArray, 1); } js_defglobal(J, "Array", JS_DONTENUM); }
void jsB_initstring(js_State *J) { J->String_prototype->u.s.string = ""; J->String_prototype->u.s.length = 0; js_pushobject(J, J->String_prototype); { jsB_propf(J, "toString", Sp_toString, 0); jsB_propf(J, "valueOf", Sp_valueOf, 0); jsB_propf(J, "charAt", Sp_charAt, 1); jsB_propf(J, "charCodeAt", Sp_charCodeAt, 1); jsB_propf(J, "concat", Sp_concat, 1); jsB_propf(J, "indexOf", Sp_indexOf, 1); jsB_propf(J, "lastIndexOf", Sp_lastIndexOf, 1); jsB_propf(J, "localeCompare", Sp_localeCompare, 1); jsB_propf(J, "match", Sp_match, 1); jsB_propf(J, "replace", Sp_replace, 2); jsB_propf(J, "search", Sp_search, 1); jsB_propf(J, "slice", Sp_slice, 2); jsB_propf(J, "split", Sp_split, 2); jsB_propf(J, "substring", Sp_substring, 2); jsB_propf(J, "toLowerCase", Sp_toLowerCase, 0); jsB_propf(J, "toLocaleLowerCase", Sp_toLowerCase, 0); jsB_propf(J, "toUpperCase", Sp_toUpperCase, 0); jsB_propf(J, "toLocaleUpperCase", Sp_toUpperCase, 0); /* ES5 */ jsB_propf(J, "trim", Sp_trim, 0); } js_newcconstructor(J, jsB_String, jsB_new_String, 1); { jsB_propf(J, "fromCharCode", S_fromCharCode, 1); } js_defglobal(J, "String", JS_DONTENUM); }
void js_newobjectx(js_State *J) { js_Object *prototype = NULL; if (js_isobject(J, -1)) prototype = js_toobject(J, -1); js_pop(J, 1); js_pushobject(J, jsV_newobject(J, JS_COBJECT, prototype)); }
static int js_hasvar(js_State *J, const char *name) { js_Environment *E = J->E; do { js_Property *ref = jsV_getproperty(J, E->variables, name); if (ref) { if (ref->getter) { js_pushobject(J, ref->getter); js_pushobject(J, E->variables); js_call(J, 0); } else { js_pushvalue(J, ref->value); } return 1; } E = E->outer; } while (E); return 0; }
static void O_getPrototypeOf(js_State *J) { js_Object *obj; if (!js_isobject(J, 1)) js_typeerror(J, "not an object"); obj = js_toobject(J, 1); if (obj->prototype) js_pushobject(J, obj->prototype); else js_pushnull(J); }
void jsB_initboolean(js_State *J) { J->Boolean_prototype->u.boolean = 0; js_pushobject(J, J->Boolean_prototype); { jsB_propf(J, "toString", Bp_toString, 0); jsB_propf(J, "valueOf", Bp_valueOf, 0); } js_newcconstructor(J, jsB_Boolean, jsB_new_Boolean, 1); js_defglobal(J, "Boolean", JS_DONTENUM); }
void js_newuserdata(js_State *J, const char *tag, void *data) { js_Object *prototype = NULL; js_Object *obj; if (js_isobject(J, -1)) prototype = js_toobject(J, -1); js_pop(J, 1); obj = jsV_newobject(J, JS_CUSERDATA, prototype); obj->u.user.tag = tag; obj->u.user.data = data; js_pushobject(J, obj); }
/* prototype -- constructor */ void js_newcconstructor(js_State *J, js_CFunction cfun, js_CFunction ccon, unsigned int length) { js_Object *obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype); obj->u.c.function = cfun; obj->u.c.constructor = ccon; js_pushobject(J, obj); /* proto obj */ { js_pushnumber(J, length); js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF); js_rot2(J); /* obj proto */ js_copy(J, -2); /* obj proto obj */ js_defproperty(J, -2, "constructor", JS_DONTENUM); js_defproperty(J, -2, "prototype", JS_READONLY | JS_DONTENUM | JS_DONTCONF); } }
void jsB_initfunction(js_State *J) { J->Function_prototype->u.c.function = jsB_Function_prototype; J->Function_prototype->u.c.constructor = NULL; js_pushobject(J, J->Function_prototype); { jsB_propf(J, "toString", Fp_toString, 2); jsB_propf(J, "apply", Fp_apply, 2); jsB_propf(J, "call", Fp_call, 1); jsB_propf(J, "bind", Fp_bind, 1); } js_newcconstructor(J, jsB_Function, jsB_Function, "Function", 1); js_defglobal(J, "Function", JS_DONTENUM); }
/* obj.valueOf() */ static int jsV_valueOf(js_State *J, js_Object *obj) { js_pushobject(J, obj); js_getproperty(J, -1, "valueOf"); if (js_iscallable(J, -1)) { js_rot2(J); js_call(J, 0); if (js_isprimitive(J, -1)) return 1; js_pop(J, 1); return 0; } js_pop(J, 2); return 0; }
void js_newfunction(js_State *J, js_Function *fun, js_Environment *scope) { js_Object *obj = jsV_newobject(J, JS_CFUNCTION, J->Function_prototype); obj->u.f.function = fun; obj->u.f.scope = scope; js_pushobject(J, obj); { js_pushnumber(J, fun->numparams); js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF); js_newobject(J); { js_copy(J, -2); js_defproperty(J, -2, "constructor", JS_DONTENUM); } js_defproperty(J, -2, "prototype", JS_DONTCONF); } }
void js_newuserdatax(js_State *J, const char *tag, void *data, js_HasProperty has, js_Put put, js_Delete jdelete, js_Finalize finalize) { js_Object *prototype = NULL; js_Object *obj; if (js_isobject(J, -1)) prototype = js_toobject(J, -1); js_pop(J, 1); obj = jsV_newobject(J, JS_CUSERDATA, prototype); obj->u.user.tag = tag; obj->u.user.data = data; obj->u.user.has = has; obj->u.user.put = put; obj->u.user.jdelete = jdelete; obj->u.user.finalize = finalize; js_pushobject(J, obj); }
void js_newcfunction(js_State *J, js_CFunction cfun, const char *name, unsigned int length) { js_Object *obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype); obj->u.c.name = name; obj->u.c.function = cfun; obj->u.c.constructor = NULL; obj->u.c.length = length; js_pushobject(J, obj); { js_pushnumber(J, length); js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF); js_newobject(J); { js_copy(J, -2); js_defproperty(J, -2, "constructor", JS_DONTENUM); } js_defproperty(J, -2, "prototype", JS_DONTCONF); } }
static void jsR_run(js_State *J, js_Function *F) { js_Function **FT = F->funtab; double *NT = F->numtab; const char **ST = F->strtab; js_Instruction *pcstart = F->code; js_Instruction *pc = F->code; enum js_OpCode opcode; int offset; const char *str; js_Object *obj; double x, y; unsigned int ux, uy; int ix, iy, okay; int b; while (1) { if (J->gccounter > JS_GCLIMIT) { J->gccounter = 0; js_gc(J, 0); } opcode = *pc++; switch (opcode) { case OP_POP: js_pop(J, 1); break; case OP_DUP: js_dup(J); break; case OP_DUP2: js_dup2(J); break; case OP_ROT2: js_rot2(J); break; case OP_ROT3: js_rot3(J); break; case OP_ROT4: js_rot4(J); break; case OP_NUMBER_0: js_pushnumber(J, 0); break; case OP_NUMBER_1: js_pushnumber(J, 1); break; case OP_NUMBER_POS: js_pushnumber(J, *pc++); break; case OP_NUMBER_NEG: js_pushnumber(J, -(*pc++)); break; case OP_NUMBER: js_pushnumber(J, NT[*pc++]); break; case OP_STRING: js_pushliteral(J, ST[*pc++]); break; case OP_CLOSURE: js_newfunction(J, FT[*pc++], J->E); break; case OP_NEWOBJECT: js_newobject(J); break; case OP_NEWARRAY: js_newarray(J); break; case OP_NEWREGEXP: js_newregexp(J, ST[pc[0]], pc[1]); pc += 2; break; case OP_UNDEF: js_pushundefined(J); break; case OP_NULL: js_pushnull(J); break; case OP_TRUE: js_pushboolean(J, 1); break; case OP_FALSE: js_pushboolean(J, 0); break; case OP_THIS: js_copy(J, 0); break; case OP_GLOBAL: js_pushobject(J, J->G); break; case OP_CURRENT: js_currentfunction(J); break; case OP_INITLOCAL: STACK[BOT + *pc++] = STACK[--TOP]; break; case OP_GETLOCAL: CHECKSTACK(1); STACK[TOP++] = STACK[BOT + *pc++]; break; case OP_SETLOCAL: STACK[BOT + *pc++] = STACK[TOP-1]; break; case OP_DELLOCAL: ++pc; js_pushboolean(J, 0); break; case OP_INITVAR: js_initvar(J, ST[*pc++], -1); js_pop(J, 1); break; case OP_DEFVAR: js_defvar(J, ST[*pc++]); break; case OP_GETVAR: str = ST[*pc++]; if (!js_hasvar(J, str)) js_referenceerror(J, "'%s' is not defined", str); break; case OP_HASVAR: if (!js_hasvar(J, ST[*pc++])) js_pushundefined(J); break; case OP_SETVAR: js_setvar(J, ST[*pc++]); break; case OP_DELVAR: b = js_delvar(J, ST[*pc++]); js_pushboolean(J, b); break; case OP_IN: str = js_tostring(J, -2); if (!js_isobject(J, -1)) js_typeerror(J, "operand to 'in' is not an object"); b = js_hasproperty(J, -1, str); js_pop(J, 2 + b); js_pushboolean(J, b); break; case OP_INITPROP: obj = js_toobject(J, -3); str = js_tostring(J, -2); jsR_setproperty(J, obj, str, stackidx(J, -1)); js_pop(J, 2); break; case OP_INITGETTER: obj = js_toobject(J, -3); str = js_tostring(J, -2); jsR_defproperty(J, obj, str, 0, NULL, jsR_tofunction(J, -1), NULL); js_pop(J, 2); break; case OP_INITSETTER: obj = js_toobject(J, -3); str = js_tostring(J, -2); jsR_defproperty(J, obj, str, 0, NULL, NULL, jsR_tofunction(J, -1)); js_pop(J, 2); break; case OP_GETPROP: str = js_tostring(J, -1); obj = js_toobject(J, -2); jsR_getproperty(J, obj, str); js_rot3pop2(J); break; case OP_GETPROP_S: str = ST[*pc++]; obj = js_toobject(J, -1); jsR_getproperty(J, obj, str); js_rot2pop1(J); break; case OP_SETPROP: str = js_tostring(J, -2); obj = js_toobject(J, -3); jsR_setproperty(J, obj, str, stackidx(J, -1)); js_rot3pop2(J); break; case OP_SETPROP_S: str = ST[*pc++]; obj = js_toobject(J, -2); jsR_setproperty(J, obj, str, stackidx(J, -1)); js_rot2pop1(J); break; case OP_DELPROP: str = js_tostring(J, -1); obj = js_toobject(J, -2); b = jsR_delproperty(J, obj, str); js_pop(J, 2); js_pushboolean(J, b); break; case OP_DELPROP_S: str = ST[*pc++]; obj = js_toobject(J, -1); b = jsR_delproperty(J, obj, str); js_pop(J, 1); js_pushboolean(J, b); break; case OP_ITERATOR: if (!js_isundefined(J, -1) && !js_isnull(J, -1)) { obj = jsV_newiterator(J, js_toobject(J, -1), 0); js_pop(J, 1); js_pushobject(J, obj); } break; case OP_NEXTITER: obj = js_toobject(J, -1); str = jsV_nextiterator(J, obj); if (str) { js_pushliteral(J, str); js_pushboolean(J, 1); } else { js_pop(J, 1); js_pushboolean(J, 0); } break; /* Function calls */ case OP_EVAL: js_eval(J); break; case OP_CALL: js_call(J, *pc++); break; case OP_NEW: js_construct(J, *pc++); break; /* Unary operators */ case OP_TYPEOF: str = js_typeof(J, -1); js_pop(J, 1); js_pushliteral(J, str); break; case OP_POS: x = js_tonumber(J, -1); js_pop(J, 1); js_pushnumber(J, x); break; case OP_NEG: x = js_tonumber(J, -1); js_pop(J, 1); js_pushnumber(J, -x); break; case OP_BITNOT: ix = js_toint32(J, -1); js_pop(J, 1); js_pushnumber(J, ~ix); break; case OP_LOGNOT: b = js_toboolean(J, -1); js_pop(J, 1); js_pushboolean(J, !b); break; case OP_INC: x = js_tonumber(J, -1); js_pop(J, 1); js_pushnumber(J, x + 1); break; case OP_DEC: x = js_tonumber(J, -1); js_pop(J, 1); js_pushnumber(J, x - 1); break; case OP_POSTINC: x = js_tonumber(J, -1); js_pop(J, 1); js_pushnumber(J, x + 1); js_pushnumber(J, x); break; case OP_POSTDEC: x = js_tonumber(J, -1); js_pop(J, 1); js_pushnumber(J, x - 1); js_pushnumber(J, x); break; /* Multiplicative operators */ case OP_MUL: x = js_tonumber(J, -2); y = js_tonumber(J, -1); js_pop(J, 2); js_pushnumber(J, x * y); break; case OP_DIV: x = js_tonumber(J, -2); y = js_tonumber(J, -1); js_pop(J, 2); js_pushnumber(J, x / y); break; case OP_MOD: x = js_tonumber(J, -2); y = js_tonumber(J, -1); js_pop(J, 2); js_pushnumber(J, fmod(x, y)); break; /* Additive operators */ case OP_ADD: js_concat(J); break; case OP_SUB: x = js_tonumber(J, -2); y = js_tonumber(J, -1); js_pop(J, 2); js_pushnumber(J, x - y); break; /* Shift operators */ case OP_SHL: ix = js_toint32(J, -2); uy = js_touint32(J, -1); js_pop(J, 2); js_pushnumber(J, ix << (uy & 0x1F)); break; case OP_SHR: ix = js_toint32(J, -2); uy = js_touint32(J, -1); js_pop(J, 2); js_pushnumber(J, ix >> (uy & 0x1F)); break; case OP_USHR: ux = js_touint32(J, -2); uy = js_touint32(J, -1); js_pop(J, 2); js_pushnumber(J, ux >> (uy & 0x1F)); break; /* Relational operators */ case OP_LT: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b < 0); break; case OP_GT: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b > 0); break; case OP_LE: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b <= 0); break; case OP_GE: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b >= 0); break; case OP_INSTANCEOF: b = js_instanceof(J); js_pop(J, 2); js_pushboolean(J, b); break; /* Equality */ case OP_EQ: b = js_equal(J); js_pop(J, 2); js_pushboolean(J, b); break; case OP_NE: b = js_equal(J); js_pop(J, 2); js_pushboolean(J, !b); break; case OP_STRICTEQ: b = js_strictequal(J); js_pop(J, 2); js_pushboolean(J, b); break; case OP_STRICTNE: b = js_strictequal(J); js_pop(J, 2); js_pushboolean(J, !b); break; case OP_JCASE: offset = *pc++; b = js_strictequal(J); if (b) { js_pop(J, 2); pc = pcstart + offset; } else { js_pop(J, 1); } break; /* Binary bitwise operators */ case OP_BITAND: ix = js_toint32(J, -2); iy = js_toint32(J, -1); js_pop(J, 2); js_pushnumber(J, ix & iy); break; case OP_BITXOR: ix = js_toint32(J, -2); iy = js_toint32(J, -1); js_pop(J, 2); js_pushnumber(J, ix ^ iy); break; case OP_BITOR: ix = js_toint32(J, -2); iy = js_toint32(J, -1); js_pop(J, 2); js_pushnumber(J, ix | iy); break; /* Try and Catch */ case OP_THROW: js_throw(J); case OP_TRY: offset = *pc++; if (js_trypc(J, pc)) { pc = J->trybuf[J->trytop].pc; } else { pc = pcstart + offset; } break; case OP_ENDTRY: js_endtry(J); break; case OP_CATCH: str = ST[*pc++]; obj = jsV_newobject(J, JS_COBJECT, NULL); js_pushobject(J, obj); js_rot2(J); js_setproperty(J, -2, str); J->E = jsR_newenvironment(J, obj, J->E); js_pop(J, 1); break; case OP_ENDCATCH: J->E = J->E->outer; break; /* With */ case OP_WITH: obj = js_toobject(J, -1); J->E = jsR_newenvironment(J, obj, J->E); js_pop(J, 1); break; case OP_ENDWITH: J->E = J->E->outer; break; /* Branching */ case OP_DEBUGGER: js_trap(J, (int)(pc - pcstart) - 1); break; case OP_JUMP: pc = pcstart + *pc; break; case OP_JTRUE: offset = *pc++; b = js_toboolean(J, -1); js_pop(J, 1); if (b) pc = pcstart + offset; break; case OP_JFALSE: offset = *pc++; b = js_toboolean(J, -1); js_pop(J, 1); if (!b) pc = pcstart + offset; break; case OP_RETURN: return; case OP_LINE: J->trace[J->tracetop].line = *pc++; break; } } }
void js_newstring(js_State *J, const char *v) { js_pushobject(J, jsV_newstring(J, v)); }
void js_pushglobal(js_State *J) { js_pushobject(J, J->G); }
static void jsR_setproperty(js_State *J, js_Object *obj, const char *name, js_Value *value) { js_Property *ref; unsigned int k; int own; if (obj->type == JS_CARRAY) { if (!strcmp(name, "length")) { double rawlen = jsV_tonumber(J, value); unsigned int newlen = jsV_numbertouint32(rawlen); if (newlen != rawlen) js_rangeerror(J, "array length"); jsV_resizearray(J, obj, newlen); return; } if (js_isarrayindex(J, name, &k)) if (k >= obj->u.a.length) obj->u.a.length = k + 1; } if (obj->type == JS_CSTRING) { if (!strcmp(name, "length")) goto readonly; if (js_isarrayindex(J, name, &k)) if (js_runeat(J, obj->u.s.string, k)) goto readonly; } if (obj->type == JS_CREGEXP) { if (!strcmp(name, "source")) goto readonly; if (!strcmp(name, "global")) goto readonly; if (!strcmp(name, "ignoreCase")) goto readonly; if (!strcmp(name, "multiline")) goto readonly; if (!strcmp(name, "lastIndex")) { obj->u.r.last = jsV_tointeger(J, value); return; } } /* First try to find a setter in prototype chain */ ref = jsV_getpropertyx(J, obj, name, &own); if (ref && ref->setter) { js_pushobject(J, ref->setter); js_pushobject(J, obj); js_pushvalue(J, *value); js_call(J, 1); js_pop(J, 1); return; } /* Property not found on this object, so create one */ if (!ref || !own) ref = jsV_setproperty(J, obj, name); if (ref) { if (!(ref->atts & JS_READONLY)) ref->value = *value; else goto readonly; } return; readonly: if (J->strict) js_typeerror(J, "'%s' is read-only", name); }
void js_pushiterator(js_State *J, int idx, int own) { js_pushobject(J, jsV_newiterator(J, js_toobject(J, idx), own)); }