-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSEV8.cpp
More file actions
107 lines (82 loc) · 2.7 KB
/
JSEV8.cpp
File metadata and controls
107 lines (82 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "JSE.h"
#include <fstream>
#include <QtWidgets/QMessageBox>
#include "JSEProcess.h"
#include "JSEUIWindow.h"
#include "JSEUIButton.h"
#include "JSEUILabel.h"
#include "JSEUILineEdit.h"
#include "JSEUIMenu.h"
#include "JSEUIMenuLeaf.h"
#include "JSEV8.h"
using namespace v8;
using namespace JSE::UI;
namespace JSE {
JSEV8::JSEV8(JSEApplication &app)
{
isolate = Isolate::GetCurrent();
V8::Initialize();
this->app = &app;
}
JSEV8::~JSEV8()
{
}
void JSEV8::exit(void)
{
V8::TerminateExecution();
}
void JSEV8::start(void)
{
V8::Initialize();
Locker locker(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = Context::New(isolate);
Context::Scope context_scope(context);
auto argv = app->arguments();
/*
if (argv.length() != 2)
{
QMessageBox::critical(nullptr, "Fatal Error", "No input file specified.", QMessageBox::Ok);
app->exit(-1);
return;
}
*/
//std::ifstream ifs(argv[1].toLocal8Bit().constData(), std::ios::in | std::ios::binary);
std::ifstream ifs("C:\\Users\\zcy\\Desktop\\jsx\\JavaScriptExecutable\\x64\\Debug\\app.js", std::ios::in | std::ios::binary);
if (!ifs.good())
{
QMessageBox::critical(nullptr, "Fatal Error", "Cannot open input file.", QMessageBox::Ok);
app->exit(-2);
return;
}
std::string content;
ifs.seekg(0, std::ios::end);
content.resize(ifs.tellg());
ifs.seekg(0);
ifs.read(&content[0], content.size());
ifs.close();
// Setup global objects
Handle<Object> objGlobal = context->Global();
// Setup process
Persistent<Object> objProcess;
SetupProcessObject(app, this, objProcess, isolate);
objGlobal->Set(String::NewFromUtf8(isolate, "process"), Local<Object>::New(isolate, objProcess));
// Setup common controls
JSEUIWindow::Initialize(objGlobal, "Window");
JSEUIButton::Initialize(objGlobal, "Button");
JSEUILabel::Initialize(objGlobal, "Label");
JSEUILineEdit::Initialize(objGlobal, "LineEdit");
JSEUIMenu::Initialize(objGlobal, "Menu");
JSEUIMenuLeaf::Initialize(objGlobal, "MenuLeaf");
TryCatch try_catch;
Local<String> source = String::NewFromUtf8(isolate, content.c_str());
Local<Script> script = Script::Compile(source, String::NewFromUtf8(isolate, "app.js"));
script->Run();
if (try_catch.HasCaught())
{
QMessageBox::critical(nullptr, "Runtime Error", (std::string() + "Line " + std::to_string(try_catch.Message()->GetLineNumber()) + ", column " + std::to_string(try_catch.Message()->GetStartColumn()) + ":\n" + *String::Utf8Value(try_catch.Message()->Get()) + ".").c_str(), QMessageBox::Ok);
app->exit(-3);
return;
}
}
}