Skip to content

Instantly share code, notes, and snippets.

View gabrielschulhof's full-sized avatar

Gabriel Schulhof gabrielschulhof

  • California, USA
View GitHub Profile
We couldn’t find that file to show.

| Parent Class | Subclasses | |---|---|---| | | [Napi::Boolean][], [Napi::Number][], |

> const x = new Error();
undefined
> const vm = require('vm');
undefined
> vm.runInNewContext;
[Function: runInNewContext]
> vm.runInNewContext('x instanceof Error');
Thrown:
evalmachine.<anonymous>:1
x instanceof Error
@gabrielschulhof
gabrielschulhof / addon.js
Created June 27, 2019 17:02
Send modules to native
const addon = require('bindings')('addon');
addon.receiveModule('fs', require('fs'));
addon.receiveModule('crypto', require('crypto'));
console.log(addon.sendModule('fs') === require('fs'));
console.log(addon.sendModule('crypto') === require('crypto'));
host mac-node-ci
Hostname 207.254.58.162
Port 10005
User administrator
Cipher aes128-ctr
#include <stdio.h>
template <int x_init, int y_init>
class HasStructures {
public:
struct InnerStruct {
InnerStruct(): x(x_init), y(y_init) {}
int x;
int y;
};
@gabrielschulhof
gabrielschulhof / table.md
Created April 11, 2019 19:40
Table 2 for Medium post
JavaScript modules Native add-ons then Native add-ons now
1. ... need to be compiled. No Not if there are pre-builds Not if there are pre-builds
2. ... will work on all platforms and architectures. Yes Yes if there are pre-builds Yes if there are pre-builds
3. ... are designed to work with all current and subsequent Node.js versions once written. Yes No Yes if using N-API
4. ... can be loaded multiple times. Yes No Yes if written as a context-aware add-on
5. ... are thread-safe if not explicitly making use of threading infrastructure. Yes No Yes if written as a context-aware add-on
6. ... can be unloaded. Yes No Yes if written as a context-aware add-on and using [cleanup hooks](https://nodejs.
@gabrielschulhof
gabrielschulhof / table.md
Created April 11, 2019 19:33
Table 1 for Medium Post
JavaScript modules Native add-ons
1. ... need to be compiled. No Not if there are pre-builds
2. ... will work on all platforms and architectures. Yes Yes if there are pre-builds
3. ... are designed to work with all current and subsequent Node.js versions once written. Yes No
4. ... can be loaded multiple times. Yes No
5. ... are thread-safe if not explicitly making use of threading infrastructure. Yes No
6. ... can be unloaded. Yes No
@gabrielschulhof
gabrielschulhof / binding.cc
Created March 19, 2019 02:26
Crash on OSX because of symbols exposed by default
#include <assert.h>
#include <node.h>
#include "common.h"
namespace {
void Init(v8::Local<v8::Object> exports,
v8::Local<v8::Object> module,
v8::Local<v8::Context> context) {
v8::Local<v8::Name> propName =
@gabrielschulhof
gabrielschulhof / addon.cc
Last active February 22, 2019 21:36
Example of an addon conf file stored in the addon's build directory
#include <napi.h>
#include <stdio.h>
namespace {
// Per-instance addon data. An instance of this class is passed to every binding
// and makes it possible to avoid setting global static variables.
class AddonData {
public:
AddonData(std::string config_file): config_file(config_file) {}