fix logic
[personal-kdelibs.git] / kjs / api / kjsinterpreter.cpp
blob6a346b6bf711d07a065516ca3c73313c5b88fafb
1 /*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2008 Harri Porten (porten@kde.org)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #include "kjsinterpreter.h"
23 #include "kjsprivate.h"
24 #include "kjs/interpreter.h"
25 #include "kjs/completion.h"
26 #include "kjs/object.h"
27 #include "kjs/JSVariableObject.h"
28 #include <QString>
29 #include <stdio.h>
31 using namespace KJS;
33 class KJSResultHandle
35 public:
36 KJSResultHandle() : rc(1), val(KJSUndefined()) { }
38 int rc;
39 KJSObject val;
40 UString errMsg;
42 void ref() { ++rc; }
43 void deref() { if (--rc == 0) delete this; }
46 KJSResult::KJSResult()
47 : hnd(new KJSResultHandle())
51 KJSResult::KJSResult(const KJSResult& r)
53 hnd = r.hnd;
54 hnd->ref();
57 KJSResult& KJSResult::operator=(const KJSResult& r)
59 if (hnd != r.hnd) {
60 r.hnd->ref();
61 hnd->deref();
62 hnd = r.hnd;
65 return *this;
68 KJSResult::~KJSResult()
70 hnd->deref();
73 bool KJSResult::isException() const
75 return !hnd->errMsg.isNull();
78 QString KJSResult::errorMessage() const
80 return toQString(hnd->errMsg);
83 KJSObject KJSResult::value() const
85 return hnd->val;
88 KJSInterpreter::KJSInterpreter()
89 : globCtx(0)
91 Interpreter* ip = new Interpreter();
92 ip->ref();
93 hnd = INTERPRETER_HANDLE(ip);
96 KJSInterpreter::KJSInterpreter(const KJSGlobalObject& global)
97 : globCtx(0)
99 JSValue* gv = JSVALUE(&global);
100 assert(gv->isObject());
101 JSObject* go = static_cast<JSObject*>(gv);
102 assert(go->isGlobalObject());
103 Interpreter* ip = new Interpreter(static_cast<JSGlobalObject*>(go));
104 ip->ref();
105 assert(go->prototype()->isObject());
106 JSObject* p = static_cast<JSObject*>(go->prototype());
107 JSObject* objectProto = ip->builtinObjectPrototype();
108 p->setPrototype(objectProto);
109 hnd = INTERPRETER_HANDLE(ip);
112 KJSInterpreter::KJSInterpreter(const KJSInterpreter& other)
113 : globCtx(0)
115 Interpreter* ip = INTERPRETER(&other);
116 ip->ref();
117 hnd = INTERPRETER_HANDLE(ip);
118 globCtx.hnd = EXECSTATE_HANDLE(ip->globalExec());
121 KJSInterpreter& KJSInterpreter::operator=(const KJSInterpreter& other)
123 Interpreter* thisIp = INTERPRETER(this);
124 Interpreter* otherIp = INTERPRETER(&other);
125 if (otherIp != thisIp) {
126 otherIp->ref();
127 thisIp->deref();
128 hnd = INTERPRETER_HANDLE(otherIp);
129 globCtx.hnd = EXECSTATE_HANDLE(otherIp->globalExec());
131 return *this;
134 KJSInterpreter::KJSInterpreter(KJSInterpreterHandle* h)
135 : hnd(h), globCtx(0)
137 Interpreter* ip = INTERPRETER(this);
138 globCtx.hnd = EXECSTATE_HANDLE(ip->globalExec());
141 KJSInterpreter::~KJSInterpreter()
143 Interpreter* ip = INTERPRETER(this);
144 ip->deref();
145 ip = 0;
148 KJSContext* KJSInterpreter::globalContext()
150 Interpreter* ip = INTERPRETER(this);
152 globCtx.hnd = EXECSTATE_HANDLE(ip->globalExec());
153 return &globCtx;
156 KJSObject KJSInterpreter::globalObject()
158 Interpreter* ip = INTERPRETER(this);
160 return KJSObject(JSVALUE_HANDLE(ip->globalObject()));
163 KJSResult KJSInterpreter::evaluate(const QString& sourceURL,
164 int startingLineNumber,
165 const QString& code,
166 KJSObject* thisValue)
168 Interpreter* ip = INTERPRETER(this);
170 JSValue* tv = thisValue ? JSVALUE(thisValue) : 0;
171 KJS::Completion c = ip->evaluate(toUString(sourceURL), startingLineNumber,
172 toUString(code), tv);
174 KJSResult res;
175 if (c.complType() == Throw) {
176 ExecState* exec = ip->globalExec();
177 UString msg = c.value()->toString(exec);
178 #if 0
179 JSObject* resObj = c.value()->toObject(exec);
180 CString message = resObj->toString(exec).UTF8String();
181 int line = resObj->toObject(exec)->get(exec, "line")->toUInt32(exec);
183 if (!sourceURL.isEmpty())
184 fprintf(stderr, "%s (line %d): ", qPrintable(sourceURL), line);
185 fprintf(stderr, "%s\n", msg.c_str());
186 #endif
187 fprintf(stderr, "evaluate() threw an exception\n");
188 res.hnd->errMsg = msg;
189 } else {
190 if (c.isValueCompletion())
191 res.hnd->val = KJSObject(JSVALUE_HANDLE(c.value()));
194 return res;
197 KJSResult KJSInterpreter::evaluate(const QString& code,
198 KJSObject* thisValue)
200 return evaluate("<string>", 0, code, thisValue);
203 bool KJSInterpreter::normalizeCode(const QString& code, QString* normalized,
204 int* errLine, QString* errMsg)
206 assert(normalized);
208 UString codeOut, msg;
209 bool success = Interpreter::normalizeCode(toUString(code), &codeOut,
210 errLine, &msg);
212 *normalized = toQString(codeOut);
213 if (errMsg)
214 *errMsg = toQString(msg);
216 return success;