John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | 221f0b3 | 2018-06-04 22:11:27 +0000 | [diff] [blame] | 7 | #include "fxjs/js_define.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 8 | |
Lei Zhang | 15fef79 | 2021-08-04 17:52:39 +0000 | [diff] [blame] | 9 | #include <math.h> |
Lei Zhang | 168315c | 2021-06-30 01:23:36 +0000 | [diff] [blame] | 10 | #include <stdarg.h> |
| 11 | |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 12 | #include <algorithm> |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 13 | #include <limits> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 14 | #include <vector> |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 15 | |
Ryan Harrison | f36a464 | 2018-08-10 19:03:47 +0000 | [diff] [blame] | 16 | #include "core/fxcrt/fx_extension.h" |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 17 | #include "fxjs/cjs_document.h" |
| 18 | #include "fxjs/cjs_object.h" |
Tom Sepez | 86e5fbf | 2018-11-01 21:21:52 +0000 | [diff] [blame] | 19 | #include "fxjs/fx_date_helpers.h" |
Tom Sepez | 3466e27 | 2020-03-18 23:33:35 +0000 | [diff] [blame] | 20 | #include "fxjs/fxv8.h" |
Tom Sepez | 08b23fc | 2021-01-27 22:23:27 +0000 | [diff] [blame] | 21 | #include "third_party/base/check.h" |
Dan Elphick | 05673a3 | 2021-09-09 15:42:55 +0000 | [diff] [blame] | 22 | #include "v8/include/v8-context.h" |
| 23 | #include "v8/include/v8-function.h" |
| 24 | #include "v8/include/v8-isolate.h" |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 25 | |
Tom Sepez | b3f1046 | 2018-02-08 20:16:09 +0000 | [diff] [blame] | 26 | void JSDestructor(v8::Local<v8::Object> obj) { |
| 27 | CFXJS_Engine::SetObjectPrivate(obj, nullptr); |
Tom Sepez | 57e0977 | 2018-02-05 18:52:09 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Daniel Hosseinian | 4aa51a5 | 2021-10-22 17:32:43 +0000 | [diff] [blame] | 30 | double JS_DateParse(v8::Isolate* pIsolate, const WideString& str) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 31 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 32 | v8::HandleScope scope(pIsolate); |
| 33 | |
| 34 | v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 35 | |
| 36 | // Use the built-in object method. |
Tom Sepez | 2d61ff8 | 2020-10-28 20:51:59 +0000 | [diff] [blame] | 37 | v8::MaybeLocal<v8::Value> maybe_value = |
| 38 | context->Global()->Get(context, fxv8::NewStringHelper(pIsolate, "Date")); |
| 39 | |
| 40 | v8::Local<v8::Value> value; |
| 41 | if (!maybe_value.ToLocal(&value) || !value->IsObject()) |
| 42 | return 0; |
| 43 | |
| 44 | v8::Local<v8::Object> obj = value.As<v8::Object>(); |
| 45 | maybe_value = obj->Get(context, fxv8::NewStringHelper(pIsolate, "parse")); |
| 46 | if (!maybe_value.ToLocal(&value) || !value->IsFunction()) |
| 47 | return 0; |
| 48 | |
| 49 | v8::Local<v8::Function> func = value.As<v8::Function>(); |
| 50 | static constexpr int argc = 1; |
| 51 | v8::Local<v8::Value> argv[argc] = { |
| 52 | fxv8::NewStringHelper(pIsolate, str.AsStringView()), |
| 53 | }; |
| 54 | maybe_value = func->Call(context, context->Global(), argc, argv); |
| 55 | if (!maybe_value.ToLocal(&value) || !value->IsNumber()) |
| 56 | return 0; |
| 57 | |
| 58 | double date = value.As<v8::Number>()->Value(); |
Lei Zhang | 15fef79 | 2021-08-04 17:52:39 +0000 | [diff] [blame] | 59 | return isfinite(date) ? FX_LocalTime(date) : date; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 60 | } |
| 61 | |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 62 | std::vector<v8::Local<v8::Value>> ExpandKeywordParams( |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 63 | CJS_Runtime* pRuntime, |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 64 | const std::vector<v8::Local<v8::Value>>& originals, |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 65 | size_t nKeywords, |
| 66 | ...) { |
Tom Sepez | 08b23fc | 2021-01-27 22:23:27 +0000 | [diff] [blame] | 67 | DCHECK(nKeywords); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 68 | |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 69 | std::vector<v8::Local<v8::Value>> result(nKeywords, v8::Local<v8::Value>()); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 70 | size_t size = std::min(originals.size(), nKeywords); |
| 71 | for (size_t i = 0; i < size; ++i) |
| 72 | result[i] = originals[i]; |
| 73 | |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 74 | if (originals.size() != 1 || !originals[0]->IsObject() || |
| 75 | originals[0]->IsArray()) { |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 76 | return result; |
| 77 | } |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 78 | result[0] = v8::Local<v8::Value>(); // Make unknown. |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 79 | |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 80 | v8::Local<v8::Object> pObj = pRuntime->ToObject(originals[0]); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 81 | va_list ap; |
| 82 | va_start(ap, nKeywords); |
Wei Li | 8940993 | 2016-03-28 10:33:33 -0700 | [diff] [blame] | 83 | for (size_t i = 0; i < nKeywords; ++i) { |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 84 | const char* property = va_arg(ap, const char*); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 85 | v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 86 | if (!v8Value->IsUndefined()) |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 87 | result[i] = v8Value; |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 88 | } |
| 89 | va_end(ap); |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 90 | |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 91 | return result; |
| 92 | } |
Tom Sepez | 0e5bab1 | 2018-10-18 21:39:40 +0000 | [diff] [blame] | 93 | |
| 94 | bool IsExpandedParamKnown(v8::Local<v8::Value> value) { |
| 95 | return !value.IsEmpty() && |
| 96 | (value->IsString() || value->IsNumber() || value->IsBoolean() || |
| 97 | value->IsDate() || value->IsObject() || value->IsNull() || |
| 98 | value->IsUndefined()); |
| 99 | } |