History

Move BaseScriptEngine from script-engine to shared.

humbletim committed 9 years ago

Commit 9b096511 parent e91de172 files · +0 −0Browse files

Showing the changes to libraries/shared/src/BaseScriptEngine.cpp (1 of 2 files). Show all

Unified · Split

libraries/shared/src/BaseScriptEngine.cpp+287−0
@@ -0,0 +1,287 @@
1//
2// BaseScriptEngine.cpp
3// libraries/script-engine/src
4//
5// Created by Timothy Dedischew on 02/01/17.
6// Copyright 2017 High Fidelity, Inc.
7//
8// Distributed under the Apache License, Version 2.0.
9// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10//
11
12#include "BaseScriptEngine.h"
13
14#include <QtCore/QString>
15#include <QtCore/QThread>
16#include <QtCore/QUrl>
17#include <QtScript/QScriptValue>
18#include <QtScript/QScriptValueIterator>
19#include <QtScript/QScriptContextInfo>
20
21#include "ScriptEngineLogging.h"
22#include "Profile.h"
23
24const QString BaseScriptEngine::_SETTINGS_ENABLE_EXTENDED_EXCEPTIONS {
25 "com.highfidelity.experimental.enableExtendedJSExceptions"
26};
27
28const QString BaseScriptEngine::SCRIPT_EXCEPTION_FORMAT { "[%0] %1 in %2:%3" };
29const QString BaseScriptEngine::SCRIPT_BACKTRACE_SEP { "\n " };
30
31// engine-aware JS Error copier and factory
32QScriptValue BaseScriptEngine::makeError(const QScriptValue& _other, const QString& type) {
33 auto other = _other;
34 if (other.isString()) {
35 other = newObject();
36 other.setProperty("message", _other.toString());
37 }
38 auto proto = globalObject().property(type);
39 if (!proto.isFunction()) {
40 proto = globalObject().property(other.prototype().property("constructor").property("name").toString());
41 }
42 if (!proto.isFunction()) {
43#ifdef DEBUG_JS_EXCEPTIONS
44 qCDebug(scriptengine) << "BaseScriptEngine::makeError -- couldn't find constructor for" << type << " -- using Error instead";
45#endif
46 proto = globalObject().property("Error");
47 }
48 if (other.engine() != this) {
49 // JS Objects are parented to a specific script engine instance
50 // -- this effectively ~clones it locally by routing through a QVariant and back
51 other = toScriptValue(other.toVariant());
52 }
53 // ~ var err = new Error(other.message)
54 auto err = proto.construct(QScriptValueList({other.property("message")}));
55
56 // transfer over any existing properties
57 QScriptValueIterator it(other);
58 while (it.hasNext()) {
59 it.next();
60 err.setProperty(it.name(), it.value());
61 }
62 return err;
63}
64
65// check syntax and when there are issues returns an actual "SyntaxError" with the details
66QScriptValue BaseScriptEngine::lintScript(const QString& sourceCode, const QString& fileName, const int lineNumber) {
67 const auto syntaxCheck = checkSyntax(sourceCode);
68 if (syntaxCheck.state() != QScriptSyntaxCheckResult::Valid) {
69 auto err = globalObject().property("SyntaxError")
70 .construct(QScriptValueList({syntaxCheck.errorMessage()}));
71 err.setProperty("fileName", fileName);
72 err.setProperty("lineNumber", syntaxCheck.errorLineNumber());
73 err.setProperty("expressionBeginOffset", syntaxCheck.errorColumnNumber());
74 err.setProperty("stack", currentContext()->backtrace().join(SCRIPT_BACKTRACE_SEP));
75 {
76 const auto error = syntaxCheck.errorMessage();
77 const auto line = QString::number(syntaxCheck.errorLineNumber());
78 const auto column = QString::number(syntaxCheck.errorColumnNumber());
79 // for compatibility with legacy reporting
80 const auto message = QString("[SyntaxError] %1 in %2:%3(%4)").arg(error, fileName, line, column);
81 err.setProperty("formatted", message);
82 }
83 return err;
84 }
85 return undefinedValue();
86}
87
88// this pulls from the best available information to create a detailed snapshot of the current exception
89QScriptValue BaseScriptEngine::cloneUncaughtException(const QString& extraDetail) {
90 if (!hasUncaughtException()) {
91 return QScriptValue();
92 }
93 auto exception = uncaughtException();
94 // ensure the error object is engine-local
95 auto err = makeError(exception);
96
97 // not sure why Qt does't offer uncaughtExceptionFileName -- but the line number
98 // on its own is often useless/wrong if arbitrarily married to a filename.
99 // when the error object already has this info, it seems to be the most reliable
100 auto fileName = exception.property("fileName").toString();
101 auto lineNumber = exception.property("lineNumber").toInt32();
102
103 // the backtrace, on the other hand, seems most reliable taken from uncaughtExceptionBacktrace
104 auto backtrace = uncaughtExceptionBacktrace();
105 if (backtrace.isEmpty()) {
106 // fallback to the error object
107 backtrace = exception.property("stack").toString().split(SCRIPT_BACKTRACE_SEP);
108 }
109 // the ad hoc "detail" property can be used now to embed additional clues
110 auto detail = exception.property("detail").toString();
111 if (detail.isEmpty()) {
112 detail = extraDetail;
113 } else if (!extraDetail.isEmpty()) {
114 detail += "(" + extraDetail + ")";
115 }
116 if (lineNumber <= 0) {
117 lineNumber = uncaughtExceptionLineNumber();
118 }
119 if (fileName.isEmpty()) {
120 // climb the stack frames looking for something useful to display
121 for (auto c = currentContext(); c && fileName.isEmpty(); c = c->parentContext()) {
122 QScriptContextInfo info { c };
123 if (!info.fileName().isEmpty()) {
124 // take fileName:lineNumber as a pair
125 fileName = info.fileName();
126 lineNumber = info.lineNumber();
127 if (backtrace.isEmpty()) {
128 backtrace = c->backtrace();
129 }
130 break;
131 }
132 }
133 }
134 err.setProperty("fileName", fileName);
135 err.setProperty("lineNumber", lineNumber );
136 err.setProperty("detail", detail);
137 err.setProperty("stack", backtrace.join(SCRIPT_BACKTRACE_SEP));
138
139#ifdef DEBUG_JS_EXCEPTIONS
140 err.setProperty("_fileName", exception.property("fileName").toString());
141 err.setProperty("_stack", uncaughtExceptionBacktrace().join(SCRIPT_BACKTRACE_SEP));
142 err.setProperty("_lineNumber", uncaughtExceptionLineNumber());
143#endif
144 return err;
145}
146
147QString BaseScriptEngine::formatException(const QScriptValue& exception) {
148 QString note { "UncaughtException" };
149 QString result;
150
151 if (!exception.isObject()) {
152 return result;
153 }
154 const auto message = exception.toString();
155 const auto fileName = exception.property("fileName").toString();
156 const auto lineNumber = exception.property("lineNumber").toString();
157 const auto stacktrace = exception.property("stack").toString();
158
159 if (_enableExtendedJSExceptions.get()) {
160 // This setting toggles display of the hints now being added during the loading process.
161 // Example difference:
162 // [UncaughtExceptions] Error: Can't find variable: foobar in atp:/myentity.js\n...
163 // [UncaughtException (construct {1eb5d3fa-23b1-411c-af83-163af7220e3f})] Error: Can't find variable: foobar in atp:/myentity.js\n...
164 if (exception.property("detail").isValid()) {
165 note += " " + exception.property("detail").toString();
166 }
167 }
168
169 result = QString(SCRIPT_EXCEPTION_FORMAT).arg(note, message, fileName, lineNumber);
170 if (!stacktrace.isEmpty()) {
171 result += QString("\n[Backtrace]%1%2").arg(SCRIPT_BACKTRACE_SEP).arg(stacktrace);
172 }
173 return result;
174}
175
176QScriptValue BaseScriptEngine::evaluateInClosure(const QScriptValue& closure, const QScriptProgram& program) {
177 PROFILE_RANGE(script, "evaluateInClosure");
178 if (QThread::currentThread() != thread()) {
179 qCCritical(scriptengine) << "*** CRITICAL *** ScriptEngine::evaluateInClosure() is meant to be called from engine thread only.";
180 // note: a recursive mutex might be needed around below code if this method ever becomes Q_INVOKABLE
181 return QScriptValue();
182 }
183
184 const auto fileName = program.fileName();
185 const auto shortName = QUrl(fileName).fileName();
186
187 QScriptValue result;
188 QScriptValue oldGlobal;
189 auto global = closure.property("global");
190 if (global.isObject()) {
191#ifdef DEBUG_JS
192 qCDebug(scriptengine) << " setting global = closure.global" << shortName;
193#endif
194 oldGlobal = globalObject();
195 setGlobalObject(global);
196 }
197
198 auto context = pushContext();
199
200 auto thiz = closure.property("this");
201 if (thiz.isObject()) {
202#ifdef DEBUG_JS
203 qCDebug(scriptengine) << " setting this = closure.this" << shortName;
204#endif
205 context->setThisObject(thiz);
206 }
207
208 context->pushScope(closure);
209#ifdef DEBUG_JS
210 qCDebug(scriptengine) << QString("[%1] evaluateInClosure %2").arg(isEvaluating()).arg(shortName);
211#endif
212 {
213 result = BaseScriptEngine::evaluate(program);
214 if (hasUncaughtException()) {
215 auto err = cloneUncaughtException(__FUNCTION__);
216#ifdef DEBUG_JS_EXCEPTIONS
217 qCWarning(scriptengine) << __FUNCTION__ << "---------- hasCaught:" << err.toString() << result.toString();
218 err.setProperty("_result", result);
219#endif
220 result = err;
221 }
222 }
223#ifdef DEBUG_JS
224 qCDebug(scriptengine) << QString("[%1] //evaluateInClosure %2").arg(isEvaluating()).arg(shortName);
225#endif
226 popContext();
227
228 if (oldGlobal.isValid()) {
229#ifdef DEBUG_JS
230 qCDebug(scriptengine) << " restoring global" << shortName;
231#endif
232 setGlobalObject(oldGlobal);
233 }
234
235 return result;
236}
237
238// Lambda
239
240QScriptValue BaseScriptEngine::newLambdaFunction(std::function<QScriptValue(QScriptContext *, QScriptEngine*)> operation, const QScriptValue& data, const QScriptEngine::ValueOwnership& ownership) {
241 auto lambda = new Lambda(this, operation, data);
242 auto object = newQObject(lambda, ownership);
243 auto call = object.property("call");
244 call.setPrototype(object); // context->callee().prototype() === Lambda QObject
245 call.setData(data); // context->callee().data() will === data param
246 return call;
247}
248QString Lambda::toString() const {
249 return QString("[Lambda%1]").arg(data.isValid() ? " " + data.toString() : data.toString());
250}
251
252Lambda::~Lambda() {
253#ifdef DEBUG_JS_LAMBDA_FUNCS
254 qDebug() << "~Lambda" << "this" << this;
255#endif
256}
257
258Lambda::Lambda(QScriptEngine *engine, std::function<QScriptValue(QScriptContext *, QScriptEngine*)> operation, QScriptValue data)
259 : engine(engine), operation(operation), data(data) {
260#ifdef DEBUG_JS_LAMBDA_FUNCS
261 qDebug() << "Lambda" << data.toString();
262#endif
263}
264QScriptValue Lambda::call() {
265 return operation(engine->currentContext(), engine);
266}
267
268#ifdef DEBUG_JS
269void BaseScriptEngine::_debugDump(const QString& header, const QScriptValue& object, const QString& footer) {
270 if (!header.isEmpty()) {
271 qCDebug(scriptengine) << header;
272 }
273 if (!object.isObject()) {
274 qCDebug(scriptengine) << "(!isObject)" << object.toVariant().toString() << object.toString();
275 return;
276 }
277 QScriptValueIterator it(object);
278 while (it.hasNext()) {
279 it.next();
280 qCDebug(scriptengine) << it.name() << ":" << it.value().toString();
281 }
282 if (!footer.isEmpty()) {
283 qCDebug(scriptengine) << footer;
284 }
285}
286#endif
287