Initial commit.
[pdfium.git] / fpdfsdk / include / jsapi / fxjs_v8.h
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 FXJSAPI_H
8 #define FXJSAPI_H
9
10 #include <v8.h>
11 using namespace v8;
12
13 enum FXJSOBJTYPE
14 {
15         JS_DYNAMIC = 0,
16         JS_STATIC = 1,
17 };
18
19 enum FXJSVALUETYPE
20 {
21         VT_unknown,
22         VT_string,
23         VT_number,
24         VT_boolean,
25         VT_date,
26         VT_object,
27         VT_fxobject,
28         VT_null,
29         VT_undefined
30 };
31
32 struct FXJSErr
33 {
34         const wchar_t* message; 
35     const wchar_t* srcline;     
36     unsigned linnum;
37 };
38
39 /* --------------------------------------------- API --------------------------------------------- */
40
41 typedef v8::Isolate IJS_Runtime;
42 class IFXJS_Context;
43 class IFXJS_Runtime;
44
45
46 #ifndef JSCRIPT_ARGS
47 #define JSCRIPT_ARGS
48
49 #define JS_PROPGET_ARGS                 v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Value>& info
50 #define JS_PROPPUT_ARGS                 v8::Local<v8::String> property,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<void>& info
51 #define JS_METHOD_ARGS                  const v8::FunctionCallbackInfo<v8::Value>& info
52 #define JS_CONSTRUCTOR_ARGS             IFXJS_Context* cc, v8::Handle<v8::Object> obj, v8::Handle<v8::Object> global
53 #define JS_DESTRUCTOR_ARGS              v8::Handle<v8::Object> obj
54
55 #define JS_PROPQUERY_ARGS               v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Integer>& info
56 #define JS_NAMED_PROPGET_ARGS   JS_PROPGET_ARGS                                 
57 #define JS_NAMED_PROPPUT_ARGS   v8::Local<v8::String> property,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value>& info
58 #define JS_PROPDEL_ARGS                 v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Boolean>& info
59
60 typedef unsigned (*LP_CONSTRUCTOR)(JS_CONSTRUCTOR_ARGS);
61 typedef unsigned (*LP_DESTRUCTOR)(JS_DESTRUCTOR_ARGS);
62
63 #endif
64
65 int                                                             JS_DefineObj(IJS_Runtime* pJSRuntime, const wchar_t* sObjName, FXJSOBJTYPE eObjType, LP_CONSTRUCTOR pConstructor, LP_DESTRUCTOR pDestructor, unsigned bApplyNew);
66 int                                                             JS_DefineObjMethod(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sMethodName, FunctionCallback pMethodCall, unsigned nParamNum);
67 int                                                             JS_DefineObjProperty(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sPropName, AccessorGetterCallback pPropGet, AccessorSetterCallback pPropPut);
68 int                                                             JS_DefineObjAllProperties(IJS_Runtime* pJSRuntime, int nObjDefnID, NamedPropertyQueryCallback pPropQurey, NamedPropertyGetterCallback pPropGet, NamedPropertySetterCallback pPropPut, NamedPropertyDeleterCallback pPropDel);
69 int                                                             JS_DefineObjConst(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sConstName, v8::Handle<v8::Value> pDefault);
70 int                                                             JS_DefineGlobalMethod(IJS_Runtime* pJSRuntime, const wchar_t* sMethodName, FunctionCallback pMethodCall, unsigned nParamNum);
71 int                                                             JS_DefineGlobalConst(IJS_Runtime* pJSRuntime, const wchar_t* sConstName, v8::Handle<v8::Value> pDefault);
72
73 void                                                    JS_InitialRuntime(IJS_Runtime* pJSRuntime,IFXJS_Runtime* pFXRuntime, IFXJS_Context* context, v8::Persistent<v8::Context>& v8PersistentContext);
74 void                                                    JS_ReleaseRuntime(IJS_Runtime* pJSRuntime, v8::Persistent<v8::Context>& v8PersistentContext);
75 void                                                    JS_Initial(); 
76 void                                                    JS_Release();
77 int                                                             JS_Parse(IJS_Runtime* pJSRuntime, IFXJS_Context* pJSContext, const wchar_t* script, long length, FXJSErr* perror);
78 int                                                             JS_Execute(IJS_Runtime* pJSRuntime, IFXJS_Context* pJSContext, const wchar_t* script, long length, FXJSErr* perror);
79 v8::Handle<v8::Object>                  JS_NewFxDynamicObj(IJS_Runtime* pJSRuntime, IFXJS_Context* pJSContext, int nObjDefnID);
80 v8::Handle<v8::Object>                  JS_GetStaticObj(IJS_Runtime* pJSRuntime, int nObjDefnID);
81 void                                                    JS_SetThisObj(IJS_Runtime* pJSRuntime, int nThisObjID);
82 v8::Handle<v8::Object>                  JS_GetThisObj(IJS_Runtime * pJSRuntime);
83 int                                                             JS_GetObjDefnID(v8::Handle<v8::Object> pObj);
84 IJS_Runtime*                                    JS_GetRuntime(v8::Handle<v8::Object> pObj);
85 int                                                             JS_GetObjDefnID(IJS_Runtime * pJSRuntime, const wchar_t* pObjName);
86 void                                                    JS_Error(v8::Value * pError,const wchar_t * main,const wchar_t * sub);
87 unsigned                                                JS_CalcHash(const wchar_t* main, unsigned nLen);
88 unsigned                                                JS_CalcHash(const wchar_t* main);
89 const wchar_t*                                  JS_GetTypeof(v8::Handle<v8::Value> pObj);
90 const wchar_t*                                  JS_GetClassname(v8::Handle<v8::Object> pObj);
91 void                                                    JS_SetPrivate(IJS_Runtime* pJSRuntime, v8::Handle<v8::Object> pObj, void* p);
92 void*                                                   JS_GetPrivate(IJS_Runtime* pJSRuntime, v8::Handle<v8::Object> pObj);
93 void                                                    JS_SetPrivate(v8::Handle<v8::Object> pObj, void* p);
94 void*                                                   JS_GetPrivate(v8::Handle<v8::Object> pObj);
95 void                                                    JS_FreePrivate(v8::Handle<v8::Object> pObj);
96 v8::Handle<v8::Value>                   JS_GetObjectValue(v8::Handle<v8::Object> pObj);
97 v8::Handle<v8::Value>                   JS_GetObjectElement(IJS_Runtime* pJSRuntime, v8::Handle<v8::Object> pObj,const wchar_t* PropertyName);
98 v8::Handle<v8::Array>                   JS_GetObjectElementNames(v8::Handle<v8::Object> pObj);
99 void                                                    JS_PutObjectString(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, const wchar_t* sValue);
100 void                                                    JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, int nValue);
101 void                                                    JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, float fValue);
102 void                                                    JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, double dValue);
103 void                                                    JS_PutObjectBoolean(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, bool bValue);
104 void                                                    JS_PutObjectObject(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, v8::Handle<v8::Object> pPut);
105 void                                                    JS_PutObjectNull(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName);
106 unsigned                                                JS_PutArrayElement(v8::Handle<v8::Array> pArray,unsigned index,v8::Handle<v8::Value> pValue,FXJSVALUETYPE eType);
107 v8::Handle<v8::Value>                   JS_GetArrayElemnet(v8::Handle<v8::Array> pArray,unsigned index);
108 unsigned                                                JS_GetArrayLength(v8::Handle<v8::Array> pArray);
109 v8::Handle<v8::Value>                   JS_GetListValue(v8::Handle<v8::Value> pList, int index);
110
111
112 v8::Handle<v8::Array>                   JS_NewArray(IJS_Runtime* pJSRuntime);
113 v8::Handle<v8::Value>                   JS_NewNumber(IJS_Runtime* pJSRuntime,int number);
114 v8::Handle<v8::Value>                   JS_NewNumber(IJS_Runtime* pJSRuntime,double number);
115 v8::Handle<v8::Value>                   JS_NewNumber(IJS_Runtime* pJSRuntime,float number);
116 v8::Handle<v8::Value>                   JS_NewBoolean(IJS_Runtime* pJSRuntime,bool b);
117 v8::Handle<v8::Value>                   JS_NewObject(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj);
118 v8::Handle<v8::Value>                   JS_NewObject2(IJS_Runtime* pJSRuntime,v8::Handle<v8::Array> pObj);
119 v8::Handle<v8::Value>                   JS_NewString(IJS_Runtime* pJSRuntime,const wchar_t* string);
120 v8::Handle<v8::Value>                   JS_NewString(IJS_Runtime* pJSRuntime,const wchar_t* string, unsigned nLen);
121 v8::Handle<v8::Value>                   JS_NewNull();
122 v8::Handle<v8::Value>                   JS_NewDate(IJS_Runtime* pJSRuntime,double d);
123 v8::Handle<v8::Value>                   JS_NewValue(IJS_Runtime* pJSRuntime);
124
125
126 int                                                             JS_ToInt32(v8::Handle<v8::Value> pValue);
127 bool                                                    JS_ToBoolean(v8::Handle<v8::Value> pValue);
128 double                                                  JS_ToNumber(v8::Handle<v8::Value> pValue);
129 v8::Handle<v8::Object>                  JS_ToObject(v8::Handle<v8::Value> pValue);
130 CFX_WideString                                  JS_ToString(v8::Handle<v8::Value> pValue);
131 v8::Handle<v8::Array>                   JS_ToArray(v8::Handle<v8::Value> pValue);
132 void                                                    JS_ValueCopy(v8::Handle<v8::Value>& pTo, v8::Handle<v8::Value> pFrom);
133
134 double                                                  JS_GetDateTime();
135 int                                                             JS_GetYearFromTime(double dt);
136 int                                                             JS_GetMonthFromTime(double dt);
137 int                                                             JS_GetDayFromTime(double dt);
138 int                                                             JS_GetHourFromTime(double dt);
139 int                                                             JS_GetMinFromTime(double dt);
140 int                                                             JS_GetSecFromTime(double dt);
141 double                                                  JS_DateParse(const wchar_t* string);
142 double                                                  JS_MakeDay(int nYear, int nMonth, int nDay);
143 double                                                  JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
144 double                                                  JS_MakeDate(double day, double time);
145 bool                                                    JS_PortIsNan(double d);
146 double                                                  JS_LocalTime(double d);
147
148 #endif //FXJSAPI_H