Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
js_define.cpp
Go to the documentation of this file.
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 "third_party/base/containers/span.h"
23#include "v8/include/v8-context.h"
24#include "v8/include/v8-function.h"
25#include "v8/include/v8-isolate.h"
26
27void JSDestructor(v8::Local<v8::Object> obj) {
28 CFXJS_Engine::SetObjectPrivate(obj, nullptr);
29}
30
31double JS_DateParse(v8::Isolate* pIsolate, const WideString& str) {
32 v8::Isolate::Scope isolate_scope(pIsolate);
33 v8::HandleScope scope(pIsolate);
34
35 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
36
37 // Use the built-in object method.
38 v8::MaybeLocal<v8::Value> maybe_value =
39 context->Global()->Get(context, fxv8::NewStringHelper(pIsolate, "Date"));
40
41 v8::Local<v8::Value> value;
42 if (!maybe_value.ToLocal(&value) || !value->IsObject())
43 return 0;
44
45 v8::Local<v8::Object> obj = value.As<v8::Object>();
46 maybe_value = obj->Get(context, fxv8::NewStringHelper(pIsolate, "parse"));
47 if (!maybe_value.ToLocal(&value) || !value->IsFunction())
48 return 0;
49
50 v8::Local<v8::Function> func = value.As<v8::Function>();
51 static constexpr int argc = 1;
52 v8::Local<v8::Value> argv[argc] = {
53 fxv8::NewStringHelper(pIsolate, str.AsStringView()),
54 };
55 maybe_value = func->Call(context, context->Global(), argc, argv);
56 if (!maybe_value.ToLocal(&value) || !value->IsNumber())
57 return 0;
58
59 double date = value.As<v8::Number>()->Value();
60 return isfinite(date) ? FX_LocalTime(date) : date;
61}
62
64 CJS_Runtime* pRuntime,
67 ...) {
68 DCHECK(nKeywords);
69
70 v8::LocalVector<v8::Value> result(pRuntime->GetIsolate(), nKeywords);
71 size_t size = std::min(originals.size(), nKeywords);
72 for (size_t i = 0; i < size; ++i)
73 result[i] = originals[i];
74
75 if (originals.size() != 1 || !originals[0]->IsObject() ||
76 originals[0]->IsArray()) {
77 return result;
78 }
79 result[0] = v8::Local<v8::Value>(); // Make unknown.
80
81 v8::Local<v8::Object> pObj = pRuntime->ToObject(originals[0]);
82 va_list ap;
83 va_start(ap, nKeywords);
84 for (size_t i = 0; i < nKeywords; ++i) {
85 const char* property = va_arg(ap, const char*);
86 v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property);
87 if (!v8Value->IsUndefined())
88 result[i] = v8Value;
89 }
90 va_end(ap);
91
92 return result;
93}
94
95bool IsExpandedParamKnown(v8::Local<v8::Value> value) {
96 return !value.IsEmpty() &&
97 (value->IsString() || value->IsNumber() || value->IsBoolean() ||
98 value->IsDate() || value->IsObject() || value->IsNull() ||
99 value->IsUndefined());
100}
bool IsExpandedParamKnown(v8::Local< v8::Value > value)
Definition js_define.cpp:95
void JSDestructor(v8::Local< v8::Object > obj)
Definition js_define.cpp:27
v8::LocalVector< v8::Value > ExpandKeywordParams(CJS_Runtime *pRuntime, pdfium::span< v8::Local< v8::Value > > originals, size_t nKeywords,...)
Definition js_define.cpp:63
double JS_DateParse(v8::Isolate *pIsolate, const WideString &str)
Definition js_define.cpp:31
double FX_LocalTime(double d)