Code

Massive update for smart pointers. Rework js dom binding to be smarter. Placeholder...
[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     /**
77      *  Return the runtime of the wrapped JS engine
78      */
79     JSRuntime *getRuntime()
80             { return rt; }
81                 
82     /**
83      *  Return the current context of the wrapped JS engine
84      */
85         JSContext *getContext()
86             { return cx; }
87                 
88     /**
89      *  Return the current global object of the wrapped JS engine
90      */
91         JSObject *getGlobalObject()
92             { return globalObj; } 
93     
95 private:
97     /**
98      *  Startup the javascript engine
99      */
100     bool startup();
102     /**
103      *  Shutdown the javascript engine
104      */
105     bool shutdown();
107     void init()
108         {
109         rt        = NULL;
110         cx        = NULL;
111         globalObj = NULL;
112         }
113     
114     /**
115      *  Assignment operator.  Let's keep this private for now,
116      *  as we want one Spidermonkey runtime per c++ shell     
117      */
118     JavascriptEngine &operator=(const JavascriptEngine &other)
119         { assign(other); return *this; }
121     void assign(const JavascriptEngine &other)
122         {
123         rt        = other.rt;
124         cx        = other.cx;
125         globalObj = other.globalObj;
126         }
128     /**
129      *  Bind with the basic DOM classes
130      */
131     bool createClasses();
133     /**
134      * Ouput a printf-formatted error message
135      */
136     void error(char *fmt, ...);
138     /**
139      * Ouput a printf-formatted error message
140      */
141     void trace(char *fmt, ...);
143     JSRuntime *rt;
145     JSContext *cx;
147     JSObject *globalObj;
149     static void errorReporter(JSContext *cx,
150         const char *message, JSErrorReport *report)
151         {
152         JavascriptEngine *engine = 
153                 (JavascriptEngine *) JS_GetContextPrivate(cx);
154         engine->error((char *)message);
155         }
157     
158     
160 };
164 } // namespace dom
165 } // namespace w3c
166 } // namespace org
169 #endif /* __JSENGINE_H__ */