2012-10-12 08:12:24 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2015-01-28 11:55:39 +00:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://www.qt.io/licensing/
|
2012-10-12 08:12:24 +00:00
|
|
|
**
|
|
|
|
** This file is part of the V4VM module of the Qt Toolkit.
|
|
|
|
**
|
2014-08-22 06:13:59 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL21$
|
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
|
|
|
|
** and conditions see http://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at http://www.qt.io/contact-us.
|
2012-10-12 08:12:24 +00:00
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-08-22 06:13:59 +00:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-12 08:12:24 +00:00
|
|
|
**
|
2015-01-28 11:55:39 +00:00
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2012-10-12 08:12:24 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** $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"
|
|
|
|
#include "private/qv4isel_moth_p.h"
|
|
|
|
#include "private/qv4vme_moth_p.h"
|
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"
|
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"
|
|
|
|
#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 {
|
2014-05-09 10:15:23 +00:00
|
|
|
Data(ExecutionContext *scope)
|
2015-01-10 19:51:15 +00:00
|
|
|
: Heap::FunctionObject(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
|
|
|
|
2013-09-11 11:55:01 +00:00
|
|
|
static ReturnedValue call(Managed *, 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;
|
2013-09-25 10:24:36 +00:00
|
|
|
return 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 {
|
2014-05-09 10:15:23 +00:00
|
|
|
Data(ExecutionContext *scope)
|
2014-11-03 02:17:57 +00:00
|
|
|
: Heap::FunctionObject(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
|
|
|
|
2013-09-11 11:55:01 +00:00
|
|
|
static ReturnedValue call(Managed *m, CallData *)
|
2012-12-08 17:20:54 +00:00
|
|
|
{
|
2015-01-11 15:30:29 +00:00
|
|
|
static_cast<GC *>(m)->engine()->memoryManager->runGC();
|
2013-09-25 10:24:36 +00:00
|
|
|
return 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);
|
2013-09-12 13:27:01 +00:00
|
|
|
QV4::ErrorObject *e = ex->asErrorObject();
|
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
|
|
|
|
2013-09-11 11:23:21 +00:00
|
|
|
foreach (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)
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.first() == QLatin1String("--interpret")) {
|
|
|
|
mode = use_moth;
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-02-14 12:58:40 +00:00
|
|
|
iSelFactory = new QV4::Moth::ISelFactory;
|
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
|
|
|
|
2014-11-21 12:18:23 +00:00
|
|
|
QV4::ScopedObject globalObject(scope, vm.globalObject());
|
2014-06-13 12:30:03 +00:00
|
|
|
QV4::ScopedObject print(scope, vm.memoryManager->alloc<builtins::Print>(ctx));
|
2014-05-07 11:33:24 +00:00
|
|
|
globalObject->put(QV4::ScopedString(scope, vm.newIdentifier(QStringLiteral("print"))).getPointer(), print);
|
2014-06-13 12:30:03 +00:00
|
|
|
QV4::ScopedObject gc(scope, vm.memoryManager->alloc<builtins::GC>(ctx));
|
2014-05-07 11:33:24 +00:00
|
|
|
globalObject->put(QV4::ScopedString(scope, vm.newIdentifier(QStringLiteral("gc"))).getPointer(), gc);
|
2012-11-08 11:45:40 +00:00
|
|
|
|
|
|
|
foreach (const QString &fn, args) {
|
|
|
|
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
|
|
|
}
|