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"
|
2016-09-05 11:12:58 +00:00
|
|
|
#if QT_CONFIG(qml_interpreter)
|
2013-01-29 13:20:50 +00:00
|
|
|
#include "private/qv4isel_moth_p.h"
|
|
|
|
#include "private/qv4vme_moth_p.h"
|
2016-09-05 11:12:58 +00:00
|
|
|
#endif
|
2013-04-15 09:50:16 +00:00
|
|
|
#include "private/qv4objectproto_p.h"
|
2013-01-29 13:20:50 +00:00
|
|
|
#include "private/qv4isel_p.h"
|
2013-04-15 09:50:16 +00:00
|
|
|
#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"
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2013-05-07 09:54:45 +00:00
|
|
|
#ifdef V4_ENABLE_JIT
|
|
|
|
# include "private/qv4isel_masm_p.h"
|
2016-09-05 11:12:58 +00:00
|
|
|
#else
|
|
|
|
QT_REQUIRE_CONFIG(qml_interpreter);
|
2013-05-07 09:54:45 +00:00
|
|
|
#endif // V4_ENABLE_JIT
|
|
|
|
|
2014-03-09 21:27:34 +00:00
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
#include <QtCore/QFile>
|
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>
|
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
namespace builtins {
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2013-04-19 13:37:33 +00:00
|
|
|
using namespace QV4;
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2013-02-07 15:14:44 +00:00
|
|
|
struct Print: FunctionObject
|
2012-05-07 14:05:05 +00:00
|
|
|
{
|
2014-11-03 02:17:57 +00:00
|
|
|
struct Data : Heap::FunctionObject {
|
2016-09-09 13:37:57 +00:00
|
|
|
void init(ExecutionContext *scope)
|
2015-01-10 19:51:15 +00:00
|
|
|
{
|
2016-09-09 13:37:57 +00:00
|
|
|
Heap::FunctionObject::init(scope, QStringLiteral("print"));
|
2014-05-09 10:15:23 +00:00
|
|
|
}
|
|
|
|
};
|
2014-06-13 14:04:39 +00:00
|
|
|
V4_OBJECT(FunctionObject)
|
2012-05-13 11:50:55 +00:00
|
|
|
|
2016-06-22 08:12:13 +00:00
|
|
|
static void call(const Managed *, Scope &scope, CallData *callData)
|
2012-05-07 14:05:05 +00:00
|
|
|
{
|
2013-09-06 10:44:12 +00:00
|
|
|
for (int i = 0; i < callData->argc; ++i) {
|
2013-09-09 12:33:28 +00:00
|
|
|
QString s = callData->args[i].toQStringNoThrow();
|
2012-05-07 14:05:05 +00:00
|
|
|
if (i)
|
|
|
|
std::cout << ' ';
|
2013-06-22 08:02:06 +00:00
|
|
|
std::cout << qPrintable(s);
|
2012-05-07 14:05:05 +00:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
2016-06-22 08:12:13 +00:00
|
|
|
scope.result = Encode::undefined();
|
2012-05-07 14:05:05 +00:00
|
|
|
}
|
|
|
|
};
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2014-04-04 10:22:00 +00:00
|
|
|
DEFINE_OBJECT_VTABLE(Print);
|
2013-02-14 13:07:57 +00:00
|
|
|
|
2013-02-07 15:14:44 +00:00
|
|
|
struct GC: public FunctionObject
|
2012-12-08 17:20:54 +00:00
|
|
|
{
|
2014-11-03 02:17:57 +00:00
|
|
|
struct Data : Heap::FunctionObject {
|
2016-09-09 13:37:57 +00:00
|
|
|
void init(ExecutionContext *scope)
|
2014-05-09 10:15:23 +00:00
|
|
|
{
|
2016-09-09 13:37:57 +00:00
|
|
|
Heap::FunctionObject::init(scope, QStringLiteral("gc"));
|
2014-05-09 10:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2014-06-13 14:04:39 +00:00
|
|
|
V4_OBJECT(FunctionObject)
|
2014-05-09 10:15:23 +00:00
|
|
|
|
2016-06-22 08:12:13 +00:00
|
|
|
static void call(const Managed *m, Scope &scope, CallData *)
|
2012-12-08 17:20:54 +00:00
|
|
|
{
|
2015-02-13 12:39:20 +00:00
|
|
|
static_cast<const GC *>(m)->engine()->memoryManager->runGC();
|
2016-06-22 08:12:13 +00:00
|
|
|
scope.result = Encode::undefined();
|
2012-12-08 17:20:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-04 10:22:00 +00:00
|
|
|
DEFINE_OBJECT_VTABLE(GC);
|
2013-02-14 13:07:57 +00:00
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
} // builtins
|
|
|
|
|
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 {
|
2014-05-06 07:23:59 +00:00
|
|
|
QV4::ScopedString m(scope, scope.engine->newString(QStringLiteral("message")));
|
2014-12-01 15:13:20 +00:00
|
|
|
QV4::ScopedValue message(scope, e->get(m));
|
2013-09-18 13:34:13 +00:00
|
|
|
std::cerr << "Uncaught exception: " << qPrintable(message->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) {
|
2013-05-27 12:19:35 +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);
|
|
|
|
QStringList args = app.arguments();
|
|
|
|
args.removeFirst();
|
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
enum {
|
|
|
|
use_masm,
|
2013-08-08 06:58:48 +00:00
|
|
|
use_moth
|
2013-06-25 14:07:41 +00:00
|
|
|
} mode;
|
|
|
|
#ifdef V4_ENABLE_JIT
|
|
|
|
mode = use_masm;
|
|
|
|
#else
|
|
|
|
mode = use_moth;
|
|
|
|
#endif
|
2012-11-08 11:45:40 +00:00
|
|
|
|
2013-06-17 11:36:21 +00:00
|
|
|
bool runAsQml = false;
|
2012-11-29 13:41:26 +00:00
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
if (!args.isEmpty()) {
|
|
|
|
if (args.first() == QLatin1String("--jit")) {
|
|
|
|
mode = use_masm;
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
|
|
|
|
2016-09-05 11:12:58 +00:00
|
|
|
#if QT_CONFIG(qml_interpreter)
|
2012-11-08 11:45:40 +00:00
|
|
|
if (args.first() == QLatin1String("--interpret")) {
|
|
|
|
mode = use_moth;
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
2016-09-05 11:12:58 +00:00
|
|
|
#endif
|
2012-11-08 11:45:40 +00:00
|
|
|
|
2013-06-17 11:36:21 +00:00
|
|
|
if (args.first() == QLatin1String("--qml")) {
|
|
|
|
runAsQml = true;
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
if (args.first() == 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
|
|
|
}
|
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
switch (mode) {
|
|
|
|
case use_masm:
|
|
|
|
case use_moth: {
|
2014-02-14 12:58:40 +00:00
|
|
|
QV4::EvalISelFactory* iSelFactory = 0;
|
2012-12-04 12:40:18 +00:00
|
|
|
if (mode == use_moth) {
|
2016-09-05 11:12:58 +00:00
|
|
|
#if QT_CONFIG(qml_interpreter)
|
2014-02-14 12:58:40 +00:00
|
|
|
iSelFactory = new QV4::Moth::ISelFactory;
|
2016-09-05 11:12:58 +00:00
|
|
|
#endif
|
2013-05-07 09:54:45 +00:00
|
|
|
#ifdef V4_ENABLE_JIT
|
2012-12-04 12:40:18 +00:00
|
|
|
} else {
|
2014-02-14 12:58:40 +00:00
|
|
|
iSelFactory = new QV4::JIT::ISelFactory;
|
2013-05-07 09:54:45 +00:00
|
|
|
#endif // V4_ENABLE_JIT
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
2012-11-29 13:41:26 +00:00
|
|
|
|
2013-04-19 13:37:33 +00:00
|
|
|
QV4::ExecutionEngine vm(iSelFactory);
|
2012-11-29 13:41:26 +00:00
|
|
|
|
2014-11-28 09:05:24 +00:00
|
|
|
QV4::Scope scope(&vm);
|
|
|
|
QV4::ScopedContext ctx(scope, vm.rootContext());
|
2012-06-05 16:32:52 +00:00
|
|
|
|
2015-08-28 10:58:08 +00:00
|
|
|
QV4::ScopedObject print(scope, vm.memoryManager->allocObject<builtins::Print>(vm.rootContext()));
|
2015-03-13 16:21:18 +00:00
|
|
|
vm.globalObject->put(QV4::ScopedString(scope, vm.newIdentifier(QStringLiteral("print"))).getPointer(), print);
|
2015-08-28 10:58:08 +00:00
|
|
|
QV4::ScopedObject gc(scope, vm.memoryManager->allocObject<builtins::GC>(ctx));
|
2015-03-13 16:21:18 +00:00
|
|
|
vm.globalObject->put(QV4::ScopedString(scope, vm.newIdentifier(QStringLiteral("gc"))).getPointer(), gc);
|
2012-11-08 11:45:40 +00:00
|
|
|
|
2016-08-11 10:08:00 +00:00
|
|
|
for (const QString &fn : qAsConst(args)) {
|
2012-11-08 11:45:40 +00:00
|
|
|
QFile file(fn);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
const QString code = QString::fromUtf8(file.readAll());
|
|
|
|
file.close();
|
|
|
|
|
2013-10-21 07:50:27 +00:00
|
|
|
QV4::ScopedValue result(scope);
|
|
|
|
QV4::Script script(ctx, code, fn);
|
|
|
|
script.parseAsBinding = runAsQml;
|
|
|
|
script.parse();
|
|
|
|
if (!scope.engine->hasException)
|
|
|
|
result = script.run();
|
|
|
|
if (scope.engine->hasException) {
|
2013-09-11 11:23:21 +00:00
|
|
|
QV4::StackTrace trace;
|
2014-11-12 12:55:55 +00:00
|
|
|
QV4::ScopedValue ex(scope, scope.engine->catchException(&trace));
|
2013-09-11 11:23:21 +00:00
|
|
|
showException(ctx, ex, trace);
|
2012-11-18 23:00:50 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2013-10-21 07:50:27 +00:00
|
|
|
if (!result->isUndefined()) {
|
|
|
|
if (! qgetenv("SHOW_EXIT_VALUE").isEmpty())
|
2014-11-12 08:21:41 +00:00
|
|
|
std::cout << "exit value: " << qPrintable(result->toQString()) << std::endl;
|
2013-10-21 07:50:27 +00:00
|
|
|
}
|
2012-11-13 12:44:16 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "Error: cannot open file " << fn.toUtf8().constData() << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
2012-11-08 11:45:40 +00:00
|
|
|
}
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2013-01-03 11:26:19 +00:00
|
|
|
vm.memoryManager->dumpStats();
|
2012-11-08 11:45:40 +00:00
|
|
|
} return EXIT_SUCCESS;
|
2013-01-23 09:12:51 +00:00
|
|
|
} // switch (mode)
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|