| 1 | // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | // |
| 5 | // This file (and "raw_object_fields.cc") provide a kind of reflection that |
| 6 | // allows us to identify the name of fields in hand-written "Raw..." classes |
| 7 | // (from "raw_object.h") given the class and the offset within the object. This |
| 8 | // is used for example by the snapshot profile writer ("v8_snapshot_writer.h") |
| 9 | // to show the property names of these built-in objects in the snapshot profile. |
| 10 | |
| 11 | #ifndef RUNTIME_VM_RAW_OBJECT_FIELDS_H_ |
| 12 | #define RUNTIME_VM_RAW_OBJECT_FIELDS_H_ |
| 13 | |
| 14 | #include <utility> |
| 15 | |
| 16 | #include "vm/hash_map.h" |
| 17 | #include "vm/object.h" |
| 18 | #include "vm/raw_object.h" |
| 19 | |
| 20 | namespace dart { |
| 21 | |
| 22 | #if defined(DART_PRECOMPILER) || !defined(DART_PRODUCT) |
| 23 | |
| 24 | class OffsetsTable : public ZoneAllocated { |
| 25 | public: |
| 26 | explicit OffsetsTable(Zone* zone); |
| 27 | |
| 28 | // Returns 'nullptr' if no offset was found. |
| 29 | // Otherwise, the returned string is allocated in global static memory. |
| 30 | const char* FieldNameForOffset(intptr_t cid, intptr_t offset); |
| 31 | |
| 32 | struct OffsetsTableEntry { |
| 33 | const intptr_t class_id; |
| 34 | const char* field_name; |
| 35 | intptr_t offset; |
| 36 | }; |
| 37 | |
| 38 | static OffsetsTableEntry offsets_table[]; |
| 39 | |
| 40 | private: |
| 41 | struct IntAndIntToStringMapTraits { |
| 42 | typedef std::pair<intptr_t, intptr_t> Key; |
| 43 | typedef const char* Value; |
| 44 | |
| 45 | struct Pair { |
| 46 | Key key; |
| 47 | Value value; |
| 48 | Pair() : key({-1, -1}), value(nullptr) {} |
| 49 | Pair(Key k, Value v) : key(k), value(v) {} |
| 50 | }; |
| 51 | |
| 52 | static Value ValueOf(Pair pair) { return pair.value; } |
| 53 | static Key KeyOf(Pair pair) { return pair.key; } |
| 54 | static size_t Hashcode(Key key) { return key.first ^ key.second; } |
| 55 | static bool IsKeyEqual(Pair x, Key y) { |
| 56 | return x.key.first == y.first && x.key.second == y.second; |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | DirectChainedHashMap<IntAndIntToStringMapTraits> cached_offsets_; |
| 61 | }; |
| 62 | |
| 63 | #else |
| 64 | |
| 65 | class OffsetsTable : public ZoneAllocated { |
| 66 | public: |
| 67 | explicit OffsetsTable(Zone* zone) {} |
| 68 | |
| 69 | const char* FieldNameForOffset(intptr_t cid, intptr_t offset) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | #endif |
| 75 | |
| 76 | } // namespace dart |
| 77 | |
| 78 | #endif // RUNTIME_VM_RAW_OBJECT_FIELDS_H_ |
| 79 | |