2 * Copyright 2018 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "include/core/SkTypes.h"
12 #include "include/private/SkNoncopyable.h"
13 #include "src/core/SkArenaAlloc.h"
16 #include <string_view>
24 * A fast and likely non-conforming JSON parser.
26 * Some known limitations/compromises:
28 * -- single-precision FP numbers
30 * -- missing string unescaping (no current users, could be easily added)
33 * Values are opaque, fixed-size (64 bits), immutable records.
35 * They can be converted to facade types for type-specific functionality.
39 * if (v.is<ArrayValue>()) {
40 * for (const auto& item : v.as<ArrayValue>()) {
41 * if (const NumberValue* n = item) {
42 * printf("Found number: %f", **n);
47 * if (v.is<ObjectValue>()) {
48 * const StringValue* id = v.as<ObjectValue>()["id"];
50 * printf("Found object ID: %s", id->begin());
52 * printf("Missing object ID");
56 class alignas(8) Value {
68 * @return The type of this value.
73 * @return True if the record matches the facade type T.
76 bool is() const { return this->getType() == T::kType; }
79 * Unguarded conversion to facade types.
81 * @return The record cast as facade type T&.
85 SkASSERT(this->is<T>());
86 return *reinterpret_cast<const T*>(this);
90 * Guarded conversion to facade types.
92 * @return The record cast as facade type T*.
95 operator const T*() const {
96 return this->is<T>() ? &this->as<T>() : nullptr;
100 * @return The string representation of this value.
102 SkString toString() const;
106 Value implementation notes:
117 char[8] (short string storage)
118 external payload (tagged) pointer
120 -- lowest 3 bits reserved for tag storage
123 enum class Tag : uint8_t {
124 // n.b.: we picked kShortString == 0 on purpose,
125 // to enable certain short-string optimizations.
126 kShortString = 0b00000000, // inline payload
127 kNull = 0b00000001, // no payload
128 kBool = 0b00000010, // inline payload
129 kInt = 0b00000011, // inline payload
130 kFloat = 0b00000100, // inline payload
131 kString = 0b00000101, // ptr to external storage
132 kArray = 0b00000110, // ptr to external storage
133 kObject = 0b00000111, // ptr to external storage
135 inline static constexpr uint8_t kTagMask = 0b00000111;
137 void init_tagged(Tag);
138 void init_tagged_pointer(Tag, void*);
141 return static_cast<Tag>(fData8[0] & kTagMask);
144 // Access the record payload as T.
146 // Since the tag is stored in the lower bits, we skip the first word whenever feasible.
148 // E.g. (U == unused)
151 // -----------------------------------------------------------------------
152 // |TAG| U | val8 | U | U | U | U | U | U |
153 // -----------------------------------------------------------------------
156 // -----------------------------------------------------------------------
157 // |TAG| U | val16 | U | U |
158 // -----------------------------------------------------------------------
161 // -----------------------------------------------------------------------
163 // -----------------------------------------------------------------------
166 // -----------------------------------------------------------------------
167 // |TAG| U | T* (32bits) |
168 // -----------------------------------------------------------------------
171 // -----------------------------------------------------------------------
172 // |TAG| T* (61bits) |
173 // -----------------------------------------------------------------------
175 template <typename T>
176 const T* cast() const {
177 static_assert(sizeof (T) <= sizeof(Value), "");
178 static_assert(alignof(T) <= alignof(Value), "");
180 return (sizeof(T) > sizeof(*this) / 2)
181 ? reinterpret_cast<const T*>(this) + 0 // need all the bits
182 : reinterpret_cast<const T*>(this) + 1; // skip the first word (where the tag lives)
185 template <typename T>
186 T* cast() { return const_cast<T*>(const_cast<const Value*>(this)->cast<T>()); }
188 // Access the pointer payload.
189 template <typename T>
190 const T* ptr() const {
191 static_assert(sizeof(uintptr_t) == sizeof(Value) ||
192 sizeof(uintptr_t) * 2 == sizeof(Value), "");
194 return (sizeof(uintptr_t) < sizeof(Value))
195 // For 32-bit, pointers are stored unmodified.
196 ? *this->cast<const T*>()
197 // For 64-bit, we use the lower bits of the pointer as tag storage.
198 : reinterpret_cast<T*>(*this->cast<uintptr_t>() & ~static_cast<uintptr_t>(kTagMask));
202 inline static constexpr size_t kValueSize = 8;
204 uint8_t fData8[kValueSize];
206 #if !defined(SK_CPU_LENDIAN)
207 // The current value layout assumes LE and will take some tweaking for BE.
208 static_assert(false, "Big-endian builds are not supported at this time.");
212 class NullValue final : public Value {
214 inline static constexpr Type kType = Type::kNull;
219 class BoolValue final : public Value {
221 inline static constexpr Type kType = Type::kBool;
223 explicit BoolValue(bool);
225 bool operator *() const {
226 SkASSERT(this->getTag() == Tag::kBool);
227 return *this->cast<bool>();
231 class NumberValue final : public Value {
233 inline static constexpr Type kType = Type::kNumber;
235 explicit NumberValue(int32_t);
236 explicit NumberValue(float);
238 double operator *() const {
239 SkASSERT(this->getTag() == Tag::kInt ||
240 this->getTag() == Tag::kFloat);
242 return this->getTag() == Tag::kInt
243 ? static_cast<double>(*this->cast<int32_t>())
244 : static_cast<double>(*this->cast<float>());
248 template <typename T, Value::Type vtype>
249 class VectorValue : public Value {
252 inline static constexpr Type kType = vtype;
254 size_t size() const {
255 SkASSERT(this->getType() == kType);
256 return *this->ptr<size_t>();
259 const T* begin() const {
260 SkASSERT(this->getType() == kType);
261 const auto* size_ptr = this->ptr<size_t>();
262 return reinterpret_cast<const T*>(size_ptr + 1);
265 const T* end() const {
266 SkASSERT(this->getType() == kType);
267 const auto* size_ptr = this->ptr<size_t>();
268 return reinterpret_cast<const T*>(size_ptr + 1) + *size_ptr;
271 const T& operator[](size_t i) const {
272 SkASSERT(this->getType() == kType);
273 SkASSERT(i < this->size());
275 return *(this->begin() + i);
279 class ArrayValue final : public VectorValue<Value, Value::Type::kArray> {
281 ArrayValue(const Value* src, size_t size, SkArenaAlloc& alloc);
284 class StringValue final : public Value {
286 inline static constexpr Type kType = Type::kString;
289 StringValue(const char* src, size_t size, SkArenaAlloc& alloc);
291 size_t size() const {
292 switch (this->getTag()) {
293 case Tag::kShortString:
294 // We don't bother storing a length for short strings on the assumption
295 // that strlen is fast in this case. If this becomes problematic, we
296 // can either go back to storing (7-len) in the tag byte or write a fast
298 return strlen(this->cast<char>());
300 return this->cast<VectorValue<char, Value::Type::kString>>()->size();
306 const char* begin() const {
307 return this->getTag() == Tag::kShortString
309 : this->cast<VectorValue<char, Value::Type::kString>>()->begin();
312 const char* end() const {
313 return this->getTag() == Tag::kShortString
314 ? strchr(this->cast<char>(), '\0')
315 : this->cast<VectorValue<char, Value::Type::kString>>()->end();
318 std::string_view str() const {
319 return std::string_view(this->begin(), this->size());
328 class ObjectValue final : public VectorValue<Member, Value::Type::kObject> {
330 ObjectValue(const Member* src, size_t size, SkArenaAlloc& alloc);
332 const Value& operator[](const char*) const;
334 const Member& operator[](size_t i) const {
335 return this->VectorValue::operator[](i);
339 class DOM final : public SkNoncopyable {
341 DOM(const char*, size_t);
343 const Value& root() const { return fRoot; }
345 void write(SkWStream*) const;
352 inline Value::Type Value::getType() const {
353 switch (this->getTag()) {
354 case Tag::kNull: return Type::kNull;
355 case Tag::kBool: return Type::kBool;
356 case Tag::kInt: return Type::kNumber;
357 case Tag::kFloat: return Type::kNumber;
358 case Tag::kShortString: return Type::kString;
359 case Tag::kString: return Type::kString;
360 case Tag::kArray: return Type::kArray;
361 case Tag::kObject: return Type::kObject;
364 SkASSERT(false); // unreachable
368 } // namespace skjson
370 #endif // SkJSON_DEFINED