summaryrefslogtreecommitdiffstats
path: root/js/src/vm/RecordType.h
blob: 6295ca29e33ba6e338604df407f41c175f146f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef vm_RecordType_h
#define vm_RecordType_h

#include <cstdint>
#include <functional>
#include "js/TypeDecls.h"
#include "vm/ArrayObject.h"
#include "vm/NativeObject.h"

#include "vm/Shape.h"

namespace JS {
class RecordType;
}

namespace js {

extern JSString* RecordToSource(JSContext* cx, JS::RecordType* rec);

}

namespace JS {

class RecordType final : public js::NativeObject {
  friend JSString* js::RecordToSource(JSContext* cx, RecordType* rec);

 public:
  enum { SORTED_KEYS_SLOT = 0, IS_ATOMIZED_SLOT, SLOT_COUNT };

  static const js::ClassSpec classSpec_;
  static const JSClass class_;

  static RecordType* createUninitialized(JSContext* cx, uint32_t initialLength);
  bool initializeNextProperty(JSContext* cx, Handle<PropertyKey> key,
                              HandleValue value);
  bool finishInitialization(JSContext* cx);
  static js::Shape* getInitialShape(JSContext* cx);

  static bool copy(JSContext* cx, Handle<RecordType*> in,
                   MutableHandle<RecordType*> out);

  uint32_t length();

  bool getOwnProperty(JSContext* cx, HandleId id, MutableHandleValue vp) const;

  static bool sameValueZero(JSContext* cx, RecordType* lhs, RecordType* rhs,
                            bool* equal);
  static bool sameValue(JSContext* cx, RecordType* lhs, RecordType* rhs,
                        bool* equal);

  js::ArrayObject* keys() const {
    return &getFixedSlot(SORTED_KEYS_SLOT).toObject().as<js::ArrayObject>();
  }

  using FieldHasher = std::function<js::HashNumber(const Value& child)>;
  js::HashNumber hash(const FieldHasher& hasher);

  bool ensureAtomized(JSContext* cx);
  bool isAtomized() const { return getFixedSlot(IS_ATOMIZED_SLOT).toBoolean(); }

  // This can be used to compare atomized records.
  static bool sameValueZero(RecordType* lhs, RecordType* rhs);

 private:
  template <bool Comparator(JSContext*, HandleValue, HandleValue, bool*)>
  static bool sameValueWith(JSContext* cx, RecordType* lhs, RecordType* rhs,
                            bool* equal);
};

}  // namespace JS

#endif