bool DirectProxyHandler::defineProperty(JSContext *cx, HandleObject proxy, HandleId id, MutableHandle<PropertyDescriptor> desc) const { assertEnteredPolicy(cx, proxy, id, SET); RootedObject target(cx, proxy->as<ProxyObject>().target()); RootedValue v(cx, desc.value()); return CheckDefineProperty(cx, target, id, v, desc.attributes(), desc.getter(), desc.setter()) && JS_DefinePropertyById(cx, target, id, v, desc.attributes(), desc.getter(), desc.setter()); }
bool InterposeProperty(JSContext* cx, HandleObject target, const nsIID* iid, HandleId id, MutableHandle<JSPropertyDescriptor> descriptor) { // We only want to do interpostion on DOM instances and // wrapped natives. RootedObject unwrapped(cx, UncheckedUnwrap(target)); const js::Class* clasp = js::GetObjectClass(unwrapped); if (!mozilla::dom::IsDOMClass(clasp) && !IS_WN_CLASS(clasp) && !IS_PROTO_CLASS(clasp) && clasp != &OuterWindowProxyClass) { return true; } XPCWrappedNativeScope* scope = ObjectScope(CurrentGlobalOrNull(cx)); MOZ_ASSERT(scope->HasInterposition()); nsCOMPtr<nsIAddonInterposition> interp = scope->GetInterposition(); JSAddonId* addonId = AddonIdOfObject(target); RootedValue addonIdValue(cx, StringValue(StringOfAddonId(addonId))); RootedValue prop(cx, IdToValue(id)); RootedValue targetValue(cx, ObjectValue(*target)); RootedValue descriptorVal(cx); nsresult rv = interp->InterposeProperty(addonIdValue, targetValue, iid, prop, &descriptorVal); if (NS_FAILED(rv)) { xpc::Throw(cx, rv); return false; } if (!descriptorVal.isObject()) return true; // We need to be careful parsing descriptorVal. |cx| is in the compartment // of the add-on and the descriptor is in the compartment of the // interposition. We could wrap the descriptor in the add-on's compartment // and then parse it. However, parsing the descriptor fetches properties // from it, and we would try to interpose on those property accesses. So // instead we parse in the interposition's compartment and then wrap the // descriptor. { JSAutoCompartment ac(cx, &descriptorVal.toObject()); if (!JS::ObjectToCompletePropertyDescriptor(cx, target, descriptorVal, descriptor)) return false; } // Always make the property non-configurable regardless of what the // interposition wants. descriptor.setAttributes(descriptor.attributes() | JSPROP_PERMANENT); if (!JS_WrapPropertyDescriptor(cx, descriptor)) return false; return true; }
bool js::SetPropertyIgnoringNamedGetter(JSContext* cx, const BaseProxyHandler* handler, HandleObject proxy, HandleObject receiver, HandleId id, MutableHandle<PropertyDescriptor> desc, bool descIsOwn, bool strict, MutableHandleValue vp) { /* The control-flow here differs from ::get() because of the fall-through case below. */ MOZ_ASSERT_IF(descIsOwn, desc.object()); if (desc.object()) { MOZ_ASSERT(desc.getter() != JS_PropertyStub); MOZ_ASSERT(desc.setter() != JS_StrictPropertyStub); // Check for read-only properties. if (desc.isReadonly()) { if (strict) return Throw(cx, id, descIsOwn ? JSMSG_READ_ONLY : JSMSG_CANT_REDEFINE_PROP); return true; } if (desc.hasSetterObject() || desc.setter()) { if (!CallSetter(cx, receiver, id, desc.setter(), desc.attributes(), strict, vp)) return false; if (!proxy->is<ProxyObject>() || proxy->as<ProxyObject>().handler() != handler) return true; if (desc.isShared()) return true; } desc.value().set(vp.get()); if (descIsOwn) { MOZ_ASSERT(desc.object() == proxy); return handler->defineProperty(cx, proxy, id, desc); } return DefineProperty(cx, receiver, id, desc.value(), desc.getter(), desc.setter(), desc.attributes()); } desc.object().set(receiver); desc.value().set(vp.get()); desc.setAttributes(JSPROP_ENUMERATE); desc.setGetter(nullptr); desc.setSetter(nullptr); // Pick up the class getter/setter. return DefineProperty(cx, receiver, id, desc.value(), nullptr, nullptr, JSPROP_ENUMERATE); }
bool js::SetPropertyIgnoringNamedGetter(JSContext *cx, const BaseProxyHandler *handler, HandleObject proxy, HandleObject receiver, HandleId id, MutableHandle<PropertyDescriptor> desc, bool descIsOwn, bool strict, MutableHandleValue vp) { /* The control-flow here differs from ::get() because of the fall-through case below. */ if (descIsOwn) { MOZ_ASSERT(desc.object()); // Check for read-only properties. if (desc.isReadonly()) return strict ? Throw(cx, id, JSMSG_READ_ONLY) : true; if (!desc.setter()) { // Be wary of the odd explicit undefined setter case possible through // Object.defineProperty. if (!desc.hasSetterObject()) desc.setSetter(JS_StrictPropertyStub); } else if (desc.hasSetterObject() || desc.setter() != JS_StrictPropertyStub) { if (!CallSetter(cx, receiver, id, desc.setter(), desc.attributes(), strict, vp)) return false; if (!proxy->is<ProxyObject>() || proxy->as<ProxyObject>().handler() != handler) return true; if (desc.isShared()) return true; } if (!desc.getter()) { // Same as above for the null setter case. if (!desc.hasGetterObject()) desc.setGetter(JS_PropertyStub); } desc.value().set(vp.get()); return handler->defineProperty(cx, receiver, id, desc); } if (desc.object()) { // Check for read-only properties. if (desc.isReadonly()) return strict ? Throw(cx, id, JSMSG_CANT_REDEFINE_PROP) : true; if (!desc.setter()) { // Be wary of the odd explicit undefined setter case possible through // Object.defineProperty. if (!desc.hasSetterObject()) desc.setSetter(JS_StrictPropertyStub); } else if (desc.hasSetterObject() || desc.setter() != JS_StrictPropertyStub) { if (!CallSetter(cx, receiver, id, desc.setter(), desc.attributes(), strict, vp)) return false; if (!proxy->is<ProxyObject>() || proxy->as<ProxyObject>().handler() != handler) return true; if (desc.isShared()) return true; } if (!desc.getter()) { // Same as above for the null setter case. if (!desc.hasGetterObject()) desc.setGetter(JS_PropertyStub); } desc.value().set(vp.get()); return JSObject::defineGeneric(cx, receiver, id, desc.value(), desc.getter(), desc.setter(), desc.attributes()); } desc.object().set(receiver); desc.value().set(vp.get()); desc.setAttributes(JSPROP_ENUMERATE); desc.setGetter(nullptr); desc.setSetter(nullptr); // Pick up the class getter/setter. return JSObject::defineGeneric(cx, receiver, id, desc.value(), nullptr, nullptr, JSPROP_ENUMERATE); }