| 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 |
#include "builtin/ParseRecordObject.h" |
| 8 |
|
| 9 |
#include "jsapi.h" // JS_ValueToId, JS_IdToValue |
| 10 |
#include "builtin/Object.h" |
| 11 |
#include "vm/PlainObject.h" |
| 12 |
|
| 13 |
#include "vm/JSObject-inl.h" // NewBuiltinClassInstance |
| 14 |
|
| 15 |
using namespace js; |
| 16 |
|
| 17 |
// https://tc39.es/proposal-json-parse-with-source/#sec-json-parse-record |
| 18 |
|
| 19 |
const JSClass ParseRecordObject::class_ = { |
| 20 |
"ParseRecordObject", |
| 21 |
JSCLASS_HAS_RESERVED_SLOTS(SlotCount), |
| 22 |
}; |
| 23 |
|
| 24 |
/* static */ |
| 25 |
ParseRecordObject* ParseRecordObject::create(JSContext* cx, const Value& val) { |
| 26 |
Rooted<JSONParseNode*> parseNode(cx); |
| 27 |
return ParseRecordObject::create(cx, parseNode, val); |
| 28 |
} |
| 29 |
|
| 30 |
/* static */ |
| 31 |
ParseRecordObject* ParseRecordObject::create(JSContext* cx, |
| 32 |
Handle<JSONParseNode*> parseNode, |
| 33 |
const Value& val) { |
| 34 |
Rooted<ParseRecordObject*> obj( |
| 35 |
cx, NewObjectWithGivenProto<ParseRecordObject>(cx, nullptr)); |
| 36 |
if (!obj) { |
| 37 |
return nullptr; |
| 38 |
} |
| 39 |
|
| 40 |
if (parseNode) { |
| 41 |
obj->initSlot(ParseNodeSlot, StringValue(parseNode)); |
| 42 |
} |
| 43 |
obj->initSlot(ValueSlot, val); |
| 44 |
return obj; |
| 45 |
} |
| 46 |
|
| 47 |
JS::PropertyKey ParseRecordObject::getKey(JSContext* cx) const { |
| 48 |
Rooted<Value> slot(cx, getSlot(KeySlot)); |
| 49 |
Rooted<JS::PropertyKey> key(cx); |
| 50 |
MOZ_ALWAYS_TRUE(JS_ValueToId(cx, slot, &key)); |
| 51 |
return key; |
| 52 |
}; |
| 53 |
|
| 54 |
bool ParseRecordObject::setKey(JSContext* cx, const JS::PropertyKey& key) { |
| 55 |
Rooted<Value> val(cx); |
| 56 |
if (!JS_IdToValue(cx, key, &val)) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
setSlot(KeySlot, val); |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
void ParseRecordObject::setEntries(JSContext* cx, Handle<EntryMap*> entries) { |
| 64 |
setSlot(EntriesSlot, ObjectValue(*entries)); |
| 65 |
} |
| 66 |
|
| 67 |
void ParseRecordObject::getEntries(JSContext* cx, |
| 68 |
MutableHandle<EntryMap*> entries) { |
| 69 |
const Value& entryVal = getSlot(EntriesSlot); |
| 70 |
if (entryVal.isObject()) { |
| 71 |
entries.set(&entryVal.toObject()); |
| 72 |
} |
| 73 |
} |