From 6e6b519175ddc46b81c824bb6b0419cdeae6141a Mon Sep 17 00:00:00 2001 From: Roland Eischer Date: Thu, 6 Sep 2018 16:01:39 +0200 Subject: [PATCH] Refactored throw_ex function - Added throw_ex function without exception_ctor parameter - Refactored throw_ex function body to use v8::String::NewFromUtf8 to convert std::string function to v8::Local value. This makes the usage of windows.h obsolete. --- v8pp/throw_ex.hpp | 11 +++++++++-- v8pp/throw_ex.ipp | 27 ++++++--------------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/v8pp/throw_ex.hpp b/v8pp/throw_ex.hpp index 13fdec0e..6d9722a1 100644 --- a/v8pp/throw_ex.hpp +++ b/v8pp/throw_ex.hpp @@ -17,11 +17,18 @@ namespace v8pp { +v8::Local throw_ex(v8::Isolate* isolate, char const* str); + v8::Local throw_ex(v8::Isolate* isolate, char const* str, - v8::Local (*exception_ctor)(v8::Local) = v8::Exception::Error); + v8::Local (*exception_ctor)(v8::Local)); + +inline v8::Local throw_ex(v8::Isolate* isolate, std::string const& str) +{ + return throw_ex(isolate, str.c_str()); +} inline v8::Local throw_ex(v8::Isolate* isolate, std::string const& str, - v8::Local (*exception_ctor)(v8::Local) = v8::Exception::Error) + v8::Local (*exception_ctor)(v8::Local)) { return throw_ex(isolate, str.c_str(), exception_ctor); } diff --git a/v8pp/throw_ex.ipp b/v8pp/throw_ex.ipp index fe1aaa22..4c1738cb 100644 --- a/v8pp/throw_ex.ipp +++ b/v8pp/throw_ex.ipp @@ -1,31 +1,16 @@ #include "v8pp/throw_ex.hpp" -#ifdef _WIN32 -#include -#endif - namespace v8pp { +V8PP_IMPL v8::Local throw_ex(v8::Isolate* isolate, char const* str) +{ + return isolate->ThrowException(v8::String::NewFromUtf8(isolate, str)); +} + V8PP_IMPL v8::Local throw_ex(v8::Isolate* isolate, char const* str, v8::Local (*exception_ctor)(v8::Local)) { - v8::EscapableHandleScope scope(isolate); - - v8::Local message; -#ifdef _WIN32 - int const len = ::MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - if (len > 0) - { - std::vector buf(len); - ::MultiByteToWideChar(CP_ACP, 0, str, -1, &buf[0], len); - uint16_t const* data = reinterpret_cast(&buf[0]); - message = v8::String::NewFromTwoByte(isolate, data, - v8::NewStringType::kNormal, len - 1).ToLocalChecked(); - } -#else - message = v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal).ToLocalChecked(); -#endif - return scope.Escape(isolate->ThrowException(exception_ctor(message))); + return isolate->ThrowException(exception_ctor(v8::String::NewFromUtf8(isolate, str))); } } // namespace v8pp