-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use node logic for globals and modules
- Loading branch information
1 parent
50f33e7
commit 88e9e1c
Showing
7 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
}; | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters