Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 1 | // Copyright 2020 PDFium 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 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #include "fxjs/fxv8.h" |
| 8 | |
| 9 | namespace fxv8 { |
| 10 | |
Tom Sepez | 1512c36 | 2020-03-19 22:13:52 +0000 | [diff] [blame] | 11 | v8::Local<v8::Value> NewNullHelper(v8::Isolate* pIsolate) { |
| 12 | return v8::Null(pIsolate); |
| 13 | } |
| 14 | |
| 15 | v8::Local<v8::Value> NewUndefinedHelper(v8::Isolate* pIsolate) { |
| 16 | return v8::Undefined(pIsolate); |
| 17 | } |
| 18 | |
| 19 | v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, int number) { |
| 20 | return v8::Int32::New(pIsolate, number); |
| 21 | } |
| 22 | |
| 23 | v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, double number) { |
| 24 | return v8::Number::New(pIsolate, number); |
| 25 | } |
| 26 | |
| 27 | v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, float number) { |
| 28 | return v8::Number::New(pIsolate, number); |
| 29 | } |
| 30 | |
| 31 | v8::Local<v8::Boolean> NewBooleanHelper(v8::Isolate* pIsolate, bool b) { |
| 32 | return v8::Boolean::New(pIsolate, b); |
| 33 | } |
| 34 | |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 35 | v8::Local<v8::String> NewStringHelper(v8::Isolate* pIsolate, |
| 36 | ByteStringView str) { |
| 37 | return v8::String::NewFromUtf8(pIsolate, str.unterminated_c_str(), |
| 38 | v8::NewStringType::kNormal, str.GetLength()) |
| 39 | .ToLocalChecked(); |
| 40 | } |
| 41 | |
| 42 | v8::Local<v8::String> NewStringHelper(v8::Isolate* pIsolate, |
| 43 | WideStringView str) { |
| 44 | return NewStringHelper(pIsolate, FX_UTF8Encode(str).AsStringView()); |
| 45 | } |
| 46 | |
Tom Sepez | 1512c36 | 2020-03-19 22:13:52 +0000 | [diff] [blame] | 47 | v8::Local<v8::Array> NewArrayHelper(v8::Isolate* pIsolate) { |
| 48 | return v8::Array::New(pIsolate); |
| 49 | } |
| 50 | |
| 51 | v8::Local<v8::Object> NewObjectHelper(v8::Isolate* pIsolate) { |
| 52 | return v8::Object::New(pIsolate); |
| 53 | } |
| 54 | |
| 55 | v8::Local<v8::Date> NewDateHelper(v8::Isolate* pIsolate, double d) { |
| 56 | return v8::Date::New(pIsolate->GetCurrentContext(), d) |
| 57 | .ToLocalChecked() |
| 58 | .As<v8::Date>(); |
| 59 | } |
| 60 | |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 61 | int ReentrantToInt32Helper(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) { |
| 62 | if (pValue.IsEmpty()) |
| 63 | return 0; |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 64 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 65 | return pValue->Int32Value(pIsolate->GetCurrentContext()).FromMaybe(0); |
| 66 | } |
| 67 | |
| 68 | bool ReentrantToBooleanHelper(v8::Isolate* pIsolate, |
| 69 | v8::Local<v8::Value> pValue) { |
| 70 | if (pValue.IsEmpty()) |
| 71 | return false; |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 72 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 73 | return pValue->BooleanValue(pIsolate); |
| 74 | } |
| 75 | |
| 76 | double ReentrantToDoubleHelper(v8::Isolate* pIsolate, |
| 77 | v8::Local<v8::Value> pValue) { |
| 78 | if (pValue.IsEmpty()) |
| 79 | return 0.0; |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 80 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 81 | return pValue->NumberValue(pIsolate->GetCurrentContext()).FromMaybe(0.0); |
| 82 | } |
| 83 | |
| 84 | WideString ReentrantToWideStringHelper(v8::Isolate* pIsolate, |
| 85 | v8::Local<v8::Value> pValue) { |
| 86 | if (pValue.IsEmpty()) |
| 87 | return WideString(); |
Tom Sepez | c72e23f | 2020-03-19 18:58:17 +0000 | [diff] [blame] | 88 | |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 89 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 90 | v8::MaybeLocal<v8::String> maybe_string = |
| 91 | pValue->ToString(pIsolate->GetCurrentContext()); |
| 92 | if (maybe_string.IsEmpty()) |
| 93 | return WideString(); |
Tom Sepez | c72e23f | 2020-03-19 18:58:17 +0000 | [diff] [blame] | 94 | |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 95 | v8::String::Utf8Value s(pIsolate, maybe_string.ToLocalChecked()); |
| 96 | return WideString::FromUTF8(ByteStringView(*s, s.length())); |
| 97 | } |
| 98 | |
| 99 | ByteString ReentrantToByteStringHelper(v8::Isolate* pIsolate, |
| 100 | v8::Local<v8::Value> pValue) { |
| 101 | if (pValue.IsEmpty()) |
| 102 | return ByteString(); |
Tom Sepez | c72e23f | 2020-03-19 18:58:17 +0000 | [diff] [blame] | 103 | |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 104 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 105 | v8::MaybeLocal<v8::String> maybe_string = |
| 106 | pValue->ToString(pIsolate->GetCurrentContext()); |
| 107 | if (maybe_string.IsEmpty()) |
| 108 | return ByteString(); |
Tom Sepez | c72e23f | 2020-03-19 18:58:17 +0000 | [diff] [blame] | 109 | |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 110 | v8::String::Utf8Value s(pIsolate, maybe_string.ToLocalChecked()); |
| 111 | return ByteString(*s); |
| 112 | } |
| 113 | |
Tom Sepez | 1512c36 | 2020-03-19 22:13:52 +0000 | [diff] [blame] | 114 | v8::Local<v8::Object> ReentrantToObjectHelper(v8::Isolate* pIsolate, |
| 115 | v8::Local<v8::Value> pValue) { |
| 116 | if (pValue.IsEmpty() || !pValue->IsObject()) |
| 117 | return v8::Local<v8::Object>(); |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 118 | |
| 119 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | 1512c36 | 2020-03-19 22:13:52 +0000 | [diff] [blame] | 120 | v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 121 | return pValue->ToObject(context).ToLocalChecked(); |
| 122 | } |
| 123 | |
| 124 | v8::Local<v8::Array> ReentrantToArrayHelper(v8::Isolate* pIsolate, |
| 125 | v8::Local<v8::Value> pValue) { |
| 126 | if (pValue.IsEmpty() || !pValue->IsArray()) |
| 127 | return v8::Local<v8::Array>(); |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 128 | |
| 129 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | 1512c36 | 2020-03-19 22:13:52 +0000 | [diff] [blame] | 130 | v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 131 | return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
| 132 | } |
| 133 | |
Tom Sepez | c72e23f | 2020-03-19 18:58:17 +0000 | [diff] [blame] | 134 | v8::Local<v8::Value> ReentrantGetObjectPropertyHelper( |
| 135 | v8::Isolate* pIsolate, |
| 136 | v8::Local<v8::Object> pObj, |
| 137 | ByteStringView bsUTF8PropertyName) { |
| 138 | if (pObj.IsEmpty()) |
| 139 | return v8::Local<v8::Value>(); |
| 140 | |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 141 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | c72e23f | 2020-03-19 18:58:17 +0000 | [diff] [blame] | 142 | v8::Local<v8::Value> val; |
| 143 | if (!pObj->Get(pIsolate->GetCurrentContext(), |
| 144 | NewStringHelper(pIsolate, bsUTF8PropertyName)) |
| 145 | .ToLocal(&val)) { |
| 146 | return v8::Local<v8::Value>(); |
| 147 | } |
| 148 | return val; |
| 149 | } |
| 150 | |
| 151 | std::vector<WideString> ReentrantGetObjectPropertyNamesHelper( |
| 152 | v8::Isolate* pIsolate, |
| 153 | v8::Local<v8::Object> pObj) { |
| 154 | if (pObj.IsEmpty()) |
| 155 | return std::vector<WideString>(); |
| 156 | |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 157 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | c72e23f | 2020-03-19 18:58:17 +0000 | [diff] [blame] | 158 | v8::Local<v8::Array> val; |
| 159 | v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 160 | if (!pObj->GetPropertyNames(context).ToLocal(&val)) |
| 161 | return std::vector<WideString>(); |
| 162 | |
| 163 | std::vector<WideString> result; |
| 164 | for (uint32_t i = 0; i < val->Length(); ++i) { |
| 165 | result.push_back(ReentrantToWideStringHelper( |
| 166 | pIsolate, val->Get(context, i).ToLocalChecked())); |
| 167 | } |
| 168 | return result; |
| 169 | } |
| 170 | |
| 171 | bool ReentrantPutObjectPropertyHelper(v8::Isolate* pIsolate, |
| 172 | v8::Local<v8::Object> pObj, |
| 173 | ByteStringView bsUTF8PropertyName, |
| 174 | v8::Local<v8::Value> pPut) { |
| 175 | ASSERT(!pPut.IsEmpty()); |
| 176 | if (pObj.IsEmpty()) |
| 177 | return false; |
| 178 | |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 179 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | c72e23f | 2020-03-19 18:58:17 +0000 | [diff] [blame] | 180 | v8::Local<v8::String> name = NewStringHelper(pIsolate, bsUTF8PropertyName); |
Tom Sepez | 2fb992d | 2020-03-20 19:45:59 +0000 | [diff] [blame] | 181 | v8::Maybe<bool> result = pObj->Set(pIsolate->GetCurrentContext(), name, pPut); |
| 182 | return result.IsJust() && result.FromJust(); |
Tom Sepez | c72e23f | 2020-03-19 18:58:17 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Tom Sepez | 1512c36 | 2020-03-19 22:13:52 +0000 | [diff] [blame] | 185 | bool ReentrantPutArrayElementHelper(v8::Isolate* pIsolate, |
| 186 | v8::Local<v8::Array> pArray, |
| 187 | unsigned index, |
| 188 | v8::Local<v8::Value> pValue) { |
| 189 | if (pArray.IsEmpty()) |
| 190 | return false; |
Tom Sepez | 2fb992d | 2020-03-20 19:45:59 +0000 | [diff] [blame] | 191 | |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 192 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | 2fb992d | 2020-03-20 19:45:59 +0000 | [diff] [blame] | 193 | v8::Maybe<bool> result = |
| 194 | pArray->Set(pIsolate->GetCurrentContext(), index, pValue); |
| 195 | return result.IsJust() && result.FromJust(); |
Tom Sepez | 1512c36 | 2020-03-19 22:13:52 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | v8::Local<v8::Value> ReentrantGetArrayElementHelper(v8::Isolate* pIsolate, |
| 199 | v8::Local<v8::Array> pArray, |
| 200 | unsigned index) { |
| 201 | if (pArray.IsEmpty()) |
| 202 | return v8::Local<v8::Value>(); |
Tom Sepez | 5f3e581 | 2020-08-11 22:46:10 +0000 | [diff] [blame] | 203 | |
| 204 | v8::TryCatch squash_exceptions(pIsolate); |
Tom Sepez | 1512c36 | 2020-03-19 22:13:52 +0000 | [diff] [blame] | 205 | v8::Local<v8::Value> val; |
| 206 | if (!pArray->Get(pIsolate->GetCurrentContext(), index).ToLocal(&val)) |
| 207 | return v8::Local<v8::Value>(); |
| 208 | return val; |
| 209 | } |
| 210 | |
| 211 | unsigned GetArrayLengthHelper(v8::Local<v8::Array> pArray) { |
| 212 | if (pArray.IsEmpty()) |
| 213 | return 0; |
| 214 | return pArray->Length(); |
| 215 | } |
| 216 | |
Tom Sepez | acc0778 | 2020-03-19 20:02:00 +0000 | [diff] [blame] | 217 | void ThrowExceptionHelper(v8::Isolate* pIsolate, ByteStringView str) { |
| 218 | pIsolate->ThrowException(NewStringHelper(pIsolate, str)); |
| 219 | } |
| 220 | |
| 221 | void ThrowExceptionHelper(v8::Isolate* pIsolate, WideStringView str) { |
| 222 | pIsolate->ThrowException(NewStringHelper(pIsolate, str)); |
| 223 | } |
| 224 | |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 225 | } // namespace fxv8 |