PTLib  Version 2.12.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
jscript.h
Go to the documentation of this file.
1 /*
2  * v8script.h
3  *
4  * Interface library for V8 Javascript interpreter
5  *
6  * Portable Tools Library
7  *
8  * Copyright (C) 2012 by Post Increment
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Tools Library.
21  *
22  * The Initial Developer of the Original Code is Post Increment
23  *
24  * Contributor(s): Craig Southeren
25  *
26  * $Revision: 28224 $
27  * $Author: rjongbloed $
28  * $Date: 2012-08-21 18:09:25 +1000 (Tue, 21 Aug 2012) $
29  */
30 
31 #ifndef PTLIB_V8SCRIPT_H
32 #define PTLIB_V8SCRIPT_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <ptlib.h>
39 #include <ptbuildopts.h>
40 
41 #if P_V8
42 
43 #include <ptclib/script.h>
44 #include <ptclib/vartype.h>
45 
46 #ifdef _MSC_VER
47  #pragma warning(disable:4100 4127)
48 #endif
49 
50 #include <v8.h>
51 
52 #ifdef _MSC_VER
53 #pragma warning(default:4100 4127)
54 #endif
55 
56 
58 
61 class PJavaScript : public PScriptLanguage
62 {
63  PCLASSINFO(PJavaScript, PScriptLanguage)
64 
65  public:
70  PJavaScript();
71 
73  ~PJavaScript();
75 
80  virtual bool LoadFile(
81  const PFilePath & filename
82  );
83 
86  virtual bool LoadText(
87  const PString & text
88  );
89 
96  virtual bool Run(
97  const char * script = NULL
98  );
99 
100 
104  virtual bool CreateComposite(
105  const PString & name
106  );
107 
111  virtual bool GetVar(
112  const PString & name,
113  PVarType & var
114  );
115 
119  virtual bool SetVar(
120  const PString & name,
121  const PVarType & var
122  );
123 
127  bool GetBoolean(
128  const PString & name
129  );
130 
134  bool SetBoolean(
135  const PString & name,
136  bool value
137  );
138 
142  int GetInteger(
143  const PString & name
144  );
145 
149  bool SetInteger(
150  const PString & name,
151  int value
152  );
153 
157  double GetNumber(
158  const PString & name
159  );
160 
164  bool SetNumber(
165  const PString & name,
166  double value
167  );
168 
172  PString GetString(
173  const PString & name
174  );
175 
179  bool SetString(
180  const PString & name,
181  const char * value
182  );
183 
186  virtual bool ReleaseVariable(
187  const PString & name
188  );
189 
216  bool Call(
217  const PString & name,
218  const char * sigString = NULL,
219  ...
220  );
221  bool Call(
222  const PString & name,
223  Signature & signature
224  );
225 
226 
230  bool SetFunction(
231  const PString & name,
232  const FunctionNotifier & func
233  );
235 
236  protected:
237  template <class v8Type, class cppType>
238  bool SetValue(const PString & name, const cppType & value)
239  {
240  v8::Locker locker;
241  v8::HandleScope handleScope;
242  v8::Context::Scope contextScope(m_context);
243  return m_context->Global()->Set(v8::String::New(name), v8Type::New(value));
244  }
245 
246  v8::Persistent<v8::Context> m_context;
247  PString m_resultText;
248 };
249 
250 
251 #endif // P_V8
252 
253 #endif // PTLIB_V8SCRIPT_H
254