Code

format string protection/clean up (CVE-2007-1463, CVE-2007-1464)
[inkscape.git] / src / dom / jsengine.h
1 #ifndef __JSENGINE_H__
2 #define __JSENGINE_H__
3 /**
4  * Phoebe DOM Implementation.
5  *
6  * This is a C++ approximation of the W3C DOM model, which follows
7  * fairly closely the specifications in the various .idl files, copies of
8  * which are provided for reference.  Most important is this one:
9  *
10  * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
11  *
12  * Authors:
13  *   Bob Jamison
14  *
15  * Copyright (C) 2006 Bob Jamison
16  *
17  *  This library is free software; you can redistribute it and/or
18  *  modify it under the terms of the GNU Lesser General Public
19  *  License as published by the Free Software Foundation; either
20  *  version 2.1 of the License, or (at your option) any later version.
21  *
22  *  This library is distributed in the hope that it will be useful,
23  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  *  Lesser General Public License for more details.
26  *
27  *  You should have received a copy of the GNU Lesser General Public
28  *  License along with this library; if not, write to the Free Software
29  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30  */
32 #include <glib.h>
34 #include "dom.h"
35 #include "js/jsapi.h"
38 namespace org
39 {
40 namespace w3c
41 {
42 namespace dom
43 {
45 /**
46  * Encapsulate a Spidermonkey JavaScript interpreter.  Init classes, then
47  * wrap around any objects that are needed. 
48  */
49 class JavascriptEngine
50 {
51 public:
53     /**
54      *  Constructor
55      */
56     JavascriptEngine()
57         { startup(); }
60     /**
61      *  Destructor
62      */
63     virtual ~JavascriptEngine()
64         { shutdown(); }
66     /**
67      *  Evaluate a script
68      */
69     bool evaluate(const DOMString &script);
71     /**
72      *  Evaluate a script from a file
73      */
74     bool evaluateFile(const DOMString &script);
77     /**
78      *  Return the runtime of the wrapped JS engine
79      */
80     JSRuntime *getRuntime()
81             { return rt; }
82                 
83     /**
84      *  Return the current context of the wrapped JS engine
85      */
86         JSContext *getContext()
87             { return cx; }
88                 
89     /**
90      *  Return the current global object of the wrapped JS engine
91      */
92         JSObject *getGlobalObject()
93             { return globalObj; } 
94     
96 private:
98     /**
99      *  Startup the javascript engine
100      */
101     bool startup();
103     /**
104      *  Shutdown the javascript engine
105      */
106     bool shutdown();
108     void init()
109         {
110         rt        = NULL;
111         cx        = NULL;
112         globalObj = NULL;
113         }
114     
115     /**
116      *  Assignment operator.  Let's keep this private for now,
117      *  as we want one Spidermonkey runtime per c++ shell     
118      */
119     JavascriptEngine &operator=(const JavascriptEngine &other)
120         { assign(other); return *this; }
122     void assign(const JavascriptEngine &other)
123         {
124         rt        = other.rt;
125         cx        = other.cx;
126         globalObj = other.globalObj;
127         }
129     /**
130      *  Bind with the basic DOM classes
131      */
132     bool createClasses();
134     /**
135      * Ouput a printf-formatted error message
136      */
137     void error(char *fmt, ...) G_GNUC_PRINTF(2,3);
139     /**
140      * Ouput a printf-formatted error message
141      */
142     void trace(char *fmt, ...) G_GNUC_PRINTF(2,3);
144     JSRuntime *rt;
146     JSContext *cx;
148     JSObject *globalObj;
150     static void errorReporter(JSContext *cx,
151         const char *message, JSErrorReport *report)
152         {
153         JavascriptEngine *engine = 
154                 (JavascriptEngine *) JS_GetContextPrivate(cx);
155         engine->error((char *)message);
156         }
158     
159     
161 };
165 } // namespace dom
166 } // namespace w3c
167 } // namespace org
170 #endif /* __JSENGINE_H__ */