• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "fxjs/js_define.h"
8 
9 #include <algorithm>
10 #include <cmath>
11 #include <limits>
12 #include <vector>
13 
14 #include "core/fxcrt/fx_extension.h"
15 #include "fxjs/cjs_document.h"
16 #include "fxjs/cjs_object.h"
17 #include "fxjs/fx_date_helpers.h"
18 
JSDestructor(v8::Local<v8::Object> obj)19 void JSDestructor(v8::Local<v8::Object> obj) {
20   CFXJS_Engine::SetObjectPrivate(obj, nullptr);
21 }
22 
JS_DateParse(const WideString & str)23 double JS_DateParse(const WideString& str) {
24   v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
25   v8::Isolate::Scope isolate_scope(pIsolate);
26   v8::HandleScope scope(pIsolate);
27 
28   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
29 
30   // Use the built-in object method.
31   v8::Local<v8::Value> v =
32       context->Global()
33           ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date",
34                                                  v8::NewStringType::kNormal)
35                              .ToLocalChecked())
36           .ToLocalChecked();
37   if (v->IsObject()) {
38     v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked();
39     v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse",
40                                                 v8::NewStringType::kNormal)
41                             .ToLocalChecked())
42             .ToLocalChecked();
43     if (v->IsFunction()) {
44       v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
45       const int argc = 1;
46       v8::Local<v8::Value> timeStr =
47           v8::String::NewFromUtf8(pIsolate,
48                                   FX_UTF8Encode(str.AsStringView()).c_str(),
49                                   v8::NewStringType::kNormal)
50               .ToLocalChecked();
51       v8::Local<v8::Value> argv[argc] = {timeStr};
52       v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked();
53       if (v->IsNumber()) {
54         double date = v->ToNumber(context).ToLocalChecked()->Value();
55         if (!std::isfinite(date))
56           return date;
57         return FX_LocalTime(date);
58       }
59     }
60   }
61   return 0;
62 }
63 
ExpandKeywordParams(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & originals,size_t nKeywords,...)64 std::vector<v8::Local<v8::Value>> ExpandKeywordParams(
65     CJS_Runtime* pRuntime,
66     const std::vector<v8::Local<v8::Value>>& originals,
67     size_t nKeywords,
68     ...) {
69   ASSERT(nKeywords);
70 
71   std::vector<v8::Local<v8::Value>> result(nKeywords, v8::Local<v8::Value>());
72   size_t size = std::min(originals.size(), nKeywords);
73   for (size_t i = 0; i < size; ++i)
74     result[i] = originals[i];
75 
76   if (originals.size() != 1 || !originals[0]->IsObject() ||
77       originals[0]->IsArray()) {
78     return result;
79   }
80   result[0] = v8::Local<v8::Value>();  // Make unknown.
81 
82   v8::Local<v8::Object> pObj = pRuntime->ToObject(originals[0]);
83   va_list ap;
84   va_start(ap, nKeywords);
85   for (size_t i = 0; i < nKeywords; ++i) {
86     const char* property = va_arg(ap, const char*);
87     v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property);
88     if (!v8Value->IsUndefined())
89       result[i] = v8Value;
90   }
91   va_end(ap);
92 
93   return result;
94 }
95 
IsExpandedParamKnown(v8::Local<v8::Value> value)96 bool IsExpandedParamKnown(v8::Local<v8::Value> value) {
97   return !value.IsEmpty() &&
98          (value->IsString() || value->IsNumber() || value->IsBoolean() ||
99           value->IsDate() || value->IsObject() || value->IsNull() ||
100           value->IsUndefined());
101 }
102