Skip to content

Instantly share code, notes, and snippets.

View ghaiklor's full-sized avatar

Eugene Obrezkov ghaiklor

View GitHub Profile
@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-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-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);
}
}
@ghaiklor
ghaiklor / developers-days-in-year.es6
Created September 13, 2015 19:03
Script calculates developers days in year based on shifts
'use strict';
const CURRENT_YEAR = 2015;
// Generator that shifts days by bits
function* developersDayGenerator() {
let day = 1;
while (day < 365) {
yield day;
day = day << 1;
@ghaiklor
ghaiklor / hackathon-2015-09-19.es6
Created September 21, 2015 17:03
Listen to radio and record it to file
'use strict';
import fs from 'fs';
import program from 'commander';
import Speaker from 'speaker';
import { Decoder } from 'lame';
import Parser from 'icecast-parser';
program
.option('-s, --source <source>', 'Source station')
@ghaiklor
ghaiklor / aoc-1-1.js
Last active January 5, 2016 15:59
Advent of Code (Day 1 Part 1)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('');
const result = INPUT.reduce((floor, direction) => direction === '(' ? ++floor : --floor, 0);
console.log(result);
@ghaiklor
ghaiklor / aoc-1-2.js
Last active January 5, 2016 16:02
Advent of Code (Day 1 Part 2)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('');
let floor = 0;
let result = INPUT.map(direction => direction === '(' ? ++floor : --floor).indexOf(-1) + 1;
console.log(result);