This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
/
qode.cc
102 lines (85 loc) · 3.11 KB
/
qode.cc
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
// Copyright 2017 Atul R. All rights reserved.
#include "qode.h"
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "helpers/qode_helpers.h"
#include "node/src/env-inl.h"
#include "src/integration/node_integration.h"
std::string qodeVersion = "1.0.6";
namespace qode {
QApplication *qtAppInstance = nullptr;
static int qode_argc = 0;
static char **qode_argv = nullptr;
// The global instance of NodeIntegration.
std::unique_ptr<NodeIntegration> g_node_integration;
// Has we run message loop before.
bool g_first_runloop = true;
inline v8::Local<v8::String> ToV8String(node::Environment *env,
const std::string str) {
return v8::String::NewFromUtf8(env->isolate(), str.c_str(),
v8::NewStringType::kNormal,
static_cast<int>(str.length()))
.ToLocalChecked();
}
// Force running uv loop.
void ActivateUvLoop(const v8::FunctionCallbackInfo<v8::Value> &args) {
g_node_integration->CallNextTick();
}
bool InitWrapper(node::Environment *env) {
v8::HandleScope handle_scope(env->isolate());
v8::Local<v8::Value> versions =
env->process_object()
->Get(env->context(), ToV8String(env, "versions"))
.ToLocalChecked();
versions.As<v8::Object>()
->Set(env->context(), ToV8String(env, "qode"),
ToV8String(env, qodeVersion))
.ToChecked();
versions.As<v8::Object>()
->Set(env->context(), ToV8String(env, "qt(compiled)"),
ToV8String(env, QT_VERSION_STR))
.ToChecked();
versions.As<v8::Object>()
->Set(env->context(), ToV8String(env, "qt(runtime)"),
ToV8String(env, qVersion()))
.ToChecked();
env->SetMethod(env->process_object(), "activateUvLoop", &ActivateUvLoop);
return true;
}
bool RunLoopWrapper(node::Environment *env) {
// Run uv loop for once before entering GUI message loop.
if (g_first_runloop) {
g_node_integration->UvRunOnce();
g_first_runloop = false;
}
int exitCode = qtAppInstance->exec();
std::cout << "Qt exited with " << exitCode;
exit(exitCode);
// No need to keep uv loop alive.
// g_node_integration->ReleaseHandleRef();
// Enter uv loop to handle unfinished uv tasks.
// return uv_run(env->event_loop(), UV_RUN_DEFAULT);
}
int Start(int argc, char *argv[]) {
qode_argc = argc;
qode_argv = argv;
qtAppInstance = new QApplication(qode_argc, qode_argv);
qtAppInstance->processEvents(); // Just run it once.
// Prepare node integration.
g_node_integration.reset(NodeIntegration::Create());
g_node_integration->Init();
// Set run loop and init function on node.
node::InjectQode(&InitWrapper, &RunLoopWrapper);
// Always enable GC this app is almost always running on desktop.
v8::V8::SetFlagsFromString("--expose_gc", 11);
QJsonDocument qodeConfig = QodeHelpers::readConfig();
QodeHelpers::setStartFile(qodeConfig);
QodeHelpers::setConsoleVisibility(qodeConfig);
QodeHelpers::addLibraryPaths(qodeConfig);
int code = node::Start(qode_argc, qode_argv);
// Clean up node integration and quit.
g_node_integration.reset();
return code;
}
} // namespace qode