xref: /aosp_15_r20/external/pdfium/fxjs/cjs_object.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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_CJS_OBJECT_H_
8 #define FXJS_CJS_OBJECT_H_
9 
10 #include "core/fxcrt/unowned_ptr.h"
11 #include "fxjs/cjs_runtime.h"
12 #include "third_party/base/containers/span.h"
13 
14 class CFXJS_Engine;
15 
16 struct JSConstSpec {
17   enum Type { Number = 0, String = 1 };
18 
19   const char* pName;
20   Type eType;
21   double number;
22   const char* pStr;
23 };
24 
25 struct JSPropertySpec {
26   const char* pName;
27   v8::AccessorGetterCallback pPropGet;
28   v8::AccessorSetterCallback pPropPut;
29 };
30 
31 struct JSMethodSpec {
32   const char* pName;
33   v8::FunctionCallback pMethodCall;
34 };
35 
36 class CJS_Object {
37  public:
38   static void DefineConsts(CFXJS_Engine* pEngine,
39                            uint32_t nObjDefnID,
40                            pdfium::span<const JSConstSpec> consts);
41   static void DefineProps(CFXJS_Engine* pEngine,
42                           uint32_t nObjDefnID,
43                           pdfium::span<const JSPropertySpec> consts);
44   static void DefineMethods(CFXJS_Engine* pEngine,
45                             uint32_t nObjDefnID,
46                             pdfium::span<const JSMethodSpec> consts);
47 
48   CJS_Object(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime);
49   virtual ~CJS_Object();
50 
ToV8Object()51   v8::Local<v8::Object> ToV8Object() {
52     return m_pV8Object.Get(GetRuntime()->GetIsolate());
53   }
GetRuntime()54   CJS_Runtime* GetRuntime() const { return m_pRuntime.Get(); }
55 
56  private:
57   v8::Global<v8::Object> m_pV8Object;
58   ObservedPtr<CJS_Runtime> m_pRuntime;
59 };
60 
61 #endif  // FXJS_CJS_OBJECT_H_
62