OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 namespace AdblockPlus | 32 namespace AdblockPlus |
33 { | 33 { |
34 namespace Utils | 34 namespace Utils |
35 { | 35 { |
36 void CheckTryCatch(v8::Isolate* isolate, const v8::TryCatch& tryCatch); | 36 void CheckTryCatch(v8::Isolate* isolate, const v8::TryCatch& tryCatch); |
37 | 37 |
38 /* | 38 /* |
39 * Check for exception and then that a MaybeLocal<> isn't empty, | 39 * Check for exception and then that a MaybeLocal<> isn't empty, |
40 * and throw a JsError if it is, otherwise return the Local<> | 40 * and throw a JsError if it is, otherwise return the Local<> |
41 * Call using the macro %CHECKED_MAYBE to get the location. | 41 * Call using the macro %CHECKED_TO_LOCAL to get the location. |
42 */ | 42 */ |
43 template<class T> | 43 template<class T> |
44 v8::Local<T> CheckedToLocal(v8::Isolate* isolate, | 44 v8::Local<T> CheckedToLocal(v8::Isolate* isolate, |
45 v8::MaybeLocal<T>&& value, const v8::TryCatch* tryCatch, | 45 v8::MaybeLocal<T>&& value, const v8::TryCatch* tryCatch, |
46 const char* filename, int line) | 46 const char* filename, int line) |
47 { | 47 { |
48 if (tryCatch) | 48 if (tryCatch) |
49 CheckTryCatch(isolate, *tryCatch); | 49 CheckTryCatch(isolate, *tryCatch); |
50 if (value.IsEmpty()) | 50 if (value.IsEmpty()) |
51 throw AdblockPlus::JsError("Empty value at ", filename, line); | 51 throw AdblockPlus::JsError("Empty value at ", filename, line); |
52 return value.ToLocalChecked(); | 52 return value.ToLocalChecked(); |
53 } | 53 } |
54 | 54 |
55 #define CHECKED_TO_LOCAL_WITH_TRY_CATCH(isolate, value, tryCatch) \ | 55 #define CHECKED_TO_LOCAL_WITH_TRY_CATCH(isolate, value, tryCatch) \ |
56 AdblockPlus::Utils::CheckedToLocal((isolate), (value), &(tryCatch), __FILE__
, __LINE__) | 56 AdblockPlus::Utils::CheckedToLocal((isolate), (value), &(tryCatch), __FILE__
, __LINE__) |
57 | 57 |
58 #define CHECKED_TO_LOCAL(isolate, value) \ | 58 #define CHECKED_TO_LOCAL(isolate, value) \ |
59 AdblockPlus::Utils::CheckedToLocal((isolate), (value), nullptr, __FILE__, __
LINE__) | 59 AdblockPlus::Utils::CheckedToLocal((isolate), (value), nullptr, __FILE__, __
LINE__) |
60 | 60 |
| 61 /* |
| 62 * Check that a Maybe<> isn't empty, |
| 63 * and throw a JsError if it is, otherwise return the value |
| 64 * Call using the macro %CHECKED_TO_VALUE to get the location. |
| 65 */ |
| 66 template<class T> |
| 67 T CheckedToValue(v8::Maybe<T>&& value, const char* filename, int line) |
| 68 { |
| 69 if (value.IsNothing()) |
| 70 throw AdblockPlus::JsError("Empty value at ", filename, line); |
| 71 return value.FromJust(); |
| 72 } |
| 73 |
| 74 #define CHECKED_TO_VALUE(value) \ |
| 75 AdblockPlus::Utils::CheckedToValue(value, __FILE__, __LINE__) |
| 76 |
61 std::string FromV8String(v8::Isolate* isolate, const v8::Local<v8::Value>& v
alue); | 77 std::string FromV8String(v8::Isolate* isolate, const v8::Local<v8::Value>& v
alue); |
62 StringBuffer StringBufferFromV8String(v8::Isolate* isolate, const v8::Local<
v8::Value>& value); | 78 StringBuffer StringBufferFromV8String(v8::Isolate* isolate, const v8::Local<
v8::Value>& value); |
63 v8::MaybeLocal<v8::String> ToV8String(v8::Isolate* isolate, const std::strin
g& str); | 79 v8::MaybeLocal<v8::String> ToV8String(v8::Isolate* isolate, const std::strin
g& str); |
64 v8::MaybeLocal<v8::String> StringBufferToV8String(v8::Isolate* isolate, cons
t StringBuffer& bytes); | 80 v8::MaybeLocal<v8::String> StringBufferToV8String(v8::Isolate* isolate, cons
t StringBuffer& bytes); |
65 void ThrowExceptionInJS(v8::Isolate* isolate, const std::string& str); | 81 void ThrowExceptionInJS(v8::Isolate* isolate, const std::string& str); |
66 | 82 |
67 // Code for templated function has to be in a header file, can't be in .cpp | 83 // Code for templated function has to be in a header file, can't be in .cpp |
68 template<class T> | 84 template<class T> |
69 T TrimString(const T& text) | 85 T TrimString(const T& text) |
70 { | 86 { |
71 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-tri
m-stdstring | 87 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-tri
m-stdstring |
72 T trimmed(text); | 88 T trimmed(text); |
73 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end()
, std::not1(std::ptr_fun<int, int>(std::isspace)))); | 89 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end()
, std::not1(std::ptr_fun<int, int>(std::isspace)))); |
74 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std
::ptr_fun<int, int>(std::isspace))).base(), trimmed.end()); | 90 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std
::ptr_fun<int, int>(std::isspace))).base(), trimmed.end()); |
75 return trimmed; | 91 return trimmed; |
76 } | 92 } |
77 #ifdef _WIN32 | 93 #ifdef _WIN32 |
78 std::wstring ToUtf16String(const std::string& str); | 94 std::wstring ToUtf16String(const std::string& str); |
79 std::string ToUtf8String(const std::wstring& str); | 95 std::string ToUtf8String(const std::wstring& str); |
80 std::wstring CanonizeUrl(const std::wstring& url); | 96 std::wstring CanonizeUrl(const std::wstring& url); |
81 #endif | 97 #endif |
82 } | 98 } |
83 } | 99 } |
84 | 100 |
85 #endif | 101 #endif |
OLD | NEW |