Skip to content

Commit

Permalink
chore: use node way of binding modules
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Jan 20, 2024
1 parent a2583a8 commit 48fcd5e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 28 deletions.
4 changes: 3 additions & 1 deletion NativeScript/runtime/ModuleBinding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 0 additions & 4 deletions NativeScript/runtime/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ class Runtime {
void DefineTimeMethod(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineDrainMicrotaskMethod(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);

void DefineURL(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);

void DefineURLSearchParams(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);

static void PerformanceNowCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
v8::Isolate* isolate_;
std::unique_ptr<ModuleInternal> moduleInternal_;
Expand Down
22 changes: 1 addition & 21 deletions NativeScript/runtime/Runtime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -252,11 +252,6 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
}








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

Expand Down Expand Up @@ -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<ObjectTemplate> globalTemplate) {
auto URLTemplate = URLImpl::GetCtor(isolate);

Local<v8::String> urlPropertyName = ToV8String(isolate, "URL");
globalTemplate->Set(urlPropertyName, URLTemplate);
}

void Runtime::DefineURLSearchParams(Isolate* isolate, Local<ObjectTemplate> globalTemplate) {
auto URLSearchParamsTemplate = URLSearchParamsImpl::GetCtor(isolate);

Local<v8::String> urlSearchParamsPropertyName = ToV8String(isolate, "URLSearchParams");
globalTemplate->Set(urlSearchParamsPropertyName, URLSearchParamsTemplate);
}

std::shared_ptr<Platform> Runtime::platform_;
std::vector<Isolate*> Runtime::isolates_;
bool Runtime::v8Initialized_ = false;
Expand Down
15 changes: 13 additions & 2 deletions NativeScript/runtime/URLImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::ObjectTemplate> globalTemplate) {
auto URLTemplate = URLImpl::GetCtor(isolate);

v8::Local<v8::String> urlPropertyName = ToV8String(isolate, "URL");
globalTemplate->Set(urlPropertyName, URLTemplate);
}

URLImpl *URLImpl::GetPointer(v8::Local<v8::Object> object) {
auto ptr = object->GetAlignedPointerFromInternalField(0);
if (ptr == nullptr) {
Expand Down Expand Up @@ -474,9 +482,9 @@ void URLImpl::ToString(const v8::FunctionCallbackInfo<v8::Value> &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);
}
Expand All @@ -499,3 +507,6 @@ void URLImpl::CanParse(const v8::FunctionCallbackInfo<v8::Value> &args) {

args.GetReturnValue().Set(value);
}


NODE_BINDING_PER_ISOLATE_INIT_OBJ(url, tns::URLImpl::Init)
4 changes: 4 additions & 0 deletions NativeScript/runtime/URLImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ namespace tns {
URLImpl(url_aggregator url);

url_aggregator *GetURL();

static void Init(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);


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

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


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

Expand Down
11 changes: 11 additions & 0 deletions NativeScript/runtime/URLSearchParamsImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::ObjectTemplate> globalTemplate) {
auto URLSearchParamsTemplate = URLSearchParamsImpl::GetCtor(isolate);

v8::Local<v8::String> urlSearchParamsPropertyName = ToV8String(isolate, "URLSearchParams");
globalTemplate->Set(urlSearchParamsPropertyName, URLSearchParamsTemplate);
}

URLSearchParamsImpl *URLSearchParamsImpl::GetPointer(v8::Local<v8::Object> object) {
auto ptr = object->GetAlignedPointerFromInternalField(0);
if (ptr == nullptr) {
Expand Down Expand Up @@ -347,3 +355,6 @@ namespace tns {
}

} // tns


NODE_BINDING_PER_ISOLATE_INIT_OBJ(urlsearchparams, tns::URLSearchParamsImpl::Init)
2 changes: 2 additions & 0 deletions NativeScript/runtime/URLSearchParamsImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace tns {

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

static void Init(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);

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

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

0 comments on commit 48fcd5e

Please sign in to comment.