1 // Copyright 2020 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_TOOLS_V8WINDBG_SRC_V8WINDBG_EXTENSION_H_
6 #define V8_TOOLS_V8WINDBG_SRC_V8WINDBG_EXTENSION_H_
7 
8 #include <memory>
9 #include <unordered_map>
10 #include <vector>
11 
12 #include "tools/v8windbg/base/utilities.h"
13 
14 // Responsible for initializing and uninitializing the extension. Also provides
15 // various convenience functions.
16 class Extension {
17  public:
18   Extension();
19   HRESULT Initialize();
20   ~Extension();
21   WRL::ComPtr<IDebugHostModule> GetV8Module(
22       WRL::ComPtr<IDebugHostContext>& sp_ctx);
23   WRL::ComPtr<IDebugHostType> GetTypeFromV8Module(
24       WRL::ComPtr<IDebugHostContext>& sp_ctx, const char16_t* type_name);
25   WRL::ComPtr<IDebugHostType> GetV8ObjectType(
26       WRL::ComPtr<IDebugHostContext>& sp_ctx);
27   void TryRegisterType(WRL::ComPtr<IDebugHostType>& sp_type,
28                        std::u16string type_name);
29   bool DoesTypeDeriveFromObject(const WRL::ComPtr<IDebugHostType>& sp_type);
Current()30   static Extension* Current() { return current_extension_.get(); }
SetExtension(std::unique_ptr<Extension> new_extension)31   static void SetExtension(std::unique_ptr<Extension> new_extension) {
32     current_extension_ = std::move(new_extension);
33   }
34 
35   // Returns the parent model for instances of v8::internal::Object and similar
36   // classes, which contain as their first and only field a tagged V8 value.
GetObjectDataModel()37   IModelObject* GetObjectDataModel() { return sp_object_data_model_.Get(); }
38 
39   // Returns the parent model that provides indexing support for fields that
40   // contain arrays of something more complicated than basic native types.
GetIndexedFieldDataModel()41   IModelObject* GetIndexedFieldDataModel() {
42     return sp_indexed_field_model_.Get();
43   }
44 
45  private:
46   HRESULT OverrideLocalsGetter(IModelObject* parent, const wchar_t* key_name,
47                                bool is_parameters);
48 
49   // A property that has been overridden by this extension. The original value
50   // must be put back in place during ~Extension.
51   struct PropertyOverride {
52     PropertyOverride();
53     PropertyOverride(IModelObject* parent, std::u16string key_name,
54                      IModelObject* original_value,
55                      IKeyStore* original_metadata);
56     ~PropertyOverride();
57     PropertyOverride(const PropertyOverride&);
58     PropertyOverride& operator=(const PropertyOverride&);
59     WRL::ComPtr<IModelObject> parent;
60     std::u16string key_name;
61     WRL::ComPtr<IModelObject> original_value;
62     WRL::ComPtr<IKeyStore> original_metadata;
63   };
64 
65   static std::unique_ptr<Extension> current_extension_;
66 
67   WRL::ComPtr<IModelObject> sp_object_data_model_;
68   WRL::ComPtr<IModelObject> sp_local_data_model_;
69   WRL::ComPtr<IModelObject> sp_indexed_field_model_;
70 
71   WRL::ComPtr<IDebugHostModule> sp_v8_module_;
72   std::unordered_map<std::u16string, WRL::ComPtr<IDebugHostType>>
73       cached_v8_module_types_;
74   std::vector<WRL::ComPtr<IDebugHostTypeSignature>> registered_object_types_;
75   std::vector<WRL::ComPtr<IDebugHostTypeSignature>> registered_handle_types_;
76   std::vector<PropertyOverride> overridden_properties_;
77   WRL::ComPtr<IDebugHostContext> sp_v8_module_ctx_;
78   ULONG v8_module_proc_id_;
79 };
80 
81 #endif  // V8_TOOLS_V8WINDBG_SRC_V8WINDBG_EXTENSION_H_
82