Skip to content

Commit 1ce3509

Browse files
triniwizNathanWalker
authored andcommitted
chore: use node way of binding modules
1 parent 69a659a commit 1ce3509

File tree

7 files changed

+34
-28
lines changed

7 files changed

+34
-28
lines changed

NativeScript/runtime/ModuleBinding.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ namespace tns {
5858

5959
#define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) \
6060
V(worker) \
61-
V(timers)
61+
V(timers) \
62+
V(url) \
63+
V(urlsearchparams)
6264

6365
enum {
6466
NM_F_BUILTIN = 1 << 0, // Unused.

NativeScript/runtime/Runtime.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ class Runtime {
7070
void DefineTimeMethod(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
7171
void DefineDrainMicrotaskMethod(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
7272

73-
void DefineURL(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
74-
75-
void DefineURLSearchParams(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
76-
7773
static void PerformanceNowCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
7874
v8::Isolate* isolate_;
7975
std::unique_ptr<ModuleInternal> moduleInternal_;

NativeScript/runtime/Runtime.mm

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
185185
URL.createObjectURL = function (object, options = null) {
186186
try {
187187
if (object instanceof Blob || object instanceof File) {
188-
const id = java.util.UUID.randomUUID().toString();
188+
const id = NSUUID.UUID().UUIDString;
189189
const ret = `blob:nativescript/${id}`;
190190
BLOB_STORE.set(ret, {
191191
blob: object,
@@ -252,11 +252,6 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
252252
}
253253

254254

255-
256-
257-
258-
259-
260255

261256
this->moduleInternal_ = std::make_unique<ModuleInternal>(context);
262257

@@ -396,21 +391,6 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
396391
return std::find(Runtime::isolates_.begin(), Runtime::isolates_.end(), isolate) != Runtime::isolates_.end();
397392
}
398393

399-
400-
void Runtime::DefineURL(Isolate* isolate, Local<ObjectTemplate> globalTemplate) {
401-
auto URLTemplate = URLImpl::GetCtor(isolate);
402-
403-
Local<v8::String> urlPropertyName = ToV8String(isolate, "URL");
404-
globalTemplate->Set(urlPropertyName, URLTemplate);
405-
}
406-
407-
void Runtime::DefineURLSearchParams(Isolate* isolate, Local<ObjectTemplate> globalTemplate) {
408-
auto URLSearchParamsTemplate = URLSearchParamsImpl::GetCtor(isolate);
409-
410-
Local<v8::String> urlSearchParamsPropertyName = ToV8String(isolate, "URLSearchParams");
411-
globalTemplate->Set(urlSearchParamsPropertyName, URLSearchParamsTemplate);
412-
}
413-
414394
std::shared_ptr<Platform> Runtime::platform_;
415395
std::vector<Isolate*> Runtime::isolates_;
416396
bool Runtime::v8Initialized_ = false;

NativeScript/runtime/URLImpl.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44

55
#include "URLImpl.h"
66
#include "Helpers.h"
7+
#include "ModuleBinding.hpp"
78

89
using namespace tns;
910
using namespace ada;
1011

1112
URLImpl::URLImpl(url_aggregator url) : url_(url) {}
1213

14+
void URLImpl::Init(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate) {
15+
auto URLTemplate = URLImpl::GetCtor(isolate);
16+
17+
v8::Local<v8::String> urlPropertyName = ToV8String(isolate, "URL");
18+
globalTemplate->Set(urlPropertyName, URLTemplate);
19+
}
20+
1321
URLImpl *URLImpl::GetPointer(v8::Local<v8::Object> object) {
1422
auto ptr = object->GetAlignedPointerFromInternalField(0);
1523
if (ptr == nullptr) {
@@ -474,9 +482,9 @@ void URLImpl::ToString(const v8::FunctionCallbackInfo<v8::Value> &args) {
474482
auto isolate = args.GetIsolate();
475483

476484

477-
auto value = ptr->GetURL()->to_string();
485+
auto value = ptr->GetURL()->get_href();
478486

479-
auto ret = ToV8String(isolate, value.c_str());
487+
auto ret = ToV8String(isolate, value.data(), (int)value.length());
480488

481489
args.GetReturnValue().Set(ret);
482490
}
@@ -499,3 +507,6 @@ void URLImpl::CanParse(const v8::FunctionCallbackInfo<v8::Value> &args) {
499507

500508
args.GetReturnValue().Set(value);
501509
}
510+
511+
512+
NODE_BINDING_PER_ISOLATE_INIT_OBJ(url, tns::URLImpl::Init)

NativeScript/runtime/URLImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ namespace tns {
1515
URLImpl(url_aggregator url);
1616

1717
url_aggregator *GetURL();
18+
19+
static void Init(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
20+
1821

1922
static URLImpl *GetPointer(v8::Local<v8::Object> object);
2023

2124
static v8::Local<v8::FunctionTemplate> GetCtor(v8::Isolate *isolate);
25+
2226

2327
static void Ctor(const v8::FunctionCallbackInfo<v8::Value> &args);
2428

NativeScript/runtime/URLSearchParamsImpl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44

55
#include "URLSearchParamsImpl.h"
66
#include "Helpers.h"
7+
#include "ModuleBinding.hpp"
78

89
using namespace ada;
910

1011
namespace tns {
1112
URLSearchParamsImpl::URLSearchParamsImpl(ada::url_search_params params) : params_(params) {}
1213

14+
void URLSearchParamsImpl::Init(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate) {
15+
auto URLSearchParamsTemplate = URLSearchParamsImpl::GetCtor(isolate);
16+
17+
v8::Local<v8::String> urlSearchParamsPropertyName = ToV8String(isolate, "URLSearchParams");
18+
globalTemplate->Set(urlSearchParamsPropertyName, URLSearchParamsTemplate);
19+
}
20+
1321
URLSearchParamsImpl *URLSearchParamsImpl::GetPointer(v8::Local<v8::Object> object) {
1422
auto ptr = object->GetAlignedPointerFromInternalField(0);
1523
if (ptr == nullptr) {
@@ -347,3 +355,6 @@ namespace tns {
347355
}
348356

349357
} // tns
358+
359+
360+
NODE_BINDING_PER_ISOLATE_INIT_OBJ(urlsearchparams, tns::URLSearchParamsImpl::Init)

NativeScript/runtime/URLSearchParamsImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace tns {
1919

2020
static v8::Local<v8::FunctionTemplate> GetCtor(v8::Isolate *isolate);
2121

22+
static void Init(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
23+
2224
static void Ctor(const v8::FunctionCallbackInfo<v8::Value> &args);
2325

2426
static void Append(const v8::FunctionCallbackInfo<v8::Value> &args);

0 commit comments

Comments
 (0)