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_RecordTupleShared_h
8 #define vm_RecordTupleShared_h
9 
10 #include "vm/RecordTupleShared.h"
11 
12 #include "NamespaceImports.h"
13 #include "builtin/RecordObject.h"
14 #include "builtin/TupleObject.h"
15 #include "gc/Rooting.h"
16 #include "js/Value.h"
17 #include "vm/GlobalObject.h"
18 #include "vm/JSObject.h"
19 #include "vm/NativeObject.h"
20 
21 #include "gc/Marking-inl.h"
22 
23 namespace js {
24 
IsExtendedPrimitive(const JSObject & obj)25 bool IsExtendedPrimitive(const JSObject& obj) {
26   return obj.is<RecordType>() || obj.is<TupleType>();
27 }
28 
MaybeForwardedIsExtendedPrimitive(const JSObject & obj)29 bool gc::MaybeForwardedIsExtendedPrimitive(const JSObject& obj) {
30   return MaybeForwardedObjectIs<RecordType>(&obj) ||
31          MaybeForwardedObjectIs<TupleType>(&obj);
32 }
33 
IsExtendedPrimitiveWrapper(const JSObject & obj)34 bool IsExtendedPrimitiveWrapper(const JSObject& obj) {
35   return obj.is<RecordObject>() || obj.is<TupleObject>();
36 }
37 
ExtendedPrimitiveGetProperty(JSContext * cx,HandleObject obj,HandleValue receiver,HandleId id,MutableHandleValue vp)38 bool ExtendedPrimitiveGetProperty(JSContext* cx, HandleObject obj,
39                                   HandleValue receiver, HandleId id,
40                                   MutableHandleValue vp) {
41   MOZ_ASSERT(IsExtendedPrimitive(*obj));
42 
43   if (obj->is<RecordType>()) {
44     if (obj->as<RecordType>().getOwnProperty(cx, id, vp)) {
45       return true;
46     }
47     // If records will not have a null prototype, this should use a mehanism
48     // similar to tuples.
49     vp.set(JS::UndefinedValue());
50     return true;
51   }
52 
53   MOZ_ASSERT(obj->is<TupleType>());
54   if (obj->as<TupleType>().getOwnProperty(id, vp)) {
55     return true;
56   }
57 
58   JSObject* proto = GlobalObject::getOrCreateTuplePrototype(cx, cx->global());
59   if (!proto) {
60     return false;
61   }
62 
63   RootedNativeObject rootedProto(cx, &proto->as<NativeObject>());
64   return NativeGetProperty(cx, rootedProto, receiver, id, vp);
65 }
66 
67 }  // namespace js
68 
69 #endif
70