tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ParseRecordObject.cpp (2192B)


      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 "js/PropertyAndElement.h"  // JS_SetPropertyById
     12 #include "vm/PlainObject.h"
     13 
     14 #include "vm/JSObject-inl.h"  // NewBuiltinClassInstance
     15 
     16 using namespace js;
     17 
     18 // https://tc39.es/proposal-json-parse-with-source/#sec-json-parse-record
     19 
     20 const JSClass ParseRecordObject::class_ = {
     21    "ParseRecordObject",
     22    JSCLASS_HAS_RESERVED_SLOTS(SlotCount),
     23 };
     24 
     25 /* static */
     26 ParseRecordObject* ParseRecordObject::create(JSContext* cx, const Value& val) {
     27  Rooted<JSONParseNode*> parseNode(cx);
     28  return ParseRecordObject::create(cx, parseNode, val);
     29 }
     30 
     31 /* static */
     32 ParseRecordObject* ParseRecordObject::create(JSContext* cx,
     33                                             Handle<JSONParseNode*> parseNode,
     34                                             const Value& val) {
     35  auto* obj = 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 bool ParseRecordObject::addEntries(JSContext* cx, Handle<JS::PropertyKey> key,
     64                                   Handle<ParseRecordObject*> parseRecord) {
     65  parseRecord->setKey(cx, key.get());
     66  Rooted<Value> pro(cx, ObjectValue(*parseRecord));
     67  Rooted<JSObject*> obj(cx, this);
     68  return JS_SetPropertyById(cx, obj, key, pro);
     69 }