This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
93 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,102 @@ | ||
// Copyright 2017 Atul R. All rights reserved. | ||
|
||
#include "qode.h" | ||
#include "src/integration/node_integration.h" | ||
#include "node/src/env-inl.h" | ||
#include <string.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.5"; | ||
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(); | ||
} | ||
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; | ||
|
||
// Force running uv loop. | ||
void ActivateUvLoop(const v8::FunctionCallbackInfo<v8::Value>& args) { | ||
g_node_integration->CallNextTick(); | ||
} | ||
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(); | ||
} | ||
|
||
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; | ||
} | ||
// Force running uv loop. | ||
void ActivateUvLoop(const v8::FunctionCallbackInfo<v8::Value> &args) { | ||
g_node_integration->CallNextTick(); | ||
} | ||
|
||
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); | ||
} | ||
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; | ||
} | ||
|
||
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; | ||
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 | ||
} // namespace qode |