|
1 | 1 | // Copyright 2017 Atul R. All rights reserved. |
2 | 2 |
|
3 | 3 | #include "qode.h" |
4 | | -#include "src/integration/node_integration.h" |
5 | | -#include "node/src/env-inl.h" |
6 | | -#include <string.h> |
| 4 | + |
7 | 5 | #include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | + |
8 | 8 | #include <iostream> |
| 9 | + |
9 | 10 | #include "helpers/qode_helpers.h" |
| 11 | +#include "node/src/env-inl.h" |
| 12 | +#include "src/integration/node_integration.h" |
10 | 13 |
|
11 | | -std::string qodeVersion = "1.0.5"; |
| 14 | +std::string qodeVersion = "1.0.6"; |
12 | 15 |
|
13 | 16 | namespace qode { |
14 | 17 |
|
15 | | - QApplication *qtAppInstance = nullptr; |
16 | | - static int qode_argc = 0; |
17 | | - static char **qode_argv = nullptr; |
18 | | - // The global instance of NodeIntegration. |
19 | | - std::unique_ptr<NodeIntegration> g_node_integration; |
20 | | - // Has we run message loop before. |
21 | | - bool g_first_runloop = true; |
22 | | - |
23 | | - inline v8::Local<v8::String> ToV8String(node::Environment *env, const std::string str) { |
24 | | - return v8::String::NewFromUtf8( |
25 | | - env->isolate(), |
26 | | - str.c_str(), |
27 | | - v8::NewStringType::kNormal, |
28 | | - static_cast<int>(str.length())) |
29 | | - .ToLocalChecked(); |
30 | | - } |
| 18 | +QApplication *qtAppInstance = nullptr; |
| 19 | +static int qode_argc = 0; |
| 20 | +static char **qode_argv = nullptr; |
| 21 | +// The global instance of NodeIntegration. |
| 22 | +std::unique_ptr<NodeIntegration> g_node_integration; |
| 23 | +// Has we run message loop before. |
| 24 | +bool g_first_runloop = true; |
31 | 25 |
|
32 | | - // Force running uv loop. |
33 | | - void ActivateUvLoop(const v8::FunctionCallbackInfo<v8::Value>& args) { |
34 | | - g_node_integration->CallNextTick(); |
35 | | - } |
| 26 | +inline v8::Local<v8::String> ToV8String(node::Environment *env, |
| 27 | + const std::string str) { |
| 28 | + return v8::String::NewFromUtf8(env->isolate(), str.c_str(), |
| 29 | + v8::NewStringType::kNormal, |
| 30 | + static_cast<int>(str.length())) |
| 31 | + .ToLocalChecked(); |
| 32 | +} |
36 | 33 |
|
37 | | - bool InitWrapper(node::Environment *env) |
38 | | - { |
39 | | - v8::HandleScope handle_scope(env->isolate()); |
40 | | - v8::Local<v8::Value> versions = env->process_object()->Get( |
41 | | - env->context(), |
42 | | - ToV8String(env, "versions")) |
43 | | - .ToLocalChecked(); |
44 | | - versions.As<v8::Object>()->Set( |
45 | | - env->context(), |
46 | | - ToV8String(env, "qode"), |
47 | | - ToV8String(env, qodeVersion)) |
48 | | - .ToChecked(); |
49 | | - versions.As<v8::Object>()->Set( |
50 | | - env->context(), |
51 | | - ToV8String(env, "qt(compiled)"), |
52 | | - ToV8String(env, QT_VERSION_STR)) |
53 | | - .ToChecked(); |
54 | | - versions.As<v8::Object>()->Set( |
55 | | - env->context(), |
56 | | - ToV8String(env, "qt(runtime)"), |
57 | | - ToV8String(env, qVersion()) |
58 | | - ).ToChecked(); |
59 | | - |
60 | | - env->SetMethod(env->process_object(), "activateUvLoop", &ActivateUvLoop); |
61 | | - return true; |
62 | | - } |
| 34 | +// Force running uv loop. |
| 35 | +void ActivateUvLoop(const v8::FunctionCallbackInfo<v8::Value> &args) { |
| 36 | + g_node_integration->CallNextTick(); |
| 37 | +} |
63 | 38 |
|
64 | | - bool RunLoopWrapper(node::Environment *env) |
65 | | - { |
66 | | - // Run uv loop for once before entering GUI message loop. |
67 | | - if (g_first_runloop) |
68 | | - { |
69 | | - g_node_integration->UvRunOnce(); |
70 | | - g_first_runloop = false; |
71 | | - } |
72 | | - int exitCode = qtAppInstance->exec(); |
73 | | - std::cout << "Qt exited with " << exitCode; |
74 | | - exit(exitCode); |
75 | | - // No need to keep uv loop alive. |
76 | | - // g_node_integration->ReleaseHandleRef(); |
77 | | - // Enter uv loop to handle unfinished uv tasks. |
78 | | - // return uv_run(env->event_loop(), UV_RUN_DEFAULT); |
79 | | - } |
| 39 | +bool InitWrapper(node::Environment *env) { |
| 40 | + v8::HandleScope handle_scope(env->isolate()); |
| 41 | + v8::Local<v8::Value> versions = |
| 42 | + env->process_object() |
| 43 | + ->Get(env->context(), ToV8String(env, "versions")) |
| 44 | + .ToLocalChecked(); |
| 45 | + versions.As<v8::Object>() |
| 46 | + ->Set(env->context(), ToV8String(env, "qode"), |
| 47 | + ToV8String(env, qodeVersion)) |
| 48 | + .ToChecked(); |
| 49 | + versions.As<v8::Object>() |
| 50 | + ->Set(env->context(), ToV8String(env, "qt(compiled)"), |
| 51 | + ToV8String(env, QT_VERSION_STR)) |
| 52 | + .ToChecked(); |
| 53 | + versions.As<v8::Object>() |
| 54 | + ->Set(env->context(), ToV8String(env, "qt(runtime)"), |
| 55 | + ToV8String(env, qVersion())) |
| 56 | + .ToChecked(); |
| 57 | + |
| 58 | + env->SetMethod(env->process_object(), "activateUvLoop", &ActivateUvLoop); |
| 59 | + return true; |
| 60 | +} |
80 | 61 |
|
81 | | - int Start(int argc, char *argv[]) |
82 | | - { |
83 | | - qode_argc = argc; |
84 | | - qode_argv = argv; |
85 | | - |
86 | | - qtAppInstance = new QApplication(qode_argc, qode_argv); |
87 | | - qtAppInstance->processEvents(); // Just run it once. |
88 | | - // Prepare node integration. |
89 | | - g_node_integration.reset(NodeIntegration::Create()); |
90 | | - g_node_integration->Init(); |
91 | | - // Set run loop and init function on node. |
92 | | - node::InjectQode(&InitWrapper, &RunLoopWrapper); |
93 | | - // Always enable GC this app is almost always running on desktop. |
94 | | - v8::V8::SetFlagsFromString("--expose_gc", 11); |
95 | | - |
96 | | - QJsonDocument qodeConfig = QodeHelpers::readConfig(); |
97 | | - QodeHelpers::setStartFile(qodeConfig); |
98 | | - QodeHelpers::setConsoleVisibility(qodeConfig); |
99 | | - QodeHelpers::addLibraryPaths(qodeConfig); |
100 | | - |
101 | | - int code = node::Start(qode_argc, qode_argv); |
102 | | - // Clean up node integration and quit. |
103 | | - g_node_integration.reset(); |
104 | | - return code; |
| 62 | +bool RunLoopWrapper(node::Environment *env) { |
| 63 | + // Run uv loop for once before entering GUI message loop. |
| 64 | + if (g_first_runloop) { |
| 65 | + g_node_integration->UvRunOnce(); |
| 66 | + g_first_runloop = false; |
105 | 67 | } |
| 68 | + int exitCode = qtAppInstance->exec(); |
| 69 | + std::cout << "Qt exited with " << exitCode; |
| 70 | + exit(exitCode); |
| 71 | + // No need to keep uv loop alive. |
| 72 | + // g_node_integration->ReleaseHandleRef(); |
| 73 | + // Enter uv loop to handle unfinished uv tasks. |
| 74 | + // return uv_run(env->event_loop(), UV_RUN_DEFAULT); |
| 75 | +} |
| 76 | + |
| 77 | +int Start(int argc, char *argv[]) { |
| 78 | + qode_argc = argc; |
| 79 | + qode_argv = argv; |
| 80 | + |
| 81 | + qtAppInstance = new QApplication(qode_argc, qode_argv); |
| 82 | + qtAppInstance->processEvents(); // Just run it once. |
| 83 | + // Prepare node integration. |
| 84 | + g_node_integration.reset(NodeIntegration::Create()); |
| 85 | + g_node_integration->Init(); |
| 86 | + // Set run loop and init function on node. |
| 87 | + node::InjectQode(&InitWrapper, &RunLoopWrapper); |
| 88 | + // Always enable GC this app is almost always running on desktop. |
| 89 | + v8::V8::SetFlagsFromString("--expose_gc", 11); |
| 90 | + |
| 91 | + QJsonDocument qodeConfig = QodeHelpers::readConfig(); |
| 92 | + QodeHelpers::setStartFile(qodeConfig); |
| 93 | + QodeHelpers::setConsoleVisibility(qodeConfig); |
| 94 | + QodeHelpers::addLibraryPaths(qodeConfig); |
| 95 | + |
| 96 | + int code = node::Start(qode_argc, qode_argv); |
| 97 | + // Clean up node integration and quit. |
| 98 | + g_node_integration.reset(); |
| 99 | + return code; |
| 100 | +} |
106 | 101 |
|
107 | | -} // namespace qode |
| 102 | +} // namespace qode |
0 commit comments