tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PlainObject-inl.h (3379B)


      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 #ifndef vm_PlainObject_inl_h
      8 #define vm_PlainObject_inl_h
      9 
     10 #include "vm/PlainObject.h"
     11 
     12 #include "mozilla/Assertions.h"  // MOZ_ASSERT, MOZ_ASSERT_IF
     13 #include "mozilla/Attributes.h"  // MOZ_ALWAYS_INLINE
     14 
     15 #include "gc/AllocKind.h"     // js::gc::Heap
     16 #include "js/RootingAPI.h"    // JS::Handle, JS::Rooted, JS::MutableHandle
     17 #include "js/Value.h"         // JS::Value, JS_IS_CONSTRUCTING
     18 #include "vm/JSFunction.h"    // JSFunction
     19 #include "vm/JSObject.h"      // js::GenericObject, js::NewObjectKind
     20 #include "vm/NativeObject.h"  // js::NativeObject::create
     21 #include "vm/Shape.h"         // js::Shape
     22 
     23 #include "gc/ObjectKind-inl.h"  // js::gc::GetGCObjectKind
     24 #include "vm/JSObject-inl.h"  // js::GetInitialHeap, js::NewBuiltinClassInstance
     25 #include "vm/NativeObject-inl.h"  // js::NativeObject::{create,setLastProperty}
     26 
     27 /* static */ inline js::PlainObject* js::PlainObject::createWithShape(
     28    JSContext* cx, JS::Handle<SharedShape*> shape, gc::AllocKind kind,
     29    NewObjectKind newKind) {
     30  MOZ_ASSERT(shape->getObjectClass() == &PlainObject::class_);
     31  gc::Heap heap = GetInitialHeap(newKind, &PlainObject::class_);
     32 
     33  MOZ_ASSERT(!IsFinalizedKind(kind));
     34  MOZ_ASSERT(gc::GetObjectFinalizeKind(&PlainObject::class_) ==
     35             gc::FinalizeKind::None);
     36  return NativeObject::create<PlainObject>(cx, kind, heap, shape);
     37 }
     38 
     39 /* static */ inline js::PlainObject* js::PlainObject::createWithShape(
     40    JSContext* cx, JS::Handle<SharedShape*> shape, NewObjectKind newKind) {
     41  gc::AllocKind kind = gc::GetGCObjectKind(shape->numFixedSlots());
     42  return createWithShape(cx, shape, kind, newKind);
     43 }
     44 
     45 /* static */ inline js::PlainObject* js::PlainObject::createWithTemplate(
     46    JSContext* cx, JS::Handle<PlainObject*> templateObject) {
     47  JS::Rooted<SharedShape*> shape(cx, templateObject->sharedShape());
     48  return createWithShape(cx, shape);
     49 }
     50 
     51 inline js::gc::AllocKind js::PlainObject::allocKindForTenure() const {
     52  gc::AllocKind kind = gc::GetGCObjectFixedSlotsKind(numFixedSlots());
     53  MOZ_ASSERT(!IsFinalizedKind(kind));
     54  MOZ_ASSERT(gc::GetObjectFinalizeKind(&PlainObject::class_) ==
     55             gc::FinalizeKind::None);
     56  return kind;
     57 }
     58 
     59 namespace js {
     60 
     61 static MOZ_ALWAYS_INLINE bool CreateThis(JSContext* cx,
     62                                         JS::Handle<JSFunction*> callee,
     63                                         JS::Handle<JSObject*> newTarget,
     64                                         NewObjectKind newKind,
     65                                         JS::MutableHandle<JS::Value> thisv) {
     66  if (callee->constructorNeedsUninitializedThis()) {
     67    thisv.setMagic(JS_UNINITIALIZED_LEXICAL);
     68    return true;
     69  }
     70 
     71  MOZ_ASSERT(thisv.isMagic(JS_IS_CONSTRUCTING));
     72 
     73  Rooted<SharedShape*> shape(cx, ThisShapeForFunction(cx, callee, newTarget));
     74  if (!shape) {
     75    return false;
     76  }
     77 
     78  PlainObject* obj = PlainObject::createWithShape(cx, shape, newKind);
     79  if (!obj) {
     80    return false;
     81  }
     82 
     83  MOZ_ASSERT(obj->nonCCWRealm() == callee->realm());
     84  thisv.setObject(*obj);
     85  return true;
     86 }
     87 
     88 }  // namespace js
     89 
     90 #endif  // vm_PlainObject_inl_h