Skip to content

Commit

Permalink
feat: use node logic for globals and modules
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni committed Jul 23, 2023
1 parent 50f33e7 commit 88e9e1c
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NativeScript/runtime/Caches.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Caches {

Caches(v8::Isolate* isolate, const int& isolateId_ = -1);
~Caches();

bool isWorker = false;

static std::shared_ptr<ConcurrentMap<std::string, const Meta*>> Metadata;
static std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>> Workers;
Expand Down
101 changes: 101 additions & 0 deletions NativeScript/runtime/ModuleBinding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// ModuleBinding.cpp
// NativeScript
//
// Created by Eduardo Speroni on 5/11/23.
// Copyright © 2023 Progress. All rights reserved.
//

#include "ModuleBinding.hpp"

// TODO: add here
//#define NSC_BUILTIN_STANDARD_BINDINGS(V) \
//V(fs)

#define NSC_BUILTIN_STANDARD_BINDINGS(V)


#define NSC_BUILTIN_BINDINGS(V) \
NSC_BUILTIN_STANDARD_BINDINGS(V)

// This is used to load built-in bindings. Instead of using
// __attribute__((constructor)), we call the _register_<modname>
// function for each built-in bindings explicitly in
// binding::RegisterBuiltinBindings(). This is only forward declaration.
// The definitions are in each binding's implementation when calling
// the NODE_BINDING_CONTEXT_AWARE_INTERNAL.
#define V(modname) void _register_##modname();
NSC_BUILTIN_BINDINGS(V)
#undef V




#define V(modname) \
void _register_isolate_##modname(v8::Isolate* isolate, \
v8::Local<v8::FunctionTemplate> target);
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
#undef V



using v8::Context;
using v8::EscapableHandleScope;
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

namespace tns {
// Globals per process
static ns_module* modlist_internal;
static ns_module* modlist_linked;
static thread_local ns_module* thread_local_modpending;
bool node_is_initialized = false;

extern "C" void nativescript_module_register(void* m) {
struct ns_module* mp = reinterpret_cast<struct ns_module*>(m);

if (mp->nm_flags & NM_F_INTERNAL) {
mp->nm_link = modlist_internal;
modlist_internal = mp;
} else if (!node_is_initialized) {
// "Linked" modules are included as part of the node project.
// Like builtins they are registered *before* node::Init runs.
mp->nm_flags = NM_F_LINKED;
mp->nm_link = modlist_linked;
modlist_linked = mp;
} else {
thread_local_modpending = mp;
}
}

namespace binding {

void RegisterBuiltinBindings() {
#define V(modname) _register_##modname();
NSC_BUILTIN_BINDINGS(V)
#undef V
}

void CreateInternalBindingTemplates(v8::Isolate* isolate, Local<FunctionTemplate> templ) {
#define V(modname) \
do { \
/*templ->InstanceTemplate()->SetInternalFieldCount( \
BaseObject::kInternalFieldCount);*/ \
_register_isolate_##modname(isolate, templ); \
/*isolate_data->set_##modname##_binding(templ);*/ \
} while (0);
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
#undef V
}

};

};

101 changes: 101 additions & 0 deletions NativeScript/runtime/ModuleBinding.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// ModuleBinding.hpp
// NativeScript
//
// Created by Eduardo Speroni on 5/11/23.
// Copyright © 2023 Progress. All rights reserved.
//

#ifndef ModuleBinding_hpp
#define ModuleBinding_hpp

#include "Common.h"


namespace tns {

#define NODE_BINDING_CONTEXT_AWARE_CPP(modname, regfunc, priv, flags) \
static tns::ns_module _module = { \
NODE_MODULE_VERSION, \
flags, \
nullptr, \
__FILE__, \
nullptr, \
(tns::addon_context_register_func)(regfunc), \
NODE_STRINGIFY(modname), \
priv, \
nullptr}; \
void _register_##modname() { node_module_register(&_module); }

#define NODE_BINDING_CONTEXT_AWARE_INTERNAL(modname, regfunc) \
NODE_BINDING_CONTEXT_AWARE_CPP(modname, regfunc, nullptr, NM_F_INTERNAL)








#define NODE_BINDING_PER_ISOLATE_INIT(modname, per_isolate_func) \
void _register_isolate_##modname(v8::Isolate* isolate, \
v8::Local<v8::FunctionTemplate> target) { \
per_isolate_func(isolate, target); \
}


