2012-10-12 08:12:24 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2012-10-12 08:12:24 +00:00
|
|
|
**
|
|
|
|
** This file is part of the V4VM module of the Qt Toolkit.
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2012-10-12 08:12:24 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 11:55:39 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-19 11:23:05 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-10-12 08:12:24 +00:00
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2012-10-12 08:12:24 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2013-04-15 09:50:16 +00:00
|
|
|
#include "private/qv4object_p.h"
|
|
|
|
#include "private/qv4runtime_p.h"
|
|
|
|
#include "private/qv4functionobject_p.h"
|
|
|
|
#include "private/qv4errorobject_p.h"
|
|
|
|
#include "private/qv4globalobject_p.h"
|
2013-01-29 13:20:50 +00:00
|
|
|
#include "private/qv4codegen_p.h"
|
2013-04-15 09:50:16 +00:00
|
|
|
#include "private/qv4objectproto_p.h"
|
|
|
|
#include "private/qv4mm_p.h"
|
|
|
|
#include "private/qv4context_p.h"
|
2013-05-22 14:53:35 +00:00
|
|
|
#include "private/qv4script_p.h"
|
2015-02-14 21:46:41 +00:00
|
|
|
#include "private/qv4string_p.h"
|
2016-10-22 03:24:20 +00:00
|
|
|
#include "private/qqmlbuiltinfunctions_p.h"
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2014-03-09 21:27:34 +00:00
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
#include <QtCore/QFile>
|
2016-10-22 02:08:27 +00:00
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
#include <QtCore/QDateTime>
|
2012-04-16 19:23:25 +00:00
|
|
|
#include <private/qqmljsengine_p.h>
|
|
|
|
#include <private/qqmljslexer_p.h>
|
|
|
|
#include <private/qqmljsparser_p.h>
|
|
|
|
#include <private/qqmljsast_p.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2015-01-15 14:01:06 +00:00
|
|
|
static void showException(QV4::ExecutionContext *ctx, const QV4::Value &exception, const QV4::StackTrace &trace)
|
2012-11-20 08:47:18 +00:00
|
|
|
{
|
2013-09-12 13:27:01 +00:00
|
|
|
QV4::Scope scope(ctx);
|
2015-01-15 14:01:06 +00:00
|
|
|
QV4::ScopedValue ex(scope, exception);
|
2015-02-13 09:42:01 +00:00
|
|
|
QV4::ErrorObject *e = ex->as<QV4::ErrorObject>();
|
2012-11-28 10:00:23 +00:00
|
|
|
if (!e) {
|
2014-11-12 08:21:41 +00:00
|
|
|
std::cerr << "Uncaught exception: " << qPrintable(ex->toQString()) << std::endl;
|
2013-05-25 13:31:23 +00:00
|
|
|
} else {
|
2017-01-31 16:19:33 +00:00
|
|
|
std::cerr << "Uncaught exception: " << qPrintable(e->toQStringNoThrow()) << std::endl;
|
2013-05-25 13:31:23 +00:00
|
|
|
}
|
2012-11-28 10:00:23 +00:00
|
|
|
|
2016-08-11 10:08:00 +00:00
|
|
|
for (const QV4::StackFrame &frame : trace) {
|
2017-08-04 16:54:12 +00:00
|
|
|
std::cerr << " at " << qPrintable(frame.function) << " (" << qPrintable(frame.source);
|
2013-05-25 13:31:23 +00:00
|
|
|
if (frame.line >= 0)
|
2015-10-13 10:26:45 +00:00
|
|
|
std::cerr << ':' << frame.line;
|
|
|
|
std::cerr << ')' << std::endl;
|
2012-11-28 10:00:23 +00:00
|
|
|
}
|
2012-11-20 08:47:18 +00:00
|
|
|
}
|
|
|
|
|
2012-04-16 19:23:25 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
2016-11-08 12:09:54 +00:00
|
|
|
QCoreApplication::setApplicationVersion(QLatin1String(QT_VERSION_STR));
|
2012-04-16 19:23:25 +00:00
|
|
|
QStringList args = app.arguments();
|
|
|
|
args.removeFirst();
|
|
|
|
|
2013-06-17 11:36:21 +00:00
|
|
|
bool runAsQml = false;
|
2016-10-22 02:08:27 +00:00
|
|
|
bool cache = false;
|
2012-11-29 13:41:26 +00:00
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
if (!args.isEmpty()) {
|
2016-08-15 10:30:48 +00:00
|
|
|
if (args.constFirst() == QLatin1String("--qml")) {
|
2013-06-17 11:36:21 +00:00
|
|
|
runAsQml = true;
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
|
|
|
|
2016-11-03 10:01:45 +00:00
|
|
|
if (args.constFirst() == QLatin1String("--cache")) {
|
2016-10-22 02:08:27 +00:00
|
|
|
cache = true;
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
|
|
|
|
2016-08-15 10:30:48 +00:00
|
|
|
if (args.constFirst() == QLatin1String("--help")) {
|
2014-12-18 10:46:37 +00:00
|
|
|
std::cerr << "Usage: qmljs [|--jit|--interpret|--qml] file..." << std::endl;
|
2012-11-08 11:45:40 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2012-06-05 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 07:44:55 +00:00
|
|
|
QV4::ExecutionEngine vm;
|
2012-11-29 13:41:26 +00:00
|
|
|
|
2017-06-22 07:44:55 +00:00
|
|
|
QV4::Scope scope(&vm);
|
|
|
|
QV4::ScopedContext ctx(scope, vm.rootContext());
|
2012-06-05 16:32:52 +00:00
|
|
|
|
2017-06-22 07:44:55 +00:00
|
|
|
QV4::GlobalExtensions::init(vm.globalObject, QJSEngine::ConsoleExtension | QJSEngine::GarbageCollectionExtension);
|
2012-11-08 11:45:40 +00:00
|
|
|
|
2017-06-22 07:44:55 +00:00
|
|
|
for (const QString &fn : qAsConst(args)) {
|
|
|
|
QFile file(fn);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
QScopedPointer<QV4::Script> script;
|
|
|
|
if (cache && QFile::exists(fn + QLatin1Char('c'))) {
|
2017-06-30 13:34:12 +00:00
|
|
|
QQmlRefPointer<QV4::CompiledData::CompilationUnit> unit = QV4::Compiler::Codegen::createUnitForLoading();
|
2017-06-22 07:44:55 +00:00
|
|
|
QString error;
|
|
|
|
if (unit->loadFromDisk(QUrl::fromLocalFile(fn), QFileInfo(fn).lastModified(), &error)) {
|
|
|
|
script.reset(new QV4::Script(&vm, nullptr, unit));
|
|
|
|
} else {
|
|
|
|
std::cout << "Error loading" << qPrintable(fn) << "from disk cache:" << qPrintable(error) << std::endl;
|
2016-10-22 02:08:27 +00:00
|
|
|
}
|
2017-06-22 07:44:55 +00:00
|
|
|
}
|
|
|
|
if (!script) {
|
|
|
|
const QString code = QString::fromUtf8(file.readAll());
|
|
|
|
file.close();
|
2012-11-08 11:45:40 +00:00
|
|
|
|
2017-08-30 19:53:03 +00:00
|
|
|
script.reset(new QV4::Script(ctx, QV4::Compiler::GlobalCode, code, fn));
|
2017-06-22 07:44:55 +00:00
|
|
|
script->parseAsBinding = runAsQml;
|
|
|
|
script->parse();
|
|
|
|
}
|
|
|
|
QV4::ScopedValue result(scope);
|
|
|
|
if (!scope.engine->hasException) {
|
|
|
|
const auto unit = script->compilationUnit;
|
|
|
|
if (cache && unit && !(unit->data->flags & QV4::CompiledData::Unit::StaticData)) {
|
|
|
|
if (unit->data->sourceTimeStamp == 0) {
|
|
|
|
const_cast<QV4::CompiledData::Unit*>(unit->data)->sourceTimeStamp = QFileInfo(fn).lastModified().toMSecsSinceEpoch();
|
|
|
|
}
|
|
|
|
QString saveError;
|
|
|
|
if (!unit->saveToDisk(QUrl::fromLocalFile(fn), &saveError)) {
|
|
|
|
std::cout << "Error saving JS cache file: " << qPrintable(saveError) << std::endl;
|
2016-10-22 02:08:27 +00:00
|
|
|
}
|
2013-10-21 07:50:27 +00:00
|
|
|
}
|
2017-06-22 07:44:55 +00:00
|
|
|
result = script->run();
|
|
|
|
}
|
|
|
|
if (scope.engine->hasException) {
|
|
|
|
QV4::StackTrace trace;
|
|
|
|
QV4::ScopedValue ex(scope, scope.engine->catchException(&trace));
|
|
|
|
showException(ctx, ex, trace);
|
2012-11-13 12:44:16 +00:00
|
|
|
return EXIT_FAILURE;
|
2012-11-08 11:45:40 +00:00
|
|
|
}
|
2017-06-22 07:44:55 +00:00
|
|
|
if (!result->isUndefined()) {
|
|
|
|
if (! qgetenv("SHOW_EXIT_VALUE").isEmpty())
|
|
|
|
std::cout << "exit value: " << qPrintable(result->toQString()) << std::endl;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::cerr << "Error: cannot open file " << fn.toUtf8().constData() << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
2017-06-22 07:44:55 +00:00
|
|
|
}
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2017-06-22 07:44:55 +00:00
|
|
|
vm.memoryManager->dumpStats();
|
|
|
|
return EXIT_SUCCESS;
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|