From c3aa1e991367cc759243fb8dff2e7a0882b5b7f6 Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Fri, 19 Jan 2024 23:31:11 -0400 Subject: [PATCH] chore: use node way of binding modules --- NativeScript/runtime/ModuleBinding.hpp | 4 +++- NativeScript/runtime/Runtime.h | 4 ---- NativeScript/runtime/Runtime.mm | 22 +------------------- NativeScript/runtime/URLImpl.cpp | 15 +++++++++++-- NativeScript/runtime/URLImpl.h | 4 ++++ NativeScript/runtime/URLSearchParamsImpl.cpp | 11 ++++++++++ NativeScript/runtime/URLSearchParamsImpl.h | 2 ++ 7 files changed, 34 insertions(+), 28 deletions(-) diff --git a/NativeScript/runtime/ModuleBinding.hpp b/NativeScript/runtime/ModuleBinding.hpp index 5b3790ca..ade30a86 100644 --- a/NativeScript/runtime/ModuleBinding.hpp +++ b/NativeScript/runtime/ModuleBinding.hpp @@ -58,7 +58,9 @@ namespace tns { #define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) \ V(worker) \ -V(timers) +V(timers) \ +V(url) \ +V(urlsearchparams) enum { NM_F_BUILTIN = 1 << 0, // Unused. diff --git a/NativeScript/runtime/Runtime.h b/NativeScript/runtime/Runtime.h index 973aeebd..0b86b32f 100644 --- a/NativeScript/runtime/Runtime.h +++ b/NativeScript/runtime/Runtime.h @@ -70,10 +70,6 @@ class Runtime { void DefineTimeMethod(v8::Isolate* isolate, v8::Local globalTemplate); void DefineDrainMicrotaskMethod(v8::Isolate* isolate, v8::Local globalTemplate); - void DefineURL(v8::Isolate* isolate, v8::Local globalTemplate); - - void DefineURLSearchParams(v8::Isolate* isolate, v8::Local globalTemplate); - static void PerformanceNowCallback(const v8::FunctionCallbackInfo& args); v8::Isolate* isolate_; std::unique_ptr moduleInternal_; diff --git a/NativeScript/runtime/Runtime.mm b/NativeScript/runtime/Runtime.mm index 0886cfb9..21c3d87c 100644 --- a/NativeScript/runtime/Runtime.mm +++ b/NativeScript/runtime/Runtime.mm @@ -185,7 +185,7 @@ void DisposeIsolateWhenPossible(Isolate* isolate) { URL.createObjectURL = function (object, options = null) { try { if (object instanceof Blob || object instanceof File) { - const id = java.util.UUID.randomUUID().toString(); + const id = NSUUID.UUID().UUIDString; const ret = `blob:nativescript/${id}`; BLOB_STORE.set(ret, { blob: object, @@ -252,11 +252,6 @@ void DisposeIsolateWhenPossible(Isolate* isolate) { } - - - - - this->moduleInternal_ = std::make_unique(context); @@ -396,21 +391,6 @@ void DisposeIsolateWhenPossible(Isolate* isolate) { return std::find(Runtime::isolates_.begin(), Runtime::isolates_.end(), isolate) != Runtime::isolates_.end(); } - -void Runtime::DefineURL(Isolate* isolate, Local globalTemplate) { - auto URLTemplate = URLImpl::GetCtor(isolate); - - Local urlPropertyName = ToV8String(isolate, "URL"); - globalTemplate->Set(urlPropertyName, URLTemplate); -} - -void Runtime::DefineURLSearchParams(Isolate* isolate, Local globalTemplate) { - auto URLSearchParamsTemplate = URLSearchParamsImpl::GetCtor(isolate); - - Local urlSearchParamsPropertyName = ToV8String(isolate, "URLSearchParams"); - globalTemplate->Set(urlSearchParamsPropertyName, URLSearchParamsTemplate); -} - std::shared_ptr Runtime::platform_; std::vector Runtime::isolates_; bool Runtime::v8Initialized_ = false; diff --git a/NativeScript/runtime/URLImpl.cpp b/NativeScript/runtime/URLImpl.cpp index 87effb64..54a10530 100644 --- a/NativeScript/runtime/URLImpl.cpp +++ b/NativeScript/runtime/URLImpl.cpp @@ -4,12 +4,20 @@ #include "URLImpl.h" #include "Helpers.h" +#include "ModuleBinding.hpp" using namespace tns; using namespace ada; URLImpl::URLImpl(url_aggregator url) : url_(url) {} +void URLImpl::Init(v8::Isolate* isolate, v8::Local globalTemplate) { + auto URLTemplate = URLImpl::GetCtor(isolate); + + v8::Local urlPropertyName = ToV8String(isolate, "URL"); + globalTemplate->Set(urlPropertyName, URLTemplate); +} + URLImpl *URLImpl::GetPointer(v8::Local object) { auto ptr = object->GetAlignedPointerFromInternalField(0); if (ptr == nullptr) { @@ -474,9 +482,9 @@ void URLImpl::ToString(const v8::FunctionCallbackInfo &args) { auto isolate = args.GetIsolate(); - auto value = ptr->GetURL()->to_string(); + auto value = ptr->GetURL()->get_href(); - auto ret = ToV8String(isolate, value.c_str()); + auto ret = ToV8String(isolate, value.data(), (int)value.length()); args.GetReturnValue().Set(ret); } @@ -499,3 +507,6 @@ void URLImpl::CanParse(const v8::FunctionCallbackInfo &args) { args.GetReturnValue().Set(value); } + + +NODE_BINDING_PER_ISOLATE_INIT_OBJ(url, tns::URLImpl::Init) diff --git a/NativeScript/runtime/URLImpl.h b/NativeScript/runtime/URLImpl.h index 079efa5a..4a77cb2a 100644 --- a/NativeScript/runtime/URLImpl.h +++ b/NativeScript/runtime/URLImpl.h @@ -15,10 +15,14 @@ namespace tns { URLImpl(url_aggregator url); url_aggregator *GetURL(); + + static void Init(v8::Isolate* isolate, v8::Local globalTemplate); + static URLImpl *GetPointer(v8::Local object); static v8::Local GetCtor(v8::Isolate *isolate); + static void Ctor(const v8::FunctionCallbackInfo &args); diff --git a/NativeScript/runtime/URLSearchParamsImpl.cpp b/NativeScript/runtime/URLSearchParamsImpl.cpp index d41d0ecd..d0ab2a63 100644 --- a/NativeScript/runtime/URLSearchParamsImpl.cpp +++ b/NativeScript/runtime/URLSearchParamsImpl.cpp @@ -4,12 +4,20 @@ #include "URLSearchParamsImpl.h" #include "Helpers.h" +#include "ModuleBinding.hpp" using namespace ada; namespace tns { URLSearchParamsImpl::URLSearchParamsImpl(ada::url_search_params params) : params_(params) {} + void URLSearchParamsImpl::Init(v8::Isolate* isolate, v8::Local globalTemplate) { + auto URLSearchParamsTemplate = URLSearchParamsImpl::GetCtor(isolate); + + v8::Local urlSearchParamsPropertyName = ToV8String(isolate, "URLSearchParams"); + globalTemplate->Set(urlSearchParamsPropertyName, URLSearchParamsTemplate); + } + URLSearchParamsImpl *URLSearchParamsImpl::GetPointer(v8::Local object) { auto ptr = object->GetAlignedPointerFromInternalField(0); if (ptr == nullptr) { @@ -347,3 +355,6 @@ namespace tns { } } // tns + + +NODE_BINDING_PER_ISOLATE_INIT_OBJ(urlsearchparams, tns::URLSearchParamsImpl::Init) \ No newline at end of file diff --git a/NativeScript/runtime/URLSearchParamsImpl.h b/NativeScript/runtime/URLSearchParamsImpl.h index 3cfe698a..8e3dbd13 100644 --- a/NativeScript/runtime/URLSearchParamsImpl.h +++ b/NativeScript/runtime/URLSearchParamsImpl.h @@ -19,6 +19,8 @@ namespace tns { static v8::Local GetCtor(v8::Isolate *isolate); + static void Init(v8::Isolate* isolate, v8::Local globalTemplate); + static void Ctor(const v8::FunctionCallbackInfo &args); static void Append(const v8::FunctionCallbackInfo &args);