2012-04-16 19:23:25 +00:00
|
|
|
|
|
|
|
#include "qmljs_objects.h"
|
|
|
|
#include "qv4codegen_p.h"
|
2012-05-07 12:46:37 +00:00
|
|
|
#include "qv4isel_p.h"
|
2012-05-08 11:21:52 +00:00
|
|
|
#include "qv4syntaxchecker_p.h"
|
2012-05-14 14:03:10 +00:00
|
|
|
#include "qv4ecmaobjects_p.h"
|
2012-04-16 19:23:25 +00:00
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
#include <private/qqmljsengine_p.h>
|
|
|
|
#include <private/qqmljslexer_p.h>
|
|
|
|
#include <private/qqmljsparser_p.h>
|
|
|
|
#include <private/qqmljsast_p.h>
|
|
|
|
|
2012-05-07 12:46:37 +00:00
|
|
|
#include <sys/mman.h>
|
2012-04-16 19:23:25 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2012-05-07 12:46:37 +00:00
|
|
|
static inline bool protect(const void *addr, size_t size)
|
|
|
|
{
|
|
|
|
size_t pageSize = sysconf(_SC_PAGESIZE);
|
|
|
|
size_t iaddr = reinterpret_cast<size_t>(addr);
|
|
|
|
size_t roundAddr = iaddr & ~(pageSize - static_cast<size_t>(1));
|
|
|
|
int mode = PROT_READ | PROT_WRITE | PROT_EXEC;
|
|
|
|
return mprotect(reinterpret_cast<void*>(roundAddr), size + (iaddr - roundAddr), mode) == 0;
|
|
|
|
}
|
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
namespace builtins {
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
using namespace QQmlJS::VM;
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
struct Print: FunctionObject
|
|
|
|
{
|
2012-05-13 11:50:55 +00:00
|
|
|
Print(Context *scope): FunctionObject(scope) {}
|
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
virtual void call(Context *ctx)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < ctx->argumentCount; ++i) {
|
2012-05-18 13:28:59 +00:00
|
|
|
String *s = ctx->argument(i).toString(ctx);
|
2012-05-07 14:05:05 +00:00
|
|
|
if (i)
|
|
|
|
std::cout << ' ';
|
2012-05-18 13:28:59 +00:00
|
|
|
std::cout << qPrintable(s->toQString());
|
2012-05-07 14:05:05 +00:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
};
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
} // builtins
|
|
|
|
|
2012-05-08 11:21:52 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
void evaluate(QQmlJS::VM::ExecutionEngine *vm, const QString &fileName, const QString &source)
|
2012-05-08 11:21:52 +00:00
|
|
|
{
|
|
|
|
using namespace QQmlJS;
|
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
IR::Module module;
|
|
|
|
IR::Function *globalCode = 0;
|
2012-05-08 11:21:52 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
const size_t codeSize = 10 * getpagesize();
|
|
|
|
uchar *code = (uchar *) malloc(codeSize);
|
|
|
|
assert(! (size_t(code) & 15));
|
2012-05-08 11:21:52 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
{
|
|
|
|
QQmlJS::Engine ee, *engine = ⅇ
|
|
|
|
Lexer lexer(engine);
|
|
|
|
lexer.setCode(source, 1, false);
|
|
|
|
Parser parser(engine);
|
2012-05-08 11:21:52 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
const bool parsed = parser.parseProgram();
|
2012-05-08 11:21:52 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
foreach (const DiagnosticMessage &m, parser.diagnosticMessages()) {
|
|
|
|
std::cerr << qPrintable(fileName) << ':' << m.loc.startLine << ':' << m.loc.startColumn
|
|
|
|
<< ": error: " << qPrintable(m.message) << std::endl;
|
|
|
|
}
|
2012-05-08 11:21:52 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
if (parsed) {
|
|
|
|
using namespace AST;
|
|
|
|
Program *program = AST::cast<Program *>(parser.rootNode());
|
2012-05-08 11:21:52 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
Codegen cg;
|
|
|
|
globalCode = cg(program, &module);
|
|
|
|
|
|
|
|
x86_64::InstructionSelection isel(vm, &module, code);
|
|
|
|
foreach (IR::Function *function, module.functions) {
|
|
|
|
isel(function);
|
|
|
|
}
|
2012-05-08 11:21:52 +00:00
|
|
|
}
|
2012-05-18 08:10:36 +00:00
|
|
|
}
|
2012-05-08 11:21:52 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
if (! protect(code, codeSize))
|
|
|
|
Q_UNREACHABLE();
|
2012-05-08 11:21:52 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
VM::Object *globalObject = vm->globalObject.objectValue;
|
|
|
|
VM::Context *ctx = vm->rootContext;
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2012-05-25 10:54:36 +00:00
|
|
|
globalObject->setProperty(ctx, vm->identifier(QStringLiteral("print")),
|
|
|
|
VM::Value::fromObject(new builtins::Print(ctx)));
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
ctx->varCount = globalCode->locals.size();
|
|
|
|
if (ctx->varCount) {
|
|
|
|
ctx->locals = new VM::Value[ctx->varCount];
|
|
|
|
ctx->vars = new VM::String*[ctx->varCount];
|
|
|
|
std::fill(ctx->locals, ctx->locals + ctx->varCount, VM::Value::undefinedValue());
|
|
|
|
for (size_t i = 0; i < ctx->varCount; ++i)
|
|
|
|
ctx->vars[i] = ctx->engine->identifier(*globalCode->locals.at(i));
|
|
|
|
}
|
2012-05-16 17:00:31 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
globalCode->code(ctx);
|
2012-05-16 17:00:31 +00:00
|
|
|
|
2012-05-25 09:55:50 +00:00
|
|
|
if (ctx->hasUncaughtException) {
|
|
|
|
std::cerr << "Uncaught exception: " << qPrintable(ctx->result.toString(ctx)->toQString()) << std::endl;
|
|
|
|
}
|
2012-05-23 16:48:48 +00:00
|
|
|
|
2012-05-18 08:10:36 +00:00
|
|
|
delete[] ctx->locals;
|
|
|
|
delete[] ctx->vars;
|
2012-05-08 11:21:52 +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();
|
|
|
|
|
|
|
|
foreach (const QString &fn, args) {
|
|
|
|
QFile file(fn);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
const QString code = QString::fromUtf8(file.readAll());
|
|
|
|
file.close();
|
2012-05-21 08:26:13 +00:00
|
|
|
QQmlJS::VM::ExecutionEngine vm;
|
2012-05-18 08:10:36 +00:00
|
|
|
evaluate(&vm, fn, code);
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|