Skip to content

Revert "feat(inspector): implement console in the runtime. fix consol… #920

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions test-app/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ add_library( # Sets the name of the library. When it's built you can find it wit
src/main/cpp/ObjectManager.cpp
src/main/cpp/V8GlobalHelpers.cpp
src/main/cpp/MetadataTreeNode.cpp
src/main/cpp/console/Console.cpp

# Inspector source (we build it because we've customized it a bit)
src/main/cpp/v8_inspector/src/inspector/string-16.cc
Expand Down Expand Up @@ -106,7 +105,6 @@ add_library( # Sets the name of the library. When it's built you can find it wit
src/main/cpp/v8_inspector/src/inspector/injected-script-native.cc
src/main/cpp/v8_inspector/src/inspector/v8-inspector-session-impl.cc
src/main/cpp/v8_inspector/src/inspector/v8-debugger-agent-impl.cc
src/main/cpp/v8_inspector/src/inspector/v8-log-agent-impl.cpp
src/main/cpp/v8_inspector/src/inspector/injected-script.cc
src/main/cpp/v8_inspector/src/inspector/protocol/Overlay.cpp
src/main/cpp/v8_inspector/src/inspector/protocol/Log.cpp
Expand Down
9 changes: 9 additions & 0 deletions test-app/runtime/src/main/cpp/CallbackHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ void CallbackHandlers::Init(Isolate* isolate) {

assert(INIT_WORKER_METHOD_ID != nullptr);

Local<Object> json = isolate->GetCurrentContext()->Global()->Get(String::NewFromUtf8(isolate, "JSON"))->ToObject();
Local<Function> stringify = json->Get(String::NewFromUtf8(isolate, "stringify")).As<Function>();

auto persistentStringify = new Persistent<Function>(isolate, stringify);

isolateToJsonStringify.insert({isolate, persistentStringify});

MetadataNode::Init(isolate);

MethodCache::Init();
Expand Down Expand Up @@ -1374,6 +1381,8 @@ void CallbackHandlers::TerminateWorkerThread(Isolate* isolate) {
int CallbackHandlers::nextWorkerId = 0;
std::map<int, Persistent<Object>*> CallbackHandlers::id2WorkerMap;

std::map<Isolate*, Persistent<Function>*> CallbackHandlers::isolateToJsonStringify;

short CallbackHandlers::MAX_JAVA_STRING_ARRAY_LENGTH = 100;
jclass CallbackHandlers::RUNTIME_CLASS = nullptr;
jclass CallbackHandlers::JAVA_LANG_STRING = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions test-app/runtime/src/main/cpp/CallbackHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class CallbackHandlers {
*/
static std::map<int, v8::Persistent<v8::Object>*> id2WorkerMap;

static std::map<v8::Isolate*, v8::Persistent<v8::Function>*> isolateToJsonStringify;

static int nextWorkerId;

static void Init(v8::Isolate* isolate);
Expand Down
36 changes: 35 additions & 1 deletion test-app/runtime/src/main/cpp/JsV8InspectorClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <sstream>
#include <assert.h>
#include <include/libplatform/libplatform.h>
#include <v8_inspector/src/inspector/v8-log-agent-impl.h>
#include "Runtime.h"
#include "NativeScriptException.h"

Expand Down Expand Up @@ -199,6 +198,41 @@ JsV8InspectorClient* JsV8InspectorClient::GetInstance() {
return instance;
}


void JsV8InspectorClient::sendToFrontEndCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {

if ((instance == nullptr) || (instance->connection == nullptr)) {
return;
}

try {
if ((args.Length() > 0) && args[0]->IsString()) {
std::string message = ArgConverter::ConvertToString(args[0]->ToString());

std:
string level = "log";
if (args.Length() > 1 && args[1]->IsString()) {
level = ArgConverter::ConvertToString(args[1]->ToString());
}

JEnv env;
JniLocalRef str(env.NewStringUTF(message.c_str()));
JniLocalRef lev(env.NewStringUTF(level.c_str()));
env.CallStaticVoidMethod(inspectorClass, sendToDevToolsConsoleMethod, instance->connection, (jstring) str, (jstring)lev);
}
} catch (NativeScriptException& e) {
e.ReThrowToV8();
} catch (std::exception e) {
stringstream ss;
ss << "Error: c++ exception: " << e.what() << endl;
NativeScriptException nsEx(ss.str());
nsEx.ReThrowToV8();
} catch (...) {
NativeScriptException nsEx(std::string("Error: c++ exception!"));
nsEx.ReThrowToV8();
}
}

void MessageHandler(v8::Local<v8::Message> message, v8::Local<v8::Value> exception) {
// v8::Isolate *isolate = v8::Isolate::GetCurrent();
// v8::Local<v8::Context> context = isolate->GetEnteredContext();
Expand Down
5 changes: 2 additions & 3 deletions test-app/runtime/src/main/cpp/JsV8InspectorClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ class JsV8InspectorClient : V8InspectorClient, v8_inspector::V8Inspector::Channe
void sendProtocolNotification(const v8_inspector::StringView& message) override;
void flushProtocolNotifications() override;

static void sendToFrontEndCallback(const v8::FunctionCallbackInfo<v8::Value>& args);

void runMessageLoopOnPause(int context_group_id) override;
void quitMessageLoopOnPause() override;
v8::Local<v8::Context> ensureDefaultContextInGroup(int contextGroupId) override;

static void attachInspectorCallbacks(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate>& globalObjectTemplate);
static void InspectorIsConnectedGetterCallback(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info);
static bool inspectorIsConnected() {
return JsV8InspectorClient::GetInstance()->isConnected;
}

std::unique_ptr<V8Inspector> inspector_;
v8::Isolate* isolate_;
Expand Down
8 changes: 1 addition & 7 deletions test-app/runtime/src/main/cpp/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "include/zipconf.h"
#include <sstream>
#include <dlfcn.h>
#include <console/Console.h>
#include "NetworkDomainCallbackHandlers.h"
#include "sys/system_properties.h"
#include "JsV8InspectorClient.h"
Expand Down Expand Up @@ -501,6 +500,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native

globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__log"), FunctionTemplate::New(isolate, CallbackHandlers::LogMethodCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__dumpReferenceTables"), FunctionTemplate::New(isolate, CallbackHandlers::DumpReferenceTablesMethodCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__consoleMessage"), FunctionTemplate::New(isolate, JsV8InspectorClient::sendToFrontEndCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__enableVerboseLogging"), FunctionTemplate::New(isolate, CallbackHandlers::EnableVerboseLoggingMethodCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__disableVerboseLogging"), FunctionTemplate::New(isolate, CallbackHandlers::DisableVerboseLoggingMethodCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__exit"), FunctionTemplate::New(isolate, CallbackHandlers::ExitMethodCallback));
Expand Down Expand Up @@ -572,12 +572,6 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
global->ForceSet(ArgConverter::ConvertToV8String(isolate, "self"), global, readOnlyFlags);
}

/*
* Attach 'console' object to the global object
*/
v8::Local<v8::Object> console = Console::createConsole(context, filesPath);
global->ForceSet(context, ArgConverter::ConvertToV8String(isolate, "console"), console, readOnlyFlags);

ArgConverter::Init(isolate);

CallbackHandlers::Init(isolate);
Expand Down
8 changes: 1 addition & 7 deletions test-app/runtime/src/main/cpp/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,4 @@ u16string Util::ConvertFromUtf8ToUtf16(const string& str) {
auto utf16String = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>().from_bytes(str);

return utf16String;
}

uint16_t* Util::ConvertFromUtf8ToProtocolUtf16(const string& str) {
auto utf16String = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>().from_bytes(str);

return (uint16_t*)utf16String.c_str();
}
}
2 changes: 0 additions & 2 deletions test-app/runtime/src/main/cpp/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class Util {
static std::string ReplaceAll(std::string& str, const std::string& from, const std::string& to);

static std::u16string ConvertFromUtf8ToUtf16(const std::string& str);

static std::uint16_t* ConvertFromUtf8ToProtocolUtf16(const std::string& str);
};
}

Expand Down
15 changes: 5 additions & 10 deletions test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,17 @@ string tns::ConvertToString(const Local<v8::String>& s) {
}

Local<String> tns::JsonStringifyObject(Isolate* isolate, Handle<v8::Value> value) {
v8::HandleScope scope(isolate);

if (value.IsEmpty()) {
return String::Empty(isolate);
}

v8::Local<v8::String> resultString;
v8::TryCatch tc;
auto success = v8::JSON::Stringify(isolate->GetCurrentContext(), value->ToObject(isolate)).ToLocal(&resultString);
auto stringifyFunction = CallbackHandlers::isolateToJsonStringify.find(isolate)->second;
auto func = Local<Function>::New(isolate, *stringifyFunction);
Local<Value> args[] = { value };

if (!success && tc.HasCaught()) {
auto message = tc.Message()->Get();
resultString = v8::String::Concat(ArgConverter::ConvertToV8String(isolate, "Couldn't convert object to a JSON string: "), message);
}
auto result = func->Call(Undefined(isolate), 1, args);

return resultString;
return result->ToString(isolate);
}

jstring tns::ConvertToJavaString(const Local<Value>& value) {
Expand Down
Loading