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 JSObject *new_Attr(Attr *obj);
79 JSObject *new_CDATASection(CDATASection *obj);
80 JSObject *new_CharacterData(CharacterData *obj);
81 JSObject *new_Comment(Comment *obj);
82 JSObject *new_Document(Document *obj);
83 JSObject *new_DocumentFragment(DocumentFragment *obj);
84 JSObject *new_DocumentType(DocumentType *obj);
85 JSObject *new_DOMConfiguration(DOMConfiguration *obj);
86 JSObject *new_DOMError(DOMError *obj);
87 JSObject *new_DOMException(DOMException *obj);
88 JSObject *new_DOMImplementation(DOMImplementation *obj);
89 JSObject *new_DOMImplementationList(DOMImplementationList *obj);
90 JSObject *new_DOMImplementationRegistry(DOMImplementationSource *obj);
91 JSObject *new_DOMImplementationSource(DOMImplementationSource *obj);
92 JSObject *new_DOMLocator(DOMLocator *obj);
93 JSObject *new_DOMStringList(DOMStringList *obj);
94 JSObject *new_Element(Element *obj);
95 JSObject *new_Entity(Entity *obj);
96 JSObject *new_EntityReference(EntityReference *obj);
97 JSObject *new_NamedNodeMap(NamedNodeMap *obj);
98 JSObject *new_NameList(NameList *obj);
99 JSObject *new_Node(Node *obj);
100 JSObject *new_NodeList(NodeList *obj);
101 JSObject *new_Notation(Notation *obj);
102 JSObject *new_ProcessingInstruction(ProcessingInstruction *obj);
103 JSObject *new_Text(Text *obj);
104 JSObject *new_TypeInfo(TypeInfo *obj);
105 JSObject *new_UserDataHandler(UserDataHandler *obj);
107 private:
109 /**
110 * Startup the javascript engine
111 */
112 bool startup();
114 /**
115 * Shutdown the javascript engine
116 */
117 bool shutdown();
119 void init()
120 {
121 rt = NULL;
122 cx = NULL;
123 globalObj = NULL;
124 }
126 /**
127 * Assignment operator. Let's keep this private for now,
128 * as we want one Spidermonkey runtime per c++ shell
129 */
130 JavascriptEngine &operator=(const JavascriptEngine &other)
131 { assign(other); return *this; }
133 void assign(const JavascriptEngine &other)
134 {
135 rt = other.rt;
136 cx = other.cx;
137 globalObj = other.globalObj;
138 }
140 /**
141 * Bind with the basic DOM classes
142 */
143 bool createClasses();
145 /**
146 * Ouput a printf-formatted error message
147 */
148 void error(char *fmt, ...);
150 /**
151 * Ouput a printf-formatted error message
152 */
153 void trace(char *fmt, ...);
155 JSRuntime *rt;
157 JSContext *cx;
159 JSObject *globalObj;
161 static void errorReporter(JSContext *cx,
162 const char *message, JSErrorReport *report)
163 {
164 JavascriptEngine *engine =
165 (JavascriptEngine *) JS_GetContextPrivate(cx);
166 engine->error((char *)message);
167 }
169 JSObject *proto_Attr;
170 JSObject *proto_CDATASection;
171 JSObject *proto_CharacterData;
172 JSObject *proto_Comment;
173 JSObject *proto_Document;
174 JSObject *proto_DocumentFragment;
175 JSObject *proto_DocumentType;
176 JSObject *proto_DOMConfiguration;
177 JSObject *proto_DOMError;
178 JSObject *proto_DOMException;
179 JSObject *proto_DOMImplementation;
180 JSObject *proto_DOMImplementationList;
181 JSObject *proto_DOMImplementationRegistry;
182 JSObject *proto_DOMImplementationSource;
183 JSObject *proto_DOMLocator;
184 JSObject *proto_DOMStringList;
185 JSObject *proto_Element;
186 JSObject *proto_Entity;
187 JSObject *proto_EntityReference;
188 JSObject *proto_NamedNodeMap;
189 JSObject *proto_NameList;
190 JSObject *proto_Node;
191 JSObject *proto_NodeList;
192 JSObject *proto_Notation;
193 JSObject *proto_ProcessingInstruction;
194 JSObject *proto_Text;
195 JSObject *proto_TypeInfo;
196 JSObject *proto_UserDataHandler;
200 };
204 } // namespace dom
205 } // namespace w3c
206 } // namespace org
209 #endif /* __JSENGINE_H__ */