diff --git a/android/runtime/v8/src/native/V8Runtime.cpp b/android/runtime/v8/src/native/V8Runtime.cpp index 66c986d12a3..e774300d453 100644 --- a/android/runtime/v8/src/native/V8Runtime.cpp +++ b/android/runtime/v8/src/native/V8Runtime.cpp @@ -6,6 +6,7 @@ */ #include #include +#include #include #include @@ -90,14 +91,24 @@ static void krollLog(const FunctionCallbackInfo& args) Local tag = args[0].As(); Local message = args[1].As(); - Local space = STRING_NEW(isolate, " "); - for (uint32_t i = 2; i < len; ++i) { - message = String::Concat(isolate, String::Concat(isolate, message, space), args[i].As()); - } - String::Utf8Value tagValue(isolate, tag); - String::Utf8Value messageValue(isolate, message); - __android_log_print(ANDROID_LOG_DEBUG, *tagValue, *messageValue); + if (len > 2) { + String::Utf8Value firstMessage(isolate, message); + std::string combined(*firstMessage, firstMessage.length()); + + for (uint32_t i = 2; i < len; ++i) { + String::Utf8Value argValue(isolate, args[i].As()); + combined += ' '; + combined.append(*argValue, argValue.length()); + } + + String::Utf8Value tagValue(isolate, tag); + __android_log_print(ANDROID_LOG_DEBUG, *tagValue, "%s", combined.c_str()); + } else { + String::Utf8Value tagValue(isolate, tag); + String::Utf8Value messageValue(isolate, message); + __android_log_print(ANDROID_LOG_DEBUG, *tagValue, *messageValue); + } } /* static */ diff --git a/android/runtime/v8/src/native/V8Util.cpp b/android/runtime/v8/src/native/V8Util.cpp index 36cccaaf9bc..d34d7c3018c 100644 --- a/android/runtime/v8/src/native/V8Util.cpp +++ b/android/runtime/v8/src/native/V8Util.cpp @@ -220,6 +220,7 @@ void V8Util::openJSErrorDialog(Isolate* isolate, TryCatch &tryCatch) } static int uncaughtExceptionCounter = 0; +static Persistent jsonStringifyFunction; void V8Util::fatalException(Isolate* isolate, TryCatch &tryCatch) { @@ -240,22 +241,29 @@ Local V8Util::jsonStringify(Isolate* isolate, Local value) TryCatch tryCatch(isolate); - MaybeLocal jsonGlobal = context->Global()->Get(context, STRING_NEW(isolate, "JSON")); - if (jsonGlobal.IsEmpty()) { - LOGE(TAG, "!!!! JSON global not found/accessible !!!"); - return scope.Escape(STRING_NEW(isolate, "ERROR")); - } + Local stringify; + if (jsonStringifyFunction.IsEmpty()) { + MaybeLocal jsonGlobal = context->Global()->Get(context, STRING_NEW(isolate, "JSON")); + if (jsonGlobal.IsEmpty()) { + LOGE(TAG, "!!!! JSON global not found/accessible !!!"); + return scope.Escape(STRING_NEW(isolate, "ERROR")); + } - Local jsonObject = jsonGlobal.ToLocalChecked().As(); - MaybeLocal stringifyValue = jsonObject->Get(context, STRING_NEW(isolate, "stringify")); - if (stringifyValue.IsEmpty()) { - LOGE(TAG, "!!!! JSON.stringifyValue not found/accessible !!!"); - return scope.Escape(STRING_NEW(isolate, "ERROR")); + Local jsonObject = jsonGlobal.ToLocalChecked().As(); + MaybeLocal stringifyValue = jsonObject->Get(context, STRING_NEW(isolate, "stringify")); + if (stringifyValue.IsEmpty()) { + LOGE(TAG, "!!!! JSON.stringifyValue not found/accessible !!!"); + return scope.Escape(STRING_NEW(isolate, "ERROR")); + } + + stringify = stringifyValue.ToLocalChecked().As(); + jsonStringifyFunction.Reset(isolate, stringify); + } else { + stringify = jsonStringifyFunction.Get(isolate); } - Local stringify = stringifyValue.ToLocalChecked().As(); Local args[] = { value }; - MaybeLocal result = stringify->Call(context, jsonObject, 1, args); + MaybeLocal result = stringify->Call(context, stringify, 1, args); if (result.IsEmpty()) { LOGE(TAG, "!!!! JSON.stringify() result is null/undefined.!!!"); return scope.Escape(STRING_NEW(isolate, "ERROR")); @@ -296,6 +304,7 @@ bool V8Util::isNaN(Isolate* isolate, Local value) void V8Util::dispose() { isNaNFunction.Reset(); + jsonStringifyFunction.Reset(); } std::string V8Util::stackTraceString(Isolate* isolate, Local frames, int maxCount) { diff --git a/android/runtime/v8/src/native/V8Util.h b/android/runtime/v8/src/native/V8Util.h index a9623323e7e..258a1f0551f 100644 --- a/android/runtime/v8/src/native/V8Util.h +++ b/android/runtime/v8/src/native/V8Util.h @@ -22,9 +22,25 @@ #define STRING_NEW(isolate, string_literal) \ v8::String::NewFromUtf8(isolate, string_literal "", v8::NewStringType::kNormal).ToLocalChecked() +namespace titanium { + // Helper for DEFINE_CONSTANT to work with both Object and Template + template + inline void SetConstant(v8::Isolate* isolate, T target, v8::Local name, v8::Local value) { + target->Set(isolate->GetCurrentContext(), name, value, + static_cast(v8::ReadOnly | v8::DontDelete)).FromJust(); + } + template <> + inline void SetConstant>(v8::Isolate* isolate, v8::Local target, v8::Local name, v8::Local value) { + target->Set(name, value, static_cast(v8::ReadOnly | v8::DontDelete)); + } + template <> + inline void SetConstant>(v8::Isolate* isolate, v8::Local target, v8::Local name, v8::Local value) { + target->PrototypeTemplate()->Set(name, value, static_cast(v8::ReadOnly | v8::DontDelete)); + } +} + #define DEFINE_CONSTANT(isolate, target, name, value) \ - (target)->Set(NEW_SYMBOL(isolate, name), \ - value, static_cast(v8::ReadOnly | v8::DontDelete)) + titanium::SetConstant(isolate, target, NEW_SYMBOL(isolate, name), value) #define DEFINE_INT_CONSTANT(isolate, target, name, value) \ DEFINE_CONSTANT(isolate, target, name, v8::Integer::New(isolate, value)) @@ -72,8 +88,8 @@ LOGV(TAG, __VA_ARGS__); \ for (uint32_t i = 0; i < frameCount; i++) { \ v8::Local frame = stackTrace->GetFrame(i); \ - v8::String::Utf8Value fnName(frame->GetFunctionName()); \ - v8::String::Utf8Value scriptUrl(frame->GetScriptName()); \ + v8::String::Utf8Value fnName(isolate, frame->GetFunctionName()); \ + v8::String::Utf8Value scriptUrl(isolate, frame->GetScriptName()); \ LOGV(TAG, " at %s [%s:%d:%d]", *fnName, *scriptUrl, frame->GetLineNumber(), frame->GetColumn()); \ } \ }