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_BASE_UTILITIES_H_
6 #define V8_TOOLS_V8WINDBG_BASE_UTILITIES_H_
7 
8 #include "tools/v8windbg/base/dbgext.h"
9 
U16ToWChar(const char16_t * p_u16)10 inline const wchar_t* U16ToWChar(const char16_t* p_u16) {
11   static_assert(sizeof(wchar_t) == sizeof(char16_t), "wrong wchar size");
12   return reinterpret_cast<const wchar_t*>(p_u16);
13 }
14 
U16ToWChar(std::u16string & str)15 inline const wchar_t* U16ToWChar(std::u16string& str) {
16   return U16ToWChar(str.data());
17 }
18 
19 #if defined(WIN32)
ConvertToU16String(std::string utf8_string)20 inline std::u16string ConvertToU16String(std::string utf8_string) {
21   int len_chars =
22       ::MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), -1, nullptr, 0);
23 
24   char16_t* p_buff =
25       static_cast<char16_t*>(malloc(len_chars * sizeof(char16_t)));
26 
27   // On Windows wchar_t is the same a char16_t
28   static_assert(sizeof(wchar_t) == sizeof(char16_t), "wrong wchar size");
29   len_chars =
30       ::MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), -1,
31                             reinterpret_cast<wchar_t*>(p_buff), len_chars);
32   std::u16string result{p_buff};
33   free(p_buff);
34 
35   return result;
36 }
37 #else
38 #error String encoding conversion must be provided for the target platform.
39 #endif
40 
41 HRESULT CreateProperty(IDataModelManager* p_manager,
42                        IModelPropertyAccessor* p_property,
43                        IModelObject** pp_property_object);
44 
45 HRESULT CreateMethod(IDataModelManager* p_manager, IModelMethod* p_method,
46                      IModelObject** pp_method_object);
47 
48 HRESULT UnboxProperty(IModelObject* object, IModelPropertyAccessor** result);
49 
50 HRESULT CreateTypedIntrinsic(uint64_t value, IDebugHostType* type,
51                              IModelObject** result);
52 
53 HRESULT CreateULong64(ULONG64 value, IModelObject** pp_int);
54 
55 HRESULT UnboxULong64(IModelObject* object, ULONG64* value,
56                      bool convert = false);
57 
58 HRESULT GetInt32(IDebugHostConstant* object, int* value);
59 
60 HRESULT CreateInt32(int value, IModelObject** pp_int);
61 
62 HRESULT CreateUInt32(uint32_t value, IModelObject** pp_int);
63 
64 HRESULT CreateBool(bool value, IModelObject** pp_val);
65 
66 HRESULT CreateNumber(double value, IModelObject** pp_val);
67 
68 HRESULT CreateString(std::u16string value, IModelObject** pp_val);
69 
70 HRESULT UnboxString(IModelObject* object, BSTR* value);
71 
72 HRESULT GetModelAtIndex(WRL::ComPtr<IModelObject>& sp_parent,
73                         WRL::ComPtr<IModelObject>& sp_index,
74                         IModelObject** p_result);
75 
76 HRESULT GetCurrentThread(WRL::ComPtr<IDebugHostContext>& sp_host_context,
77                          IModelObject** p_current_thread);
78 
79 #define RETURN_IF_FAIL(expression) \
80   do {                             \
81     HRESULT hr = expression;       \
82     if (FAILED(hr)) {              \
83       return hr;                   \
84     }                              \
85   } while (false)
86 
87 #endif  // V8_TOOLS_V8WINDBG_BASE_UTILITIES_H_
88