Skip to content

Instantly share code, notes, and snippets.

View ghaiklor's full-sized avatar

Eugene Obrezkov ghaiklor

View GitHub Profile
@ghaiklor
ghaiklor / v8-templates-example.cc
Last active October 12, 2019 11:27
Simple example how V8 Function\Object Templates can be used
// Create a template for the global object and set the built-in global functions
Local<ObjectTemplate> global = ObjectTemplate::New(isolate);
global->Set(
String::NewFromUtf8(isolate, "log"),
FunctionTemplate::New(isolate, LogCallback)
);
// Each processor gets its own context so different processors do not affect each other
Persistent<Context> context = Context::New(isolate, NULL, global);
@ghaiklor
ghaiklor / v8-compile-javascript-example.cc
Last active February 1, 2022 13:25
Simple example how V8 can compile JavaScript source and run it
// Create a new context.
Local<Context> context = Context::New(isolate);
// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
// Compile the source code.
@ghaiklor
ghaiklor / nodejs-module-struct.cc
Last active August 29, 2015 14:26
Structure where NodeJS is storing data about native modules
struct node_module {
int nm_version;
unsigned int nm_flags;
void* nm_dso_handle;
const char* nm_filename;
node::addon_register_func nm_register_func;
node::addon_context_register_func nm_context_register_func;
const char* nm_modname;
void* nm_priv;
struct node_module* nm_link;
@ghaiklor
ghaiklor / nodejs-module-register.cc
Last active August 29, 2015 14:26
NodeJS method that registers native module
void node_module_register(void* m) {
struct node_module* mp = reinterpret_cast<struct node_module*>(m);
if (mp->nm_flags & NM_F_BUILTIN) {
mp->nm_link = modlist_builtin;
modlist_builtin = 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;
@ghaiklor
ghaiklor / nodejs-get-builtin-module.cc
Last active August 29, 2015 14:26
NodeJS method that get builtin native module
struct node_module* get_builtin_module(const char* name) {
struct node_module* mp;
for (mp = modlist_builtin; mp != nullptr; mp = mp->nm_link) {
if (strcmp(mp->nm_modname, name) == 0)
break;
}
CHECK(mp == nullptr || (mp->nm_flags & NM_F_BUILTIN) != 0);
return (mp);
@ghaiklor
ghaiklor / nodejs-binding-method.cc
Last active October 13, 2019 13:28
NodeJS method that returns JavaScript object from bindings
static void Binding(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<String> module = args[0]->ToString(env->isolate());
node::Utf8Value module_v(env->isolate(), module);
Local<Object> cache = env->binding_cache_object();
Local<Object> exports;
if (cache->Has(module)) {
@ghaiklor
ghaiklor / nodejs-module-register-macros.cc
Created August 12, 2015 13:33
NodeJS macros for registering native modules
#define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags) \
extern "C" { \
static node::node_module _module = \
{ \
NODE_MODULE_VERSION, \
flags, \
NULL, \
__FILE__, \
NULL, \
(node::addon_context_register_func) (regfunc), \
@ghaiklor
ghaiklor / nodejs-module-register-example.cc
Last active August 29, 2015 14:27
NodeJS example how native modules is loading
// Include header files here
namespace node {
// Using namespaces
// Define useful macros
// And a lot other stuff here in C++
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
// Some logic here...
}
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
@ghaiklor
ghaiklor / nodejs-native-module.js
Created August 12, 2015 16:25
NodeJS Native Module wrapper in JS
function NativeModule(id) {
this.filename = id + '.js';
this.id = id;
this.exports = {};
this.loaded = false;
}
NativeModule._source = process.binding('natives');
NativeModule._cache = {};
@ghaiklor
ghaiklor / nodejs-define-javascript-method.cc
Created August 12, 2015 16:58
DefineJavaScript method in NodeJS that loading all the JS modules
void DefineJavaScript(Environment* env, Handle<Object> target) {
HandleScope scope(env->isolate());
for (int i = 0; natives[i].name; i++) {
if (natives[i].source != node_native) {
Local<String> name = String::NewFromUtf8(env->isolate(), natives[i].name);
Handle<String> source = String::NewFromUtf8(env->isolate(), natives[i].source, String::kNormalString, natives[i].source_len);
target->Set(name, source);
}
}