| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* Fundamental operations on objects. */ |
| 8 |
|
| 9 |
#ifndef vm_ObjectOperations_inl_h |
| 10 |
#define vm_ObjectOperations_inl_h |
| 11 |
|
| 12 |
#include "vm/ObjectOperations.h" |
| 13 |
|
| 14 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 15 |
#include "mozilla/Attributes.h" // MOZ_ALWAYS_INLINE |
| 16 |
#include "mozilla/Likely.h" // MOZ_UNLIKELY |
| 17 |
|
| 18 |
#include <stdint.h> // uint32_t |
| 19 |
|
| 20 |
#include "js/Class.h" // js::{Delete,Get,Has}PropertyOp, JSMayResolveOp, JS::ObjectOpResult |
| 21 |
#include "js/GCAPI.h" // JS::AutoSuppressGCAnalysis |
| 22 |
#include "js/Id.h" // JS::PropertyKey, jsid |
| 23 |
#include "js/RootingAPI.h" // JS::Handle, JS::MutableHandle, JS::Rooted |
| 24 |
#include "js/Value.h" // JS::ObjectValue, JS::Value |
| 25 |
#include "proxy/Proxy.h" // js::Proxy |
| 26 |
#include "vm/JSContext.h" // JSContext |
| 27 |
#include "vm/JSObject.h" // JSObject |
| 28 |
#include "vm/NativeObject.h" // js::NativeObject, js::Native{Get,Has,Set}Property, js::NativeGetPropertyNoGC, js::Qualified |
| 29 |
#include "vm/ProxyObject.h" // js::ProxyObject |
| 30 |
#include "vm/StringType.h" // js::NameToId |
| 31 |
#include "vm/SymbolType.h" // JS::Symbol |
| 32 |
|
| 33 |
#include "vm/JSAtomUtils-inl.h" // js::PrimitiveValueToId, js::IndexToId |
| 34 |
|
| 35 |
namespace js { |
| 36 |
|
| 37 |
// The functions below are the fundamental operations on objects. See the |
| 38 |
// comment about "Standard internal methods" in jsapi.h. |
| 39 |
|
| 40 |
/* |
| 41 |
* ES6 [[GetPrototypeOf]]. Get obj's prototype, storing it in protop. |
| 42 |
* |
| 43 |
* If obj is definitely not a proxy, the infallible obj->getProto() can be used |
| 44 |
* instead. See the comment on JSObject::getTaggedProto(). |
| 45 |
*/ |
| 46 |
inline bool GetPrototype(JSContext* cx, JS::Handle<JSObject*> obj, |
| 47 |
JS::MutableHandle<JSObject*> protop) { |
| 48 |
if (obj->hasDynamicPrototype()) { |
| 49 |
MOZ_ASSERT(obj->is<ProxyObject>()); |
| 50 |
return Proxy::getPrototype(cx, obj, protop); |
| 51 |
} |
| 52 |
|
| 53 |
protop.set(obj->staticPrototype()); |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
/* |
| 58 |
* ES6 [[IsExtensible]]. Extensible objects can have new properties defined on |
| 59 |
* them. Inextensible objects can't, and their [[Prototype]] slot is fixed as |
| 60 |
* well. |
| 61 |
*/ |
| 62 |
inline bool IsExtensible(JSContext* cx, JS::Handle<JSObject*> obj, |
| 63 |
bool* extensible) { |
| 64 |
if (obj->is<ProxyObject>()) { |
| 65 |
return Proxy::isExtensible(cx, obj, extensible); |
| 66 |
} |
| 67 |
|
| 68 |
*extensible = obj->nonProxyIsExtensible(); |
| 69 |
|
| 70 |
// If the following assertion fails, there's somewhere else a missing |
| 71 |
// call to shrinkCapacityToInitializedLength() which needs to be found and |
| 72 |
// fixed. |
| 73 |
MOZ_ASSERT_IF(obj->is<NativeObject>() && !*extensible, |
| 74 |
obj->as<NativeObject>().getDenseInitializedLength() == |
| 75 |
obj->as<NativeObject>().getDenseCapacity()); |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
/* |
| 80 |
* ES6 [[Has]]. Set *foundp to true if `id in obj` (that is, if obj has an own |
| 81 |
* or inherited property obj[id]), false otherwise. |
| 82 |
*/ |
| 83 |
inline bool HasProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 84 |
JS::Handle<jsid> id, bool* foundp) { |
| 85 |
if (HasPropertyOp op = obj->getOpsHasProperty()) { |
| 86 |
return op(cx, obj, id, foundp); |
| 87 |
} |
| 88 |
|
| 89 |
return NativeHasProperty(cx, obj.as<NativeObject>(), id, foundp); |
| 90 |
} |
| 91 |
|
| 92 |
inline bool HasProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 93 |
PropertyName* name, bool* foundp) { |
| 94 |
JS::Rooted<jsid> id(cx, NameToId(name)); |
| 95 |
return HasProperty(cx, obj, id, foundp); |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* ES6 [[Get]]. Get the value of the property `obj[id]`, or undefined if no |
| 100 |
* such property exists. |
| 101 |
* |
| 102 |
* Typically obj == receiver; if obj != receiver then the caller is most likely |
| 103 |
* a proxy using GetProperty to finish a property get that started out as |
| 104 |
* `receiver[id]`, and we've already searched the prototype chain up to `obj`. |
| 105 |
*/ |
| 106 |
inline bool GetProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 107 |
JS::Handle<JS::Value> receiver, JS::Handle<jsid> id, |
| 108 |
JS::MutableHandle<JS::Value> vp) { |
| 109 |
if (GetPropertyOp op = obj->getOpsGetProperty()) { |
| 110 |
return op(cx, obj, receiver, id, vp); |
| 111 |
} |
| 112 |
|
| 113 |
return NativeGetProperty(cx, obj.as<NativeObject>(), receiver, id, vp); |
| 114 |
} |
| 115 |
|
| 116 |
inline bool GetProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 117 |
JS::Handle<JS::Value> receiver, PropertyName* name, |
| 118 |
JS::MutableHandle<JS::Value> vp) { |
| 119 |
JS::Rooted<jsid> id(cx, NameToId(name)); |
| 120 |
return GetProperty(cx, obj, receiver, id, vp); |
| 121 |
} |
| 122 |
|
| 123 |
inline bool GetProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 124 |
JS::Handle<JSObject*> receiver, JS::Handle<jsid> id, |
| 125 |
JS::MutableHandle<JS::Value> vp) { |
| 126 |
JS::Rooted<JS::Value> receiverValue(cx, JS::ObjectValue(*receiver)); |
| 127 |
return GetProperty(cx, obj, receiverValue, id, vp); |
| 128 |
} |
| 129 |
|
| 130 |
inline bool GetProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 131 |
JS::Handle<JSObject*> receiver, PropertyName* name, |
| 132 |
JS::MutableHandle<JS::Value> vp) { |
| 133 |
JS::Rooted<JS::Value> receiverValue(cx, JS::ObjectValue(*receiver)); |
| 134 |
return GetProperty(cx, obj, receiverValue, name, vp); |
| 135 |
} |
| 136 |
|
| 137 |
inline bool GetElement(JSContext* cx, JS::Handle<JSObject*> obj, |
| 138 |
JS::Handle<JS::Value> receiver, uint32_t index, |
| 139 |
JS::MutableHandle<JS::Value> vp) { |
| 140 |
JS::Rooted<jsid> id(cx); |
| 141 |
if (!IndexToId(cx, index, &id)) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
return GetProperty(cx, obj, receiver, id, vp); |
| 146 |
} |
| 147 |
|
| 148 |
inline bool GetElement(JSContext* cx, JS::Handle<JSObject*> obj, |
| 149 |
JS::Handle<JSObject*> receiver, uint32_t index, |
| 150 |
JS::MutableHandle<JS::Value> vp) { |
| 151 |
JS::Rooted<JS::Value> receiverValue(cx, JS::ObjectValue(*receiver)); |
| 152 |
return GetElement(cx, obj, receiverValue, index, vp); |
| 153 |
} |
| 154 |
|
| 155 |
inline bool GetElementLargeIndex(JSContext* cx, JS::Handle<JSObject*> obj, |
| 156 |
JS::Handle<JSObject*> receiver, uint64_t index, |
| 157 |
JS::MutableHandle<JS::Value> vp) { |
| 158 |
MOZ_ASSERT(index < uint64_t(DOUBLE_INTEGRAL_PRECISION_LIMIT)); |
| 159 |
|
| 160 |
if (MOZ_LIKELY(index <= UINT32_MAX)) { |
| 161 |
return GetElement(cx, obj, receiver, uint32_t(index), vp); |
| 162 |
} |
| 163 |
|
| 164 |
RootedValue tmp(cx, DoubleValue(index)); |
| 165 |
RootedId id(cx); |
| 166 |
if (!PrimitiveValueToId<CanGC>(cx, tmp, &id)) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
return GetProperty(cx, obj, obj, id, vp); |
| 171 |
} |
| 172 |
|
| 173 |
inline bool GetPropertyNoGC(JSContext* cx, JSObject* obj, |
| 174 |
const JS::Value& receiver, jsid id, JS::Value* vp) { |
| 175 |
if (obj->getOpsGetProperty()) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
return NativeGetPropertyNoGC(cx, &obj->as<NativeObject>(), receiver, id, vp); |
| 180 |
} |
| 181 |
|
| 182 |
inline bool GetPropertyNoGC(JSContext* cx, JSObject* obj, |
| 183 |
const JS::Value& receiver, PropertyName* name, |
| 184 |
JS::Value* vp) { |
| 185 |
return GetPropertyNoGC(cx, obj, receiver, NameToId(name), vp); |
| 186 |
} |
| 187 |
|
| 188 |
inline bool GetElementNoGC(JSContext* cx, JSObject* obj, |
| 189 |
const JS::Value& receiver, uint32_t index, |
| 190 |
JS::Value* vp) { |
| 191 |
if (obj->getOpsGetProperty()) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
if (index > PropertyKey::IntMax) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
return GetPropertyNoGC(cx, obj, receiver, PropertyKey::Int(index), vp); |
| 200 |
} |
| 201 |
|
| 202 |
static MOZ_ALWAYS_INLINE bool ClassMayResolveId(const JSAtomState& names, |
| 203 |
const JSClass* clasp, jsid id, |
| 204 |
JSObject* maybeObj) { |
| 205 |
MOZ_ASSERT_IF(maybeObj, maybeObj->getClass() == clasp); |
| 206 |
|
| 207 |
if (!clasp->getResolve()) { |
| 208 |
// Sanity check: we should only have a mayResolve hook if we have a |
| 209 |
// resolve hook. |
| 210 |
MOZ_ASSERT(!clasp->getMayResolve(), |
| 211 |
"Class with mayResolve hook but no resolve hook"); |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
if (JSMayResolveOp mayResolve = clasp->getMayResolve()) { |
| 216 |
// Tell the analysis our mayResolve hooks won't trigger GC. |
| 217 |
JS::AutoSuppressGCAnalysis nogc; |
| 218 |
if (!mayResolve(names, id, maybeObj)) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
// Returns whether |obj| or an object on its proto chain may have an interesting |
| 227 |
// symbol property (see JSObject::hasInterestingSymbolProperty). If it returns |
| 228 |
// true, *holder is set to the object that may have this property. |
| 229 |
MOZ_ALWAYS_INLINE bool MaybeHasInterestingSymbolProperty( |
| 230 |
JSContext* cx, JSObject* obj, JS::Symbol* symbol, |
| 231 |
JSObject** holder /* = nullptr */) { |
| 232 |
MOZ_ASSERT(symbol->isInterestingSymbol()); |
| 233 |
|
| 234 |
jsid id = PropertyKey::Symbol(symbol); |
| 235 |
do { |
| 236 |
if (obj->maybeHasInterestingSymbolProperty() || |
| 237 |
MOZ_UNLIKELY( |
| 238 |
ClassMayResolveId(cx->names(), obj->getClass(), id, obj))) { |
| 239 |
if (holder) { |
| 240 |
*holder = obj; |
| 241 |
} |
| 242 |
return true; |
| 243 |
} |
| 244 |
obj = obj->staticPrototype(); |
| 245 |
} while (obj); |
| 246 |
|
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
// Like GetProperty but optimized for interesting symbol properties like |
| 251 |
// @@toStringTag. |
| 252 |
MOZ_ALWAYS_INLINE bool GetInterestingSymbolProperty( |
| 253 |
JSContext* cx, JS::Handle<JSObject*> obj, JS::Symbol* sym, |
| 254 |
JS::MutableHandle<JS::Value> vp) { |
| 255 |
JSObject* holder; |
| 256 |
if (!MaybeHasInterestingSymbolProperty(cx, obj, sym, &holder)) { |
| 257 |
#ifdef DEBUG |
| 258 |
JS::Rooted<JS::Value> receiver(cx, JS::ObjectValue(*obj)); |
| 259 |
JS::Rooted<jsid> id(cx, PropertyKey::Symbol(sym)); |
| 260 |
if (!GetProperty(cx, obj, receiver, id, vp)) { |
| 261 |
return false; |
| 262 |
} |
| 263 |
MOZ_ASSERT(vp.isUndefined()); |
| 264 |
#endif |
| 265 |
|
| 266 |
vp.setUndefined(); |
| 267 |
return true; |
| 268 |
} |
| 269 |
|
| 270 |
JS::Rooted<JSObject*> holderRoot(cx, holder); |
| 271 |
JS::Rooted<JS::Value> receiver(cx, JS::ObjectValue(*obj)); |
| 272 |
JS::Rooted<jsid> id(cx, PropertyKey::Symbol(sym)); |
| 273 |
return GetProperty(cx, holderRoot, receiver, id, vp); |
| 274 |
} |
| 275 |
|
| 276 |
/* |
| 277 |
* ES6 [[Set]]. Carry out the assignment `obj[id] = v`. |
| 278 |
* |
| 279 |
* The `receiver` argument has to do with how [[Set]] interacts with the |
| 280 |
* prototype chain and proxies. It's hard to explain and ES6 doesn't really |
| 281 |
* try. Long story short, if you just want bog-standard assignment, pass |
| 282 |
* `ObjectValue(*obj)` as receiver. Or better, use one of the signatures that |
| 283 |
* doesn't have a receiver parameter. |
| 284 |
* |
| 285 |
* Callers pass obj != receiver e.g. when a proxy is involved, obj is the |
| 286 |
* proxy's target, and the proxy is using SetProperty to finish an assignment |
| 287 |
* that started out as `receiver[id] = v`, by delegating it to obj. |
| 288 |
*/ |
| 289 |
inline bool SetProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 290 |
JS::Handle<jsid> id, JS::Handle<JS::Value> v, |
| 291 |
JS::Handle<JS::Value> receiver, |
| 292 |
JS::ObjectOpResult& result) { |
| 293 |
if (obj->getOpsSetProperty()) { |
| 294 |
return JSObject::nonNativeSetProperty(cx, obj, id, v, receiver, result); |
| 295 |
} |
| 296 |
|
| 297 |
return NativeSetProperty<Qualified>(cx, obj.as<NativeObject>(), id, v, |
| 298 |
receiver, result); |
| 299 |
} |
| 300 |
|
| 301 |
inline bool SetProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 302 |
JS::Handle<jsid> id, JS::Handle<JS::Value> v) { |
| 303 |
JS::Rooted<JS::Value> receiver(cx, JS::ObjectValue(*obj)); |
| 304 |
JS::ObjectOpResult result; |
| 305 |
return SetProperty(cx, obj, id, v, receiver, result) && |
| 306 |
result.checkStrict(cx, obj, id); |
| 307 |
} |
| 308 |
|
| 309 |
inline bool SetProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 310 |
PropertyName* name, JS::Handle<JS::Value> v, |
| 311 |
JS::Handle<JS::Value> receiver, |
| 312 |
JS::ObjectOpResult& result) { |
| 313 |
JS::Rooted<jsid> id(cx, NameToId(name)); |
| 314 |
return SetProperty(cx, obj, id, v, receiver, result); |
| 315 |
} |
| 316 |
|
| 317 |
inline bool SetProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 318 |
PropertyName* name, JS::Handle<JS::Value> v) { |
| 319 |
JS::Rooted<jsid> id(cx, NameToId(name)); |
| 320 |
JS::Rooted<JS::Value> receiver(cx, JS::ObjectValue(*obj)); |
| 321 |
JS::ObjectOpResult result; |
| 322 |
return SetProperty(cx, obj, id, v, receiver, result) && |
| 323 |
result.checkStrict(cx, obj, id); |
| 324 |
} |
| 325 |
|
| 326 |
inline bool SetElement(JSContext* cx, JS::Handle<JSObject*> obj, uint32_t index, |
| 327 |
JS::Handle<JS::Value> v, JS::Handle<JS::Value> receiver, |
| 328 |
JS::ObjectOpResult& result) { |
| 329 |
if (obj->getOpsSetProperty()) { |
| 330 |
return JSObject::nonNativeSetElement(cx, obj, index, v, receiver, result); |
| 331 |
} |
| 332 |
|
| 333 |
return NativeSetElement(cx, obj.as<NativeObject>(), index, v, receiver, |
| 334 |
result); |
| 335 |
} |
| 336 |
|
| 337 |
/* |
| 338 |
* ES6 draft rev 31 (15 Jan 2015) 7.3.3 Put (O, P, V, Throw), except that on |
| 339 |
* success, the spec says this is supposed to return a boolean value, which we |
| 340 |
* don't bother doing. |
| 341 |
*/ |
| 342 |
inline bool PutProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 343 |
JS::Handle<jsid> id, JS::Handle<JS::Value> v, |
| 344 |
bool strict) { |
| 345 |
JS::Rooted<JS::Value> receiver(cx, JS::ObjectValue(*obj)); |
| 346 |
JS::ObjectOpResult result; |
| 347 |
return SetProperty(cx, obj, id, v, receiver, result) && |
| 348 |
result.checkStrictModeError(cx, obj, id, strict); |
| 349 |
} |
| 350 |
|
| 351 |
/* |
| 352 |
* ES6 [[Delete]]. Equivalent to the JS code `delete obj[id]`. |
| 353 |
*/ |
| 354 |
inline bool DeleteProperty(JSContext* cx, JS::Handle<JSObject*> obj, |
| 355 |
JS::Handle<jsid> id, JS::ObjectOpResult& result) { |
| 356 |
if (DeletePropertyOp op = obj->getOpsDeleteProperty()) { |
| 357 |
return op(cx, obj, id, result); |
| 358 |
} |
| 359 |
|
| 360 |
return NativeDeleteProperty(cx, obj.as<NativeObject>(), id, result); |
| 361 |
} |
| 362 |
|
| 363 |
inline bool DeleteElement(JSContext* cx, JS::Handle<JSObject*> obj, |
| 364 |
uint32_t index, JS::ObjectOpResult& result) { |
| 365 |
JS::Rooted<jsid> id(cx); |
| 366 |
if (!IndexToId(cx, index, &id)) { |
| 367 |
return false; |
| 368 |
} |
| 369 |
|
| 370 |
return DeleteProperty(cx, obj, id, result); |
| 371 |
} |
| 372 |
|
| 373 |
} /* namespace js */ |
| 374 |
|
| 375 |
#endif /* vm_ObjectOperations_inl_h */ |