📄 jsobj.c
字号:
return JS_FALSE; return OBJ_DEFINE_PROPERTY(cx, obj, id, JSVAL_VOID, (JSPropertyOp) JSVAL_TO_OBJECT(fval), NULL, JSPROP_ENUMERATE | JSPROP_GETTER | JSPROP_SHARED, NULL);}static JSBoolobj_defineSetter(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ jsval fval, junk; jsid id; uintN attrs; fval = argv[1]; if (JS_TypeOfValue(cx, fval) != JSTYPE_FUNCTION) { JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_GETTER_OR_SETTER, js_setter_str); return JS_FALSE; } if (!JS_ValueToId(cx, argv[0], &id)) return JS_FALSE; if (!js_CheckRedeclaration(cx, obj, id, JSPROP_SETTER, NULL, NULL)) return JS_FALSE; /* * Getters and setters are just like watchpoints from an access * control point of view. */ if (!OBJ_CHECK_ACCESS(cx, obj, id, JSACC_WATCH, &junk, &attrs)) return JS_FALSE; return OBJ_DEFINE_PROPERTY(cx, obj, id, JSVAL_VOID, NULL, (JSPropertyOp) JSVAL_TO_OBJECT(fval), JSPROP_ENUMERATE | JSPROP_SETTER | JSPROP_SHARED, NULL);}static JSBoolobj_lookupGetter(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ jsid id; JSObject *pobj; JSProperty *prop; JSScopeProperty *sprop; if (!JS_ValueToId(cx, argv[0], &id)) return JS_FALSE; if (!OBJ_LOOKUP_PROPERTY(cx, obj, id, &pobj, &prop)) return JS_FALSE; if (prop) { if (OBJ_IS_NATIVE(pobj)) { sprop = (JSScopeProperty *) prop; if (sprop->attrs & JSPROP_GETTER) *rval = OBJECT_TO_JSVAL(sprop->getter); } OBJ_DROP_PROPERTY(cx, pobj, prop); } return JS_TRUE;}static JSBoolobj_lookupSetter(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ jsid id; JSObject *pobj; JSProperty *prop; JSScopeProperty *sprop; if (!JS_ValueToId(cx, argv[0], &id)) return JS_FALSE; if (!OBJ_LOOKUP_PROPERTY(cx, obj, id, &pobj, &prop)) return JS_FALSE; if (prop) { if (OBJ_IS_NATIVE(pobj)) { sprop = (JSScopeProperty *) prop; if (sprop->attrs & JSPROP_SETTER) *rval = OBJECT_TO_JSVAL(sprop->setter); } OBJ_DROP_PROPERTY(cx, pobj, prop); } return JS_TRUE;}#endif /* JS_HAS_GETTER_SETTER */#if JS_HAS_OBJ_WATCHPOINTconst char js_watch_str[] = "watch";const char js_unwatch_str[] = "unwatch";#endifconst char js_hasOwnProperty_str[] = "hasOwnProperty";const char js_isPrototypeOf_str[] = "isPrototypeOf";const char js_propertyIsEnumerable_str[] = "propertyIsEnumerable";#if JS_HAS_GETTER_SETTERconst char js_defineGetter_str[] = "__defineGetter__";const char js_defineSetter_str[] = "__defineSetter__";const char js_lookupGetter_str[] = "__lookupGetter__";const char js_lookupSetter_str[] = "__lookupSetter__";#endifstatic JSFunctionSpec object_methods[] = {#if JS_HAS_TOSOURCE {js_toSource_str, js_obj_toSource, 0, 0, OBJ_TOSTRING_EXTRA},#endif {js_toString_str, js_obj_toString, 0, 0, OBJ_TOSTRING_EXTRA}, {js_toLocaleString_str, js_obj_toLocaleString, 0, 0, OBJ_TOSTRING_EXTRA}, {js_valueOf_str, obj_valueOf, 0,0,0}, {js_eval_str, obj_eval, 1,0,0},#if JS_HAS_OBJ_WATCHPOINT {js_watch_str, obj_watch, 2,0,0}, {js_unwatch_str, obj_unwatch, 1,0,0},#endif {js_hasOwnProperty_str, obj_hasOwnProperty, 1,0,0}, {js_isPrototypeOf_str, obj_isPrototypeOf, 1,0,0}, {js_propertyIsEnumerable_str, obj_propertyIsEnumerable, 1,0,0},#if JS_HAS_GETTER_SETTER {js_defineGetter_str, obj_defineGetter, 2,0,0}, {js_defineSetter_str, obj_defineSetter, 2,0,0}, {js_lookupGetter_str, obj_lookupGetter, 1,0,0}, {js_lookupSetter_str, obj_lookupSetter, 1,0,0},#endif {0,0,0,0,0}};static JSBoolObject(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ if (argc == 0) { /* Trigger logic below to construct a blank object. */ obj = NULL; } else { /* If argv[0] is null or undefined, obj comes back null. */ if (!js_ValueToObject(cx, argv[0], &obj)) return JS_FALSE; } if (!obj) { JS_ASSERT(!argc || JSVAL_IS_NULL(argv[0]) || JSVAL_IS_VOID(argv[0])); if (cx->fp->flags & JSFRAME_CONSTRUCTING) return JS_TRUE; obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL); if (!obj) return JS_FALSE; } *rval = OBJECT_TO_JSVAL(obj); return JS_TRUE;}/* * ObjectOps and Class for with-statement stack objects. */static JSBoolwith_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp, JSProperty **propp){ JSObject *proto = OBJ_GET_PROTO(cx, obj); if (!proto) return js_LookupProperty(cx, obj, id, objp, propp); return OBJ_LOOKUP_PROPERTY(cx, proto, id, objp, propp);}static JSBoolwith_GetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp){ JSObject *proto = OBJ_GET_PROTO(cx, obj); if (!proto) return js_GetProperty(cx, obj, id, vp); return OBJ_GET_PROPERTY(cx, proto, id, vp);}static JSBoolwith_SetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp){ JSObject *proto = OBJ_GET_PROTO(cx, obj); if (!proto) return js_SetProperty(cx, obj, id, vp); return OBJ_SET_PROPERTY(cx, proto, id, vp);}static JSBoolwith_GetAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop, uintN *attrsp){ JSObject *proto = OBJ_GET_PROTO(cx, obj); if (!proto) return js_GetAttributes(cx, obj, id, prop, attrsp); return OBJ_GET_ATTRIBUTES(cx, proto, id, prop, attrsp);}static JSBoolwith_SetAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop, uintN *attrsp){ JSObject *proto = OBJ_GET_PROTO(cx, obj); if (!proto) return js_SetAttributes(cx, obj, id, prop, attrsp); return OBJ_SET_ATTRIBUTES(cx, proto, id, prop, attrsp);}static JSBoolwith_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, jsval *rval){ JSObject *proto = OBJ_GET_PROTO(cx, obj); if (!proto) return js_DeleteProperty(cx, obj, id, rval); return OBJ_DELETE_PROPERTY(cx, proto, id, rval);}static JSBoolwith_DefaultValue(JSContext *cx, JSObject *obj, JSType hint, jsval *vp){ JSObject *proto = OBJ_GET_PROTO(cx, obj); if (!proto) return js_DefaultValue(cx, obj, hint, vp); return OBJ_DEFAULT_VALUE(cx, proto, hint, vp);}static JSBoolwith_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op, jsval *statep, jsid *idp){ JSObject *proto = OBJ_GET_PROTO(cx, obj); if (!proto) return js_Enumerate(cx, obj, enum_op, statep, idp); return OBJ_ENUMERATE(cx, proto, enum_op, statep, idp);}static JSBoolwith_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode, jsval *vp, uintN *attrsp){ JSObject *proto = OBJ_GET_PROTO(cx, obj); if (!proto) return js_CheckAccess(cx, obj, id, mode, vp, attrsp); return OBJ_CHECK_ACCESS(cx, proto, id, mode, vp, attrsp);}static JSObject *with_ThisObject(JSContext *cx, JSObject *obj){ JSObject *proto = OBJ_GET_PROTO(cx, obj); if (!proto) return obj; return OBJ_THIS_OBJECT(cx, proto);}JS_FRIEND_DATA(JSObjectOps) js_WithObjectOps = { js_NewObjectMap, js_DestroyObjectMap, with_LookupProperty, js_DefineProperty, with_GetProperty, with_SetProperty, with_GetAttributes, with_SetAttributes, with_DeleteProperty, with_DefaultValue, with_Enumerate, with_CheckAccess, with_ThisObject, NATIVE_DROP_PROPERTY, NULL, NULL, NULL, NULL, js_SetProtoOrParent, js_SetProtoOrParent, js_Mark, js_Clear, NULL, NULL};static JSObjectOps *with_getObjectOps(JSContext *cx, JSClass *clasp){ return &js_WithObjectOps;}JSClass js_WithClass = { "With", JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_IS_ANONYMOUS, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, with_getObjectOps, 0,0,0,0,0,0,0};JSObject *js_NewWithObject(JSContext *cx, JSObject *proto, JSObject *parent, jsint depth){ JSObject *obj; obj = js_NewObject(cx, &js_WithClass, proto, parent); if (!obj) return NULL; obj->slots[JSSLOT_PRIVATE] = PRIVATE_TO_JSVAL(cx->fp); OBJ_SET_BLOCK_DEPTH(cx, obj, depth); return obj;}JSObject *js_NewBlockObject(JSContext *cx){ JSObject *obj; /* * Null obj's proto slot so that Object.prototype.* does not pollute block * scopes. Make sure obj has its own scope too, since clearing proto does * not affect OBJ_SCOPE(obj). */ obj = js_NewObject(cx, &js_BlockClass, NULL, NULL); if (!obj || !js_GetMutableScope(cx, obj)) return NULL; OBJ_SET_PROTO(cx, obj, NULL); return obj;}JSObject *js_CloneBlockObject(JSContext *cx, JSObject *proto, JSObject *parent, JSStackFrame *fp){ JSObject *clone; clone = js_NewObject(cx, &js_BlockClass, proto, parent); if (!clone) return NULL; clone->slots[JSSLOT_PRIVATE] = PRIVATE_TO_JSVAL(fp); clone->slots[JSSLOT_BLOCK_DEPTH] = OBJ_GET_SLOT(cx, proto, JSSLOT_BLOCK_DEPTH); return clone;}/* * XXXblock this reverses a path in the property tree -- try to share * the prototype's scope harder! */JSBooljs_PutBlockObject(JSContext *cx, JSObject *obj){ JSStackFrame *fp; uintN depth, slot; JSScopeProperty *sprop; fp = (JSStackFrame *) JS_GetPrivate(cx, obj); JS_ASSERT(fp); depth = OBJ_BLOCK_DEPTH(cx, obj); for (sprop = OBJ_SCOPE(obj)->lastProp; sprop; sprop = sprop->parent) { if (sprop->getter != js_BlockClass.getProperty) continue; if (!(sprop->flags & SPROP_HAS_SHORTID)) continue; slot = depth + (uintN)sprop->shortid; JS_ASSERT(slot < fp->script->depth); if (!js_DefineNativeProperty(cx, obj, sprop->id, fp->spbase[slot], NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT, SPROP_HAS_SHORTID, sprop->shortid, NULL)) { JS_SetPrivate(cx, obj, NULL); return JS_FALSE; } } return JS_SetPrivate(cx, obj, NULL);}static JSBoolblock_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){ JSStackFrame *fp; jsint slot; JS_ASSERT(JS_InstanceOf(cx, obj, &js_BlockClass, NULL)); if (!JSVAL_IS_INT(id)) return JS_TRUE; fp = (JSStackFrame *) JS_GetPrivate(cx, obj); if (!fp) return JS_TRUE; slot = OBJ_BLOCK_DEPTH(cx, obj) + (uint16) JSVAL_TO_INT(id); JS_ASSERT((uintN)slot < fp->script->depth); *vp = fp->spbase[slot]; return JS_TRUE;}static JSBoolblock_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){ JSStackFrame *fp; jsint slot; JS_ASSERT(JS_InstanceOf(cx, obj, &js_BlockClass, NULL)); if (!JSVAL_IS_INT(id)) return JS_TRUE; fp = (JSStackFrame *) JS_GetPrivate(cx, obj); if (!fp) return JS_TRUE; slot = OBJ_BLOCK_DEPTH(cx, obj) + (uint16) JSVAL_TO_INT(id); JS_ASSERT((uintN)slot < fp->script->depth); fp->spbase[slot] = *vp; return JS_TRUE;}#if JS_HAS_XDR#define NO_PARENT_INDEX (jsatomid)-1jsatomidFindObjectAtomIndex(JSAtomMap *map, JSObject *obj){ size_t i; JSAtom *atom; for (i = 0; i < map->length; i++) { atom = map->vector[i]; if (ATOM_KEY(atom) == OBJECT_TO_JSVAL(obj)) return i; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -