summaryrefslogtreecommitdiffstats
path: root/js/src/vm/RecordTupleShared.cpp
blob: 0358865883ee927ef2259a3fe47f12be0c388c82 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* -*- 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_RecordTupleShared_h
#define vm_RecordTupleShared_h

#include "vm/RecordTupleShared.h"

#include "NamespaceImports.h"
#include "builtin/RecordObject.h"
#include "builtin/TupleObject.h"
#include "js/Value.h"
#include "vm/Compartment.h"
#include "vm/GlobalObject.h"
#include "vm/JSObject.h"
#include "vm/NativeObject.h"

#include "gc/Marking-inl.h"

namespace js {

bool IsExtendedPrimitive(const JSObject& obj) {
  return obj.is<RecordType>() || obj.is<TupleType>();
}

template <typename T>
JSObject* CopyExtendedPrimitiveHelper(JSContext* cx, HandleObject extPrim) {
  MOZ_ASSERT(gc::MaybeForwardedObjectIs<T>(extPrim));
  Rooted<T*> in(cx, &extPrim->template as<T>());
  Rooted<T*> out(cx);
  if (!T::copy(cx, in, &out)) {
    return nullptr;
  }
  return out;
}

JSObject* CopyExtendedPrimitive(JSContext* cx, HandleObject extPrim) {
  if (gc::MaybeForwardedObjectIs<RecordType>(extPrim)) {
    return CopyExtendedPrimitiveHelper<RecordType>(cx, extPrim);
  }
  MOZ_ASSERT(gc::MaybeForwardedObjectIs<TupleType>(extPrim));
  return CopyExtendedPrimitiveHelper<TupleType>(cx, extPrim);
}

// Returns false if v is not a valid record/tuple element (e.g. it's an Object
// or Symbol
bool CopyRecordTupleElement(JSContext* cx, HandleValue v,
                            MutableHandleValue out) {
  switch (v.type()) {
    case JS::ValueType::Double:
    case JS::ValueType::Int32:
    case JS::ValueType::Undefined:
    case JS::ValueType::Boolean: {
      out.set(v);
      break;
    }
    case JS::ValueType::String: {
      RootedString vStr(cx, v.toString());
      JSString* copy = CopyStringPure(cx, vStr);
      if (!copy) {
        return false;
      }
      out.setString(copy);
      break;
    }
    case JS::ValueType::BigInt: {
      RootedBigInt bi(cx, v.toBigInt());
      BigInt* copy = BigInt::copy(cx, bi);
      if (!copy) {
        return false;
      }
      out.setBigInt(copy);
      break;
    }
    case JS::ValueType::ExtendedPrimitive: {
      RootedObject extPrim(cx, &v.toExtendedPrimitive());
      JSObject* copy = CopyExtendedPrimitive(cx, extPrim);
      if (!copy) {
        return false;
      }
      out.setExtendedPrimitive(*copy);
      break;
    }
    default:
      MOZ_CRASH("CopyRecordTupleElement(): unexpected element type");
  }
  return true;
}

bool gc::MaybeForwardedIsExtendedPrimitive(const JSObject& obj) {
  return MaybeForwardedObjectIs<RecordType>(&obj) ||
         MaybeForwardedObjectIs<TupleType>(&obj);
}

bool IsExtendedPrimitiveWrapper(const JSObject& obj) {
  return obj.is<RecordObject>() || obj.is<TupleObject>();
}

bool ExtendedPrimitiveGetProperty(JSContext* cx, HandleObject obj,
                                  HandleValue receiver, HandleId id,
                                  MutableHandleValue vp) {
  MOZ_ASSERT(IsExtendedPrimitive(*obj));

  if (obj->is<RecordType>()) {
    if (obj->as<RecordType>().getOwnProperty(cx, id, vp)) {
      return true;
    }
    // If records will not have a null prototype, this should use a mehanism
    // similar to tuples.
    vp.set(JS::UndefinedValue());
    return true;
  }

  MOZ_ASSERT(obj->is<TupleType>());
  if (obj->as<TupleType>().getOwnProperty(id, vp)) {
    return true;
  }

  JSObject* proto = GlobalObject::getOrCreateTuplePrototype(cx, cx->global());
  if (!proto) {
    return false;
  }

  Rooted<NativeObject*> rootedProto(cx, &proto->as<NativeObject>());
  return NativeGetProperty(cx, rootedProto, receiver, id, vp);
}

}  // namespace js

#endif