From b3f2467e065fdb36ae7c46e0a3e0b2902c8eb7f8 Mon Sep 17 00:00:00 2001 From: dsinclair Date: Tue, 12 Jul 2016 10:42:14 -0700 Subject: Move fpdfsdk/jsapi into fxjs/ This CL moves the fpdfsdk/sjapi code info fxjs/. The "fxjs" library is moved from being XFA specific to being compiled if V8 is enabled. The fxjs_v8 files are required when building for XFA (they have XFA defines in them) and are used in CFXJS_RuntimeData. The cfxjse_* files are only added if XFA is also enabled. Review-Url: https://codereview.chromium.org/2144603003 --- fxjs/include/fxjs_v8.h | 290 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 fxjs/include/fxjs_v8.h (limited to 'fxjs/include') diff --git a/fxjs/include/fxjs_v8.h b/fxjs/include/fxjs_v8.h new file mode 100644 index 0000000000..465feef5e8 --- /dev/null +++ b/fxjs/include/fxjs_v8.h @@ -0,0 +1,290 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +// FXJS_V8 is a layer that makes it easier to define native objects in V8, but +// has no knowledge of PDF-specific native objects. It could in theory be used +// to implement other sets of native objects. + +// PDFium code should include this file rather than including V8 headers +// directly. + +#ifndef FXJS_INCLUDE_FXJS_V8_H_ +#define FXJS_INCLUDE_FXJS_V8_H_ + +#include +#include + +#include + +#include "core/fxcrt/include/fx_string.h" + +class CFXJS_ObjDefinition; + +// FXJS_V8 places no restrictions on these two classes; it merely passes them +// on to caller-provided methods. +class IJS_Context; // A description of the event that caused JS execution. +class IJS_Runtime; // A native runtime, typically owns the v8::Context. + +#ifdef PDF_ENABLE_XFA +// FXJS_V8 places no interpreation on this calass; it merely passes it +// along to XFA. +class CFXJSE_RuntimeData; +#endif // PDF_ENABLE_XFA + +enum FXJSOBJTYPE { + FXJSOBJTYPE_DYNAMIC = 0, // Created by native method and returned to JS. + FXJSOBJTYPE_STATIC, // Created by init and hung off of global object. + FXJSOBJTYPE_GLOBAL, // The global object itself (may only appear once). +}; + +struct FXJSErr { + const wchar_t* message; + const wchar_t* srcline; + unsigned linnum; +}; + +// Global weak map to save dynamic objects. +class V8TemplateMapTraits : public v8::StdMapTraits { + public: + typedef v8::GlobalValueMap MapType; + typedef void WeakCallbackDataType; + + static WeakCallbackDataType* WeakCallbackParameter( + MapType* map, + void* key, + const v8::Local& value) { + return key; + } + static MapType* MapFromWeakCallbackInfo( + const v8::WeakCallbackInfo&); + + static void* KeyFromWeakCallbackInfo( + const v8::WeakCallbackInfo& data) { + return data.GetParameter(); + } + static const v8::PersistentContainerCallbackType kCallbackType = + v8::kWeakWithInternalFields; + static void DisposeWeak( + const v8::WeakCallbackInfo& data) {} + static void OnWeakCallback( + const v8::WeakCallbackInfo& data) {} + static void Dispose(v8::Isolate* isolate, + v8::Global value, + void* key); + static void DisposeCallbackData(WeakCallbackDataType* callbackData) {} +}; + +class V8TemplateMap { + public: + typedef v8::GlobalValueMap MapType; + + explicit V8TemplateMap(v8::Isolate* isolate); + ~V8TemplateMap(); + + void set(void* key, v8::Local handle); + + friend class V8TemplateMapTraits; + + private: + MapType m_map; +}; + +class FXJS_PerIsolateData { + public: + ~FXJS_PerIsolateData(); + + static void SetUp(v8::Isolate* pIsolate); + static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate); + + void CreateDynamicObjsMap(v8::Isolate* pIsolate) { + if (!m_pDynamicObjsMap) + m_pDynamicObjsMap = new V8TemplateMap(pIsolate); + } + void ReleaseDynamicObjsMap() { + delete m_pDynamicObjsMap; + m_pDynamicObjsMap = nullptr; + } + + std::vector m_ObjectDefnArray; +#ifdef PDF_ENABLE_XFA + CFXJSE_RuntimeData* m_pFXJSERuntimeData; +#endif // PDF_ENABLE_XFA + V8TemplateMap* m_pDynamicObjsMap; + + protected: + FXJS_PerIsolateData(); +}; + +extern const wchar_t kFXJSValueNameString[]; +extern const wchar_t kFXJSValueNameNumber[]; +extern const wchar_t kFXJSValueNameBoolean[]; +extern const wchar_t kFXJSValueNameDate[]; +extern const wchar_t kFXJSValueNameObject[]; +extern const wchar_t kFXJSValueNameFxobj[]; +extern const wchar_t kFXJSValueNameNull[]; +extern const wchar_t kFXJSValueNameUndefined[]; + +class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { + void* Allocate(size_t length) override; + void* AllocateUninitialized(size_t length) override; + void Free(void* data, size_t length) override; +}; + +using FXJS_CONSTRUCTOR = void (*)(IJS_Runtime* cc, v8::Local obj); +using FXJS_DESTRUCTOR = void (*)(v8::Local obj); + +void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate); +void FXJS_Release(); + +// Gets the global isolate set by FXJS_Initialize(), or makes a new one each +// time if there is no such isolate. Returns true if a new isolate had to be +// created. +bool FXJS_GetIsolate(v8::Isolate** pResultIsolate); + +// Get the global isolate's ref count. +size_t FXJS_GlobalIsolateRefCount(); + +// Always returns a valid, newly-created objDefnID. +int FXJS_DefineObj(v8::Isolate* pIsolate, + const wchar_t* sObjName, + FXJSOBJTYPE eObjType, + FXJS_CONSTRUCTOR pConstructor, + FXJS_DESTRUCTOR pDestructor); + +void FXJS_DefineObjMethod(v8::Isolate* pIsolate, + int nObjDefnID, + const wchar_t* sMethodName, + v8::FunctionCallback pMethodCall); +void FXJS_DefineObjProperty(v8::Isolate* pIsolate, + int nObjDefnID, + const wchar_t* sPropName, + v8::AccessorGetterCallback pPropGet, + v8::AccessorSetterCallback pPropPut); +void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, + int nObjDefnID, + v8::NamedPropertyQueryCallback pPropQurey, + v8::NamedPropertyGetterCallback pPropGet, + v8::NamedPropertySetterCallback pPropPut, + v8::NamedPropertyDeleterCallback pPropDel); +void FXJS_DefineObjConst(v8::Isolate* pIsolate, + int nObjDefnID, + const wchar_t* sConstName, + v8::Local pDefault); +void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, + const wchar_t* sMethodName, + v8::FunctionCallback pMethodCall); +void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, + const wchar_t* sConstName, + v8::FunctionCallback pConstGetter); + +// Called after FXJS_Define* calls made. +void FXJS_InitializeRuntime( + v8::Isolate* pIsolate, + IJS_Runtime* pIRuntime, + v8::Global* pV8PersistentContext, + std::vector*>* pStaticObjects); +void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, + v8::Global* pV8PersistentContext, + std::vector*>* pStaticObjects); +IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate); + +#ifdef PDF_ENABLE_XFA +// Called as part of FXJS_InitializeRuntime, exposed so PDF can make its +// own contexts compatible with XFA or vice versa. +void FXJS_SetRuntimeForV8Context(v8::Local v8Context, + IJS_Runtime* pIRuntime); +#endif // PDF_ENABLE_XFA + +// Called after FXJS_InitializeRuntime call made. +int FXJS_Execute(v8::Isolate* pIsolate, + const CFX_WideString& script, + FXJSErr* perror); + +v8::Local FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, + IJS_Runtime* pJSContext, + int nObjDefnID, + bool bStatic = false); +v8::Local FXJS_GetThisObj(v8::Isolate* pIsolate); +int FXJS_GetObjDefnID(v8::Local pObj); +const wchar_t* FXJS_GetTypeof(v8::Local pObj); + +void FXJS_SetPrivate(v8::Isolate* pIsolate, + v8::Local pObj, + void* p); +void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local pObj); +void FXJS_FreePrivate(void* p); +void FXJS_FreePrivate(v8::Local pObj); +void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message); + +v8::Local FXJS_WSToJSString(v8::Isolate* pIsolate, + const CFX_WideString& wsPropertyName); +v8::Local FXJS_GetObjectElement(v8::Isolate* pIsolate, + v8::Local pObj, + const CFX_WideString& PropertyName); +v8::Local FXJS_GetObjectElementNames(v8::Isolate* pIsolate, + v8::Local pObj); +v8::Local FXJS_GetArrayElement(v8::Isolate* pIsolate, + v8::Local pArray, + unsigned index); + +unsigned FXJS_GetArrayLength(v8::Local pArray); +void FXJS_PutObjectString(v8::Isolate* pIsolate, + v8::Local pObj, + const CFX_WideString& wsPropertyName, + const CFX_WideString& wsValue); +void FXJS_PutObjectNumber(v8::Isolate* pIsolate, + v8::Local pObj, + const CFX_WideString& PropertyName, + int nValue); +void FXJS_PutObjectNumber(v8::Isolate* pIsolate, + v8::Local pObj, + const CFX_WideString& PropertyName, + float fValue); +void FXJS_PutObjectNumber(v8::Isolate* pIsolate, + v8::Local pObj, + const CFX_WideString& PropertyName, + double dValue); +void FXJS_PutObjectBoolean(v8::Isolate* pIsolate, + v8::Local pObj, + const CFX_WideString& PropertyName, + bool bValue); +void FXJS_PutObjectObject(v8::Isolate* pIsolate, + v8::Local pObj, + const CFX_WideString& PropertyName, + v8::Local pPut); +void FXJS_PutObjectNull(v8::Isolate* pIsolate, + v8::Local pObj, + const CFX_WideString& PropertyName); +unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate, + v8::Local pArray, + unsigned index, + v8::Local pValue); + +v8::Local FXJS_NewArray(v8::Isolate* pIsolate); +v8::Local FXJS_NewNumber(v8::Isolate* pIsolate, int number); +v8::Local FXJS_NewNumber(v8::Isolate* pIsolate, double number); +v8::Local FXJS_NewNumber(v8::Isolate* pIsolate, float number); +v8::Local FXJS_NewBoolean(v8::Isolate* pIsolate, bool b); +v8::Local FXJS_NewObject(v8::Isolate* pIsolate, + v8::Local pObj); +v8::Local FXJS_NewObject2(v8::Isolate* pIsolate, + v8::Local pObj); +v8::Local FXJS_NewString(v8::Isolate* pIsolate, const wchar_t* str); +v8::Local FXJS_NewNull(); +v8::Local FXJS_NewDate(v8::Isolate* pIsolate, double d); + +int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local pValue); +bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local pValue); +double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local pValue); +v8::Local FXJS_ToObject(v8::Isolate* pIsolate, + v8::Local pValue); +CFX_WideString FXJS_ToString(v8::Isolate* pIsolate, + v8::Local pValue); +v8::Local FXJS_ToArray(v8::Isolate* pIsolate, + v8::Local pValue); +void FXJS_ValueCopy(v8::Local& pTo, v8::Local pFrom); + +#endif // FXJS_INCLUDE_FXJS_V8_H_ -- cgit v1.2.3