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 #ifndef FXJS_CJS_OBJECT_H_
8 #define FXJS_CJS_OBJECT_H_
9 
10 #include <memory>
11 
12 #include "fpdfsdk/fsdk_define.h"
13 #include "fxjs/cjs_embedobj.h"
14 #include "fxjs/cjs_runtime.h"
15 #include "fxjs/fxjs_v8.h"
16 
17 struct JSConstSpec {
18   enum Type { Number = 0, String = 1 };
19 
20   const char* pName;
21   Type eType;
22   double number;
23   const char* pStr;
24 };
25 
26 struct JSPropertySpec {
27   const char* pName;
28   v8::AccessorGetterCallback pPropGet;
29   v8::AccessorSetterCallback pPropPut;
30 };
31 
32 struct JSMethodSpec {
33   const char* pName;
34   v8::FunctionCallback pMethodCall;
35 };
36 
37 class CJS_Object {
38  public:
39   static void DefineConsts(CFXJS_Engine* pEngine,
40                            int objId,
41                            const JSConstSpec consts[],
42                            size_t count);
43   static void DefineProps(CFXJS_Engine* pEngine,
44                           int objId,
45                           const JSPropertySpec props[],
46                           size_t count);
47   static void DefineMethods(CFXJS_Engine* pEngine,
48                             int objId,
49                             const JSMethodSpec methods[],
50                             size_t count);
51 
52   explicit CJS_Object(v8::Local<v8::Object> pObject);
53   virtual ~CJS_Object();
54 
55   virtual void InitInstance(IJS_Runtime* pIRuntime);
56 
ToV8Object()57   v8::Local<v8::Object> ToV8Object() { return m_pV8Object.Get(m_pIsolate); }
58 
59   // Takes ownership of |pObj|.
SetEmbedObject(CJS_EmbedObj * pObj)60   void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj.reset(pObj); }
GetEmbedObject()61   CJS_EmbedObj* GetEmbedObject() const { return m_pEmbedObj.get(); }
62 
GetIsolate()63   v8::Isolate* GetIsolate() const { return m_pIsolate; }
64 
65  protected:
66   std::unique_ptr<CJS_EmbedObj> m_pEmbedObj;
67   v8::Global<v8::Object> m_pV8Object;
68   v8::Isolate* m_pIsolate;
69 };
70 
71 #endif  // FXJS_CJS_OBJECT_H_
72