| 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 builtin_ParseRecordObject_h |
| 8 |
#define builtin_ParseRecordObject_h |
| 9 |
|
| 10 |
#include "js/HashTable.h" |
| 11 |
#include "js/TracingAPI.h" |
| 12 |
#include "vm/JSContext.h" |
| 13 |
|
| 14 |
namespace js { |
| 15 |
|
| 16 |
using JSONParseNode = JSString; |
| 17 |
|
| 18 |
class ParseRecordObject : public NativeObject { |
| 19 |
enum { ParseNodeSlot, ValueSlot, KeySlot, EntriesSlot, SlotCount }; |
| 20 |
|
| 21 |
public: |
| 22 |
using EntryMap = JSObject; |
| 23 |
|
| 24 |
static const JSClass class_; |
| 25 |
|
| 26 |
static ParseRecordObject* create(JSContext* cx, const Value& val); |
| 27 |
static ParseRecordObject* create(JSContext* cx, |
| 28 |
Handle<js::JSONParseNode*> parseNode, |
| 29 |
const Value& val); |
| 30 |
|
| 31 |
// The source text that was parsed for this record. According to the spec, we |
| 32 |
// don't track this for objects and arrays, so it will be a null pointer. |
| 33 |
JSONParseNode* getParseNode() const { |
| 34 |
const Value& slot = getSlot(ParseNodeSlot); |
| 35 |
return slot.isUndefined() ? nullptr : slot.toString(); |
| 36 |
} |
| 37 |
|
| 38 |
// For object members, the member key. For arrays, the index. For JSON |
| 39 |
// primitives, it will be undefined. |
| 40 |
JS::PropertyKey getKey(JSContext* cx) const; |
| 41 |
|
| 42 |
bool setKey(JSContext* cx, const JS::PropertyKey& key); |
| 43 |
|
| 44 |
// The original value corresponding to this record, used to determine if the |
| 45 |
// reviver function has modified it. |
| 46 |
const Value& getValue() const { return getSlot(ValueSlot); } |
| 47 |
|
| 48 |
void setValue(JS::Handle<JS::Value> value) { setSlot(ValueSlot, value); } |
| 49 |
|
| 50 |
bool hasValue() const { return !getValue().isUndefined(); } |
| 51 |
|
| 52 |
// For objects and arrays, the records for the members and elements |
| 53 |
// (respectively). If there are none, or for JSON primitives, the entries |
| 54 |
// parameter is unmodified. |
| 55 |
void getEntries(JSContext* cx, MutableHandle<EntryMap*> entries); |
| 56 |
|
| 57 |
void setEntries(JSContext* cx, Handle<EntryMap*> entries); |
| 58 |
}; |
| 59 |
|
| 60 |
} // namespace js |
| 61 |
|
| 62 |
#endif |