11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_TOOLS_V8WINDBG_BASE_UTILITIES_H_
61cb0ef41Sopenharmony_ci#define V8_TOOLS_V8WINDBG_BASE_UTILITIES_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "tools/v8windbg/base/dbgext.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciinline const wchar_t* U16ToWChar(const char16_t* p_u16) {
111cb0ef41Sopenharmony_ci  static_assert(sizeof(wchar_t) == sizeof(char16_t), "wrong wchar size");
121cb0ef41Sopenharmony_ci  return reinterpret_cast<const wchar_t*>(p_u16);
131cb0ef41Sopenharmony_ci}
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciinline const wchar_t* U16ToWChar(std::u16string& str) {
161cb0ef41Sopenharmony_ci  return U16ToWChar(str.data());
171cb0ef41Sopenharmony_ci}
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci#if defined(WIN32)
201cb0ef41Sopenharmony_ciinline std::u16string ConvertToU16String(std::string utf8_string) {
211cb0ef41Sopenharmony_ci  int len_chars =
221cb0ef41Sopenharmony_ci      ::MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), -1, nullptr, 0);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  char16_t* p_buff =
251cb0ef41Sopenharmony_ci      static_cast<char16_t*>(malloc(len_chars * sizeof(char16_t)));
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  // On Windows wchar_t is the same a char16_t
281cb0ef41Sopenharmony_ci  static_assert(sizeof(wchar_t) == sizeof(char16_t), "wrong wchar size");
291cb0ef41Sopenharmony_ci  len_chars =
301cb0ef41Sopenharmony_ci      ::MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), -1,
311cb0ef41Sopenharmony_ci                            reinterpret_cast<wchar_t*>(p_buff), len_chars);
321cb0ef41Sopenharmony_ci  std::u16string result{p_buff};
331cb0ef41Sopenharmony_ci  free(p_buff);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  return result;
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci#else
381cb0ef41Sopenharmony_ci#error String encoding conversion must be provided for the target platform.
391cb0ef41Sopenharmony_ci#endif
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciHRESULT CreateProperty(IDataModelManager* p_manager,
421cb0ef41Sopenharmony_ci                       IModelPropertyAccessor* p_property,
431cb0ef41Sopenharmony_ci                       IModelObject** pp_property_object);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciHRESULT CreateMethod(IDataModelManager* p_manager, IModelMethod* p_method,
461cb0ef41Sopenharmony_ci                     IModelObject** pp_method_object);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciHRESULT UnboxProperty(IModelObject* object, IModelPropertyAccessor** result);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciHRESULT CreateTypedIntrinsic(uint64_t value, IDebugHostType* type,
511cb0ef41Sopenharmony_ci                             IModelObject** result);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ciHRESULT CreateULong64(ULONG64 value, IModelObject** pp_int);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciHRESULT UnboxULong64(IModelObject* object, ULONG64* value,
561cb0ef41Sopenharmony_ci                     bool convert = false);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciHRESULT GetInt32(IDebugHostConstant* object, int* value);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ciHRESULT CreateInt32(int value, IModelObject** pp_int);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ciHRESULT CreateUInt32(uint32_t value, IModelObject** pp_int);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciHRESULT CreateBool(bool value, IModelObject** pp_val);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciHRESULT CreateNumber(double value, IModelObject** pp_val);
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciHRESULT CreateString(std::u16string value, IModelObject** pp_val);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciHRESULT UnboxString(IModelObject* object, BSTR* value);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ciHRESULT GetModelAtIndex(WRL::ComPtr<IModelObject>& sp_parent,
731cb0ef41Sopenharmony_ci                        WRL::ComPtr<IModelObject>& sp_index,
741cb0ef41Sopenharmony_ci                        IModelObject** p_result);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ciHRESULT GetCurrentThread(WRL::ComPtr<IDebugHostContext>& sp_host_context,
771cb0ef41Sopenharmony_ci                         IModelObject** p_current_thread);
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci#define RETURN_IF_FAIL(expression) \
801cb0ef41Sopenharmony_ci  do {                             \
811cb0ef41Sopenharmony_ci    HRESULT hr = expression;       \
821cb0ef41Sopenharmony_ci    if (FAILED(hr)) {              \
831cb0ef41Sopenharmony_ci      return hr;                   \
841cb0ef41Sopenharmony_ci    }                              \
851cb0ef41Sopenharmony_ci  } while (false)
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci#endif  // V8_TOOLS_V8WINDBG_BASE_UTILITIES_H_
88