diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..750886e --- /dev/null +++ b/.clang-format @@ -0,0 +1,36 @@ +# Use the Google style in this project. +BasedOnStyle: Google + +# Some folks prefer to write "int& foo" while others prefer "int &foo". The +# Google Style Guide only asks for consistency within a project, we chose +# "int& foo" for this project: +DerivePointerAlignment: false +PointerAlignment: Left + +IncludeBlocks: Merge +IncludeCategories: +# Matches common headers first, but sorts them after project includes +- Regex: '^\"google/cloud/(internal/|grpc_utils/|testing_util/|[^/]+\.h)' + Priority: 1000 +- Regex: '^\"google/cloud/' # project includes should sort first + Priority: 500 +- Regex: '^\"' + Priority: 1500 +- Regex: '^' + Priority: 5000 + +# Format raw string literals with a `pb` or `proto` tag as proto. +RawStringFormats: +- Language: TextProto + Delimiters: + - 'pb' + - 'proto' + BasedOnStyle: Google + +CommentPragmas: '(@copydoc)' diff --git a/app/build.gradle b/app/build.gradle index 4df5c81..720d41a 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,6 +32,14 @@ android { minifyEnabled false shrinkResources false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + + // The doNotStrip option is not properly scoped to the "debug" build type + // See https://issuetracker.google.com/issues/155215248. + packagingOptions { + doNotStrip "**/*.so" + } + debuggable true + jniDebuggable true } release { minifyEnabled true @@ -54,9 +62,6 @@ android { } } - packagingOptions { - doNotStrip "**/*.so" - } ndkVersion '21.0.6113669' preBuild.dependsOn(rootProject.cargo) @@ -64,7 +69,7 @@ android { } dependencies { - implementation project(":inspector") + // implementation project(":inspector") implementation 'com.github.tbruyelle:rxpermissions:0.10.2' implementation fileTree(dir: 'libs', include: ['*.jar']) androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { diff --git a/app/rust/build/api.cpp b/app/rust/build/api.cpp index 6b86659..7ff7e4a 100644 --- a/app/rust/build/api.cpp +++ b/app/rust/build/api.cpp @@ -2,23 +2,23 @@ #include "util/util.h" #include "v8_jni/wrapper.h" -extern "C" void __unused deno_lock(void *d_) { - auto *d = Deno::unwrap(d_); +extern "C" void __unused deno_lock(void* d_) { + auto* d = Deno::unwrap(d_); d->locker_ = new Locker(d->isolate_); } -extern "C" void __unused deno_unlock(void *d_) { - auto *d = Deno::unwrap(d_); +extern "C" void __unused deno_unlock(void* d_) { + auto* d = Deno::unwrap(d_); delete d->locker_; d->locker_ = nullptr; } -extern "C" void __unused set_deno_data(void *d_, void *user_data_) { +extern "C" void __unused set_deno_data(void* d_, void* user_data_) { auto d = Deno::unwrap(d_); d->user_data_ = user_data_; } -extern "C" void __unused set_deno_resolver(void *d_) { +extern "C" void __unused set_deno_resolver(void* d_) { auto d = Deno::unwrap(d_); lock_isolate(d->isolate_); Local context_ = d->context_.Get(d->isolate_); @@ -31,36 +31,35 @@ extern "C" void __unused set_deno_resolver(void *d_) { d->resolver_.Reset(d->isolate_, resolver_); } -const char *__unused jStringToChar(JNIEnv *env, jstring name) { - const char *str = env->GetStringUTFChars(name, 0); +const char* __unused jStringToChar(JNIEnv* env, jstring name) { + const char* str = env->GetStringUTFChars(name, 0); env->ReleaseStringUTFChars(name, str); return str; } -extern "C" Local - __unused v8_function_callback_info_get(FunctionCallbackInfo *info, - int32_t index) { +extern "C" Local __unused v8_function_callback_info_get( + FunctionCallbackInfo* info, int32_t index) { return (*info)[index]; } extern "C" int32_t __unused -v8_function_callback_length(FunctionCallbackInfo *info) { +v8_function_callback_length(FunctionCallbackInfo* info) { return info->Length(); } -extern "C" void throw_exception(const uint8_t *data, uint32_t len) { - Isolate *isolate_ = Isolate::GetCurrent(); - Local message = String::NewFromUtf8(isolate_, (const char *)data, +extern "C" void throw_exception(const uint8_t* data, uint32_t len) { + Isolate* isolate_ = Isolate::GetCurrent(); + Local message = String::NewFromUtf8(isolate_, (const char*) data, NewStringType::kNormal, len) - .ToLocalChecked(); + .ToLocalChecked(); isolate_->ThrowException(message); } -const char *ToCString(const String::Utf8Value &value) { +const char* ToCString(const String::Utf8Value& value) { return *value ? *value : ""; } -void Log(const FunctionCallbackInfo &args) { +void Log(const FunctionCallbackInfo& args) { auto d = Deno::unwrap(args.Data().As()->Value()); lock_isolate(d->isolate_); @@ -72,19 +71,19 @@ void Log(const FunctionCallbackInfo &args) { } // exception -void ExceptionString(TryCatch *try_catch) { - Isolate *isolate = Isolate::GetCurrent(); +void ExceptionString(TryCatch* try_catch) { + Isolate* isolate = Isolate::GetCurrent(); Local context = isolate->GetCurrentContext(); String::Utf8Value exception(isolate, try_catch->Exception()); - const char *exception_string = ToCString(exception); + const char* exception_string = ToCString(exception); adb_debug(exception_string); Handle message = try_catch->Message(); if (!message.IsEmpty()) { String::Utf8Value filename(isolate, message->GetScriptOrigin().ResourceName()); - const char *filename_string = ToCString(filename); + const char* filename_string = ToCString(filename); int line_number = message->GetLineNumber(context).ToChecked(); char s[1024]; // (filename):(line number) @@ -93,7 +92,7 @@ void ExceptionString(TryCatch *try_catch) { } } -void Fetch(const FunctionCallbackInfo &args) { +void Fetch(const FunctionCallbackInfo& args) { assert(args[0]->IsString()); assert(args[1]->IsNumber()); @@ -108,8 +107,8 @@ void Fetch(const FunctionCallbackInfo &args) { fetch(d->user_data_, *url, promise_id); } -void HeapStatic(const FunctionCallbackInfo &args) { - auto d = reinterpret_cast(args.Data().As()->Value()); +void HeapStatic(const FunctionCallbackInfo& args) { + auto d = reinterpret_cast(args.Data().As()->Value()); lock_isolate(d->isolate_); d->isolate_->RequestGarbageCollectionForTesting( @@ -118,23 +117,23 @@ void HeapStatic(const FunctionCallbackInfo &args) { HeapStatistics stats; d->isolate_->GetHeapStatistics(&stats); args.GetReturnValue().Set( - Number::New(d->isolate_, (double)stats.used_heap_size())); + Number::New(d->isolate_, (double) stats.used_heap_size())); } -void Toast(const FunctionCallbackInfo &args) { +void Toast(const FunctionCallbackInfo& args) { assert(args[0]->IsString()); - auto d = reinterpret_cast(args.Data().As()->Value()); + auto d = reinterpret_cast(args.Data().As()->Value()); lock_isolate(d->isolate_); String::Utf8Value value(d->isolate_, args[0]->ToObject(d->isolate_)); } -void NewTimer(const FunctionCallbackInfo &args) { - assert(args[0]->IsUint32()); // promise_id - assert(args[1]->IsUint32()); // delay +void NewTimer(const FunctionCallbackInfo& args) { + assert(args[0]->IsUint32()); // promise_id + assert(args[1]->IsUint32()); // delay - void *d_ = args.Data().As()->Value(); + void* d_ = args.Data().As()->Value(); auto d = Deno::unwrap(d_); lock_isolate(d->isolate_); @@ -144,7 +143,7 @@ void NewTimer(const FunctionCallbackInfo &args) { } /* do not remove */ -extern "C" void __unused fire_callback(void *d_, uint32_t promise_id) { +extern "C" void __unused fire_callback(void* d_, uint32_t promise_id) { auto d = Deno::unwrap(d_); lock_isolate(d->isolate_); @@ -162,8 +161,8 @@ extern "C" void __unused fire_callback(void *d_, uint32_t promise_id) { } /* do not remove */ -extern "C" __unused void resolve(void *d_, uint32_t promise_id, - const char *value) { +extern "C" __unused void resolve(void* d_, uint32_t promise_id, + const char* value) { auto d = Deno::unwrap(d_); lock_isolate(d->isolate_); @@ -181,8 +180,8 @@ extern "C" __unused void resolve(void *d_, uint32_t promise_id, resolver_->Call(context_, Null(d->isolate_), argc, argv); } -extern "C" void __unused SendBuffer(const FunctionCallbackInfo &args) { - Isolate *isolate_ = args.GetIsolate(); +extern "C" void __unused SendBuffer(const FunctionCallbackInfo& args) { + Isolate* isolate_ = args.GetIsolate(); assert(args[0]->IsArrayBuffer()); assert(args[1]->IsFunction()); @@ -190,20 +189,20 @@ extern "C" void __unused SendBuffer(const FunctionCallbackInfo &args) { auto ab = Local::Cast(args[0]); auto contents = ab->GetContents(); - char *str = worker_send_bytes(contents.Data(), ab->ByteLength(), args[1]); + char* str = worker_send_bytes(contents.Data(), ab->ByteLength(), args[1]); args.GetReturnValue().Set(String::NewFromUtf8(isolate_, str)); } -extern "C" void *__unused deno_init(deno_recv_cb recv_cb, uint32_t uuid) { +extern "C" void* __unused deno_init(deno_recv_cb recv_cb, uint32_t uuid) { V8::InitializeICU(); - Platform *platform_ = platform::CreateDefaultPlatform(); + Platform* platform_ = platform::CreateDefaultPlatform(); V8::InitializePlatform(platform_); V8::Initialize(); Isolate::CreateParams create_params; create_params.array_buffer_allocator = ArrayBuffer::Allocator::NewDefaultAllocator(); - Isolate *isolate_ = Isolate::New(create_params); + Isolate* isolate_ = Isolate::New(create_params); Isolate::Scope isolate_scope(isolate_); HandleScope scope(isolate_); @@ -255,8 +254,8 @@ extern "C" void *__unused deno_init(deno_recv_cb recv_cb, uint32_t uuid) { return deno->Into(); } -extern "C" void __unused eval_script(void *deno_, const char *name_s, - const char *script_s) { +extern "C" void __unused eval_script(void* deno_, const char* name_s, + const char* script_s) { auto deno = Deno::unwrap(deno_); lock_isolate(deno->isolate_); @@ -284,7 +283,7 @@ extern "C" void __unused eval_script(void *deno_, const char *name_s, } } -Deno *__unused lookup_deno_by_uuid(std::map isolate_map_, +Deno* __unused lookup_deno_by_uuid(std::map isolate_map_, uint32_t uuid) { auto it = isolate_map_.find(uuid); if (it != isolate_map_.end()) { @@ -294,25 +293,25 @@ Deno *__unused lookup_deno_by_uuid(std::map isolate_map_, } // Utils for Rust represent -extern "C" void __unused upcast_value(Local *source, - Local *dest) { +extern "C" void __unused upcast_value(Local* source, + Local* dest) { // upcast *dest = Local::Cast(*source); } -extern "C" void __unused new_number(Local *out, double value) { - Isolate *isolate_ = Isolate::GetCurrent(); +extern "C" void __unused new_number(Local* out, double value) { + Isolate* isolate_ = Isolate::GetCurrent(); *out = Number::New(isolate_, value); } -extern "C" double __unused number_value(Local *number) { +extern "C" double __unused number_value(Local* number) { assert((*number)->IsNumber()); - Isolate *isolate = Isolate::GetCurrent(); + Isolate* isolate = Isolate::GetCurrent(); return (*number)->NumberValue(isolate->GetCurrentContext()).ToChecked(); } -extern "C" void __unused new_array(Local *out, uint32_t length) { - Isolate *isolate_ = Isolate::GetCurrent(); +extern "C" void __unused new_array(Local* out, uint32_t length) { + Isolate* isolate_ = Isolate::GetCurrent(); *out = Array::New(isolate_, length); } @@ -321,29 +320,28 @@ extern "C" bool __unused mem_same_handle(Local v1, Local v2) { } extern "C" void __unused -set_return_value(const FunctionCallbackInfo &args, Local value) { +set_return_value(const FunctionCallbackInfo& args, Local value) { args.GetReturnValue().Set(value); } -extern "C" void __unused new_array_buffer(Local *out, void *data, +extern "C" void __unused new_array_buffer(Local* out, void* data, size_t byte_length) { - Isolate *isolate_ = Isolate::GetCurrent(); + Isolate* isolate_ = Isolate::GetCurrent(); *out = ArrayBuffer::New(isolate_, data, byte_length, ArrayBufferCreationMode::kInternalized); } -extern "C" bool __unused function_call(Local *out, Local fun, +extern "C" bool __unused function_call(Local* out, Local fun, Local self, uint32_t argc, Local argv[]) { - - Isolate *isolate_ = Isolate::GetCurrent(); + Isolate* isolate_ = Isolate::GetCurrent(); MaybeLocal maybe_result = fun->Call(isolate_->GetCurrentContext(), self, argc, argv); return maybe_result.ToLocal(out); } -extern "C" const char *__unused raw_value(Local val) { - Isolate *isolate_ = Isolate::GetCurrent(); +extern "C" const char* __unused raw_value(Local val) { + Isolate* isolate_ = Isolate::GetCurrent(); if (val->IsNullOrUndefined()) { isolate_->ThrowException(String::NewFromUtf8(isolate_, "")); @@ -355,27 +353,27 @@ extern "C" const char *__unused raw_value(Local val) { } Local result = - JSON::Stringify(isolate_->GetCurrentContext(), val->ToObject(isolate_), - String::NewFromUtf8(isolate_, " ")) + JSON::Stringify(isolate_->GetCurrentContext(), val->ToObject(isolate_), + String::NewFromUtf8(isolate_, " ")) .ToLocalChecked(); String::Utf8Value utf8(isolate_, result); return *utf8; } -extern "C" void __unused new_utf8_string(Local *out, - const uint8_t *data, uint32_t len) { +extern "C" void __unused new_utf8_string(Local* out, + const uint8_t* data, uint32_t len) { MaybeLocal maybe_local = String::NewFromUtf8( - Isolate::GetCurrent(), (const char *)data, NewStringType::kNormal, len); + Isolate::GetCurrent(), (const char*) data, NewStringType::kNormal, len); maybe_local.ToLocal(out); } -extern "C" void __unused new_object(Local *out) { - Isolate *isolate_ = Isolate::GetCurrent(); +extern "C" void __unused new_object(Local* out) { + Isolate* isolate_ = Isolate::GetCurrent(); *out = Object::New(isolate_); } -extern "C" bool __unused object_set(bool *out, Local obj, +extern "C" bool __unused object_set(bool* out, Local obj, Local key, Local val) { Local context_ = Isolate::GetCurrent()->GetCurrentContext(); Maybe maybe = obj->Set(context_, key, val); @@ -386,25 +384,24 @@ extern "C" bool __unused object_set(bool *out, Local obj, return false; } -extern "C" bool __unused object_index_set(bool *out, Local obj, +extern "C" bool __unused object_index_set(bool* out, Local obj, uint32_t index, Local value) { Local context_ = Isolate::GetCurrent()->GetCurrentContext(); Maybe maybe = obj->Set(context_, index, value); return maybe.IsJust() && (*out = maybe.FromJust(), true); } -bool string_get(Local *key, const uint8_t *data, uint32_t len) { - Isolate *isolate_ = Isolate::GetCurrent(); +bool string_get(Local* key, const uint8_t* data, uint32_t len) { + Isolate* isolate_ = Isolate::GetCurrent(); MaybeLocal maybe_key = String::NewFromUtf8( - isolate_, (const char *)data, NewStringType::kNormal, len); + isolate_, (const char*) data, NewStringType::kNormal, len); return maybe_key.ToLocal(key); } -extern "C" bool __unused object_string_set(bool *out, Local obj, - const uint8_t *data, uint32_t len, +extern "C" bool __unused object_string_set(bool* out, Local obj, + const uint8_t* data, uint32_t len, Local val) { - - Isolate *isolate_ = Isolate::GetCurrent(); + Isolate* isolate_ = Isolate::GetCurrent(); Local key; if (!string_get(&key, data, len)) { return false; @@ -414,10 +411,9 @@ extern "C" bool __unused object_string_set(bool *out, Local obj, return maybe.IsJust() && (*out = maybe.FromJust(), true); } -extern "C" bool __unused object_string_get(Local *out, Local obj, - const uint8_t *data, uint32_t len) { - - Isolate *isolate_ = Isolate::GetCurrent(); +extern "C" bool __unused object_string_get(Local* out, Local obj, + const uint8_t* data, uint32_t len) { + Isolate* isolate_ = Isolate::GetCurrent(); Local key; if (!string_get(&key, data, len)) { return false; @@ -427,37 +423,37 @@ extern "C" bool __unused object_string_get(Local *out, Local obj, return maybe_local.ToLocal(out); } -extern "C" void __unused null_value(Local *out) { +extern "C" void __unused null_value(Local* out) { *out = Null(Isolate::GetCurrent()); } -extern "C" void __unused undefined_value(Local *out) { +extern "C" void __unused undefined_value(Local* out) { *out = Undefined(Isolate::GetCurrent()); } -extern "C" void new_function(Local *out, FunctionCallback cb) { - Isolate *isolate_ = Isolate::GetCurrent(); +extern "C" void new_function(Local* out, FunctionCallback cb) { + Isolate* isolate_ = Isolate::GetCurrent(); MaybeLocal maybe_local = Function::New(isolate_->GetCurrentContext(), cb); maybe_local.ToLocal(out); } -extern "C" void __unused promise_then(Local *promise, +extern "C" void __unused promise_then(Local* promise, Local handler) { - Isolate *isolate_ = Isolate::GetCurrent(); + Isolate* isolate_ = Isolate::GetCurrent(); Local context_ = isolate_->GetCurrentContext(); MaybeLocal maybe_local = (*promise)->Then(context_, handler); maybe_local.ToLocal(promise); } -extern "C" void callback_info_get(const FunctionCallbackInfo &args, - uint32_t index, Local *out) { +extern "C" void callback_info_get(const FunctionCallbackInfo& args, + uint32_t index, Local* out) { assert(args.Length() >= index); *out = args[index]; } -extern "C" void attach_current_thread(JNIEnv **env) { - int res = vm->GetEnv(reinterpret_cast(&(*env)), JNI_VERSION_1_6); +extern "C" void attach_current_thread(JNIEnv** env) { + int res = vm->GetEnv(reinterpret_cast(&(*env)), JNI_VERSION_1_6); if (res != JNI_OK) { res = vm->AttachCurrentThread(&(*env), nullptr); if (JNI_OK != res) { diff --git a/app/rust/build/api.h b/app/rust/build/api.h index 75337e5..122910c 100644 --- a/app/rust/build/api.h +++ b/app/rust/build/api.h @@ -4,60 +4,60 @@ #ifdef __cplusplus #endif +#include #include #include #include #include #include -#include #include #include #include #include -#define lock_isolate(isolate_) \ - Locker locker(isolate_); \ - Isolate::Scope isolate_scope(isolate_); \ +#define lock_isolate(isolate_) \ + Locker locker(isolate_); \ + Isolate::Scope isolate_scope(isolate_); \ HandleScope handle_scope(isolate_); using namespace v8; using ResolverPersistent = Persistent; -typedef void (*deno_recv_cb)(void *data, uint32_t promise_id, uint32_t delay); +typedef void (* deno_recv_cb)(void* data, uint32_t promise_id, uint32_t delay); // NDK vm instance -static JavaVM *vm; +static JavaVM* vm; // Rust bridge extern "C" { -void adb_debug(const char *); -void fetch(void *data, const char *, uint32_t); -void test_fn(const FunctionCallbackInfo &); -char *worker_send_bytes(void *, size_t, Local val); -void attach_current_thread(JNIEnv **env); -void register_vm(JavaVM *_vm) { vm = _vm; } -JavaVM *__unused get_java_vm() { return vm; } +void adb_debug(const char*); +void fetch(void* data, const char*, uint32_t); +void test_fn(const FunctionCallbackInfo&); +char* worker_send_bytes(void*, size_t, Local val); +void attach_current_thread(JNIEnv** env); +void register_vm(JavaVM* _vm) { vm = _vm; } +JavaVM* __unused get_java_vm() { return vm; } } class Deno { public: - Isolate *isolate_; + Isolate* isolate_; Persistent context_; Persistent global_; Persistent resolver_; - Locker *locker_; - JNIEnv *env_; + Locker* locker_; + JNIEnv* env_; uint32_t uuid_; - void *user_data_; + void* user_data_; deno_recv_cb recv_cb_; - explicit Deno(Isolate *isolate, uint32_t uuid) + explicit Deno(Isolate* isolate, uint32_t uuid) : isolate_(isolate), uuid_(uuid) { attach_current_thread(&this->env_); } - Deno(Isolate *isolate, Local context, Local global) + Deno(Isolate* isolate, Local context, Local global) : isolate_(isolate) { this->context_.Reset(this->isolate_, context); this->global_.Reset(this->isolate_, global); @@ -77,12 +77,12 @@ class Deno { this->recv_cb_ = recv_cb; } - void *Into() { return reinterpret_cast(this); } + void* Into() { return reinterpret_cast(this); } - static Deno *unwrap(void *d_) { return reinterpret_cast(d_); } + static Deno* unwrap(void* d_) { return reinterpret_cast(d_); } }; -static std::map isolate_map_; +static std::map isolate_map_; #ifdef __cplusplus #endif diff --git a/app/rust/build/util/util.cpp b/app/rust/build/util/util.cpp index ee4fc0f..aacf101 100644 --- a/app/rust/build/util/util.cpp +++ b/app/rust/build/util/util.cpp @@ -1,8 +1,8 @@ #include "util.h" -string_t _new_string_t(const std::string &s) { +string_t _new_string_t(const std::string& s) { string_t st; - st.ptr = reinterpret_cast(s.c_str()); + st.ptr = reinterpret_cast(s.c_str()); st.len = static_cast(s.length()); return st; } @@ -14,7 +14,7 @@ value_t _new_int_value(uint32_t val) { return v; } -value_t _new_string_value(char *bytes, int length) { +value_t _new_string_value(char* bytes, int length) { value_t v; v.data.s = _rust_new_string(bytes); v.t = 3; @@ -22,14 +22,14 @@ value_t _new_string_value(char *bytes, int length) { } std::string v8str(Local input) { - Isolate *isolate = Isolate::GetCurrent(); + Isolate* isolate = Isolate::GetCurrent(); String::Utf8Value val(isolate, input); std::string s(*val); return s; } string_t v8string_t(Local input) { - Isolate *isolate = Isolate::GetCurrent(); + Isolate* isolate = Isolate::GetCurrent(); String::Utf8Value val(isolate, input); std::string s(*val); return _new_string_t(s); diff --git a/app/rust/build/util/util.h b/app/rust/build/util/util.h index 12645e2..0ddc6ba 100644 --- a/app/rust/build/util/util.h +++ b/app/rust/build/util/util.h @@ -10,10 +10,10 @@ using namespace v8; using namespace std; -typedef void (*JNIClosure)(void *, jlong, jlong); +typedef void (* JNIClosure)(void*, jlong, jlong); typedef struct { - const uint8_t *ptr; + const uint8_t* ptr; uint32_t len; } string_t; @@ -31,20 +31,20 @@ using namespace v8; using namespace std; extern "C" { -jlong _rust_new_string(const char *); -jlong new_instance(string_t, const value_t *, uint32_t); -void instance_call_args(jlong, jlong, const value_t *, uint32_t, - const FunctionCallbackInfo &); -void adb_debug(const char *); +jlong _rust_new_string(const char*); +jlong new_instance(string_t, const value_t*, uint32_t); +void instance_call_args(jlong, jlong, const value_t*, uint32_t, + const FunctionCallbackInfo&); +void adb_debug(const char*); } -string_t _new_string_t(const std::string &s); +string_t _new_string_t(const std::string& s); value_t _new_int_value(uint32_t val); -value_t _new_string_value(char *, int); +value_t _new_string_value(char*, int); std::string v8str(Local input); string_t v8string_t(Local input); Local get_function(Local obj, Local key); -#endif // JNI_UTIL_H_ +#endif // JNI_UTIL_H_ diff --git a/app/rust/build/v8_jni/object_wrap.h b/app/rust/build/v8_jni/object_wrap.h old mode 100755 new mode 100644 index 281cabb..ef01efe --- a/app/rust/build/v8_jni/object_wrap.h +++ b/app/rust/build/v8_jni/object_wrap.h @@ -19,25 +19,26 @@ class ObjectWrap { persistent().Reset(); } - template static inline T *Unwrap(v8::Local handle) { + template + static inline T* Unwrap(v8::Local handle) { assert(!handle.IsEmpty()); assert(handle->InternalFieldCount() > 0); // Cast to ObjectWrap before casting to T. A direct cast from void // to T won't work right when T has more than one base class. - void *ptr = handle->GetAlignedPointerFromInternalField(0); - ObjectWrap *wrap = static_cast(ptr); - return static_cast(wrap); + void* ptr = handle->GetAlignedPointerFromInternalField(0); + ObjectWrap* wrap = static_cast(ptr); + return static_cast(wrap); } inline v8::Local handle() { return handle(v8::Isolate::GetCurrent()); } - inline v8::Local handle(v8::Isolate *isolate) { + inline v8::Local handle(v8::Isolate* isolate) { return v8::Local::New(isolate, persistent()); } - inline v8::Persistent &persistent() { return handle_; } + inline v8::Persistent& persistent() { return handle_; } protected: inline void Wrap(v8::Local handle) { @@ -79,11 +80,11 @@ class ObjectWrap { MakeWeak(); } - int refs_; // ro + int refs_; // ro private: - static void WeakCallback(const v8::WeakCallbackInfo &data) { - ObjectWrap *wrap = data.GetParameter(); + static void WeakCallback(const v8::WeakCallbackInfo& data) { + ObjectWrap* wrap = data.GetParameter(); assert(wrap->refs_ == 0); wrap->handle_.Reset(); delete wrap; @@ -92,6 +93,6 @@ class ObjectWrap { v8::Persistent handle_; }; -} // namespace rust +} // namespace rust -#endif // OBJECT_WRAP_H_ \ No newline at end of file +#endif // OBJECT_WRAP_H_ \ No newline at end of file diff --git a/app/rust/build/v8_jni/wrapper.cpp b/app/rust/build/v8_jni/wrapper.cpp index c8b27e7..f5cb522 100644 --- a/app/rust/build/v8_jni/wrapper.cpp +++ b/app/rust/build/v8_jni/wrapper.cpp @@ -6,7 +6,7 @@ Persistent JavaWrapper::registerUITask_; Persistent JavaWrapper::resolverUITask_; Persistent JavaWrapper::resolverContext_; -void JavaWrapper::Init(Isolate *isolate_, Local exports) { +void JavaWrapper::Init(Isolate* isolate_, Local exports) { Local tpl = FunctionTemplate::New(isolate_, New); tpl->SetClassName(String::NewFromUtf8(isolate_, "Java")); @@ -29,19 +29,19 @@ void JavaWrapper::SetContext(Local context_) { resolverContext_.Reset(Isolate::GetCurrent(), context_); } -void JavaWrapper::New(const FunctionCallbackInfo &info) { +void JavaWrapper::New(const FunctionCallbackInfo& info) { assert(info[0]->IsString()); - Isolate *isolate = Isolate::GetCurrent(); + Isolate* isolate = Isolate::GetCurrent(); if (info.IsConstructCall()) { std::string package = v8str(info[0]->ToString(isolate)); - auto *wrapper = new JavaWrapper(package); + auto* wrapper = new JavaWrapper(package); if (package == "context") { wrapper->ptr_ = get_current_activity(); } else { uint32_t argc = 0; - value_t *args = nullptr; + value_t* args = nullptr; string_t packageName = _new_string_t(package); wrapper->ptr_ = new_instance(packageName, args, argc); delete args; @@ -52,44 +52,44 @@ void JavaWrapper::New(const FunctionCallbackInfo &info) { } } -void JavaWrapper::IsMethod(const FunctionCallbackInfo &args) { +void JavaWrapper::IsMethod(const FunctionCallbackInfo& args) { assert(args[0]->IsString()); - Isolate *isolate_ = args.GetIsolate(); - auto *wrapper = rust::ObjectWrap::Unwrap(args.This()); + Isolate* isolate_ = args.GetIsolate(); + auto* wrapper = rust::ObjectWrap::Unwrap(args.This()); args.GetReturnValue().Set( Boolean::New(isolate_, is_method(wrapper->ptr_, v8string_t(args[0])))); } -void JavaWrapper::IsField(const FunctionCallbackInfo &args) { +void JavaWrapper::IsField(const FunctionCallbackInfo& args) { assert(args[0]->IsString()); - Isolate *isolate_ = args.GetIsolate(); - auto *wrapper = rust::ObjectWrap::Unwrap(args.This()); + Isolate* isolate_ = args.GetIsolate(); + auto* wrapper = rust::ObjectWrap::Unwrap(args.This()); args.GetReturnValue().Set( Boolean::New(isolate_, is_field(wrapper->ptr_, v8string_t(args[0])))); } -void JavaWrapper::Call(const FunctionCallbackInfo &info) { - Isolate *isolate_ = info.GetIsolate(); +void JavaWrapper::Call(const FunctionCallbackInfo& info) { + Isolate* isolate_ = info.GetIsolate(); info.GetReturnValue().Set(Undefined(isolate_)); } -void JavaWrapper::InvokeJavaFunction(const FunctionCallbackInfo &info) { +void JavaWrapper::InvokeJavaFunction(const FunctionCallbackInfo& info) { assert(info[0]->IsObject()); assert(info[1]->IsString()); assert(info[2]->IsArray()); - Isolate *isolate_ = info.GetIsolate(); - auto *wrapper = - rust::ObjectWrap::Unwrap(info[0]->ToObject(isolate_)); + Isolate* isolate_ = info.GetIsolate(); + auto* wrapper = + rust::ObjectWrap::Unwrap(info[0]->ToObject(isolate_)); std::string method(v8str(info[1]->ToString(isolate_))); Local array = Local::Cast(info[2]); Local context = isolate_->GetCurrentContext(); uint32_t argc = array->Length(); - auto *args = new value_t[argc]; + auto* args = new value_t[argc]; for (unsigned int i = 0; i < argc; i++) { if (array->Has(context, i).ToChecked()) { Local value = array->Get(i); @@ -107,16 +107,16 @@ void JavaWrapper::InvokeJavaFunction(const FunctionCallbackInfo &info) { Local javaContext = info[0]->ToObject(isolate_); Local contextName = - javaContext->Get(context, String::NewFromUtf8(isolate_, "name")) - .ToLocalChecked() - ->ToString(isolate_); + javaContext->Get(context, String::NewFromUtf8(isolate_, "name")) + .ToLocalChecked() + ->ToString(isolate_); jlong name = _rust_new_string(method.c_str()); instance_call_args(wrapper->ptr_, name, args, argc, info); } -void JavaWrapper::CallbackRegister(Isolate *isolate_, Local context) { +void JavaWrapper::CallbackRegister(Isolate* isolate_, Local context) { Local global = context->Global(); resolverContext_.Reset(isolate_, context); @@ -132,6 +132,6 @@ void JavaWrapper::CallbackRegister(Isolate *isolate_, Local context) { JavaWrapper::~JavaWrapper() { adb_debug("Destroyed"); } -void java_register_callback(Isolate *isolate_, Local context) { +void java_register_callback(Isolate* isolate_, Local context) { JavaWrapper::CallbackRegister(isolate_, context); } \ No newline at end of file diff --git a/app/rust/build/v8_jni/wrapper.h b/app/rust/build/v8_jni/wrapper.h index 9c2859f..16658d8 100644 --- a/app/rust/build/v8_jni/wrapper.h +++ b/app/rust/build/v8_jni/wrapper.h @@ -1,15 +1,13 @@ #ifndef JNI_WRAPPER_H_ #define JNI_WRAPPER_H_ -#include -#include -#include - #include "../util/util.h" - #include "object_wrap.h" #include "v8.h" #include +#include +#include +#include using namespace v8; @@ -19,34 +17,33 @@ bool is_method(jlong, string_t); bool is_field(jlong, string_t); } -void java_register_callback(Isolate *isolate_, Local context); +void java_register_callback(Isolate* isolate_, Local context); class JavaWrapper : public rust::ObjectWrap { - public: - static void Init(Isolate *isolate_, Local exports); + static void Init(Isolate* isolate_, Local exports); static void SetContext(Local context_); - static void CallbackRegister(Isolate *isolate_, Local context); + static void CallbackRegister(Isolate* isolate_, Local context); static Persistent resolverUITask_; static Persistent resolverContext_; private: - explicit JavaWrapper(std::string package) : package_(package){}; + explicit JavaWrapper(std::string package) : package_(package) {}; ~JavaWrapper(); - static void New(const FunctionCallbackInfo &args); + static void New(const FunctionCallbackInfo& args); - static void IsField(const FunctionCallbackInfo &args); + static void IsField(const FunctionCallbackInfo& args); - static void IsMethod(const FunctionCallbackInfo &args); + static void IsMethod(const FunctionCallbackInfo& args); - static void Call(const FunctionCallbackInfo &args); + static void Call(const FunctionCallbackInfo& args); - static void InvokeJavaFunction(const FunctionCallbackInfo &args); + static void InvokeJavaFunction(const FunctionCallbackInfo& args); jlong ptr_; std::string package_; @@ -55,4 +52,4 @@ class JavaWrapper : public rust::ObjectWrap { static Persistent registerUITask_; }; -#endif // JNI_WRAPPER_H_ \ No newline at end of file +#endif // JNI_WRAPPER_H_ \ No newline at end of file diff --git a/app/rust/src/runtime/event_loop.rs b/app/rust/src/runtime/event_loop.rs index 8b036f8..aefc209 100755 --- a/app/rust/src/runtime/event_loop.rs +++ b/app/rust/src/runtime/event_loop.rs @@ -130,13 +130,13 @@ pub extern "C" fn init_event_loop() { console.log(`timeout 3s: ${Date.now() - start}`); }, 3000); - /*Promise.all(users.map(fetchUserInfo)) + Promise.all(users.map(fetchUserInfo)) .then(data => { const names = data.map(user => user.name).join(', '); console.log(`Name: ${names}`); console.log(`api call: ${Date.now() - start}`); }) - .catch(e => console.log(e.message));*/ + .catch(e => console.log(e.message)); "#, ); diff --git a/app/src/main/cpp/lib/node-ext.cpp b/app/src/main/cpp/lib/node-ext.cpp index 419a70b..b7af489 100644 --- a/app/src/main/cpp/lib/node-ext.cpp +++ b/app/src/main/cpp/lib/node-ext.cpp @@ -18,18 +18,26 @@ using v8::Value; using v8::JSON; using v8::MaybeLocal; -const char *ToCString(Local str) { - Isolate *isolate = Isolate::GetCurrent(); +#define NATIVE_METHOD(className, functionName, signature) \ + { \ +#functionName, signature, \ + reiterpret_cast < void*>(className##_##functionName) \ + } + +#define CLASS_NAME "benchmarks/MicroNative/java/NativeMethods"; + +const char* ToCString(Local str) { + Isolate* isolate = Isolate::GetCurrent(); String::Utf8Value value(isolate, str); return *value ? *value : ""; } -void AndroidToast(const FunctionCallbackInfo &args) { - Isolate *isolate = args.GetIsolate(); +void AndroidToast(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); Local str = args[0]->ToString(isolate); - const char *msg = ToCString(str); + const char* msg = ToCString(str); - JNIEnv *env_ = static_cast(args.Data().As()->Value()); + JNIEnv* env_ = static_cast(args.Data().As()->Value()); jmethodID methodId = env_->GetMethodID(g_ctx.mainActivityClz, "subscribe", "(Ljava/lang/String;)V"); @@ -39,35 +47,34 @@ void AndroidToast(const FunctionCallbackInfo &args) { args.GetReturnValue().Set(str); } -void AndroidLog(const FunctionCallbackInfo &args) { - Isolate *isolate = args.GetIsolate(); +void AndroidLog(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); Local context = isolate->GetCurrentContext(); EscapableHandleScope handle_scope(isolate); Local result = handle_scope.Escape( - JSON::Stringify(context, args[0]->ToObject(isolate)).ToLocalChecked()); - const char *jsonString = ToCString(result); + JSON::Stringify(context, args[0]->ToObject(isolate)).ToLocalChecked()); + const char* jsonString = ToCString(result); LOGD("%s", jsonString); } -void AndroidError(const FunctionCallbackInfo &args) { - Isolate *isolate = args.GetIsolate(); +void AndroidError(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); Local context = isolate->GetCurrentContext(); EscapableHandleScope handle_scope(isolate); Local result = handle_scope.Escape( - JSON::Stringify(context, args[0]->ToObject(isolate)).ToLocalChecked()); - const char *jsonString = ToCString(result); + JSON::Stringify(context, args[0]->ToObject(isolate)).ToLocalChecked()); + const char* jsonString = ToCString(result); LOGE("%s", jsonString); } -static jdouble msqrt(JNIEnv *env, jclass clazz, jdouble value) { +static jdouble msqrt(JNIEnv* env, jclass clazz, jdouble value) { return std::sqrt(value); } extern "C" void JNICALL Java_com_node_sample_MainActivity_initVM( - JNIEnv *env, jobject instance, jobject callback) { - + JNIEnv* env, jobject instance, jobject callback) { // init objects jclass clz = env->GetObjectClass(callback); g_ctx.mainActivityClz = (jclass) env->NewGlobalRef(clz); @@ -76,8 +83,7 @@ extern "C" void JNICALL Java_com_node_sample_MainActivity_initVM( } extern "C" void JNICALL -Java_com_node_sample_MainActivity_releaseVM(JNIEnv *env, jobject instance) { - +Java_com_node_sample_MainActivity_releaseVM(JNIEnv* env, jobject instance) { // release allocated objects env->DeleteGlobalRef(g_ctx.mainActivityObj); env->DeleteGlobalRef(g_ctx.mainActivityClz); @@ -88,7 +94,7 @@ Java_com_node_sample_MainActivity_releaseVM(JNIEnv *env, jobject instance) { g_ctx.mainActivity = nullptr; } -JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) { +JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/) { memset(&g_ctx, 0, sizeof(NodeContext)); register_vm(vm); g_ctx.javaVM = vm; @@ -101,9 +107,9 @@ JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) { return JNI_VERSION_1_6; } -JNIEXPORT void JNI_OnUnload(JavaVM *vm, void *) { - JNIEnv *env; - if (vm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_EDETACHED) { +JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* /*reserved*/) { + JNIEnv* env; + if (vm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_EDETACHED) { vm->DetachCurrentThread(); } } diff --git a/app/src/main/cpp/lib/node-ext.h b/app/src/main/cpp/lib/node-ext.h index d08fe2a..0ceed53 100644 --- a/app/src/main/cpp/lib/node-ext.h +++ b/app/src/main/cpp/lib/node-ext.h @@ -1,6 +1,7 @@ #ifndef _node_extension_h_ #define _node_extension_h_ +#include "../utils/utils.h" #include "v8.h" #include #include @@ -13,23 +14,21 @@ #include #include -#include "../utils/utils.h" - using namespace util; -extern "C" void register_vm(JavaVM *vm); -extern "C" void register_native(JNIEnv *env); +extern "C" void register_vm(JavaVM* vm); +extern "C" void register_native(JNIEnv* env); NodeContext g_ctx; -static ALooper *mainThreadLooper; +static ALooper* mainThreadLooper; static int messagePipe[2]; -void AndroidToast(const FunctionCallbackInfo &args); +void AndroidToast(const FunctionCallbackInfo& args); -void AndroidLog(const FunctionCallbackInfo &args); +void AndroidLog(const FunctionCallbackInfo& args); -void AndroidError(const FunctionCallbackInfo &args); +void AndroidError(const FunctionCallbackInfo& args); -void OnLoad(const FunctionCallbackInfo &args); +void OnLoad(const FunctionCallbackInfo& args); #endif diff --git a/app/src/main/cpp/utils/utils.cpp b/app/src/main/cpp/utils/utils.cpp index 4a65511..09ad8fd 100644 --- a/app/src/main/cpp/utils/utils.cpp +++ b/app/src/main/cpp/utils/utils.cpp @@ -4,20 +4,20 @@ namespace util { -string Util::JavaToString(JNIEnv *env, jstring str) { +string Util::JavaToString(JNIEnv* env, jstring str) { jclass objClazz = env->GetObjectClass(str); jmethodID methodId = env->GetMethodID(objClazz, "getBytes", "(Ljava/lang/String;)[B"); jstring charsetName = env->NewStringUTF("UTF-8"); auto byteArray = - (jbyteArray)env->CallObjectMethod(str, methodId, charsetName); + (jbyteArray) env->CallObjectMethod(str, methodId, charsetName); env->DeleteLocalRef(charsetName); - jbyte *pBytes = env->GetByteArrayElements(byteArray, nullptr); + jbyte* pBytes = env->GetByteArrayElements(byteArray, nullptr); const jsize length = env->GetArrayLength(byteArray); - std::string results((const char *)pBytes, (unsigned long)length); + std::string results((const char*) pBytes, (unsigned long) length); env->ReleaseByteArrayElements(byteArray, pBytes, JNI_ABORT); env->DeleteLocalRef(byteArray); @@ -25,14 +25,14 @@ string Util::JavaToString(JNIEnv *env, jstring str) { return results; } -Local Util::ConvertToV8String(const string &s) { +Local Util::ConvertToV8String(const string& s) { auto isolate = Isolate::GetCurrent(); return String::NewFromUtf8(isolate, s.c_str()); } -void Util::InitEnvironment(Isolate *isolate, JNIEnv **env) { +void Util::InitEnvironment(Isolate* isolate, JNIEnv** env) { jint res = - g_ctx.javaVM->GetEnv(reinterpret_cast(&(*env)), JNI_VERSION_1_6); + g_ctx.javaVM->GetEnv(reinterpret_cast(&(*env)), JNI_VERSION_1_6); if (res != JNI_OK) { res = g_ctx.javaVM->AttachCurrentThread(&(*env), nullptr); if (JNI_OK != res) { @@ -42,9 +42,9 @@ void Util::InitEnvironment(Isolate *isolate, JNIEnv **env) { } } -void Util::AttachCurrentThread(JNIEnv **env) { +void Util::AttachCurrentThread(JNIEnv** env) { int res = - g_ctx.javaVM->GetEnv(reinterpret_cast(&(*env)), JNI_VERSION_1_6); + g_ctx.javaVM->GetEnv(reinterpret_cast(&(*env)), JNI_VERSION_1_6); if (res != JNI_OK) { res = g_ctx.javaVM->AttachCurrentThread(&(*env), nullptr); if (JNI_OK != res) { @@ -53,4 +53,4 @@ void Util::AttachCurrentThread(JNIEnv **env) { } } -} // namespace util +} // namespace util diff --git a/app/src/main/cpp/utils/utils.h b/app/src/main/cpp/utils/utils.h index 47b7468..9dbaee3 100644 --- a/app/src/main/cpp/utils/utils.h +++ b/app/src/main/cpp/utils/utils.h @@ -10,33 +10,33 @@ using namespace std; using namespace v8; typedef struct NodeContext { - JavaVM *javaVM; - JNIEnv *env; + JavaVM* javaVM; + JNIEnv* env; jclass mainActivityClz; jobject mainActivityObj; jobject mainActivity; - Isolate *isolate_; + Isolate* isolate_; } NodeContext; namespace util { class Util { public: - static string JavaToString(JNIEnv *env, jstring str); - static Local ConvertToV8String(const string &s); - static void InitEnvironment(Isolate *isolate, JNIEnv **env); - static void AttachCurrentThread(JNIEnv **env); + static string JavaToString(JNIEnv* env, jstring str); + static Local ConvertToV8String(const string& s); + static void InitEnvironment(Isolate* isolate, JNIEnv** env); + static void AttachCurrentThread(JNIEnv** env); }; -} // namespace util +} // namespace util extern NodeContext g_ctx; -static const char *kTAG = "V8 Runtime"; +static const char* kTAG = "V8 Runtime"; -#define LOGD(...) \ +#define LOGD(...) \ ((void)__android_log_print(ANDROID_LOG_DEBUG, kTAG, __VA_ARGS__)) -#define LOGE(...) \ +#define LOGE(...) \ ((void)__android_log_print(ANDROID_LOG_ERROR, kTAG, __VA_ARGS__)) #endif diff --git a/app/src/main/java/com/node/sample/MainActivity.java b/app/src/main/java/com/node/sample/MainActivity.java index bbdacfe..e0fc485 100755 --- a/app/src/main/java/com/node/sample/MainActivity.java +++ b/app/src/main/java/com/node/sample/MainActivity.java @@ -15,8 +15,6 @@ import com.node.util.ResourceUtil; import com.node.util.Util; -import inspector.Inspector; - @Keep public class MainActivity extends AppCompatActivity { private TextView txtMessage = null; @@ -70,9 +68,7 @@ public void subscribe(String arg) { } }); - new Thread(Inspector::runServer).start(); new Thread(this::demoMain).start(); - } @Keep diff --git a/inspector/build.gradle b/inspector/build.gradle deleted file mode 100644 index 5b0c7e1..0000000 --- a/inspector/build.gradle +++ /dev/null @@ -1,2 +0,0 @@ -configurations.maybeCreate("default") -artifacts.add("default", file('inspector.aar')) \ No newline at end of file diff --git a/inspector/inspector.aar b/inspector/inspector.aar deleted file mode 100644 index 7bfaf72..0000000 Binary files a/inspector/inspector.aar and /dev/null differ diff --git a/pre-build.sh b/pre-build.sh index 1721ffc..123d9b7 100755 --- a/pre-build.sh +++ b/pre-build.sh @@ -2,8 +2,6 @@ source ~/.bash_profile dir=`pwd` -NDK_HOME=$NDK20 - export AARCH64LINUX_ANDROID_OPENSSL_INCLUDE_DIR="`pwd`/app/rust/libnode/include" export AARCH64_LINUX_ANDROID_OPENSSL_LIB_DIR="`pwd`/app/rust/libnode/bin/arm64-v8a" @@ -39,17 +37,17 @@ fi cd `pwd`/app/rust # rm -f ./target/arm64-v8a/librust.a -# rm -f ./target/armeabi-v7a/librust.a +rm -f ./target/armeabi-v7a/librust.a rm -f ./target/x86/librust.a # RUST_BACKTRACE=1 cargo +nightly build --target aarch64-linux-android --release -# RUST_BACKTRACE=1 cargo build --target armv7-linux-androideabi --release -RUST_BACKTRACE=1 cargo build --target i686-linux-android --release +# RUST_BACKTRACE=1 cargo build --target armv7-linux-androideabi +RUST_BACKTRACE=1 cargo build --target i686-linux-android # mkdir -p ./target/arm64-v8a -# mkdir -p ./target/armeabi-v7a +mkdir -p ./target/armeabi-v7a mkdir -p ./target/x86 # cp ./target/aarch64-linux-android/release/librust.a ./target/arm64-v8a/librust.a -# cp ./target/armv7-linux-androideabi/release/librust.a ./target/armeabi-v7a/librust.a -cp ./target/i686-linux-android/release/librust.a ./target/x86/librust.a +# cp ./target/armv7-linux-androideabi/debug/librust.a ./target/armeabi-v7a/librust.a +cp ./target/i686-linux-android/debug/librust.a ./target/x86/librust.a diff --git a/settings.gradle b/settings.gradle index fbc24b3..e7b4def 100755 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':app', ':inspector' +include ':app'