blob: 4a5044230e185792a66059e474bb857bf84b09e1 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez221f0b32018-06-04 22:11:27 +00007#include "fxjs/js_define.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Lei Zhang15fef792021-08-04 17:52:39 +00009#include <math.h>
Lei Zhang168315c2021-06-30 01:23:36 +000010#include <stdarg.h>
11
Tom Sepezbd932572016-01-29 09:10:41 -080012#include <algorithm>
Tom Sepez39bfe122015-09-17 15:25:23 -070013#include <limits>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050014#include <vector>
Tom Sepez39bfe122015-09-17 15:25:23 -070015
Ryan Harrisonf36a4642018-08-10 19:03:47 +000016#include "core/fxcrt/fx_extension.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000017#include "fxjs/cjs_document.h"
18#include "fxjs/cjs_object.h"
Tom Sepez86e5fbf2018-11-01 21:21:52 +000019#include "fxjs/fx_date_helpers.h"
Tom Sepez3466e272020-03-18 23:33:35 +000020#include "fxjs/fxv8.h"
Tom Sepez08b23fc2021-01-27 22:23:27 +000021#include "third_party/base/check.h"
Dan Elphick05673a32021-09-09 15:42:55 +000022#include "v8/include/v8-context.h"
23#include "v8/include/v8-function.h"
24#include "v8/include/v8-isolate.h"
tsepezfbf52c22016-07-25 11:17:07 -070025
Tom Sepezb3f10462018-02-08 20:16:09 +000026void JSDestructor(v8::Local<v8::Object> obj) {
27 CFXJS_Engine::SetObjectPrivate(obj, nullptr);
Tom Sepez57e09772018-02-05 18:52:09 +000028}
29
Daniel Hosseinian4aa51a52021-10-22 17:32:43 +000030double JS_DateParse(v8::Isolate* pIsolate, const WideString& str) {
Tom Sepez39bfe122015-09-17 15:25:23 -070031 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 Sepez2d61ff82020-10-28 20:51:59 +000037 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 Zhang15fef792021-08-04 17:52:39 +000059 return isfinite(date) ? FX_LocalTime(date) : date;
Tom Sepez39bfe122015-09-17 15:25:23 -070060}
61
dan sinclair80435cb2017-10-24 21:40:24 -040062std::vector<v8::Local<v8::Value>> ExpandKeywordParams(
Tom Sepezbd932572016-01-29 09:10:41 -080063 CJS_Runtime* pRuntime,
dan sinclair80435cb2017-10-24 21:40:24 -040064 const std::vector<v8::Local<v8::Value>>& originals,
Tom Sepezbd932572016-01-29 09:10:41 -080065 size_t nKeywords,
66 ...) {
Tom Sepez08b23fc2021-01-27 22:23:27 +000067 DCHECK(nKeywords);
Tom Sepezbd932572016-01-29 09:10:41 -080068
dan sinclair80435cb2017-10-24 21:40:24 -040069 std::vector<v8::Local<v8::Value>> result(nKeywords, v8::Local<v8::Value>());
Tom Sepezbd932572016-01-29 09:10:41 -080070 size_t size = std::min(originals.size(), nKeywords);
71 for (size_t i = 0; i < size; ++i)
72 result[i] = originals[i];
73
dan sinclair80435cb2017-10-24 21:40:24 -040074 if (originals.size() != 1 || !originals[0]->IsObject() ||
75 originals[0]->IsArray()) {
Tom Sepezbd932572016-01-29 09:10:41 -080076 return result;
77 }
dan sinclair80435cb2017-10-24 21:40:24 -040078 result[0] = v8::Local<v8::Value>(); // Make unknown.
Tom Sepezbd932572016-01-29 09:10:41 -080079
dan sinclair80435cb2017-10-24 21:40:24 -040080 v8::Local<v8::Object> pObj = pRuntime->ToObject(originals[0]);
Tom Sepezbd932572016-01-29 09:10:41 -080081 va_list ap;
82 va_start(ap, nKeywords);
Wei Li89409932016-03-28 10:33:33 -070083 for (size_t i = 0; i < nKeywords; ++i) {
Tom Sepezca610782018-12-13 18:01:04 +000084 const char* property = va_arg(ap, const char*);
tsepezb4694242016-08-15 16:44:55 -070085 v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property);
Tom Sepezbd932572016-01-29 09:10:41 -080086 if (!v8Value->IsUndefined())
dan sinclair80435cb2017-10-24 21:40:24 -040087 result[i] = v8Value;
Tom Sepezbd932572016-01-29 09:10:41 -080088 }
89 va_end(ap);
dan sinclair80435cb2017-10-24 21:40:24 -040090
Tom Sepezbd932572016-01-29 09:10:41 -080091 return result;
92}
Tom Sepez0e5bab12018-10-18 21:39:40 +000093
94bool 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}