// This is a helepr that gives the target as an ObjectTemplate (using target.PrototypeTemplate())

#define NODE_BINDING_PER_ISOLATE_INIT_OBJ(modname, per_isolate_func) \
void _register_isolate_##modname(v8::Isolate* isolate, \
v8::Local<v8::FunctionTemplate> target) { \
per_isolate_func(isolate, target->PrototypeTemplate()); \
}


//#define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) \
// V(worker)

#define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) \
V(worker)

enum {
NM_F_BUILTIN = 1 << 0, // Unused.
NM_F_LINKED = 1 << 1,
NM_F_INTERNAL = 1 << 2,
NM_F_DELETEME = 1 << 3,
};

typedef void (*addon_register_func)(
v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
void* priv);

typedef void (*addon_context_register_func)(
v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> context,
void* priv);

struct ns_module {
int nm_version;
unsigned int nm_flags;
void* nm_dso_handle;
const char* nm_filename;
tns::addon_register_func nm_register_func;
tns::addon_context_register_func nm_context_register_func;
const char* nm_modname;
void* nm_priv;
struct ns_module* nm_link;
};

namespace binding {
void RegisterBuiltinBindings();
void CreateInternalBindingTemplates(v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> templ);
};


};


#endif /* ModuleBinding_hpp */
6 changes: 5 additions & 1 deletion NativeScript/runtime/Runtime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "IsolateWrapper.h"
#include "DisposerPHV.h"

#include "ModuleBinding.hpp"

#define STRINGIZE(x) #x
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)

