Code

3f63c60607786412a2dd1923ba47f1bd4344f0c1
[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  */
33 #include "dom.h"
34 #include "js/jsapi.h"
37 namespace org
38 {
39 namespace w3c
40 {
41 namespace dom
42 {
44 /**
45  * Encapsulate a Spidermonkey JavaScript interpreter.  Init classes, then
46  * wrap around any objects that are needed. 
47  */
48 class JavascriptEngine
49 {
50 public:
52     /**
53      *  Constructor
54      */
55     JavascriptEngine()
56         { startup(); }
59     /**
60      *  Destructor
61      */
62     virtual ~JavascriptEngine()
63         { shutdown(); }
65     /**
66      *  Evaluate a script
67      */
68     bool evaluate(const DOMString &script);
70     /**
71      *  Evaluate a script from a file
72      */
73     bool evaluateFile(const DOMString &script);
76     JSObject *wrapDocument(const Document *doc);
78 private:
80     /**
81      *  Startup the javascript engine
82      */
83     bool startup();
85     /**
86      *  Shutdown the javascript engine
87      */
88     bool shutdown();
90     void init()
91         {
92         rt        = NULL;
93         cx        = NULL;
94         globalObj = NULL;
95         }
96     
97     /**
98      *  Assignment operator.  Let's keep this private for now,
99      *  as we want one Spidermonkey runtime per c++ shell     
100      */
101     JavascriptEngine &operator=(const JavascriptEngine &other)
102         { assign(other); return *this; }
104     void assign(const JavascriptEngine &other)
105         {
106         rt        = other.rt;
107         cx        = other.cx;
108         globalObj = other.globalObj;
109         }
111     /**
112      *  Bind with the basic DOM classes
113      */
114     bool createClasses();
116     /**
117      * Ouput a printf-formatted error message
118      */
119     void error(char *fmt, ...);
121     /**
122      * Ouput a printf-formatted error message
123      */
124     void trace(char *fmt, ...);
126     JSRuntime *rt;
128     JSContext *cx;
130     JSObject *globalObj;
132     static void errorReporter(JSContext *cx,
133         const char *message, JSErrorReport *report)
134         {
135         JavascriptEngine *engine = 
136                 (JavascriptEngine *) JS_GetContextPrivate(cx);
137         engine->error((char *)message);
138         }
140     JSObject *nodeProto;
141     JSObject *characterDataProto;
142     JSObject *textProto;
143     JSObject *cdataProto;
144     JSObject *documentProto;
146 };
150 } // namespace dom
151 } // namespace w3c
152 } // namespace org
155 #endif /* __JSENGINE_H__ */