fix(core): fix codecc warning

This commit is contained in:
pollyzhang
2021-06-03 22:18:27 +08:00
committed by churchill-zhang
parent 0c33d3b48b
commit 845046a7ac
24 changed files with 306 additions and 277 deletions
+2 -4
View File
@@ -28,12 +28,10 @@
#include "base/unicode_string_view.h"
namespace {
using unicode_string_view = tdf::base::unicode_string_view;
}
class Uri {
public:
using unicode_string_view = tdf::base::unicode_string_view;
explicit Uri(const unicode_string_view& uri);
~Uri();
unicode_string_view GetPath();
@@ -30,11 +30,11 @@
#include "jni/scoped_java_ref.h"
template <typename CharType>
bool ReadAsset(const unicode_string_view& path,
bool ReadAsset(const tdf::base::unicode_string_view& path,
AAssetManager* aasset_manager,
std::basic_string<CharType>& bytes,
bool is_auto_fill) {
unicode_string_view owner(""_u8s);
tdf::base::unicode_string_view owner(""_u8s);
const char* asset_path = StringViewUtils::ToConstCharPointer(path, owner);
std::string file_path = std::string(asset_path);
if (file_path.length() > 0 && file_path[0] == '/') {
+1 -1
View File
@@ -292,7 +292,7 @@ jboolean RunScriptFromUri(JNIEnv* j_env,
TDF_BASE_DLOG(INFO) << "runScriptFromUri enter tast";
bool flag = RunScript(runtime, script_name, j_can_use_code_cache,
code_cache_dir, uri, aasset_manager);
jlong value = flag == false ? 0 : 1;
jlong value = !flag ? 0 : 1;
hippy::bridge::CallJavaMethod(save_object_->GetObj(), value);
auto time_end = std::chrono::time_point_cast<std::chrono::microseconds>(
+1
View File
@@ -34,6 +34,7 @@ static jmethodID j_to_string_method_id;
static jmethodID j_get_scheme_method_id;
static jmethodID j_get_path_method_id;
using unicode_string_view = tdf::base::unicode_string_view;
using StringViewUtils = hippy::base::StringViewUtils;
bool Uri::Init() {
@@ -36,6 +36,7 @@ using unicode_string_view = tdf::base::unicode_string_view;
using StringViewUtils = hippy::base::StringViewUtils;
using HippyFile = hippy::base::HippyFile;
using u8string = unicode_string_view::u8string;
using char8_t_ = unicode_string_view::char8_t_;
static std::atomic<int64_t> global_request_id{0};
@@ -44,14 +45,16 @@ ADRLoader::ADRLoader() : aasset_manager_(nullptr) {}
bool ADRLoader::RequestUntrustedContent(const unicode_string_view& uri,
std::function<void(u8string)> cb) {
std::shared_ptr<Uri> uri_obj = std::make_shared<Uri>(uri);
unicode_string_view uri_schema = uri_obj->GetScheme();
unicode_string_view schema = uri_obj->GetScheme();
unicode_string_view path = uri_obj->GetPath();
if (uri_schema == u"file") {
TDF_BASE_DCHECK(schema.encoding() == unicode_string_view::Encoding::Utf16);
std::u16string schema_str = schema.utf16_value();
if (schema_str == u"file") {
return LoadByFile(path, cb);
} else if (uri_schema == u"http" || uri_schema == u"https" ||
uri_schema == u"debug") {
} else if (schema_str == u"http" || schema_str == u"https" ||
schema_str == u"debug") {
return LoadByHttp(uri, cb);
} else if (uri_schema == u"asset") {
} else if (schema_str == u"asset") {
if (aasset_manager_) {
return LoadByAsset(path, cb, false);
}
@@ -59,7 +62,7 @@ bool ADRLoader::RequestUntrustedContent(const unicode_string_view& uri,
TDF_BASE_DLOG(ERROR) << "aasset_manager error, uri = " << uri;
return false;
} else {
TDF_BASE_DLOG(ERROR) << "schema error, schema = %s" << uri_schema;
TDF_BASE_DLOG(ERROR) << "schema error, schema = %s" << schema;
return false;
}
}
@@ -70,9 +73,11 @@ bool ADRLoader::RequestUntrustedContent(const unicode_string_view& uri,
unicode_string_view schema = uri_obj->GetScheme();
unicode_string_view path = uri_obj->GetPath();
TDF_BASE_DCHECK(schema.encoding() == unicode_string_view::Encoding::Utf16);
if (schema.utf16_value() == u"file") {
std::u16string schema_str = schema.utf16_value();
if (schema_str == u"file") {
return HippyFile::ReadFile(path, content, false);
} else if (schema == u"http" || schema == u"https" || schema == u"debug") {
} else if (schema_str == u"http" || schema_str == u"https" ||
schema_str == u"debug") {
std::promise<u8string> promise;
std::future<u8string> read_file_future = promise.get_future();
std::function<void(u8string)> cb = hippy::base::MakeCopyable(
@@ -82,7 +87,7 @@ bool ADRLoader::RequestUntrustedContent(const unicode_string_view& uri,
bool ret = LoadByHttp(uri, cb);
content = read_file_future.get();
return ret;
} else if (schema == u"asset") {
} else if (schema_str == u"asset") {
if (aasset_manager_) {
return ReadAsset(path, aasset_manager_, content, false);
}
+2 -2
View File
@@ -62,12 +62,12 @@ class HippyFile {
std::streamsize size = file.gcount();
file.clear();
file.seekg(0, std::ios_base::beg);
long data_size = size;
std::streamsize data_size = size;
if (is_auto_fill) {
data_size += 1;
}
bytes.resize(data_size);
long read_size =
std::streamsize read_size =
file.read(reinterpret_cast<char*>(&bytes[0]), size).gcount();
if (size != read_size) {
TDF_BASE_DLOG(WARNING)
+7 -9
View File
@@ -31,24 +31,22 @@ class JSValueWrapper final {
JSValueWrapper() {}
JSValueWrapper(const JSValueWrapper& source);
JSValueWrapper(int32_t int32_value)
JSValueWrapper(int32_t int32_value) // NOLINT
: type_(Type::Int32), int32_value_(int32_value) {}
JSValueWrapper(uint32_t uint32_value)
JSValueWrapper(uint32_t uint32_value) // NOLINT
: type_(Type::Uint32), uint32_value_(uint32_value) {}
JSValueWrapper(double double_value)
JSValueWrapper(double double_value) // NOLINT
: type_(Type::Double), double_value_(double_value) {}
JSValueWrapper(bool bool_value)
JSValueWrapper(bool bool_value) // NOLINT
: type_(Type::Boolean), bool_value_(bool_value) {}
explicit JSValueWrapper(std::string&& string_value)
: type_(Type::String), string_value_(std::move(string_value)) {}
explicit JSValueWrapper(const std::string& string_value)
: type_(Type::String), string_value_(string_value) {}
JSValueWrapper(const char* string_value)
: type_(Type::String),
string_value_(std::string(string_value)) {}
JSValueWrapper(const char* string_value) // NOLINT
: type_(Type::String), string_value_(std::string(string_value)) {}
JSValueWrapper(const char* string_value, size_t length)
: type_(Type::String),
string_value_(std::string(string_value, length)) {}
: type_(Type::String), string_value_(std::string(string_value, length)) {}
explicit JSValueWrapper(JSObjectType&& object_value)
: type_(Type::Object), object_value_(std::move(object_value)) {}
explicit JSValueWrapper(const JSObjectType& object_value)
+106 -82
View File
@@ -32,18 +32,14 @@
#define EXTEND_LITERAL(ch) ch, ch, u##ch, U##ch
namespace {
using unicode_string_view = tdf::base::unicode_string_view;
using u8string = unicode_string_view::u8string;
using char8_t_ = unicode_string_view::char8_t_;
} // namespace
namespace hippy {
namespace base {
class StringViewUtils {
public:
using unicode_string_view = tdf::base::unicode_string_view;
using u8string = unicode_string_view::u8string;
using char8_t_ = unicode_string_view::char8_t_;
inline static bool IsEmpty(const unicode_string_view& str_view) {
unicode_string_view::Encoding encoding = str_view.encoding();
@@ -71,96 +67,123 @@ class StringViewUtils {
return true;
}
static unicode_string_view Convert(
static unicode_string_view CovertToLatin(
const unicode_string_view& str_view,
unicode_string_view::Encoding dst_encoding) {
unicode_string_view::Encoding src_encoding = str_view.encoding();
switch (dst_encoding) {
unicode_string_view::Encoding src_encoding) {
switch (src_encoding) {
case unicode_string_view::Encoding::Latin1: {
switch (src_encoding) {
case unicode_string_view::Encoding::Latin1: {
return unicode_string_view(str_view.latin1_value());
}
case unicode_string_view::Encoding::Utf16:
case unicode_string_view::Encoding::Utf32:
case unicode_string_view::Encoding::Utf8:
default: {
TDF_BASE_NOTREACHED();
break;
}
}
return unicode_string_view(str_view.latin1_value());
}
case unicode_string_view::Encoding::Utf16:
case unicode_string_view::Encoding::Utf32:
case unicode_string_view::Encoding::Utf8:
default: {
TDF_BASE_NOTREACHED();
break;
}
}
return unicode_string_view();
}
static unicode_string_view CovertToUtf16(
const unicode_string_view& str_view,
unicode_string_view::Encoding src_encoding) {
switch (src_encoding) {
case unicode_string_view::Encoding::Latin1: {
return unicode_string_view(
CopyChars<char, char16_t>(str_view.latin1_value()));
}
case unicode_string_view::Encoding::Utf16: {
switch (src_encoding) {
case unicode_string_view::Encoding::Latin1: {
return unicode_string_view(
CopyChars<char, char16_t>(str_view.latin1_value()));
}
case unicode_string_view::Encoding::Utf16: {
return unicode_string_view(str_view.utf16_value());
}
case unicode_string_view::Encoding::Utf32: {
return unicode_string_view(U32ToU16(str_view.utf32_value()));
}
case unicode_string_view::Encoding::Utf8: {
return unicode_string_view(U8ToU16(str_view.utf8_value()));
}
default: {
TDF_BASE_NOTREACHED();
break;
}
}
return unicode_string_view(str_view.utf16_value());
}
case unicode_string_view::Encoding::Utf32: {
switch (src_encoding) {
case unicode_string_view::Encoding::Latin1: {
return unicode_string_view(
CopyChars<char, char32_t>(str_view.latin1_value()));
}
case unicode_string_view::Encoding::Utf16: {
return unicode_string_view(U16ToU32(str_view.utf16_value()));
}
case unicode_string_view::Encoding::Utf32: {
return unicode_string_view(str_view.utf32_value());
}
case unicode_string_view::Encoding::Utf8: {
return unicode_string_view(U8ToU32(str_view.utf8_value()));
}
default: {
TDF_BASE_NOTREACHED();
break;
}
}
return unicode_string_view(U32ToU16(str_view.utf32_value()));
}
case unicode_string_view::Encoding::Utf8: {
switch (src_encoding) {
case unicode_string_view::Encoding::Latin1: {
const auto& str = str_view.latin1_value();
auto ptr = reinterpret_cast<const unicode_string_view::char8_t_*>(
str.c_str());
return unicode_string_view(ptr, str.length());
}
case unicode_string_view::Encoding::Utf16: {
return unicode_string_view(U16ToU8(str_view.utf16_value()));
}
case unicode_string_view::Encoding::Utf32: {
return unicode_string_view(U32ToU8(str_view.utf32_value()));
}
case unicode_string_view::Encoding::Utf8: {
return unicode_string_view(str_view.utf8_value());
}
default: {
TDF_BASE_NOTREACHED();
break;
}
}
return unicode_string_view(U8ToU16(str_view.utf8_value()));
}
default: {
TDF_BASE_NOTREACHED();
break;
}
}
return unicode_string_view();
}
static unicode_string_view CovertToUtf32(
const unicode_string_view& str_view,
unicode_string_view::Encoding src_encoding) {
switch (src_encoding) {
case unicode_string_view::Encoding::Latin1: {
return unicode_string_view(
CopyChars<char, char32_t>(str_view.latin1_value()));
}
case unicode_string_view::Encoding::Utf16: {
return unicode_string_view(U16ToU32(str_view.utf16_value()));
}
case unicode_string_view::Encoding::Utf32: {
return unicode_string_view(str_view.utf32_value());
}
case unicode_string_view::Encoding::Utf8: {
return unicode_string_view(U8ToU32(str_view.utf8_value()));
}
default: {
TDF_BASE_NOTREACHED();
break;
}
}
return unicode_string_view();
}
static unicode_string_view CovertToUtf8(
const unicode_string_view& str_view,
unicode_string_view::Encoding src_encoding) {
switch (src_encoding) {
case unicode_string_view::Encoding::Latin1: {
const auto& str = str_view.latin1_value();
auto ptr =
reinterpret_cast<const unicode_string_view::char8_t_*>(str.c_str());
return unicode_string_view(ptr, str.length());
}
case unicode_string_view::Encoding::Utf16: {
return unicode_string_view(U16ToU8(str_view.utf16_value()));
}
case unicode_string_view::Encoding::Utf32: {
return unicode_string_view(U32ToU8(str_view.utf32_value()));
}
case unicode_string_view::Encoding::Utf8: {
return unicode_string_view(str_view.utf8_value());
}
default: {
TDF_BASE_NOTREACHED();
break;
}
}
return unicode_string_view();
}
static unicode_string_view Convert(
const unicode_string_view& str_view,
unicode_string_view::Encoding dst_encoding) {
unicode_string_view::Encoding src_encoding = str_view.encoding();
switch (dst_encoding) {
case unicode_string_view::Encoding::Latin1: {
return CovertToLatin(str_view, src_encoding);
}
case unicode_string_view::Encoding::Utf16: {
return CovertToUtf16(str_view, src_encoding);
}
case unicode_string_view::Encoding::Utf32: {
return CovertToUtf32(str_view, src_encoding);
}
case unicode_string_view::Encoding::Utf8: {
return CovertToUtf8(str_view, src_encoding);
}
default: {
TDF_BASE_NOTREACHED();
break;
}
}
return unicode_string_view();
}
@@ -177,7 +200,8 @@ class StringViewUtils {
inline static const char* ToConstCharPointer(
const unicode_string_view& str_view,
unicode_string_view& view_owner) {
TDF_BASE_DCHECK(view_owner.encoding() == unicode_string_view::Encoding::Utf8);
TDF_BASE_DCHECK(view_owner.encoding() ==
unicode_string_view::Encoding::Utf8);
unicode_string_view::Encoding encoding = str_view.encoding();
switch (encoding) {
case unicode_string_view::Encoding::Latin1: {
@@ -24,7 +24,6 @@
#include "core/modules/module_base.h"
#include "core/napi/callback_info.h"
#include "core/napi/js_native_api_types.h"
class Scope;
@@ -22,6 +22,7 @@
#pragma once
#include "core/base/string_view_utils.h"
#include "core/modules/module_base.h"
#include "core/napi/callback_info.h"
#include "core/napi/js_native_api_types.h"
@@ -27,6 +27,7 @@
#include <memory>
#include <string>
#include "base/unicode_string_view.h"
#include "core/base/macros.h"
#include "core/modules/module_base.h"
#include "core/napi/callback_info.h"
+7 -9
View File
@@ -36,10 +36,6 @@ class Scope;
namespace hippy {
namespace napi {
namespace {
using JSValueWrapper = hippy::base::JSValueWrapper;
};
static const char kErrorHandlerJSName[] = "ExceptionHandle.js";
static const char kHippyErrorHandlerName[] = "HippyExceptionHandler";
@@ -80,6 +76,7 @@ class CtxValue {
class Ctx {
public:
using JSValueWrapper = hippy::base::JSValueWrapper;
using unicode_string_view = tdf::base::unicode_string_view;
Ctx() {}
@@ -167,10 +164,11 @@ class Ctx {
virtual std::shared_ptr<CtxValue> GetJsFn(
const unicode_string_view& name) = 0;
virtual bool ThrowExceptionToJS(std::shared_ptr<CtxValue> exception) = 0;
virtual std::shared_ptr<JSValueWrapper> ToJsValueWrapper(
std::shared_ptr<CtxValue> value) = 0;
virtual std::shared_ptr<CtxValue> CreateCtxValue(std::shared_ptr<JSValueWrapper> wrapper) = 0;
virtual std::shared_ptr<CtxValue> CreateCtxValue(
std::shared_ptr<JSValueWrapper> wrapper) = 0;
};
class VM {
@@ -183,9 +181,9 @@ class VM {
class TryCatch {
public:
TryCatch(bool enable = false, std::shared_ptr<Ctx> ctx = nullptr)
: enable_(enable), ctx_(ctx){};
virtual ~TryCatch(){};
explicit TryCatch(bool enable = false, std::shared_ptr<Ctx> ctx = nullptr)
: enable_(enable), ctx_(ctx) {}
virtual ~TryCatch() {}
virtual void ReThrow() = 0;
virtual bool HasCaught() = 0;
virtual bool CanContinue() = 0;
+16 -20
View File
@@ -28,8 +28,8 @@
#include <mutex>
#include <vector>
#include "base/unicode_string_view.h"
#include "base/logging.h"
#include "base/unicode_string_view.h"
#include "core/base/macros.h"
#include "core/napi/js_native_api_types.h"
@@ -45,11 +45,6 @@ const char16_t kLengthStr[] = u"length";
const char16_t kMessageStr[] = u"message";
const char16_t kStackStr[] = u"stack";
namespace {
using unicode_string_view = tdf::base::unicode_string_view;
using JSValueWrapper = hippy::base::JSValueWrapper;
};
class JSCVM : public VM {
public:
JSCVM() { vm_ = JSContextGroupCreate(); }
@@ -68,6 +63,9 @@ class JSCCtxValue;
class JSCCtx : public Ctx {
public:
using unicode_string_view = tdf::base::unicode_string_view;
using JSValueWrapper = hippy::base::JSValueWrapper;
explicit JSCCtx(JSContextGroupRef vm) {
context_ = JSGlobalContextCreateInGroup(vm, nullptr);
@@ -140,12 +138,9 @@ class JSCCtx : public Ctx {
size_t argument_count = 0,
const std::shared_ptr<CtxValue> argumets[] = nullptr);
virtual bool GetValueNumber(std::shared_ptr<CtxValue> value,
double* result);
virtual bool GetValueNumber(std::shared_ptr<CtxValue> value,
int32_t* result);
virtual bool GetValueBoolean(std::shared_ptr<CtxValue> value,
bool* result);
virtual bool GetValueNumber(std::shared_ptr<CtxValue> value, double* result);
virtual bool GetValueNumber(std::shared_ptr<CtxValue> value, int32_t* result);
virtual bool GetValueBoolean(std::shared_ptr<CtxValue> value, bool* result);
virtual bool GetValueString(std::shared_ptr<CtxValue> value,
unicode_string_view* result);
virtual bool GetValueJson(std::shared_ptr<CtxValue> value,
@@ -168,8 +163,7 @@ class JSCCtx : public Ctx {
// Function Helpers
virtual bool IsFunction(std::shared_ptr<CtxValue> value);
virtual unicode_string_view CopyFunctionName(
std::shared_ptr<CtxValue> value);
virtual unicode_string_view CopyFunctionName(std::shared_ptr<CtxValue> value);
virtual std::shared_ptr<CtxValue> RunScript(
const unicode_string_view& data,
@@ -177,13 +171,13 @@ class JSCCtx : public Ctx {
bool is_use_code_cache = false,
unicode_string_view* cache = nullptr,
bool is_copy = true);
virtual std::shared_ptr<CtxValue> GetJsFn(
const unicode_string_view& name);
virtual std::shared_ptr<CtxValue> GetJsFn(const unicode_string_view& name);
virtual bool ThrowExceptionToJS(std::shared_ptr<CtxValue> exception);
virtual std::shared_ptr<JSValueWrapper> ToJsValueWrapper(
std::shared_ptr<CtxValue> value);
virtual std::shared_ptr<CtxValue> CreateCtxValue(std::shared_ptr<JSValueWrapper> wrapper);
virtual std::shared_ptr<CtxValue> CreateCtxValue(
std::shared_ptr<JSValueWrapper> wrapper);
unicode_string_view GetExceptionMsg(std::shared_ptr<CtxValue> exception);
JSStringRef CreateJSCString(const unicode_string_view& str_view);
@@ -193,8 +187,10 @@ class JSCCtx : public Ctx {
bool is_exception_handled_;
};
inline unicode_string_view ToStrView(JSStringRef str) {
return unicode_string_view(reinterpret_cast<const char16_t*>(JSStringGetCharactersPtr(str)), JSStringGetLength(str));
inline tdf::base::unicode_string_view ToStrView(JSStringRef str) {
return tdf::base::unicode_string_view(
reinterpret_cast<const char16_t*>(JSStringGetCharactersPtr(str)),
JSStringGetLength(str));
}
class JSCCtxValue : public CtxValue {
+1 -1
View File
@@ -76,7 +76,7 @@ class V8TryCatch : public TryCatch {
public:
using unicode_string_view = tdf::base::unicode_string_view;
V8TryCatch(bool enable = false, std::shared_ptr<Ctx> ctx = nullptr);
explicit V8TryCatch(bool enable = false, std::shared_ptr<Ctx> ctx = nullptr);
virtual ~V8TryCatch();
virtual void ReThrow();
+5 -3
View File
@@ -48,7 +48,7 @@ class ScopeWrapper {
class Scope {
public:
using unicode_string_view = tdf::base::unicode_string_view;
using unicode_string_view = tdf::base::unicode_string_view;
using RegisterMap = hippy::base::RegisterMap;
using CtxValue = hippy::napi::CtxValue;
using Ctx = hippy::napi::Ctx;
@@ -70,8 +70,10 @@ class Scope {
ModuleBase* GetModuleClass(const unicode_string_view& moduleName);
void AddModuleClass(const unicode_string_view& name,
std::unique_ptr<ModuleBase> module);
std::shared_ptr<CtxValue> GetModuleValue(const unicode_string_view& moduleName);
void AddModuleValue(const unicode_string_view& name, std::shared_ptr<CtxValue> value);
std::shared_ptr<CtxValue> GetModuleValue(
const unicode_string_view& moduleName);
void AddModuleValue(const unicode_string_view& name,
std::shared_ptr<CtxValue> value);
void SaveFunctionData(std::unique_ptr<FunctionData> data);
+1 -4
View File
@@ -22,16 +22,14 @@
#include "core/modules/console_module.h"
#include <string.h>
#include <string>
#include "base/logging.h"
#include "core/base/string_view_utils.h"
#include "core/modules/module_register.h"
#include "core/napi/callback_info.h"
#include "core/napi/js_native_api.h"
#include "core/scope.h"
#include "core/base/string_view_utils.h"
using unicode_string_view = tdf::base::unicode_string_view;
using Ctx = hippy::napi::Ctx;
@@ -41,7 +39,6 @@ REGISTER_MODULE(ConsoleModule, Log)
namespace {
//to do 重载 replace 和 compare
unicode_string_view EscapeMessage(const unicode_string_view& str_view) {
std::string u8_str = StringViewUtils::ToU8StdStr(str_view);
size_t len = u8_str.length();
+72 -73
View File
@@ -42,6 +42,7 @@ REGISTER_MODULE(ContextifyModule, RunInThisContext)
REGISTER_MODULE(ContextifyModule, LoadUntrustedContent)
using unicode_string_view = tdf::base::unicode_string_view;
using u8string = unicode_string_view::u8string;
using Ctx = hippy::napi::Ctx;
using CtxValue = hippy::napi::CtxValue;
using CallbackInfo = hippy::napi::CallbackInfo;
@@ -69,8 +70,8 @@ void ContextifyModule::RunInThisContext(const hippy::napi::CallbackInfo& info) {
std::shared_ptr<CtxValue> ret =
context->RunScript(str_view, key, false, nullptr, false);
if (try_catch->HasCaught()) {
TDF_BASE_DLOG(ERROR) << "GetNativeSourceCode error = " <<
try_catch->GetExceptionMsg();
TDF_BASE_DLOG(ERROR) << "GetNativeSourceCode error = "
<< try_catch->GetExceptionMsg();
info.GetExceptionValue()->Set(try_catch->Exception());
} else {
info.GetReturnValue()->Set(ret);
@@ -116,81 +117,79 @@ void ContextifyModule::LoadUntrustedContent(const CallbackInfo& info) {
std::weak_ptr<Scope> weak_scope = scope;
std::weak_ptr<hippy::napi::CtxValue> weak_function = function;
std::function<void(u8string)> cb =
[this, weak_scope, weak_function, encode,
uri](u8string code) {
std::shared_ptr<Scope> scope = weak_scope.lock();
if (!scope) {
return;
}
std::function<void(u8string)> cb = [this, weak_scope, weak_function, encode,
uri](u8string code) {
std::shared_ptr<Scope> scope = weak_scope.lock();
if (!scope) {
return;
}
unicode_string_view cur_dir;
unicode_string_view file_name;
size_t pos = StringViewUtils::FindLastOf(uri, '/', '/', u'/', U'/');
if (pos != -1) {
cur_dir = StringViewUtils::SubStr(uri, 0, pos + 1);
size_t len = StringViewUtils::GetLength(uri);
file_name = StringViewUtils::SubStr(uri, pos + 1, len);
unicode_string_view cur_dir;
unicode_string_view file_name;
size_t pos = StringViewUtils::FindLastOf(uri, '/', '/', u'/', U'/');
if (pos != -1) {
cur_dir = StringViewUtils::SubStr(uri, 0, pos + 1);
size_t len = StringViewUtils::GetLength(uri);
file_name = StringViewUtils::SubStr(uri, pos + 1, len);
} else {
file_name = uri;
}
if (code.empty()) {
TDF_BASE_DLOG(WARNING) << "Load uri = " << uri << ", code empty";
} else {
TDF_BASE_DLOG(INFO) << "Load uri = " << uri << ", len = " << code.length()
<< ", encode = " << encode
<< ", code = " << unicode_string_view(code);
}
std::shared_ptr<JavaScriptTask> js_task =
std::make_shared<JavaScriptTask>();
js_task->callback = [this, weak_scope, weak_function,
move_code = std::move(code), cur_dir, file_name,
uri]() {
std::shared_ptr<Scope> scope = weak_scope.lock();
if (!scope) {
return;
}
std::shared_ptr<Ctx> ctx = scope->GetContext();
std::shared_ptr<CtxValue> error;
if (!move_code.empty()) {
auto last_dir_str_obj = ctx->GetGlobalStrVar("__HIPPYCURDIR__");
TDF_BASE_DLOG(INFO) << "__HIPPYCURDIR__ cur_dir = " << cur_dir;
ctx->SetGlobalStrVar("__HIPPYCURDIR__", cur_dir);
std::shared_ptr<TryCatch> try_catch =
CreateTryCatchScope(true, scope->GetContext());
try_catch->SetVerbose(true);
unicode_string_view view_code(std::move(move_code));
scope->RunJS(view_code, file_name);
ctx->SetGlobalObjVar("__HIPPYCURDIR__", last_dir_str_obj);
unicode_string_view view_last_dir_str;
ctx->GetValueString(last_dir_str_obj, &view_last_dir_str);
TDF_BASE_DLOG(INFO)
<< "restore __HIPPYCURDIR__ = " << view_last_dir_str;
if (try_catch->HasCaught()) {
error = try_catch->Exception();
TDF_BASE_DLOG(ERROR) << "RequestUntrustedContent error = "
<< try_catch->GetExceptionMsg();
} else {
file_name = uri;
error = ctx->CreateNull();
}
} else {
unicode_string_view err_msg = uri + " not found";
error = ctx->CreateJsError(unicode_string_view(err_msg));
}
if (code.empty()) {
TDF_BASE_DLOG(WARNING) << "Load uri = " << uri << ", code empty";
} else {
TDF_BASE_DLOG(INFO)
<< "Load uri = " << uri << ", len = " << code.length()
<< ", encode = " << encode
<< ", code = " << unicode_string_view(code);
}
std::shared_ptr<JavaScriptTask> js_task =
std::make_shared<JavaScriptTask>();
js_task->callback = [this, weak_scope, weak_function,
move_code = std::move(code), cur_dir, file_name,
uri]() {
std::shared_ptr<Scope> scope = weak_scope.lock();
if (!scope) {
return;
}
std::shared_ptr<Ctx> ctx = scope->GetContext();
std::shared_ptr<CtxValue> error;
if (!move_code.empty()) {
auto last_dir_str_obj = ctx->GetGlobalStrVar("__HIPPYCURDIR__");
TDF_BASE_DLOG(INFO) << "__HIPPYCURDIR__ cur_dir = " << cur_dir;
ctx->SetGlobalStrVar("__HIPPYCURDIR__", cur_dir);
std::shared_ptr<TryCatch> try_catch =
CreateTryCatchScope(true, scope->GetContext());
try_catch->SetVerbose(true);
unicode_string_view view_code(std::move(move_code));
scope->RunJS(view_code, file_name);
ctx->SetGlobalObjVar("__HIPPYCURDIR__", last_dir_str_obj);
unicode_string_view view_last_dir_str;
ctx->GetValueString(last_dir_str_obj, &view_last_dir_str);
TDF_BASE_DLOG(INFO)
<< "restore __HIPPYCURDIR__ = " << view_last_dir_str;
if (try_catch->HasCaught()) {
error = try_catch->Exception();
TDF_BASE_DLOG(ERROR) << "RequestUntrustedContent error = "
<< try_catch->GetExceptionMsg();
} else {
error = ctx->CreateNull();
}
} else {
unicode_string_view err_msg = uri + " not found";
error = ctx->CreateJsError(unicode_string_view(err_msg));
}
std::shared_ptr<CtxValue> function = weak_function.lock();
if (function) {
TDF_BASE_DLOG(INFO) << "run js cb";
std::shared_ptr<CtxValue> argv[] = {error};
ctx->CallFunction(function, 1, argv);
RemoveCBFunc(uri);
}
};
scope->GetTaskRunner()->PostTask(js_task);
};
std::shared_ptr<CtxValue> function = weak_function.lock();
if (function) {
TDF_BASE_DLOG(INFO) << "run js cb";
std::shared_ptr<CtxValue> argv[] = {error};
ctx->CallFunction(function, 1, argv);
RemoveCBFunc(uri);
}
};
scope->GetTaskRunner()->PostTask(js_task);
};
loader->RequestUntrustedContent(uri, cb);
info.GetReturnValue()->SetUndefined();
}
+3
View File
@@ -26,6 +26,7 @@
#include "base/logging.h"
#include "core/base/common.h"
#include "core/base/string_view_utils.h"
#include "core/modules/module_register.h"
#include "core/napi/callback_info.h"
#include "core/task/javascript_task.h"
@@ -38,10 +39,12 @@ REGISTER_MODULE(TimerModule, ClearInterval)
namespace napi = ::hippy::napi;
using unicode_string_view = tdf::base::unicode_string_view;
using Ctx = hippy::napi::Ctx;
using CtxValue = hippy::napi::CtxValue;
using RegisterFunction = hippy::base::RegisterFunction;
using RegisterMap = hippy::base::RegisterMap;
TimerModule::TimerModule() {}
TimerModule::~TimerModule() {}
+2
View File
@@ -36,7 +36,9 @@
namespace hippy {
namespace napi {
using unicode_string_view = tdf::base::unicode_string_view;
using StringViewUtils = hippy::base::StringViewUtils;
using JSValueWrapper = hippy::base::JSValueWrapper;
const char16_t kGlobalStr[] = u"global";
+39 -21
View File
@@ -34,6 +34,7 @@
namespace hippy {
namespace napi {
using unicode_string_view = tdf::base::unicode_string_view;
using StringViewUtils = hippy::base::StringViewUtils;
bool JSCCtx::GetValueNumber(std::shared_ptr<CtxValue> value, double* result) {
@@ -106,7 +107,9 @@ bool JSCCtx::GetValueString(std::shared_ptr<CtxValue> value,
SetException(std::make_shared<JSCCtxValue>(context_, exception));
return false;
}
*result = unicode_string_view(reinterpret_cast<const char16_t*>(JSStringGetCharactersPtr(str_ref)), JSStringGetLength(str_ref));
*result = unicode_string_view(
reinterpret_cast<const char16_t*>(JSStringGetCharactersPtr(str_ref)),
JSStringGetLength(str_ref));
return true;
}
@@ -136,8 +139,9 @@ uint32_t JSCCtx::GetArrayLength(std::shared_ptr<CtxValue> value) {
SetException(std::make_shared<JSCCtxValue>(context_, exception));
return 0;
}
//to do
JSStringRef prop_name = JSStringCreateWithCharacters(reinterpret_cast<const JSChar*>(kLengthStr), arraysize(kLengthStr) - 1);
// to do
JSStringRef prop_name = JSStringCreateWithCharacters(
reinterpret_cast<const JSChar*>(kLengthStr), arraysize(kLengthStr) - 1);
exception = nullptr;
JSValueRef val = JSObjectGetProperty(context_, array, prop_name, &exception);
if (exception) {
@@ -169,7 +173,9 @@ bool JSCCtx::GetValueJson(std::shared_ptr<CtxValue> value,
SetException(std::make_shared<JSCCtxValue>(context_, exception));
return false;
}
*result = unicode_string_view(reinterpret_cast<const char16_t*>(JSStringGetCharactersPtr(str_ref)), JSStringGetLength(str_ref));
*result = unicode_string_view(
reinterpret_cast<const char16_t*>(JSStringGetCharactersPtr(str_ref)),
JSStringGetLength(str_ref));
JSStringRelease(str_ref);
return true;
}
@@ -214,7 +220,8 @@ bool JSCCtx::IsFunction(std::shared_ptr<CtxValue> value) {
return JSObjectIsFunction(context_, object);
}
unicode_string_view JSCCtx::CopyFunctionName(std::shared_ptr<CtxValue> function) {
unicode_string_view JSCCtx::CopyFunctionName(
std::shared_ptr<CtxValue> function) {
TDF_BASE_NOTIMPLEMENTED();
return "";
}
@@ -229,7 +236,8 @@ std::shared_ptr<CtxValue> JSCCtx::CreateBoolean(bool b) {
return std::make_shared<JSCCtxValue>(context_, value);
}
std::shared_ptr<CtxValue> JSCCtx::CreateString(const unicode_string_view& str_view) {
std::shared_ptr<CtxValue> JSCCtx::CreateString(
const unicode_string_view& str_view) {
JSStringRef str_ref = CreateJSCString(str_view);
JSValueRef value = JSValueMakeString(context_, str_ref);
JSStringRelease(str_ref);
@@ -246,7 +254,8 @@ std::shared_ptr<CtxValue> JSCCtx::CreateNull() {
return std::make_shared<JSCCtxValue>(context_, value);
}
std::shared_ptr<CtxValue> JSCCtx::CreateObject(const unicode_string_view& json) {
std::shared_ptr<CtxValue> JSCCtx::CreateObject(
const unicode_string_view& json) {
JSStringRef str_ref = CreateJSCString(json);
JSValueRef value = JSValueMakeFromJSONString(context_, str_ref);
JSStringRelease(str_ref);
@@ -276,7 +285,8 @@ std::shared_ptr<CtxValue> JSCCtx::CreateArray(
return std::make_shared<JSCCtxValue>(context_, value_ref);
}
std::shared_ptr<CtxValue> JSCCtx::CreateJsError(const unicode_string_view& msg) {
std::shared_ptr<CtxValue> JSCCtx::CreateJsError(
const unicode_string_view& msg) {
JSStringRef str_ref = CreateJSCString(msg);
JSValueRef value = JSValueMakeString(context_, str_ref);
JSStringRelease(str_ref);
@@ -385,27 +395,28 @@ std::shared_ptr<CtxValue> JSCCtx::CallFunction(
return std::make_shared<JSCCtxValue>(context_, ret_value_ref);
}
unicode_string_view JSCCtx::GetExceptionMsg(std::shared_ptr<CtxValue> exception) {
unicode_string_view JSCCtx::GetExceptionMsg(
std::shared_ptr<CtxValue> exception) {
if (!exception) {
return unicode_string_view();
}
std::shared_ptr<CtxValue> msg_obj = CopyNamedProperty(exception, unicode_string_view(kMessageStr, arraysize(kMessageStr) - 1));
std::shared_ptr<CtxValue> msg_obj = CopyNamedProperty(
exception, unicode_string_view(kMessageStr, arraysize(kMessageStr) - 1));
unicode_string_view msg_view;
GetValueString(msg_obj, &msg_view);
std::u16string u16_msg;
if (!StringViewUtils::IsEmpty(msg_view)) {
u16_msg = msg_view.utf16_value();
}
std::shared_ptr<CtxValue> stack_obj = CopyNamedProperty(exception, unicode_string_view(kStackStr, arraysize(kStackStr) - 1));
std::shared_ptr<CtxValue> stack_obj = CopyNamedProperty(
exception, unicode_string_view(kStackStr, arraysize(kStackStr) - 1));
unicode_string_view stack_view;
GetValueString(stack_obj, &stack_view);
std::u16string u16_stack;
if (!StringViewUtils::IsEmpty(stack_view)) {
u16_stack = stack_view.utf16_value();
}
// to do
std::u16string str = u"message: " + u16_msg + u", stack: " + u16_stack;
unicode_string_view ret(str.c_str(), str.length());
TDF_BASE_DLOG(ERROR) << "GetExceptionMsg msg = " << ret;
@@ -423,10 +434,10 @@ bool JSCCtx::ThrowExceptionToJS(std::shared_ptr<CtxValue> exception) {
const auto& source_code = hippy::GetNativeSourceCode(kErrorHandlerJSName);
TDF_BASE_DCHECK(source_code.data_ && source_code.length_);
unicode_string_view content(source_code.data_, source_code.length_);
exception_handler =
RunScript(content, kErrorHandlerJSName);
exception_handler = RunScript(content, kErrorHandlerJSName);
bool is_func = IsFunction(exception_handler);
TDF_BASE_CHECK(is_func) << "HandleUncaughtJsError ExceptionHandle.js don't return function!!!";
TDF_BASE_CHECK(is_func)
<< "HandleUncaughtJsError ExceptionHandle.js don't return function!!!";
SetGlobalObjVar(kHippyErrorHandlerName, exception_handler,
PropertyAttribute::ReadOnly);
}
@@ -452,18 +463,25 @@ JSStringRef JSCCtx::CreateJSCString(const unicode_string_view& str_view) {
break;
}
case unicode_string_view::Encoding::Utf8: {
std::string aaa(reinterpret_cast<const char*>(str_view.utf8_value().c_str()), str_view.utf8_value().length());
std::string aaa(
reinterpret_cast<const char*>(str_view.utf8_value().c_str()),
str_view.utf8_value().length());
ret = JSStringCreateWithUTF8CString(aaa.c_str());
break;
}
case unicode_string_view::Encoding::Utf16: {
std::u16string u16_str = str_view.utf16_value();
ret = JSStringCreateWithCharacters(reinterpret_cast<const JSChar*>(u16_str.c_str()), u16_str.length());
ret = JSStringCreateWithCharacters(
reinterpret_cast<const JSChar*>(u16_str.c_str()), u16_str.length());
break;
}
case unicode_string_view::Encoding::Utf32: {
std::u16string u16_str = StringViewUtils::Convert(str_view, unicode_string_view::Encoding::Utf16).utf16_value();
ret = JSStringCreateWithCharacters(reinterpret_cast<const JSChar*>(u16_str.c_str()), u16_str.length());
std::u16string u16_str =
StringViewUtils::Convert(str_view,
unicode_string_view::Encoding::Utf16)
.utf16_value();
ret = JSStringCreateWithCharacters(
reinterpret_cast<const JSChar*>(u16_str.c_str()), u16_str.length());
break;
}
default:
+11 -14
View File
@@ -481,7 +481,6 @@ unicode_string_view V8Ctx::GetStackInfo(v8::Local<v8::Message> message) {
return "";
}
// to do
std::basic_stringstream<char> stack_stream;
int len = trace->GetFrameCount();
for (int i = 0; i < len; ++i) {
@@ -610,19 +609,19 @@ std::shared_ptr<CtxValue> V8Ctx::GetGlobalObjVar(
std::shared_ptr<CtxValue> V8Ctx::GetProperty(
const std::shared_ptr<CtxValue> object,
const unicode_string_view& name) {
TDF_BASE_DLOG(INFO)<< "GetGlobalStrVar name =" << name;
TDF_BASE_DLOG(INFO) << "GetGlobalStrVar name =" << name;
std::shared_ptr<V8CtxValue> ctx_value =
std::static_pointer_cast<V8CtxValue>(object);
std::static_pointer_cast<V8CtxValue>(object);
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context = context_persistent_.Get(isolate_);
v8::Context::Scope context_scope(context);
const v8::Global<v8::Value>& persistent_value =
ctx_value->global_value_;
const v8::Global<v8::Value>& persistent_value = ctx_value->global_value_;
v8::Local<v8::Value> str = CreateV8String(name);
v8::Local<v8::Value> handle_value =
v8::Local<v8::Value>::New(isolate_, persistent_value);
v8::Local<v8::Value> value =
v8::Local<v8::Object>::Cast(handle_value)->Get(context, str).ToLocalChecked();
v8::Local<v8::Value> value = v8::Local<v8::Object>::Cast(handle_value)
->Get(context, str)
.ToLocalChecked();
return std::make_shared<V8CtxValue>(isolate_, value);
}
@@ -780,8 +779,7 @@ std::shared_ptr<CtxValue> V8Ctx::InternalRunScript(
v8::ScriptCompiler::CachedData* cached_data =
new v8::ScriptCompiler::CachedData(
str.c_str(), str.length(),
v8::ScriptCompiler::CachedData::
BufferNotOwned);
v8::ScriptCompiler::CachedData::BufferNotOwned);
v8::ScriptCompiler::Source script_source(source, origin, cached_data);
script = v8::ScriptCompiler::Compile(
context, &script_source, v8::ScriptCompiler::kConsumeCodeCache);
@@ -839,18 +837,17 @@ std::shared_ptr<CtxValue> V8Ctx::GetJsFn(const unicode_string_view& name) {
}
bool V8Ctx::ThrowExceptionToJS(std::shared_ptr<CtxValue> exception) {
unicode_string_view error_handle_name(kHippyErrorHandlerName);
std::shared_ptr<CtxValue> exception_handler =
GetGlobalObjVar(kHippyErrorHandlerName);
GetGlobalObjVar(error_handle_name);
if (!IsFunction(exception_handler)) {
const auto& source_code = hippy::GetNativeSourceCode(kErrorHandlerJSName);
TDF_BASE_DCHECK(source_code.data_ && source_code.length_);
unicode_string_view str_view(source_code.data_, source_code.length_);
exception_handler =
RunScript(str_view, kErrorHandlerJSName, false, nullptr, false);
// bool is_func = IsFunction(exception_handler);
// TDF_BASE_DCHECK(is_func);
SetGlobalObjVar(kHippyErrorHandlerName, exception_handler,
RunScript(str_view, error_handle_name, false, nullptr, false);
SetGlobalObjVar(error_handle_name, exception_handler,
PropertyAttribute::ReadOnly);
}
+7 -8
View File
@@ -44,7 +44,6 @@ using BindingData = hippy::napi::BindingData;
using CtxValue = hippy::napi::CtxValue;
using TryCatch = hippy::napi::TryCatch;
const char kdeallocFuncName[] = "HippyDealloc";
const char kHippyBootstrapJSName[] = "bootstrap.js";
@@ -126,14 +125,12 @@ void Scope::Initialized() {
auto source_code = hippy::GetNativeSourceCode(kHippyBootstrapJSName);
TDF_BASE_DCHECK(source_code.data_ && source_code.length_);
unicode_string_view str_view(source_code.data_, source_code.length_);
std::shared_ptr<CtxValue> function =
context_->RunScript(str_view, kHippyBootstrapJSName, false, nullptr, true);
//to do
unicode_string_view str_view(source_code.data_, source_code.length_);
std::shared_ptr<CtxValue> function = context_->RunScript(
str_view, kHippyBootstrapJSName, false, nullptr, true);
bool is_func = context_->IsFunction(function);
TDF_BASE_CHECK(is_func)
<< "bootstrap return not function, register fail!!!";
TDF_BASE_CHECK(is_func) << "bootstrap return not function, register fail!!!";
if (!is_func) {
TDF_BASE_DLOG(ERROR) << "bootstrap return not function, js = " << str_view
<< ", len = " << source_code.length_;
@@ -182,7 +179,9 @@ void Scope::SaveFunctionData(std::unique_ptr<hippy::napi::FunctionData> data) {
function_data_.push_back(std::move(data));
}
void Scope::RunJS(const unicode_string_view& data, const unicode_string_view& name, bool is_copy) {
void Scope::RunJS(const unicode_string_view& data,
const unicode_string_view& name,
bool is_copy) {
std::weak_ptr<Ctx> weak_context = context_;
JavaScriptTask::Function callback = [data, name, is_copy, weak_context] {
std::shared_ptr<Ctx> context = weak_context.lock();
-9
View File
@@ -5,15 +5,6 @@
namespace tdf {
namespace base {
// typedef int LogSeverity;
// Default log levels. Negative values can be used for verbose log levels.
//constexpr LogSeverity TDF_LOG_INFO = 0;
//constexpr LogSeverity TDF_LOG_WARNING = 1;
//constexpr LogSeverity TDF_LOG_ERROR = 2;
//constexpr LogSeverity TDF_LOG_FATAL = 3;
//constexpr LogSeverity TDF_LOG_NUM_SEVERITIES = 4;
enum LogSeverity {
TDF_LOG_DEBUG,
TDF_LOG_INFO = 0,
+5 -5
View File
@@ -36,9 +36,9 @@ class unicode_string_view final {
return unicode_string_view(reinterpret_cast<const char8_t_*>(utf8_string));
}
unicode_string_view() {}
unicode_string_view(const unicode_string_view& source);
unicode_string_view(const unicode_string_view& source); // NOLINT
#pragma region Latin - 1
unicode_string_view(const char* latin1_string)
unicode_string_view(const char* latin1_string) // NOLINT
: encoding_(Encoding::Latin1), latin1_string_(string(latin1_string)) {}
unicode_string_view(const char* latin1_string, size_t length)
: encoding_(Encoding::Latin1), latin1_string_(string(latin1_string, length)) {}
@@ -48,7 +48,7 @@ class unicode_string_view final {
: encoding_(Encoding::Latin1), latin1_string_(std::move(latin1_string)) {}
#pragma endregion
#pragma region UTF - 8
unicode_string_view(const char8_t_* utf8_string)
unicode_string_view(const char8_t_* utf8_string) // NOLINT
: encoding_(Encoding::Utf8), u8_string_(u8string(utf8_string)) {}
unicode_string_view(const char8_t_* utf8_string, size_t length)
: encoding_(Encoding::Utf8), u8_string_(u8string(utf8_string, length)) {}
@@ -58,7 +58,7 @@ class unicode_string_view final {
: encoding_(Encoding::Utf8), u8_string_(std::move(utf8_string)) {}
#pragma endregion
#pragma region UTF - 16
unicode_string_view(const char16_t* utf16_string)
unicode_string_view(const char16_t* utf16_string) // NOLINT
: encoding_(Encoding::Utf16), u16_string_(u16string(utf16_string)) {}
unicode_string_view(const char16_t* utf16_string, size_t length)
: encoding_(Encoding::Utf16), u16_string_(u16string(utf16_string, length)) {}
@@ -68,7 +68,7 @@ class unicode_string_view final {
: encoding_(Encoding::Utf16), u16_string_(std::move(utf16_string)) {}
#pragma endregion
#pragma region UTF - 32
unicode_string_view(const char32_t* utf32_string)
unicode_string_view(const char32_t* utf32_string) // NOLINT
: encoding_(Encoding::Utf32), u32_string_(u32string(utf32_string)) {}
unicode_string_view(const char32_t* utf32_string, size_t length)
: encoding_(Encoding::Utf32), u32_string_(u32string(utf32_string, length)) {}