Code

Filter effects dialog:
[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-2007 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 "dom.h"
33 #include "js/jsapi.h"
36 namespace org
37 {
38 namespace w3c
39 {
40 namespace dom
41 {
43 /**
44  * Encapsulate a Spidermonkey JavaScript interpreter.  Init classes, then
45  * wrap around any objects that are needed. 
46  */
47 class JavascriptEngine
48 {
49 public:
51     /**
52      *  Constructor
53      */
54     JavascriptEngine()
55         { startup(); }
58     /**
59      *  Destructor
60      */
61     virtual ~JavascriptEngine()
62         { shutdown(); }
64     /**
65      *  Evaluate a script
66      */
67     bool evaluate(const DOMString &script);
69     /**
70      *  Evaluate a script from a file
71      */
72     bool evaluateFile(const DOMString &script);
75     /**
76      *  Return the runtime of the wrapped JS engine
77      */
78     JSRuntime *getRuntime()
79             { return rt; }
80                 
81     /**
82      *  Return the current context of the wrapped JS engine
83      */
84         JSContext *getContext()
85             { return cx; }
86                 
87     /**
88      *  Return the current global object of the wrapped JS engine
89      */
90         JSObject *getGlobalObject()
91             { return globalObj; } 
92     
94 private:
96     /**
97      *  Startup the javascript engine
98      */
99     bool startup();
101     /**
102      *  Shutdown the javascript engine
103      */
104     bool shutdown();
106     void init()
107         {
108         rt        = NULL;
109         cx        = NULL;
110         globalObj = NULL;
111         }
112     
113     /**
114      *  Assignment operator.  Let's keep this private for now,
115      *  as we want one Spidermonkey runtime per c++ shell     
116      */
117     JavascriptEngine &operator=(const JavascriptEngine &other)
118         { assign(other); return *this; }
120     void assign(const JavascriptEngine &other)
121         {
122         rt        = other.rt;
123         cx        = other.cx;
124         globalObj = other.globalObj;
125         }
127     /**
128      *  Bind with the basic DOM classes
129      */
130     bool createClasses();
132     /**
133      * Ouput a printf-formatted error message
134      */
135     void error(char *fmt, ...)
136     #ifdef G_GNUC_PRINTF
137     G_GNUC_PRINTF(2, 3)
138     #endif
139     ;
141     /**
142      * Ouput a printf-formatted error message
143      */
144     void trace(char *fmt, ...)
145     #ifdef G_GNUC_PRINTF
146     G_GNUC_PRINTF(2, 3)
147     #endif
148     ;
150     JSRuntime *rt;
152     JSContext *cx;
154     JSObject *globalObj;
156     static void errorReporter(JSContext *cx,
157         const char *message, JSErrorReport *report)
158         {
159         JavascriptEngine *engine = 
160                 (JavascriptEngine *) JS_GetContextPrivate(cx);
161         engine->error((char *)message);
162         }
164     
165     
167 };
171 } // namespace dom
172 } // namespace w3c
173 } // namespace org
176 #endif /* __JSENGINE_H__ */