refactor(android): remove ambiguous implicit conversions

This commit is contained in:
superzheng
2022-02-25 18:47:11 +08:00
committed by zoomchan-cxj
parent 0a51dd47cc
commit 3ef0657ddb
45 changed files with 239 additions and 182 deletions
+1
View File
@@ -44,6 +44,7 @@ set(ABI_COMPILE_OPTIONS
-Wall
-Wextra
-Wextra-semi
-Wconversion
-Wimplicit-fallthrough
-Wloop-analysis
-Wmissing-field-initializers
@@ -24,7 +24,10 @@
#include <string>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#include "v8/v8.h"
#pragma clang diagnostic pop
class Serializer : public v8::ValueSerializer::Delegate {
public:
@@ -27,7 +27,10 @@
#include "jni/jni_env.h"
#include "jni/jni_utils.h"
#include "jni/scoped_java_ref.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#include "v8/v8-inspector.h"
#pragma clang diagnostic pop
namespace hippy {
namespace inspector {
@@ -24,8 +24,7 @@
#include <jni.h>
#include "core/napi/js_native_api_types.h"
#include "hippy.h"
#include "core/core.h"
struct JNIArgs {
JNIArgs(size_t count) : args_(count) {}
@@ -26,10 +26,10 @@
#include <string>
#include "core/core.h"
#include "convert_utils.h"
#include "core/napi/js_native_turbo.h"
#include "core/napi/v8/js_native_turbo_v8.h"
#include "hippy.h"
#include "scoped_java_ref.h"
class JavaTurboModule : public hippy::napi::HippyTurboModule {
public:
@@ -25,8 +25,8 @@
#include <jni.h>
#include <string>
#include "core/core.h"
#include <unordered_map>
#include <vector>
class JNIRegisterData {
public:
@@ -27,8 +27,6 @@
#include <string>
#include "base/unicode_string_view.h"
#include "core/core.h"
#include "v8/v8.h"
class JniUtils {
using unicode_string_view = tdf::base::unicode_string_view;
@@ -24,8 +24,6 @@
#include <jni.h>
#include "core/core.h"
class JavaRef {
public:
JavaRef(JNIEnv* env, jobject obj);
@@ -22,10 +22,10 @@
#pragma once
#include <core/napi/v8/js_native_turbo_v8.h>
#include <string>
#include "core/core.h"
#include "jni_env.h"
class TurboModuleRuntime {
@@ -46,16 +46,18 @@ bool ReadAsset(const tdf::base::unicode_string_view& path,
auto asset =
AAssetManager_open(aasset_manager, asset_path, AASSET_MODE_STREAMING);
if (asset) {
int size = AAsset_getLength(asset);
if (is_auto_fill) {
size += 1;
size_t size;
if (!hippy::base::numeric_cast<off_t, size_t>(AAsset_getLength(asset) + (is_auto_fill ? 1:0),
size)) {
AAsset_close(asset);
return false;
}
bytes.resize(size);
int offset = 0;
size_t offset = 0;
int readbytes;
while ((readbytes = AAsset_read(asset, &bytes[0] + offset,
bytes.size() - offset)) > 0) {
offset += readbytes;
offset += static_cast<size_t>(readbytes);
}
if (is_auto_fill) {
bytes.back() = '\0';
+5 -4
View File
@@ -35,7 +35,6 @@
#include "bridge/java2js.h"
#include "bridge/js2java.h"
#include "bridge/runtime.h"
#include "core/base/string_view_utils.h"
#include "core/core.h"
#include "jni/turbo_module_manager.h"
#include "jni/exception_handler.h"
@@ -253,7 +252,7 @@ jboolean RunScriptFromUri(JNIEnv* j_env,
jobject j_cb) {
TDF_BASE_DLOG(INFO) << "runScriptFromUri begin, j_runtime_id = "
<< j_runtime_id;
std::shared_ptr<Runtime> runtime = Runtime::Find(hippy::base::CheckedNumericCast<jlong, int32_t>(j_runtime_id));
auto runtime = Runtime::Find(hippy::base::checked_numeric_cast<jlong, int32_t>(j_runtime_id));
if (!runtime) {
TDF_BASE_DLOG(WARNING)
<< "HippyBridgeImpl runScriptFromUri, j_runtime_id invalid";
@@ -456,10 +455,12 @@ jlong InitInstance(JNIEnv* j_env,
jclass cls = j_env->GetObjectClass(j_vm_init_param);
jfieldID init_field = j_env->GetFieldID(cls,"initialHeapSize","J");
param->initial_heap_size_in_bytes =
hippy::base::CheckedNumericCast<jlong, size_t>(j_env->GetLongField(j_vm_init_param, init_field));
hippy::base::checked_numeric_cast<jlong, size_t>(j_env->GetLongField(j_vm_init_param,
init_field));
jfieldID max_field = j_env->GetFieldID(cls,"maximumHeapSize","J");
param->maximum_heap_size_in_bytes =
hippy::base::CheckedNumericCast<jlong, size_t>(j_env->GetLongField(j_vm_init_param, max_field));
hippy::base::checked_numeric_cast<jlong, size_t>(j_env->GetLongField(j_vm_init_param,
max_field));
TDF_BASE_CHECK(param->initial_heap_size_in_bytes <= param->maximum_heap_size_in_bytes);
}
std::shared_ptr<Engine> engine;
@@ -24,7 +24,7 @@
#include "bridge/js2java.h"
#include "bridge/runtime.h"
#include "core/base/string_view_utils.h"
#include "core/core.h"
#include "jni/jni_register.h"
namespace hippy {
@@ -71,7 +71,7 @@ void CallFunction(JNIEnv* j_env,
bytes buffer_data,
std::shared_ptr<JavaRef> buffer_owner) {
TDF_BASE_DLOG(INFO) << "CallFunction j_runtime_id = " << j_runtime_id;
std::shared_ptr<Runtime> runtime = Runtime::Find(hippy::base::CheckedNumericCast<jlong, int32_t>(j_runtime_id));
auto runtime = Runtime::Find(hippy::base::checked_numeric_cast<jlong, int32_t>(j_runtime_id));
if (!runtime) {
TDF_BASE_DLOG(WARNING) << "CallFunction j_runtime_id invalid";
return;
@@ -207,7 +207,8 @@ void CallFunctionByDirectBuffer(JNIEnv* j_env,
static_cast<char*>(j_env->GetDirectBufferAddress(j_buffer));
TDF_BASE_CHECK(buffer_address != nullptr);
CallFunction(j_env, j_obj, j_action, j_runtime_id, j_callback,
bytes(buffer_address + j_offset, hippy::base::CheckedNumericCast<jint, size_t>(j_length)),
bytes(buffer_address + j_offset,
hippy::base::checked_numeric_cast<jint, size_t>(j_length)),
std::make_shared<JavaRef>(j_env, j_buffer));
}
@@ -28,7 +28,6 @@
#include "base/unicode_string_view.h"
#include "bridge/runtime.h"
#include "bridge/serializer.h"
#include "core/base/string_view_utils.h"
#include "jni/jni_env.h"
using unicode_string_view = tdf::base::unicode_string_view;
@@ -153,10 +152,10 @@ void CallJava(hippy::napi::CBDataTuple *data) {
if (transfer_type == 1) { // Direct
j_buffer = j_env->NewDirectByteBuffer(
const_cast<void *>(reinterpret_cast<const void *>(buffer_data.c_str())),
hippy::base::CheckedNumericCast<size_t, jlong>(buffer_data.length()));
hippy::base::checked_numeric_cast<size_t, jlong>(buffer_data.length()));
j_method = instance->GetMethods().j_call_natives_direct_method_id;
} else { // Default
auto buffer_size = hippy::base::CheckedNumericCast<size_t, jsize>(buffer_data.length());
auto buffer_size = hippy::base::checked_numeric_cast<size_t, jsize>(buffer_data.length());
j_buffer = j_env->NewByteArray(buffer_size);
j_env->SetByteArrayRegion(
reinterpret_cast<jbyteArray>(j_buffer), 0, buffer_size,
@@ -22,8 +22,6 @@
#include "bridge/serializer.h"
#include "v8/v8.h"
const int kMaxReusedBuffersSize = 128 * 1024; // 128k
Serializer::Serializer(v8::Isolate* isolate,
@@ -24,6 +24,7 @@
#include <string>
#include "core/core.h"
#include "jni/jni_env.h"
namespace hippy {
@@ -40,7 +41,8 @@ void V8ChannelImpl::sendResponse(
}
const uint16_t* source = message->string().characters16();
auto len = hippy::base::CheckedNumericCast<size_t, jsize>(message->string().length() * sizeof(*source));
auto len = hippy::base::checked_numeric_cast<size_t, jsize>(
message->string().length() * sizeof(*source));
std::shared_ptr<JNIEnvironment> instance = JNIEnvironment::GetInstance();
JNIEnv* j_env = instance->AttachCurrentThread();
jbyteArray msg = j_env->NewByteArray(len);
@@ -64,7 +66,8 @@ void V8ChannelImpl::sendNotification(
}
const uint16_t* source = message->string().characters16();
auto len = hippy::base::CheckedNumericCast<size_t, jsize>(message->string().length() * sizeof(*source));
auto len = hippy::base::checked_numeric_cast<size_t, jsize>(
message->string().length() * sizeof(*source));
std::shared_ptr<JNIEnvironment> instance = JNIEnvironment::GetInstance();
JNIEnv* j_env = instance->AttachCurrentThread();
jbyteArray msg = j_env->NewByteArray(len);
@@ -22,8 +22,6 @@
#include "inspector/v8_inspector_client_impl.h"
#include "core/core.h"
namespace hippy {
namespace inspector {
@@ -25,7 +25,6 @@
#include <memory>
#include <tuple>
#include "core/napi/v8/js_native_turbo_v8.h"
#include "jni/java_turbo_module.h"
using namespace hippy::napi;
@@ -128,22 +127,24 @@ std::tuple<bool, std::string, bool> ConvertUtils::HandleBasicType(TurboEnv &turb
if (type == kInt) {
int32_t num;
if (!context->GetValueNumber(value, &num)) {
return std::make_tuple(false, "Must be int.", false);
return std::make_tuple(false, "value must be int", false);
}
j_args.i = hippy::base::CheckedNumericCast<int32_t, jint>(num);
j_args.i = num;
} else {
double num;
if (!context->GetValueNumber(value, &num)) {
return std::make_tuple(false, "Must be long/float/double.", false);
return std::make_tuple(false, "value must be long/float/double", false);
}
if (type == kDouble) { // double
j_args.d = hippy::base::CheckedNumericCast<double, jdouble>(num);
j_args.d = num;
} else if (type == kFloat) { // float
j_args.f = hippy::base::CheckedNumericCast<double, jfloat>(num);
j_args.f = static_cast<jfloat>(num);
} else if (type == kLong) { // long
j_args.j = hippy::base::CheckedNumericCast<double, jlong>(num);
if (!hippy::base::numeric_cast<double, jlong>(num, j_args.j)) {
return std::make_tuple(false, "value out of jlong boundary", false);
}
}
}
@@ -154,7 +155,7 @@ std::tuple<bool, std::string, bool> ConvertUtils::HandleBasicType(TurboEnv &turb
if (type == "Z") {
bool b;
if (!context->GetValueBoolean(value, &b)) {
return std::make_tuple(false, "Must be boolean.", false);
return std::make_tuple(false, "value must be boolean", false);
}
j_args.z = b;
@@ -191,7 +192,7 @@ ConvertUtils::HandleObjectType(TurboEnv &turbo_env,
if (turbo_env.context_->GetValueString(value, &str_view)) {
str = StringViewUtils::ToU8StdStr(str_view);
} else {
return std::make_tuple(false, "Must be String.", false);
return std::make_tuple(false, "value must be string", false);
}
TDF_BASE_DLOG(INFO) << "Promise callId " << str.c_str();
@@ -211,7 +212,7 @@ ConvertUtils::HandleObjectType(TurboEnv &turbo_env,
// HippyArray
if (type == kHippyArray) {
if (!context->IsArray(value)) {
return std::make_tuple(false, "Must be Array.", false);
return std::make_tuple(false, "value must be array", false);
}
auto to_array_tuple = ToHippyArray(turbo_env, value);
if (!std::get<0>(to_array_tuple)) {
@@ -224,7 +225,7 @@ ConvertUtils::HandleObjectType(TurboEnv &turbo_env,
// HippyMap
if (type == kHippyMap) {
if (!context->IsMap(value)) {
return std::make_tuple(false, "Must be Map.", false);
return std::make_tuple(false, "value must be map", false);
}
auto to_map_tuple = ToHippyMap(turbo_env, value);
if (!std::get<0>(to_map_tuple)) {
@@ -238,7 +239,7 @@ ConvertUtils::HandleObjectType(TurboEnv &turbo_env,
if (type == kBooleanObject) {
bool b;
if (!context->GetValueBoolean(value, &b)) {
return std::make_tuple(false, "Must be Boolean.", false);
return std::make_tuple(false, "value must be boolean", false);
}
j_args.l =
make_global(env->NewObject(boolean_clazz, boolean_constructor, b));
@@ -252,7 +253,7 @@ ConvertUtils::HandleObjectType(TurboEnv &turbo_env,
if (turbo_env.context_->GetValueString(value, &str_view)) {
str = StringViewUtils::ToU8StdStr(str_view);
} else {
return std::make_tuple(false, "Must be String.", false);
return std::make_tuple(false, "value must be string", false);
}
j_args.l = make_global(env->NewStringUTF(str.c_str()));
@@ -261,25 +262,35 @@ ConvertUtils::HandleObjectType(TurboEnv &turbo_env,
// Number Object
if (IsNumberObject(type)) {
double num;
if (!context->GetValueNumber(value, &num)) {
return std::make_tuple(true, "Integer/Double/Float/Long.", false);
}
if (type == kIntegerObject) {
int32_t num;
if (!context->GetValueNumber(value, &num)) {
return std::make_tuple(true, "value must be int", false);
}
j_args.l = make_global(
env->NewObject(integer_clazz, integer_constructor, (int) num));
} else if (type == kDoubleObject) {
j_args.l =
make_global(env->NewObject(double_clazz, double_constructor, num));
} else if (type == kFloatObject) {
j_args.l = make_global(
env->NewObject(float_clazz, float_constructor, (float) num));
} else if (type == kLongObject) {
j_args.l = make_global(
env->NewObject(long_clazz, long_constructor, (int64_t) num));
env->NewObject(integer_clazz, integer_constructor, num));
} else {
return std::make_tuple(false, "", false);
double num;
if (!context->GetValueNumber(value, &num)) {
return std::make_tuple(true, "value must be long/float/double", false);
}
if (type == kDoubleObject) {
j_args.l =
make_global(env->NewObject(double_clazz, double_constructor, num));
} else if (type == kFloatObject) {
j_args.l = make_global(
env->NewObject(float_clazz, float_constructor, static_cast<float>(num)));
} else if (type == kLongObject) {
jlong jlong_value;
if (!hippy::base::numeric_cast<double, jlong>(num, jlong_value)) {
return std::make_tuple(true, "value out of jlong boundary", false);
}
j_args.l = make_global(
env->NewObject(long_clazz, long_constructor, jlong_value));
} else {
return std::make_tuple(false, "", false);
}
}
return std::make_tuple(true, "", true);
}
@@ -312,7 +323,7 @@ std::tuple<bool, std::string, jobject> ConvertUtils::ToHippyMap(TurboEnv &turbo_
if (turbo_env.context_->GetValueString(key, &str_view)) {
key_str = StringViewUtils::ToU8StdStr(str_view);
} else {
return std::make_tuple(false, "Key must be String in Map.", static_cast<jobject>(nullptr));
return std::make_tuple(false, "key must be string in map", static_cast<jobject>(nullptr));
}
jobject key_j_obj = env->NewStringUTF(key_str.c_str());
@@ -387,7 +398,7 @@ std::tuple<bool, std::string, jobject> ConvertUtils::ToJObject(TurboEnv &turbo_e
} else if (context->IsNullOrUndefined(value)) {
result = nullptr;
} else {
return std::make_tuple(false, "UnSupported Type in HippyArray or HippyMap.",
return std::make_tuple(false, "unsupported type in HippyArray or HippyMap",
static_cast<jobject>(nullptr));
}
return std::make_tuple(true, "", result);
@@ -519,7 +530,7 @@ std::tuple<bool, std::string, std::shared_ptr<CtxValue>> ConvertUtils::ConvertMe
method_info.signature_.find_last_of(')') + 1);
if (kLong == return_type) {
auto result = env->CallLongMethodA(obj, method_info.method_id_, args);
ret = ctx->CreateNumber(hippy::base::CheckedNumericCast<jlong, double>(result));
ret = ctx->CreateNumber(hippy::base::checked_numeric_cast<jlong, double>(result));
} else if (kInt == return_type) {
jint result = env->CallIntMethodA(obj, method_info.method_id_, args);
ret = ctx->CreateNumber(result);
@@ -634,7 +645,7 @@ ConvertUtils::ToJsArray(TurboEnv &turbo_env, jobject array) {
}
std::shared_ptr<V8Ctx> v8_ctx = std::static_pointer_cast<V8Ctx>(ctx);
JNIEnv *env = JNIEnvironment::GetInstance()->AttachCurrentThread();
int size = env->CallIntMethod(array, hippy_array_size);
auto size = env->CallIntMethod(array, hippy_array_size);
if (size <= 0) {
return std::make_tuple(true, "", ctx->CreateNull());
@@ -648,7 +659,7 @@ ConvertUtils::ToJsArray(TurboEnv &turbo_env, jobject array) {
}
value[i] = std::get<2>(value_tuple);
}
return std::make_tuple(true, "", ctx->CreateArray(size, value));
return std::make_tuple(true, "", ctx->CreateArray(static_cast<size_t>(size), value));
}
std::tuple<bool, std::string, std::shared_ptr<CtxValue>> ConvertUtils::ToJsMap(TurboEnv &turbo_env,
@@ -664,21 +675,21 @@ std::tuple<bool, std::string, std::shared_ptr<CtxValue>> ConvertUtils::ToJsMap(T
return std::make_tuple(true, "", ctx->CreateNull());
}
int size = env->CallIntMethod(array, hippy_array_size);
auto size = env->CallIntMethod(array, hippy_array_size);
if (size <= 0) {
return std::make_tuple(true, "", ctx->CreateNull());
}
std::shared_ptr<V8Ctx> v8_ctx = std::static_pointer_cast<V8Ctx>(ctx);
std::shared_ptr<CtxValue> value[size];
for (int i = 0; i < size; i++) {
for (auto i = 0; i < size; i++) {
auto value_tuple = ToJsValueInArray(turbo_env, array, i);
if (!std::get<0>(value_tuple)) {
return value_tuple;
}
value[i] = std::get<2>(value_tuple);
}
return std::make_tuple(true, "", v8_ctx->CreateMap(size, value));
return std::make_tuple(true, "", v8_ctx->CreateMap(static_cast<size_t>(size), value));
}
bool ConvertUtils::Init() {
@@ -22,7 +22,6 @@
#include "jni/exception_handler.h"
#include "core/base/string_view_utils.h"
#include "core/core.h"
#include "jni/jni_env.h"
#include "jni/jni_utils.h"
@@ -22,12 +22,6 @@
#include "jni/java_turbo_module.h"
#include "core/base/string_view_utils.h"
#include "core/napi/js_native_api_types.h"
#include "core/napi/v8/js_native_turbo_v8.h"
#include "hippy.h"
#include "jni/convert_utils.h"
using namespace hippy::napi;
using unicode_string_view = tdf::base::unicode_string_view;
using StringViewUtils = hippy::base::StringViewUtils;
@@ -23,7 +23,7 @@
#include "jni/jni_register.h"
#include "base/unicode_string_view.h"
#include "core/base/string_view_utils.h"
#include "core/core.h"
#include "jni/jni_env.h"
#include "jni/uri.h"
@@ -80,7 +80,9 @@ bool JNIRegister::RegisterMethods(JNIEnv* j_env) {
methods.push_back(method);
}
j_env->RegisterNatives(j_class, methods.data(), hippy::base::CheckedNumericCast<size_t, jint>(methods.size()));
j_env->RegisterNatives(j_class,
methods.data(),
hippy::base::checked_numeric_cast<size_t, jint>(methods.size()));
}
return true;
}
@@ -26,7 +26,7 @@
#include <cstdlib>
#include <cstring>
#include "core/base/string_view_utils.h"
#include "core/core.h"
using unicode_string_view = tdf::base::unicode_string_view;
using StringViewUtils = hippy::base::StringViewUtils;
@@ -56,7 +56,7 @@ JniUtils::bytes JniUtils::AppendJavaByteArrayToBytes(JNIEnv* j_env,
}
bytes ret;
ret.resize(hippy::base::CheckedNumericCast<jsize, size_t>(j_length));
ret.resize(hippy::base::checked_numeric_cast<jsize, size_t>(j_length));
j_env->GetByteArrayRegion(j_byte_array, j_offset, j_len,
reinterpret_cast<int8_t*>(&ret[0]));
return ret;
@@ -81,7 +81,7 @@ unicode_string_view JniUtils::JByteArrayToStrView(JNIEnv* j_env,
}
std::string ret;
ret.resize(hippy::base::CheckedNumericCast<jsize, size_t>(j_len));
ret.resize(hippy::base::checked_numeric_cast<jsize, size_t>(j_len));
j_env->GetByteArrayRegion(j_byte_array, j_offset, j_len,
reinterpret_cast<int8_t*>(&ret[0]));
@@ -95,7 +95,7 @@ jstring JniUtils::StrViewToJString(JNIEnv* j_env,
StringViewUtils::Convert(str_view, unicode_string_view::Encoding::Utf16)
.utf16_value();
return j_env->NewString(reinterpret_cast<const jchar*>(str.c_str()),
hippy::base::CheckedNumericCast<size_t, jsize>(str.length()));
hippy::base::checked_numeric_cast<size_t, jsize>(str.length()));
}
unicode_string_view::u8string JniUtils::ToU8String(JNIEnv* j_env,
@@ -105,7 +105,8 @@ unicode_string_view::u8string JniUtils::ToU8String(JNIEnv* j_env,
const char* c_str = j_env->GetStringUTFChars(j_str, nullptr);
auto len = j_env->GetStringLength(j_str);
unicode_string_view::u8string ret(
reinterpret_cast<const unicode_string_view::char8_t_*>(c_str), hippy::base::CheckedNumericCast<jsize, size_t>(len));
reinterpret_cast<const unicode_string_view::char8_t_ *>(c_str),
hippy::base::checked_numeric_cast<jsize, size_t>(len));
j_env->ReleaseStringUTFChars(j_str, c_str);
return ret;
}
@@ -115,7 +116,8 @@ unicode_string_view JniUtils::ToStrView(JNIEnv* j_env, jstring j_str) {
const jchar* j_char = j_env->GetStringChars(j_str, nullptr);
auto len = j_env->GetStringLength(j_str);
unicode_string_view ret(reinterpret_cast<const char16_t*>(j_char), hippy::base::CheckedNumericCast<jsize, size_t>(len));
unicode_string_view ret(reinterpret_cast<const char16_t *>(j_char),
hippy::base::checked_numeric_cast<jsize, size_t>(len));
j_env->ReleaseStringChars(j_str, j_char);
return ret;
}
@@ -22,16 +22,12 @@
#include "jni/turbo_module_manager.h"
#include <jni.h>
#include <cstdint>
#include "bridge/runtime.h"
#include "core/core.h"
#include "core/napi/v8/js_native_api_v8.h"
#include "jni/java_turbo_module.h"
#include "jni/jni_register.h"
#include "jni/jni_utils.h"
#include "jni/scoped_java_ref.h"
REGISTER_JNI("com/tencent/mtt/hippy/bridge/jsi/TurboModuleManager", // NOLINT(cert-err58-cpp)
"install",
@@ -186,7 +182,7 @@ void TurboModuleManager::Destroy() {
int Install(JNIEnv *, jobject j_obj, jlong j_runtime_id) {
TDF_BASE_LOG(INFO) << "install TurboModuleManager";
std::shared_ptr<Runtime> runtime = Runtime::Find(hippy::base::CheckedNumericCast<jlong, int32_t>(j_runtime_id));
auto runtime = Runtime::Find(hippy::base::checked_numeric_cast<jlong, int32_t>(j_runtime_id));
if (!runtime) {
TDF_BASE_LOG(ERROR) << "TurboModuleManager install, v8RuntimePtr invalid";
return -1;
@@ -212,7 +208,7 @@ int Install(JNIEnv *, jobject j_obj, jlong j_runtime_id) {
void Uninstall(JNIEnv *, jobject, jlong j_runtime_id) {
TDF_BASE_LOG(INFO) << "uninstall install TurboModuleManager";
std::shared_ptr<Runtime> runtime = Runtime::Find(hippy::base::CheckedNumericCast<jlong, int32_t>(j_runtime_id));
auto runtime = Runtime::Find(hippy::base::checked_numeric_cast<jlong, int32_t>(j_runtime_id));
if (!runtime) {
TDF_BASE_LOG(ERROR) << "TurboModuleManager install, v8RuntimePtr invalid";
return;
+1 -2
View File
@@ -22,8 +22,7 @@
#include "jni/uri.h"
#include "base/unicode_string_view.h"
#include "core/base/string_view_utils.h"
#include "core/core.h"
#include "jni/jni_env.h"
#include "jni/jni_utils.h"
@@ -25,8 +25,6 @@
#include <future>
#include "bridge/runtime.h"
#include "core/base/string_view_utils.h"
#include "core/core.h"
#include "jni/jni_env.h"
#include "jni/jni_register.h"
#include "jni/jni_utils.h"
@@ -190,7 +188,7 @@ void OnResourceReady(JNIEnv* j_env,
jlong j_request_id) {
TDF_BASE_DLOG(INFO) << "HippyBridgeImpl onResourceReady j_runtime_id = "
<< j_runtime_id;
std::shared_ptr<Runtime> runtime = Runtime::Find(hippy::base::CheckedNumericCast<jlong, int32_t>(j_runtime_id));
auto runtime = Runtime::Find(hippy::base::checked_numeric_cast<jlong, int32_t>(j_runtime_id));
if (!runtime) {
TDF_BASE_DLOG(WARNING)
<< "HippyBridgeImpl onResourceReady, j_runtime_id invalid";
@@ -231,7 +229,8 @@ void OnResourceReady(JNIEnv* j_env,
return;
}
u8string str(reinterpret_cast<const char8_t_*>(buff), hippy::base::CheckedNumericCast<jlong, size_t>(len));
u8string str(reinterpret_cast<const char8_t_ *>(buff),
hippy::base::checked_numeric_cast<jlong, size_t>(len));
cb(std::move(str));
}
+4 -1
View File
@@ -26,13 +26,16 @@
#include <chrono>
#include "common.h"
namespace hippy {
namespace base {
inline uint64_t MonotonicallyIncreasingTime() {
auto now = std::chrono::steady_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now)
.time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(now_ms).count();
auto ticks = std::chrono::duration_cast<std::chrono::milliseconds>(now_ms).count();
return checked_numeric_cast<long long, uint64_t>(ticks);
}
} // namespace base
} // namespace hippy
+18 -4
View File
@@ -26,6 +26,8 @@
#include <string>
#include <unordered_map>
#include "base/logging.h"
namespace hippy {
namespace base {
@@ -52,10 +54,22 @@ auto MakeCopyable(F&& f) {
}
template<typename SourceType, typename TargetType>
static constexpr TargetType CheckedNumericCast(SourceType value) {
using source_type_limits = typename std::numeric_limits<SourceType>;
TDF_BASE_CHECK(value <= source_type_limits::max() && value >= source_type_limits::min());
return static_cast<TargetType>(value);
static constexpr bool numeric_cast(const SourceType& source, TargetType& target) {
auto target_value = static_cast<TargetType>(source);
if (static_cast<SourceType>(target_value)!=source || (target_value < 0 && source > 0)
|| (target_value > 0 && source < 0)) {
return false;
}
target = target_value;
return true;
}
template<typename SourceType, typename TargetType>
static constexpr TargetType checked_numeric_cast(const SourceType& source) {
TargetType target;
auto result = numeric_cast<SourceType, TargetType>(source, target);
TDF_BASE_CHECK(result);
return target;
}
} // namespace base
+7 -5
View File
@@ -32,6 +32,7 @@
#include "base/logging.h"
#include "base/unicode_string_view.h"
#include "core/base/common.h"
#include "core/base/string_view_utils.h"
namespace hippy {
@@ -62,13 +63,14 @@ class HippyFile {
std::streamsize size = file.gcount();
file.clear();
file.seekg(0, std::ios_base::beg);
std::streamsize data_size = size;
if (is_auto_fill) {
data_size += 1;
size_t data_size;
if (!numeric_cast<std::streamsize, size_t>(size + (is_auto_fill ? 1:0), data_size)) {
file.close();
return false;
}
bytes.resize(data_size);
std::streamsize read_size =
file.read(reinterpret_cast<char*>(&bytes[0]), size).gcount();
auto read_size =
file.read(reinterpret_cast<char *>(&bytes[0]), size).gcount();
if (size != read_size) {
TDF_BASE_DLOG(WARNING)
<< "ReadFile file_path = " << file_path << ", size = " << size
+23
View File
@@ -1,4 +1,27 @@
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#pragma once
#include <cstdlib>
#include <string>
#include <unordered_map>
+2 -2
View File
@@ -224,7 +224,7 @@ class StringViewUtils {
}
inline static unicode_string_view ConstCharPointerToStrView(const char *p,
size_t len = -1) {
size_t len = static_cast<size_t>(-1)) {
size_t length;
if (len == static_cast<size_t>(-1)) {
length = strlen(p);
@@ -241,7 +241,7 @@ class StringViewUtils {
return std::string(U8ToConstCharPointer(str.c_str()), str.length());
}
static const size_t npos = -1;
static const size_t npos = static_cast<size_t>(-1);
inline static size_t FindLastOf(const unicode_string_view &str_view,
unicode_string_view::char8_t_ u8_ch,
+4 -4
View File
@@ -34,15 +34,15 @@ class Thread {
class Options {
public:
Options() : name_("hippy:<unknown>"), stack_size_(0) {}
explicit Options(const char* name, int stack_size = 0)
explicit Options(const char* name, size_t stack_size = 0)
: name_(name), stack_size_(stack_size) {}
const char* name() const { return name_; }
int stack_size() const { return stack_size_; }
size_t stack_size() const { return stack_size_; }
private:
const char* name_;
int stack_size_;
size_t stack_size_;
};
public:
@@ -63,7 +63,7 @@ class Thread {
protected:
char name_[kMaxThreadNameLength]{};
int stack_size_;
size_t stack_size_;
pthread_t thread_{};
ThreadId thread_id_;
+4 -3
View File
@@ -22,8 +22,6 @@
#pragma once
#include "base/logging.h"
#include "base/unicode_string_view.h"
#include "core/base/base_time.h"
#include "core/base/common.h"
#include "core/base/file.h"
@@ -33,6 +31,7 @@
#include "core/base/thread.h"
#include "core/base/thread_id.h"
#include "core/base/uri_loader.h"
#include "core/base/string_view_utils.h"
#include "core/engine.h"
#include "core/modules/console_module.h"
#include "core/modules/contextify_module.h"
@@ -45,11 +44,13 @@
#include "core/napi/native_source_code.h"
#include "core/scope.h"
#ifdef OS_ANDROID
#ifdef JS_V8
#include "core/napi/v8/js_native_api_v8.h"
#include "core/napi/v8/js_native_turbo_v8.h"
#else
#include "core/napi/jsc/js_native_api_jsc.h"
#include "core/napi/jsc/js_native_jsc_helper.h"
#include "core/napi/jsc/js_native_turbo_jsc.h"
#endif
#include "core/task/common_task.h"
+1 -3
View File
@@ -22,8 +22,6 @@
#pragma once
#include <stdio.h>
#include <memory>
#include <vector>
@@ -72,7 +70,7 @@ class CallbackInfo {
CallbackInfo &operator=(const CallbackInfo &) = delete;
void AddValue(const std::shared_ptr<CtxValue>& value);
std::shared_ptr<CtxValue> operator[](int index) const;
std::shared_ptr<CtxValue> operator[](size_t index) const;
size_t Length() const { return values_.size(); }
std::shared_ptr<Scope> GetScope() const { return scope_; }
+4 -1
View File
@@ -41,7 +41,10 @@
#include "core/scope.h"
#include "jni/jni_env.h"
#include "jni/jni_utils.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#include "v8/v8.h"
#pragma clang diagnostic pop
namespace hippy {
namespace napi {
@@ -193,7 +196,7 @@ class V8Ctx : public Ctx {
uint32_t index) override;
// Map Helpers
virtual uint32_t GetMapLength(std::shared_ptr<CtxValue>& value);
virtual size_t GetMapLength(std::shared_ptr<CtxValue>& value);
virtual std::shared_ptr<CtxValue> ConvertMapToArray(
const std::shared_ptr<CtxValue>& value);
@@ -26,7 +26,10 @@
#include "core/napi/js_native_turbo.h"
#include "core/napi/v8/js_native_api_v8.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#include "v8/v8.h"
#pragma clang diagnostic pop
namespace hippy {
namespace napi {
@@ -143,8 +146,8 @@ class V8TurboEnv : public TurboEnv {
TDF_BASE_DLOG(INFO) << "enter call";
std::vector<std::shared_ptr<CtxValue>> arg_values;
int arg_size = callback_info.Length();
for (int i = 0; i < arg_size; i++) {
auto arg_size = callback_info.Length();
for (auto i = 0; i < arg_size; i++) {
arg_values.push_back(
std::make_shared<V8CtxValue>(isolate, callback_info[i]));
}
@@ -153,7 +156,10 @@ class V8TurboEnv : public TurboEnv {
std::make_shared<V8CtxValue>(isolate, callback_info.This());
std::shared_ptr<CtxValue> result = host_function_proxy.func_(
v8_turbo_env, this_val, arg_values.data(), arg_size);
v8_turbo_env,
this_val,
arg_values.data(),
hippy::base::checked_numeric_cast<int, size_t>(arg_size));
std::shared_ptr<V8CtxValue> v8_result =
std::static_pointer_cast<V8CtxValue>(result);
callback_info.GetReturnValue().Set(v8_result->global_value_);
+7 -8
View File
@@ -25,12 +25,7 @@
#include <dirent.h>
#include <sys/stat.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "base/logging.h"
namespace hippy {
namespace base {
@@ -43,9 +38,13 @@ bool HippyFile::SaveFile(const unicode_string_view& file_path,
const char* path = StringViewUtils::ToConstCharPointer(file_path, owner);
std::ofstream file(path, mode);
if (file.is_open()) {
file.write(content.c_str(), content.length());
std::streamsize len;
bool is_success = numeric_cast<size_t, std::streamsize>(content.length(), len);
if (is_success) {
file.write(content.c_str(), len);
}
file.close();
return true;
return is_success;
} else {
return false;
}
@@ -123,7 +122,7 @@ uint64_t HippyFile::GetFileModifytime(const unicode_string_view& file_path) {
if (fstat(fd, &statInfo)) {
return 0;
}
uint64_t modify_time = statInfo.st_mtime;
uint64_t modify_time = checked_numeric_cast<time_t, uint64_t>(statInfo.st_mtime);
TDF_BASE_DLOG(INFO) << "modify_time = " << modify_time;
fclose(fp);
return modify_time;
+24 -1
View File
@@ -1,6 +1,29 @@
#include "core/base/hash.h"
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "core/base/js_value_wrapper.h"
#include "core/base/hash.h"
using JSValueWrapper = hippy::base::JSValueWrapper;
std::size_t std::hash<JSValueWrapper>::operator()(
-3
View File
@@ -22,9 +22,6 @@
#include "core/base/task_runner.h"
#include <memory>
#include <utility>
#include "base/logging.h"
#include "core/base/base_time.h"
#include "core/base/macros.h"
-4
View File
@@ -22,12 +22,8 @@
#include "core/engine.h"
#include <memory>
#include <mutex> // NOLINT(build/c++11)
#include "core/scope.h"
#include "core/task/javascript_task.h"
#include "core/task/javascript_task_runner.h"
constexpr uint32_t Engine::kDefaultWorkerPoolSize = 1;
+1 -2
View File
@@ -27,7 +27,6 @@
#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"
@@ -44,7 +43,7 @@ unicode_string_view EscapeMessage(const unicode_string_view& str_view) {
size_t len = u8_str.length();
std::string ret;
for (size_t i = 0; i < len; i++) {
char16_t c = u8_str[i];
auto c = u8_str[i];
ret += c;
if (c == '%') {
ret += '%';
-3
View File
@@ -27,12 +27,9 @@
#include <string>
#include "base/logging.h"
#include "core/base/string_view_utils.h"
#include "core/base/uri_loader.h"
#include "core/modules/module_register.h"
#include "core/napi/callback_info.h"
#include "core/napi/js_native_api.h"
#include "core/napi/js_native_api_types.h"
#include "core/napi/native_source_code.h"
#include "core/task/common_task.h"
#include "core/task/javascript_task.h"
+1 -4
View File
@@ -22,13 +22,10 @@
#include "core/modules/timer_module.h"
#include <memory>
#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"
#include "core/task/javascript_task_runner.h"
@@ -72,7 +69,7 @@ void TimerModule::ClearInterval(const napi::CallbackInfo& info) {
return;
}
TaskId task_id = argument1;
TaskId task_id = hippy::base::checked_numeric_cast<int32_t, TaskId>(argument1);
Cancel(task_id, scope);
info.GetReturnValue()->Set(context->CreateNumber(task_id));
}
+1 -1
View File
@@ -38,7 +38,7 @@ void CallbackInfo::AddValue(const std::shared_ptr<CtxValue>& value) {
values_.push_back(value);
}
std::shared_ptr<CtxValue> CallbackInfo::operator[](int index) const {
std::shared_ptr<CtxValue> CallbackInfo::operator[](size_t index) const {
if (index < 0 || static_cast<size_t>(index) >= Length()) {
return nullptr;
}
+22 -25
View File
@@ -23,20 +23,10 @@
#include "core/napi/v8/js_native_api_v8.h"
#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
#include <vector>
#include "base/logging.h"
#include "core/base/common.h"
#include "core/base/macros.h"
#include "core/base/string_view_utils.h"
#include "core/modules/module_base.h"
#include "core/napi/callback_info.h"
#include "core/napi/native_source_code.h"
#include "core/scope.h"
#include "hippy.h"
#include "v8/libplatform/libplatform.h"
namespace hippy {
@@ -140,7 +130,7 @@ void GetInternalBinding(const v8::FunctionCallbackInfo<v8::Value>& info) {
return;
}
size_t count = info.Length();
auto count = info.Length();
if (count <= 0 || !info[0]->IsString()) {
info.GetReturnValue().SetUndefined();
return;
@@ -500,9 +490,9 @@ unicode_string_view V8Ctx::GetStackInfo(v8::Local<v8::Message> message) {
}
std::basic_stringstream<char> stack_stream;
int len = trace->GetFrameCount();
for (int i = 0; i < len; ++i) {
v8::Local<v8::StackFrame> frame = trace->GetFrame(isolate_, i);
auto len = trace->GetFrameCount();
for (auto i = 0; i < len; ++i) {
v8::Local<v8::StackFrame> frame = trace->GetFrame(isolate_, static_cast<uint32_t>(i));
if (frame.IsEmpty()) {
continue;
}
@@ -733,7 +723,7 @@ std::shared_ptr<CtxValue> V8Ctx::RunScript(const unicode_string_view& str_view,
if (is_copy) {
source = v8::String::NewFromOneByte(
isolate_, reinterpret_cast<const uint8_t*>(str.c_str()),
v8::NewStringType::kInternalized, hippy::base::CheckedNumericCast<size_t, int>(str.length()));
v8::NewStringType::kInternalized, hippy::base::checked_numeric_cast<size_t, int>(str.length()));
} else {
auto* one_byte =
new ExternalOneByteStringResourceImpl(
@@ -747,7 +737,7 @@ std::shared_ptr<CtxValue> V8Ctx::RunScript(const unicode_string_view& str_view,
if (is_copy) {
source = v8::String::NewFromTwoByte(
isolate_, reinterpret_cast<const uint16_t*>(str.c_str()),
v8::NewStringType::kNormal, hippy::base::CheckedNumericCast<size_t, int>(str.length()));
v8::NewStringType::kNormal, hippy::base::checked_numeric_cast<size_t, int>(str.length()));
} else {
auto* two_byte = new ExternalStringResourceImpl(
reinterpret_cast<const uint16_t*>(str.c_str()), str.length());
@@ -763,7 +753,7 @@ std::shared_ptr<CtxValue> V8Ctx::RunScript(const unicode_string_view& str_view,
bytes.length() / sizeof(char16_t));
source = v8::String::NewFromTwoByte(
isolate_, reinterpret_cast<const uint16_t*>(str.c_str()),
v8::NewStringType::kNormal, hippy::base::CheckedNumericCast<size_t, int>(str.length()));
v8::NewStringType::kNormal, hippy::base::checked_numeric_cast<size_t, int>(str.length()));
break;
}
case unicode_string_view::Encoding::Utf8: {
@@ -808,7 +798,7 @@ std::shared_ptr<CtxValue> V8Ctx::InternalRunScript(
const unicode_string_view::u8string& str = cache->utf8_value();
auto* cached_data =
new v8::ScriptCompiler::CachedData(
str.c_str(), hippy::base::CheckedNumericCast<size_t, int>(str.length()),
str.c_str(), hippy::base::checked_numeric_cast<size_t, int>(str.length()),
v8::ScriptCompiler::CachedData::BufferNotOwned);
v8::ScriptCompiler::Source script_source(source, origin, cached_data);
script = v8::ScriptCompiler::Compile(
@@ -826,7 +816,9 @@ std::shared_ptr<CtxValue> V8Ctx::InternalRunScript(
const v8::ScriptCompiler::CachedData* cached_data =
v8::ScriptCompiler::CreateCodeCache(
script.ToLocalChecked()->GetUnboundScript());
*cache = unicode_string_view(cached_data->data, cached_data->length);
*cache = unicode_string_view(cached_data->data,
hippy::base::checked_numeric_cast<int,
size_t>(cached_data->length));
} else {
script = v8::Script::Compile(context, source, &origin);
}
@@ -1169,7 +1161,7 @@ std::shared_ptr<CtxValue> V8Ctx::CreateCtxValue(
unicode_string_view V8Ctx::ToStringView(v8::Local<v8::String> str) const {
TDF_BASE_DCHECK(!str.IsEmpty());
v8::String* v8_string = v8::String::Cast(*str);
int32_t len = v8_string->Length();
auto len = hippy::base::checked_numeric_cast<int, size_t>(v8_string->Length());
if (v8_string->IsOneByte()) {
std::string one_byte_string;
one_byte_string.resize(len);
@@ -1203,11 +1195,16 @@ std::shared_ptr<CtxValue> V8Ctx::CreateObject(const unicode_string_view& json) {
std::shared_ptr<CtxValue> V8Ctx::CreateArray(
size_t count,
std::shared_ptr<CtxValue> value[]) {
int array_size;
if (!hippy::base::numeric_cast<size_t, int>(count, array_size)) {
TDF_BASE_LOG(ERROR) << "array length out of boundary";
return nullptr;
}
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Array> array = v8::Array::New(isolate_, hippy::base::CheckedNumericCast<size_t, int>(count));
v8::Local<v8::Array> array = v8::Array::New(isolate_, array_size);
v8::Local<v8::Context> context = context_persistent_.Get(isolate_);
v8::Context::Scope context_scope(context);
for (size_t i = 0; i < count; i++) {
for (auto i = 0; i < array_size; i++) {
v8::Local<v8::Value> handle_value;
std::shared_ptr<V8CtxValue> ctx_value =
std::static_pointer_cast<V8CtxValue>(value[i]);
@@ -1218,7 +1215,7 @@ std::shared_ptr<CtxValue> V8Ctx::CreateArray(
TDF_BASE_LOG(ERROR) << "array item error";
return nullptr;
}
if (!array->Set(context, i, handle_value).FromMaybe(false)) {
if (!array->Set(context, static_cast<uint32_t >(i), handle_value).FromMaybe(false)) {
TDF_BASE_LOG(ERROR) << "set array item failed";
return nullptr;
}
@@ -1511,7 +1508,7 @@ std::shared_ptr<CtxValue> V8Ctx::CopyArrayElement(
}
// Map Helpers
uint32_t V8Ctx::GetMapLength(std::shared_ptr<CtxValue>& value) {
size_t V8Ctx::GetMapLength(std::shared_ptr<CtxValue>& value) {
if (value == nullptr) {
return 0;
}
+2 -4
View File
@@ -22,10 +22,8 @@
#include "core/napi/v8/js_native_turbo_v8.h"
#include <core/base/string_view_utils.h>
#include <jni/java_turbo_module.h>
#include "hippy.h"
#include "core/base/string_view_utils.h"
#include "jni/java_turbo_module.h"
using unicode_string_view = tdf::base::unicode_string_view;
-2
View File
@@ -28,8 +28,6 @@
#include <vector>
#include "base/logging.h"
#include "core/base/common.h"
#include "core/engine.h"
#include "core/modules/module_register.h"
#include "core/napi/native_source_code.h"
#include "core/task/javascript_task.h"