Expand Down Expand Up @@ -142,17 +144,19 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {

void Runtime::Init(Isolate* isolate, bool isWorker) {
std::shared_ptr<Caches> cache = Caches::Init(isolate, nextIsolateId.fetch_add(1, std::memory_order_relaxed));
cache->isWorker = isWorker;
cache->ObjectCtorInitializer = MetadataBuilder::GetOrCreateConstructorFunctionTemplate;
cache->StructCtorInitializer = MetadataBuilder::GetOrCreateStructCtorFunction;

Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<FunctionTemplate> globalTemplateFunction = FunctionTemplate::New(isolate);
globalTemplateFunction->SetClassName(tns::ToV8String(isolate, "NativeScriptGlobalObject"));
tns::binding::CreateInternalBindingTemplates(isolate, globalTemplateFunction);
Local<ObjectTemplate> globalTemplate = ObjectTemplate::New(isolate, globalTemplateFunction);
DefineNativeScriptVersion(isolate, globalTemplate);

Worker::Init(isolate, globalTemplate, isWorker);
//Worker::Init(isolate, globalTemplate, isWorker);
DefinePerformanceObject(isolate, globalTemplate);
DefineTimeMethod(isolate, globalTemplate);
DefineDrainMicrotaskMethod(isolate, globalTemplate);
Expand Down
1 change: 1 addition & 0 deletions NativeScript/runtime/Worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace tns {
class Worker {
public:
static void Init(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate, bool isWorkerThread);
static void Init(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
static std::vector<std::string> GlobalFunctions;
private:
static void ConstructorCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
Expand Down
7 changes: 7 additions & 0 deletions NativeScript/runtime/Worker.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Helpers.h"
#include "Runtime.h"
#include "Constants.h"
#include "ModuleBinding.hpp"

using namespace v8;

Expand All @@ -16,6 +17,10 @@
"close"
};

void Worker::Init(Isolate* isolate, Local<ObjectTemplate> globalTemplate) {
Worker::Init(isolate, globalTemplate, Caches::Get(isolate)->isWorker);
}

void Worker::Init(Isolate* isolate, Local<ObjectTemplate> globalTemplate, bool isWorkerThread) {
if (isWorkerThread) {
// Register functions in the worker thread
Expand Down Expand Up @@ -290,3 +295,5 @@
}

}

NODE_BINDING_PER_ISOLATE_INIT_OBJ(worker, tns::Worker::Init)
8 changes: 8 additions & 0 deletions v8ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
2B7EA6AF2353477000E5184E /* NativeScriptException.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2B7EA6AD2353476F00E5184E /* NativeScriptException.mm */; };
2B7EA6B02353477000E5184E /* NativeScriptException.h in Headers */ = {isa = PBXBuildFile; fileRef = 2B7EA6AE2353477000E5184E /* NativeScriptException.h */; };
3C78BA5C2A0D600100C20A88 /* ModuleBinding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3C78BA5A2A0D600100C20A88 /* ModuleBinding.cpp */; };
3C78BA5D2A0D600100C20A88 /* ModuleBinding.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3C78BA5B2A0D600100C20A88 /* ModuleBinding.hpp */; };
3CA6E53529A78C6000D30F8B /* IsolateWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 3CA6E53429A78C6000D30F8B /* IsolateWrapper.h */; };
3CBFF7442971C1C200C5DE36 /* ArcMacro.h in Headers */ = {isa = PBXBuildFile; fileRef = 3CBFF7432971C1C200C5DE36 /* ArcMacro.h */; };
3CD1D9C129AA2C14004C1C21 /* DisposerPHV.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3CD1D9BF29AA2C14004C1C21 /* DisposerPHV.mm */; };
Expand Down Expand Up @@ -418,6 +420,8 @@
/* Begin PBXFileReference section */
2B7EA6AD2353476F00E5184E /* NativeScriptException.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = NativeScriptException.mm; sourceTree = "<group>"; };
2B7EA6AE2353477000E5184E /* NativeScriptException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NativeScriptException.h; sourceTree = "<group>"; };
3C78BA5A2A0D600100C20A88 /* ModuleBinding.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ModuleBinding.cpp; sourceTree = "<group>"; };
3C78BA5B2A0D600100C20A88 /* ModuleBinding.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ModuleBinding.hpp; sourceTree = "<group>"; };
3CA6E53429A78C6000D30F8B /* IsolateWrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IsolateWrapper.h; sourceTree = "<group>"; };
3CBFF7432971C1C200C5DE36 /* ArcMacro.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ArcMacro.h; sourceTree = "<group>"; };
3CD1D9BF29AA2C14004C1C21 /* DisposerPHV.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = DisposerPHV.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1407,6 +1411,8 @@
C275F475253B37AB00A997D5 /* UnmanagedType.mm */,
3CEF9CCC28F896B70056BA45 /* SpinLock.h */,
3CA6E53429A78C6000D30F8B /* IsolateWrapper.h */,
3C78BA5A2A0D600100C20A88 /* ModuleBinding.cpp */,
3C78BA5B2A0D600100C20A88 /* ModuleBinding.hpp */,
);
path = runtime;
sourceTree = "<group>";
Expand Down Expand Up @@ -1518,6 +1524,7 @@
3CD1D9C229AA2C14004C1C21 /* DisposerPHV.h in Headers */,
C22536B7241A318900192740 /* ffitarget.h in Headers */,
C2DDEBAB229EAC8300345BFE /* WeakRef.h in Headers */,
3C78BA5D2A0D600100C20A88 /* ModuleBinding.hpp in Headers */,
C2DDEBA8229EAC8300345BFE /* SymbolLoader.h in Headers */,
C247C17322F82842001D2CA2 /* v8-internal.h in Headers */,
C266569822AFFFB000EE15CC /* Reference.h in Headers */,
Expand Down Expand Up @@ -2103,6 +2110,7 @@
91B25A0A29DAC83D00E3CE04 /* ns-v8-tracing-agent-impl.mm in Sources */,
C2DDEBA6229EAC8300345BFE /* Helpers.mm in Sources */,
C26656B322B3768C00EE15CC /* InteropTypes.mm in Sources */,
3C78BA5C2A0D600100C20A88 /* ModuleBinding.cpp in Sources */,
C2F4D0AD232F85E20008A2EB /* SymbolIterator.mm in Sources */,
C2DDEBA5229EAC8300345BFE /* Runtime.mm in Sources */,
6573B9CF291FE29F00B0ED7C /* V8Runtime.cpp in Sources */,
Expand Down

0 comments on commit 88e9e1c

Please sign in to comment.