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.h
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#ifndef FXJS_JS_DEFINE_H_
8#define FXJS_JS_DEFINE_H_
9
10#include <memory>
11
12#include "core/fxcrt/unowned_ptr.h"
13#include "fxjs/cfxjs_engine.h"
14#include "fxjs/cjs_result.h"
15#include "fxjs/cjs_runtime.h"
16#include "fxjs/js_resources.h"
17#include "third_party/base/containers/span.h"
18#include "v8/include/v8-local-handle.h"
19
20class CJS_Object;
21
22double JS_DateParse(v8::Isolate* pIsolate, const WideString& str);
23
24// Some JS methods have the bizarre convention that they may also be called
25// with a single argument which is an object containing the actual arguments
26// as its properties. The varying arguments to this method are the property
27// names as wchar_t string literals corresponding to each positional argument.
28// The result will always contain |nKeywords| value, check for the unspecified
29// ones in the result using IsExpandedParamKnown() below.
31 CJS_Runtime* pRuntime,
34 ...);
35
36bool IsExpandedParamKnown(v8::Local<v8::Value> value);
37
38// All JS classes have a name, an object defintion ID, and the ability to
39// register themselves with FXJS_V8. We never make a BASE class on its own
40// because it can't really do anything.
41
42// Rich JS classes provide constants, methods, properties, and the ability
43// to construct native object state.
44
45template <class T>
46static void JSConstructor(CFXJS_Engine* pEngine,
47 v8::Local<v8::Object> obj,
48 v8::Local<v8::Object> proxy) {
49 pEngine->SetObjectPrivate(
50 obj, std::make_unique<T>(proxy, static_cast<CJS_Runtime*>(pEngine)));
51}
52
53// CJS_Object has virtual dtor, template not required.
54void JSDestructor(v8::Local<v8::Object> obj);
55
56template <class C>
57UnownedPtr<C> JSGetObject(v8::Isolate* isolate, v8::Local<v8::Object> obj) {
58 if (CFXJS_Engine::GetObjDefnID(obj) != C::GetObjDefnID())
59 return nullptr;
60
61 CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(isolate, obj);
62 if (!pJSObj)
63 return nullptr;
64
65 return UnownedPtr<C>(static_cast<C*>(pJSObj));
66}
67
68template <class C, CJS_Result (C::*M)(CJS_Runtime*)>
69void JSPropGetter(const char* prop_name_string,
70 const char* class_name_string,
71 v8::Local<v8::String> property,
72 const v8::PropertyCallbackInfo<v8::Value>& info) {
73 auto pObj = JSGetObject<C>(info.GetIsolate(), info.Holder());
74 if (!pObj)
75 return;
76
77 CJS_Runtime* pRuntime = pObj->GetRuntime();
78 if (!pRuntime)
79 return;
80
81 CJS_Result result = (pObj.get()->*M)(pRuntime);
82 if (result.HasError()) {
83 pRuntime->Error(JSFormatErrorString(class_name_string, prop_name_string,
84 result.Error()));
85 return;
86 }
87
88 if (result.HasReturn())
89 info.GetReturnValue().Set(result.Return());
90}
91
92template <class C, CJS_Result (C::*M)(CJS_Runtime*, v8::Local<v8::Value>)>
93void JSPropSetter(const char* prop_name_string,
94 const char* class_name_string,
95 v8::Local<v8::String> property,
96 v8::Local<v8::Value> value,
97 const v8::PropertyCallbackInfo<void>& info) {
98 auto pObj = JSGetObject<C>(info.GetIsolate(), info.Holder());
99 if (!pObj)
100 return;
101
102 CJS_Runtime* pRuntime = pObj->GetRuntime();
103 if (!pRuntime)
104 return;
105
106 CJS_Result result = (pObj.get()->*M)(pRuntime, value);
107 if (result.HasError()) {
108 pRuntime->Error(JSFormatErrorString(class_name_string, prop_name_string,
109 result.Error()));
110 }
111}
112
113template <class C,
114 CJS_Result (C::*M)(CJS_Runtime*, pdfium::span<v8::Local<v8::Value>>)>
115void JSMethod(const char* method_name_string,
116 const char* class_name_string,
117 const v8::FunctionCallbackInfo<v8::Value>& info) {
118 auto pObj = JSGetObject<C>(info.GetIsolate(), info.Holder());
119 if (!pObj)
120 return;
121
122 CJS_Runtime* pRuntime = pObj->GetRuntime();
123 if (!pRuntime)
124 return;
125
126 v8::LocalVector<v8::Value> parameters(info.GetIsolate());
127 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++)
128 parameters.push_back(info[i]);
129
130 CJS_Result result = (pObj.get()->*M)(pRuntime, parameters);
131 if (result.HasError()) {
132 pRuntime->Error(JSFormatErrorString(class_name_string, method_name_string,
133 result.Error()));
134 return;
135 }
136
137 if (result.HasReturn())
138 info.GetReturnValue().Set(result.Return());
139}
140
141#define JS_STATIC_PROP(err_name, prop_name, class_name)
142 static void get_##prop_name##_static(
143 v8::Local<v8::String> property,
144 const v8::PropertyCallbackInfo<v8::Value>& info) {
145 JSPropGetter<class_name, &class_name::get_##prop_name>(
146 #err_name, class_name::kName, property, info);
147 }
148 static void set_##prop_name##_static(
149 v8::Local<v8::String> property, v8::Local<v8::Value> value,
150 const v8::PropertyCallbackInfo<void>& info) {
151 JSPropSetter<class_name, &class_name::set_##prop_name>(
152 #err_name, class_name::kName, property, value, info);
153 }
154
155#define JS_STATIC_METHOD(method_name, class_name)
156 static void method_name##_static(
157 const v8::FunctionCallbackInfo<v8::Value>& info) {
158 JSMethod<class_name, &class_name::method_name>(#method_name,
159 class_name::kName, info);
160 }
161
162#endif // FXJS_JS_DEFINE_H_
@ FXJSOBJTYPE_DYNAMIC
void Error(const WideString &message)
static uint32_t GetObjDefnID()
Definition cjs_annot.cpp:25
~CJS_Annot() override
static void DefineJSObjects(CFXJS_Engine *pEngine)
Definition cjs_annot.cpp:30
void SetSDKAnnot(CPDFSDK_BAAnnot *annot)
Definition cjs_annot.cpp:41
CJS_Annot(v8::Local< v8::Object > pObject, CJS_Runtime *pRuntime)
Definition cjs_annot.cpp:36
static void DefineProps(CFXJS_Engine *pEngine, uint32_t nObjDefnID, pdfium::span< const JSPropertySpec > consts)
bool HasReturn() const
Definition cjs_result.h:44
static CJS_Result Success()
Definition cjs_result.h:27
bool HasError() const
Definition cjs_result.h:41
static CJS_Result Failure(JSMessage id)
Definition cjs_result.h:34
const WideString & Error() const
Definition cjs_result.h:42
WideString GetAnnotName() const
void SetAnnotName(const WideString &sName)
CPDF_Annot::Subtype GetAnnotSubtype() const override
void SetFlags(uint32_t nFlags)
uint32_t GetFlags() const
static ByteString AnnotSubtypeToString(Subtype nSubtype)
bool IsHidden() const
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
void JSPropGetter(const char *prop_name_string, const char *class_name_string, v8::Local< v8::String > property, const v8::PropertyCallbackInfo< v8::Value > &info)
Definition js_define.h:69
void JSPropSetter(const char *prop_name_string, const char *class_name_string, v8::Local< v8::String > property, v8::Local< v8::Value > value, const v8::PropertyCallbackInfo< void > &info)
Definition js_define.h:93
void JSMethod(const char *method_name_string, const char *class_name_string, const v8::FunctionCallbackInfo< v8::Value > &info)
Definition js_define.h:115
UnownedPtr< C > JSGetObject(v8::Isolate *isolate, v8::Local< v8::Object > obj)
Definition js_define.h:57
double JS_DateParse(v8::Isolate *pIsolate, const WideString &str)
Definition js_define.cpp:31
#define JS_STATIC_PROP(err_name, prop_name, class_name)
Definition js_define.h:141
static void JSConstructor(CFXJS_Engine *pEngine, v8::Local< v8::Object > obj, v8::Local< v8::Object > proxy)
Definition js_define.h:46
JSMessage
WideString JSFormatErrorString(const char *class_name, const char *property_name, const WideString &details)
constexpr uint32_t kNoView
constexpr uint32_t kHidden
constexpr uint32_t kPrint
constexpr uint32_t kInvisible