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"
36 KJSResultHandle() : rc(1), val(KJSUndefined()) { }
43 void deref() { if (--rc
== 0) delete this; }
46 KJSResult::KJSResult()
47 : hnd(new KJSResultHandle())
51 KJSResult::KJSResult(const KJSResult
& r
)
57 KJSResult
& KJSResult::operator=(const KJSResult
& r
)
68 KJSResult::~KJSResult()
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
88 KJSInterpreter::KJSInterpreter()
91 Interpreter
* ip
= new Interpreter();
93 hnd
= INTERPRETER_HANDLE(ip
);
96 KJSInterpreter::KJSInterpreter(const KJSGlobalObject
& global
)
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
));
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
)
115 Interpreter
* ip
= INTERPRETER(&other
);
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
) {
128 hnd
= INTERPRETER_HANDLE(otherIp
);
129 globCtx
.hnd
= EXECSTATE_HANDLE(otherIp
->globalExec());
134 KJSInterpreter::KJSInterpreter(KJSInterpreterHandle
* h
)
137 Interpreter
* ip
= INTERPRETER(this);
138 globCtx
.hnd
= EXECSTATE_HANDLE(ip
->globalExec());
141 KJSInterpreter::~KJSInterpreter()
143 Interpreter
* ip
= INTERPRETER(this);
148 KJSContext
* KJSInterpreter::globalContext()
150 Interpreter
* ip
= INTERPRETER(this);
152 globCtx
.hnd
= EXECSTATE_HANDLE(ip
->globalExec());
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
,
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
);
175 if (c
.complType() == Throw
) {
176 ExecState
* exec
= ip
->globalExec();
177 UString msg
= c
.value()->toString(exec
);
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());
187 fprintf(stderr
, "evaluate() threw an exception\n");
188 res
.hnd
->errMsg
= msg
;
190 if (c
.isValueCompletion())
191 res
.hnd
->val
= KJSObject(JSVALUE_HANDLE(c
.value()));
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
)
208 UString codeOut
, msg
;
209 bool success
= Interpreter::normalizeCode(toUString(code
), &codeOut
,
212 *normalized
= toQString(codeOut
);
214 *errMsg
= toQString(msg
);