summaryrefslogtreecommitdiff
path: root/fxjse/include
diff options
context:
space:
mode:
authordsinclair <dsinclair@chromium.org>2016-06-09 13:24:34 -0700
committerCommit bot <commit-bot@chromium.org>2016-06-09 13:24:34 -0700
commit8bd9ce00a0755d72c9b5b8ad43d8f50c2f51e5c5 (patch)
tree9dd3a72354f353694cc52747acabf297a10251a3 /fxjse/include
parent3cace320e07bb954710c50fc7634ca3a8f3eda38 (diff)
downloadpdfium-8bd9ce00a0755d72c9b5b8ad43d8f50c2f51e5c5.tar.xz
Move xfa/fxjse/ to fxjse/
This makes it clear that fxjse/ is a standalone component and should not be entangled with other xfa/ components. BUG=pdfium:506 Review-Url: https://codereview.chromium.org/2056663004
Diffstat (limited to 'fxjse/include')
-rw-r--r--fxjse/include/cfxjse_arguments.h38
-rw-r--r--fxjse/include/cfxjse_class.h44
-rw-r--r--fxjse/include/cfxjse_value.h284
-rw-r--r--fxjse/include/fxjse.h74
4 files changed, 440 insertions, 0 deletions
diff --git a/fxjse/include/cfxjse_arguments.h b/fxjse/include/cfxjse_arguments.h
new file mode 100644
index 0000000000..0f9bddaa87
--- /dev/null
+++ b/fxjse/include/cfxjse_arguments.h
@@ -0,0 +1,38 @@
+// Copyright 2016 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
+
+#ifndef FXJSE_INCLUDE_CFXJSE_ARGUMENTS_H_
+#define FXJSE_INCLUDE_CFXJSE_ARGUMENTS_H_
+
+#include <memory>
+
+#include "fxjse/include/fxjse.h"
+
+class CFXJSE_Class;
+
+class CFXJSE_Arguments {
+ public:
+ CFXJSE_Arguments(const v8::FunctionCallbackInfo<v8::Value>* pInfo,
+ CFXJSE_Value* pRetValue)
+ : m_pInfo(pInfo), m_pRetValue(pRetValue) {}
+
+ v8::Isolate* GetRuntime() const;
+ int32_t GetLength() const;
+ std::unique_ptr<CFXJSE_Value> GetValue(int32_t index) const;
+ FX_BOOL GetBoolean(int32_t index) const;
+ int32_t GetInt32(int32_t index) const;
+ FX_FLOAT GetFloat(int32_t index) const;
+ CFX_ByteString GetUTF8String(int32_t index) const;
+ CFXJSE_HostObject* GetObject(int32_t index,
+ CFXJSE_Class* pClass = nullptr) const;
+ CFXJSE_Value* GetReturnValue();
+
+ private:
+ const v8::FunctionCallbackInfo<v8::Value>* m_pInfo;
+ CFXJSE_Value* m_pRetValue;
+};
+
+#endif // FXJSE_INCLUDE_CFXJSE_ARGUMENTS_H_
diff --git a/fxjse/include/cfxjse_class.h b/fxjse/include/cfxjse_class.h
new file mode 100644
index 0000000000..bb1db6d48c
--- /dev/null
+++ b/fxjse/include/cfxjse_class.h
@@ -0,0 +1,44 @@
+// 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
+
+#ifndef FXJSE_INCLUDE_CFXJSE_CLASS_H_
+#define FXJSE_INCLUDE_CFXJSE_CLASS_H_
+
+#include "fxjse/include/cfxjse_arguments.h"
+#include "fxjse/include/fxjse.h"
+#include "v8/include/v8.h"
+
+class CFXJSE_Context;
+class CFXJSE_Value;
+
+class CFXJSE_Class {
+ public:
+ static CFXJSE_Class* Create(CFXJSE_Context* pContext,
+ const FXJSE_CLASS_DESCRIPTOR* lpClassDefintion,
+ FX_BOOL bIsJSGlobal = FALSE);
+ static CFXJSE_Class* GetClassFromContext(CFXJSE_Context* pContext,
+ const CFX_ByteStringC& szName);
+ static void SetUpNamedPropHandler(
+ v8::Isolate* pIsolate,
+ v8::Local<v8::ObjectTemplate>& hObjectTemplate,
+ const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition);
+
+ CFXJSE_Context* GetContext() { return m_pContext; }
+ v8::Global<v8::FunctionTemplate>& GetTemplate() { return m_hTemplate; }
+
+ protected:
+ explicit CFXJSE_Class(CFXJSE_Context* lpContext)
+ : m_lpClassDefinition(nullptr), m_pContext(lpContext) {}
+
+ CFX_ByteString m_szClassName;
+ const FXJSE_CLASS_DESCRIPTOR* m_lpClassDefinition;
+ CFXJSE_Context* m_pContext;
+ v8::Global<v8::FunctionTemplate> m_hTemplate;
+ friend class CFXJSE_Context;
+ friend class CFXJSE_Value;
+};
+
+#endif // FXJSE_INCLUDE_CFXJSE_CLASS_H_
diff --git a/fxjse/include/cfxjse_value.h b/fxjse/include/cfxjse_value.h
new file mode 100644
index 0000000000..e01f64cfff
--- /dev/null
+++ b/fxjse/include/cfxjse_value.h
@@ -0,0 +1,284 @@
+// 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
+
+#ifndef FXJSE_INCLUDE_CFXJSE_VALUE_H_
+#define FXJSE_INCLUDE_CFXJSE_VALUE_H_
+
+#include "fxjse/scope_inline.h"
+
+V8_INLINE static double FXJSE_ftod(FX_FLOAT fNumber) {
+ if (sizeof(FX_FLOAT) != 4) {
+ ASSERT(FALSE);
+ return fNumber;
+ }
+
+ uint32_t nFloatBits = (uint32_t&)fNumber;
+ uint8_t nExponent = (uint8_t)(nFloatBits >> 16 >> 7);
+ if (nExponent == 0 || nExponent == 255)
+ return fNumber;
+
+ int8_t nErrExp = nExponent - 127 - 23;
+ if (nErrExp >= 0)
+ return fNumber;
+
+ double dwError = pow(2.0, nErrExp), dwErrorHalf = dwError / 2;
+ double dNumber = fNumber, dNumberAbs = fabs(fNumber);
+ double dNumberAbsMin = dNumberAbs - dwErrorHalf,
+ dNumberAbsMax = dNumberAbs + dwErrorHalf;
+ int32_t iErrPos = 0;
+ if (floor(dNumberAbsMin) == floor(dNumberAbsMax)) {
+ dNumberAbsMin = fmod(dNumberAbsMin, 1.0);
+ dNumberAbsMax = fmod(dNumberAbsMax, 1.0);
+ int32_t iErrPosMin = 1, iErrPosMax = 38;
+ do {
+ int32_t iMid = (iErrPosMin + iErrPosMax) / 2;
+ double dPow = pow(10.0, iMid);
+ if (floor(dNumberAbsMin * dPow) == floor(dNumberAbsMax * dPow)) {
+ iErrPosMin = iMid + 1;
+ } else {
+ iErrPosMax = iMid;
+ }
+ } while (iErrPosMin < iErrPosMax);
+ iErrPos = iErrPosMax;
+ }
+ double dPow = pow(10.0, iErrPos);
+ return fNumber < 0 ? ceil(dNumber * dPow - 0.5) / dPow
+ : floor(dNumber * dPow + 0.5) / dPow;
+}
+
+class CFXJSE_Value {
+ public:
+ CFXJSE_Value(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}
+
+ V8_INLINE FX_BOOL IsUndefined() const {
+ if (m_hValue.IsEmpty()) {
+ return FALSE;
+ }
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return hValue->IsUndefined();
+ }
+ V8_INLINE FX_BOOL IsNull() const {
+ if (m_hValue.IsEmpty()) {
+ return FALSE;
+ }
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return hValue->IsNull();
+ }
+ V8_INLINE FX_BOOL IsBoolean() const {
+ if (m_hValue.IsEmpty()) {
+ return FALSE;
+ }
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return hValue->IsBoolean();
+ }
+ V8_INLINE FX_BOOL IsString() const {
+ if (m_hValue.IsEmpty()) {
+ return FALSE;
+ }
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return hValue->IsString();
+ }
+ V8_INLINE FX_BOOL IsNumber() const {
+ if (m_hValue.IsEmpty()) {
+ return FALSE;
+ }
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return hValue->IsNumber();
+ }
+ V8_INLINE FX_BOOL IsInteger() const {
+ if (m_hValue.IsEmpty()) {
+ return FALSE;
+ }
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return hValue->IsInt32();
+ }
+ V8_INLINE FX_BOOL IsObject() const {
+ if (m_hValue.IsEmpty()) {
+ return FALSE;
+ }
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return hValue->IsObject();
+ }
+ V8_INLINE FX_BOOL IsArray() const {
+ if (m_hValue.IsEmpty()) {
+ return FALSE;
+ }
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return hValue->IsArray();
+ }
+ V8_INLINE FX_BOOL IsFunction() const {
+ if (m_hValue.IsEmpty()) {
+ return FALSE;
+ }
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return hValue->IsFunction();
+ }
+ V8_INLINE FX_BOOL IsDate() const {
+ if (m_hValue.IsEmpty()) {
+ return FALSE;
+ }
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return hValue->IsDate();
+ }
+
+ V8_INLINE FX_BOOL ToBoolean() const {
+ ASSERT(!m_hValue.IsEmpty());
+ CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return static_cast<FX_BOOL>(hValue->BooleanValue());
+ }
+ V8_INLINE FX_FLOAT ToFloat() const {
+ ASSERT(!m_hValue.IsEmpty());
+ CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return static_cast<FX_FLOAT>(hValue->NumberValue());
+ }
+ V8_INLINE double ToDouble() const {
+ ASSERT(!m_hValue.IsEmpty());
+ CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return static_cast<double>(hValue->NumberValue());
+ }
+ V8_INLINE int32_t ToInteger() const {
+ ASSERT(!m_hValue.IsEmpty());
+ CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ return static_cast<int32_t>(hValue->NumberValue());
+ }
+ V8_INLINE CFX_ByteString ToString() const {
+ ASSERT(!m_hValue.IsEmpty());
+ CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
+ v8::Local<v8::String> hString = hValue->ToString();
+ v8::String::Utf8Value hStringVal(hString);
+ return CFX_ByteString(*hStringVal);
+ }
+ V8_INLINE CFX_WideString ToWideString() const {
+ return CFX_WideString::FromUTF8(ToString().AsStringC());
+ }
+ CFXJSE_HostObject* ToHostObject(CFXJSE_Class* lpClass) const;
+
+ V8_INLINE void SetUndefined() {
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue = v8::Undefined(m_pIsolate);
+ m_hValue.Reset(m_pIsolate, hValue);
+ }
+ V8_INLINE void SetNull() {
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue = v8::Null(m_pIsolate);
+ m_hValue.Reset(m_pIsolate, hValue);
+ }
+ V8_INLINE void SetBoolean(FX_BOOL bBoolean) {
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue =
+ v8::Boolean::New(m_pIsolate, bBoolean != FALSE);
+ m_hValue.Reset(m_pIsolate, hValue);
+ }
+ V8_INLINE void SetInteger(int32_t nInteger) {
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue = v8::Integer::New(m_pIsolate, nInteger);
+ m_hValue.Reset(m_pIsolate, hValue);
+ }
+ V8_INLINE void SetDouble(double dDouble) {
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, dDouble);
+ m_hValue.Reset(m_pIsolate, hValue);
+ }
+ V8_INLINE void SetString(const CFX_ByteStringC& szString) {
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> hValue = v8::String::NewFromUtf8(
+ m_pIsolate, reinterpret_cast<const char*>(szString.raw_str()),
+ v8::String::kNormalString, szString.GetLength());
+ m_hValue.Reset(m_pIsolate, hValue);
+ }
+ V8_INLINE void SetFloat(FX_FLOAT fFloat) {
+ CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
+ v8::Local<v8::Value> pValue =
+ v8::Number::New(m_pIsolate, FXJSE_ftod(fFloat));
+ m_hValue.Reset(m_pIsolate, pValue);
+ }
+ V8_INLINE void SetJSObject() {
+ CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
+ v8::Local<v8::Value> hValue = v8::Object::New(m_pIsolate);
+ m_hValue.Reset(m_pIsolate, hValue);
+ }
+
+ void SetObject(CFXJSE_HostObject* lpObject, CFXJSE_Class* pClass);
+ void SetHostObject(CFXJSE_HostObject* lpObject, CFXJSE_Class* lpClass);
+ void SetArray(uint32_t uValueCount, CFXJSE_Value** rgValues);
+ void SetDate(double dDouble);
+
+ FX_BOOL GetObjectProperty(const CFX_ByteStringC& szPropName,
+ CFXJSE_Value* lpPropValue);
+ FX_BOOL SetObjectProperty(const CFX_ByteStringC& szPropName,
+ CFXJSE_Value* lpPropValue);
+ FX_BOOL GetObjectPropertyByIdx(uint32_t uPropIdx, CFXJSE_Value* lpPropValue);
+ FX_BOOL SetObjectProperty(uint32_t uPropIdx, CFXJSE_Value* lpPropValue);
+ FX_BOOL DeleteObjectProperty(const CFX_ByteStringC& szPropName);
+ FX_BOOL HasObjectOwnProperty(const CFX_ByteStringC& szPropName,
+ FX_BOOL bUseTypeGetter);
+ FX_BOOL SetObjectOwnProperty(const CFX_ByteStringC& szPropName,
+ CFXJSE_Value* lpPropValue);
+ FX_BOOL SetFunctionBind(CFXJSE_Value* lpOldFunction, CFXJSE_Value* lpNewThis);
+ FX_BOOL Call(CFXJSE_Value* lpReceiver,
+ CFXJSE_Value* lpRetValue,
+ uint32_t nArgCount,
+ CFXJSE_Value** lpArgs);
+
+ V8_INLINE v8::Isolate* GetIsolate() const { return m_pIsolate; }
+ V8_INLINE const v8::Global<v8::Value>& DirectGetValue() const {
+ return m_hValue;
+ }
+ V8_INLINE void ForceSetValue(v8::Local<v8::Value> hValue) {
+ m_hValue.Reset(m_pIsolate, hValue);
+ }
+ V8_INLINE void Assign(const CFXJSE_Value* lpValue) {
+ ASSERT(lpValue);
+ if (lpValue) {
+ m_hValue.Reset(m_pIsolate, lpValue->m_hValue);
+ } else {
+ m_hValue.Reset();
+ }
+ }
+
+ private:
+ friend class CFXJSE_Class;
+ friend class CFXJSE_Context;
+
+ CFXJSE_Value();
+ CFXJSE_Value(const CFXJSE_Value&);
+ CFXJSE_Value& operator=(const CFXJSE_Value&);
+
+ v8::Isolate* m_pIsolate;
+ v8::Global<v8::Value> m_hValue;
+};
+
+#endif // FXJSE_INCLUDE_CFXJSE_VALUE_H_
diff --git a/fxjse/include/fxjse.h b/fxjse/include/fxjse.h
new file mode 100644
index 0000000000..f9d6a67e02
--- /dev/null
+++ b/fxjse/include/fxjse.h
@@ -0,0 +1,74 @@
+// 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
+
+#ifndef FXJSE_INCLUDE_FXJSE_H_
+#define FXJSE_INCLUDE_FXJSE_H_
+
+#include "core/fxcrt/include/fx_string.h"
+#include "core/fxcrt/include/fx_system.h"
+#include "v8/include/v8.h"
+
+class CFXJSE_Arguments;
+class CFXJSE_Value;
+
+// C++ object which can be wrapped by CFXJSE_value.
+class CFXJSE_HostObject {
+ public:
+ virtual ~CFXJSE_HostObject() {}
+};
+
+typedef void (*FXJSE_FuncCallback)(CFXJSE_Value* pThis,
+ const CFX_ByteStringC& szFuncName,
+ CFXJSE_Arguments& args);
+typedef void (*FXJSE_PropAccessor)(CFXJSE_Value* pObject,
+ const CFX_ByteStringC& szPropName,
+ CFXJSE_Value* pValue);
+typedef int32_t (*FXJSE_PropTypeGetter)(CFXJSE_Value* pObject,
+ const CFX_ByteStringC& szPropName,
+ FX_BOOL bQueryIn);
+typedef FX_BOOL (*FXJSE_PropDeleter)(CFXJSE_Value* pObject,
+ const CFX_ByteStringC& szPropName);
+
+enum FXJSE_ClassPropTypes {
+ FXJSE_ClassPropType_None,
+ FXJSE_ClassPropType_Property,
+ FXJSE_ClassPropType_Method
+};
+
+struct FXJSE_FUNCTION_DESCRIPTOR {
+ const FX_CHAR* name;
+ FXJSE_FuncCallback callbackProc;
+};
+
+struct FXJSE_PROPERTY_DESCRIPTOR {
+ const FX_CHAR* name;
+ FXJSE_PropAccessor getProc;
+ FXJSE_PropAccessor setProc;
+};
+
+struct FXJSE_CLASS_DESCRIPTOR {
+ const FX_CHAR* name;
+ FXJSE_FuncCallback constructor;
+ const FXJSE_PROPERTY_DESCRIPTOR* properties;
+ const FXJSE_FUNCTION_DESCRIPTOR* methods;
+ int32_t propNum;
+ int32_t methNum;
+ FXJSE_PropTypeGetter dynPropTypeGetter;
+ FXJSE_PropAccessor dynPropGetter;
+ FXJSE_PropAccessor dynPropSetter;
+ FXJSE_PropDeleter dynPropDeleter;
+ FXJSE_FuncCallback dynMethodCall;
+};
+
+void FXJSE_Initialize();
+void FXJSE_Finalize();
+
+v8::Isolate* FXJSE_Runtime_Create_Own();
+void FXJSE_Runtime_Release(v8::Isolate* pIsolate);
+
+void FXJSE_ThrowMessage(const CFX_ByteStringC& utf8Message);
+
+#endif // FXJSE_INCLUDE_FXJSE_H_