2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
4 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "kjs_proxy.h"
24 #include "kjs_events.h"
25 #include "kjs_window.h"
27 #include "JSDOMWindow.h"
30 #include "JSSVGLazyEventListener.h"
37 KJSProxy::KJSProxy(Frame* frame)
51 JSValue* KJSProxy::evaluate(const String& filename, int baseLine, const String& str, Node* n)
53 // evaluate code. Returns the JS return value or 0
54 // if there was none, an error occured or the type couldn't be converted.
57 // inlineCode is true for <a href="javascript:doSomething()">
58 // and false for <script>doSomething()</script>. Check if it has the
59 // expected value in all cases.
60 // See smart window.open policy for where this is used.
61 bool inlineCode = filename.isNull();
63 m_script->setInlineCode(inlineCode);
67 JSValue* thisNode = n ? Window::retrieve(m_frame) : toJS(m_script->globalExec(), n);
68 Completion comp = m_script->evaluate(filename, baseLine, reinterpret_cast<const KJS::UChar*>(str.characters()), str.length(), thisNode);
70 if (comp.complType() == Normal || comp.complType() == ReturnValue)
73 if (comp.complType() == Throw) {
74 UString errorMessage = comp.value()->toString(m_script->globalExec());
75 int lineNumber = comp.value()->toObject(m_script->globalExec())->get(m_script->globalExec(), "line")->toInt32(m_script->globalExec());
76 UString sourceURL = comp.value()->toObject(m_script->globalExec())->get(m_script->globalExec(), "sourceURL")->toString(m_script->globalExec());
77 m_frame->addMessageToConsole(errorMessage, lineNumber, sourceURL);
83 void KJSProxy::clear() {
84 // clear resources allocated by the interpreter, and make it ready to be used by another page
85 // We have to keep it, so that the Window object for the frame remains the same.
86 // (we used to delete and re-create it, previously)
88 Window *win = Window::retrieveWindow(m_frame);
94 EventListener* KJSProxy::createHTMLEventHandler(const String& functionName, const String& code, Node* node)
98 return new JSLazyEventListener(functionName, code, Window::retrieveWindow(m_frame), node, m_handlerLineno);
102 EventListener* KJSProxy::createSVGEventHandler(const String& functionName, const String& code, Node* node)
104 initScriptIfNeeded();
106 return new JSSVGLazyEventListener(functionName, code, Window::retrieveWindow(m_frame), node, m_handlerLineno);
110 void KJSProxy::finishedWithEvent(Event* event)
112 // This is called when the DOM implementation has finished with a particular event. This
113 // is the case in sitations where an event has been created just for temporary usage,
114 // e.g. an image load or mouse move. Once the event has been dispatched, it is forgotten
115 // by the DOM implementation and so does not need to be cached still by the interpreter
116 m_script->forgetDOMObject(event);
119 ScriptInterpreter* KJSProxy::interpreter()
121 initScriptIfNeeded();
126 // Implementation of the debug() function
127 class TestFunctionImp : public DOMObject {
129 virtual bool implementsCall() const { return true; }
130 virtual JSValue* callAsFunction(ExecState*, JSObject*, const List& args);
133 JSValue *TestFunctionImp::callAsFunction(ExecState* exec, JSObject*, const List& args)
135 fprintf(stderr,"--> %s\n", args[0]->toString(exec).ascii());
136 return jsUndefined();
139 void KJSProxy::initScriptIfNeeded()
144 // Build the global object - which is a Window instance
146 JSObject* globalObject = new JSDOMWindow(m_frame->domWindow());
148 // Create a KJS interpreter for this frame
149 m_script = new ScriptInterpreter(globalObject, m_frame);
150 globalObject->put(m_script->globalExec(), "debug", new TestFunctionImp(), Internal);
152 String userAgent = m_frame->userAgent();
153 if (userAgent.find("Microsoft") >= 0 || userAgent.find("MSIE") >= 0)
154 m_script->setCompatMode(Interpreter::IECompat);
156 // If we find "Mozilla" but not "(compatible, ...)" we are a real Netscape
157 if (userAgent.find("Mozilla") >= 0 && userAgent.find("compatible") == -1)
158 m_script->setCompatMode(Interpreter::NetscapeCompat);