• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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/js_define.h"
8 
9 #include <math.h>
10 #include <stdarg.h>
11 
12 #include <algorithm>
13 #include <limits>
14 #include <vector>
15 
16 #include "core/fxcrt/fx_extension.h"
17 #include "fxjs/cjs_document.h"
18 #include "fxjs/cjs_object.h"
19 #include "fxjs/fx_date_helpers.h"
20 #include "fxjs/fxv8.h"
21 #include "third_party/base/check.h"
22 #include "v8/include/v8-context.h"
23 #include "v8/include/v8-function.h"
24 #include "v8/include/v8-isolate.h"
25 
JSDestructor(v8::Local<v8::Object> obj)26 void JSDestructor(v8::Local<v8::Object> obj) {
27   CFXJS_Engine::SetObjectPrivate(obj, nullptr);
28 }
29 
JS_DateParse(v8::Isolate * pIsolate,const WideString & str)30 double JS_DateParse(v8::Isolate* pIsolate, const WideString& str) {
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.
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();
59   return isfinite(date) ? FX_LocalTime(date) : date;
60 }
61 
ExpandKeywordParams(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & originals,size_t nKeywords,...)62 std::vector<v8::Local<v8::Value>> ExpandKeywordParams(
63     CJS_Runtime* pRuntime,
64     const std::vector<v8::Local<v8::Value>>& originals,
65     size_t nKeywords,
66     ...) {
67   DCHECK(nKeywords);
68 
69   std::vector<v8::Local<v8::Value>> result(nKeywords, v8::Local<v8::Value>());
70   size_t size = std::min(originals.size(), nKeywords);
71   for (size_t i = 0; i < size; ++i)
72     result[i] = originals[i];
73 
74   if (originals.size() != 1 || !originals[0]->IsObject() ||
75       originals[0]->IsArray()) {
76     return result;
77   }
78   result[0] = v8::Local<v8::Value>();  // Make unknown.
79 
80   v8::Local<v8::Object> pObj = pRuntime->ToObject(originals[0]);
81   va_list ap;
82   va_start(ap, nKeywords);
83   for (size_t i = 0; i < nKeywords; ++i) {
84     const char* property = va_arg(ap, const char*);
85     v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property);
86     if (!v8Value->IsUndefined())
87       result[i] = v8Value;
88   }
89   va_end(ap);
90 
91   return result;
92 }
93 
IsExpandedParamKnown(v8::Local<v8::Value> value)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 }
100