1 // Copyright 2017 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_CFX_V8_H_
8 #define FXJS_CFX_V8_H_
9 
10 #include <vector>
11 
12 #include "core/fxcrt/fx_string.h"
13 #include "core/fxcrt/unowned_ptr.h"
14 #include "v8/include/v8.h"
15 
16 class CFX_V8 {
17  public:
18   explicit CFX_V8(v8::Isolate* pIsolate);
19   virtual ~CFX_V8();
20 
GetIsolate()21   v8::Isolate* GetIsolate() const { return m_pIsolate.Get(); }
22 
23   v8::Local<v8::Value> NewNull();
24   v8::Local<v8::Value> NewUndefined();
25   v8::Local<v8::Array> NewArray();
26   v8::Local<v8::Object> NewObject();
27   v8::Local<v8::Number> NewNumber(int number);
28   v8::Local<v8::Number> NewNumber(double number);
29   v8::Local<v8::Number> NewNumber(float number);
30   v8::Local<v8::Boolean> NewBoolean(bool b);
31   v8::Local<v8::String> NewString(ByteStringView str);
32   v8::Local<v8::String> NewString(WideStringView str);
33   v8::Local<v8::Date> NewDate(double d);
34 
35   int ToInt32(v8::Local<v8::Value> pValue);
36   bool ToBoolean(v8::Local<v8::Value> pValue);
37   double ToDouble(v8::Local<v8::Value> pValue);
38   WideString ToWideString(v8::Local<v8::Value> pValue);
39   ByteString ToByteString(v8::Local<v8::Value> pValue);
40   v8::Local<v8::Object> ToObject(v8::Local<v8::Value> pValue);
41   v8::Local<v8::Array> ToArray(v8::Local<v8::Value> pValue);
42 
43   // Arrays.
44   unsigned GetArrayLength(v8::Local<v8::Array> pArray);
45   v8::Local<v8::Value> GetArrayElement(v8::Local<v8::Array> pArray,
46                                        unsigned index);
47   bool PutArrayElement(v8::Local<v8::Array> pArray,
48                        unsigned index,
49                        v8::Local<v8::Value> pValue);
50 
51   // Objects.
52   std::vector<WideString> GetObjectPropertyNames(v8::Local<v8::Object> pObj);
53   v8::Local<v8::Value> GetObjectProperty(v8::Local<v8::Object> pObj,
54                                          ByteStringView bsUTF8PropertyName);
55   bool PutObjectProperty(v8::Local<v8::Object> pObj,
56                          ByteStringView bsUTF8PropertyName,
57                          v8::Local<v8::Value> pValue);
58 
59  protected:
SetIsolate(v8::Isolate * pIsolate)60   void SetIsolate(v8::Isolate* pIsolate) { m_pIsolate = pIsolate; }
61   void DisposeIsolate();
62 
63  private:
64   UnownedPtr<v8::Isolate> m_pIsolate;
65 };
66 
67 class CFX_V8ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator {
68   static const size_t kMaxAllowedBytes = 0x10000000;
69   void* Allocate(size_t length) override;
70   void* AllocateUninitialized(size_t length) override;
71   void Free(void* data, size_t length) override;
72 };
73 
74 // Use with std::unique_ptr<v8::Isolate> to dispose of isolates correctly.
75 struct CFX_V8IsolateDeleter {
operatorCFX_V8IsolateDeleter76   inline void operator()(v8::Isolate* ptr) { ptr->Dispose(); }
77 };
78 
79 #endif  // FXJS_CFX_V8_H_
80