Skip to content

Instantly share code, notes, and snippets.

@trevnorris
Created July 20, 2015 21:56
Show Gist options
  • Save trevnorris/985fb05c1cd49a69ba3c to your computer and use it in GitHub Desktop.
Save trevnorris/985fb05c1cd49a69ba3c to your computer and use it in GitHub Desktop.
How to build/use
- Put these in the same folder then run
$ /path/to/nsolid/nsolid $(which node-gyp) rebuild --nodedir=/path/to/nsolid/../
If it builds properly then run
$ /path/to/nsolid/nsolid run.js
Have fun with the output
#include <v8.h>
#include <node.h>
using namespace v8;
class VisitPersistents;
VisitPersistents* visitor;
Persistent<Function> fn_p;
template <class TypeName>
inline Local<TypeName> PersistentToLocal(Isolate* isolate,
const Persistent<TypeName>* pst) {
if (pst->IsWeak()) {
return Local<TypeName>::New(isolate, *pst);
} else {
return *reinterpret_cast<Local<TypeName>*>(
const_cast<Persistent<TypeName>*>(pst));
}
}
class VisitPersistents : public PersistentHandleVisitor {
public:
virtual void VisitPersistentHandle(Persistent<Value>* value,
uint16_t class_id) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope handle_scope(isolate);
Local<Function> fn = PersistentToLocal(isolate, &fn_p).As<Function>();
// Don't need to inspect Buffers
if (class_id == 0xA10C)
return;
// class_id adjustment for AsyncWrap
if (class_id >= 0xA1C && class_id < 0xA1C + 100)
class_id = class_id - 0xA1C;
Local<Value> obj = PersistentToLocal(isolate, value);
if (obj.IsEmpty())
return;
Local<Value> argv[] = {
Integer::New(isolate, class_id),
obj
};
fn->Call(Null(Isolate::GetCurrent()), 2, argv);
}
};
void RunMe(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (!args[0]->IsFunction()) {
Local<String> message = String::NewFromUtf8(isolate, "CRAP!!!");
isolate->ThrowException(Exception::TypeError(message));
return;
}
fn_p.Reset(isolate, args[0].As<Function>());
V8::VisitHandlesWithClassIds(args.GetIsolate(), visitor);
fn_p.Reset();
}
void init(Handle<Object> exports) {
NODE_SET_METHOD(exports, "runMe", RunMe);
visitor = new VisitPersistents();
}
NODE_MODULE(addon, init)
'use strict';
const addon = require('./build/Release/addon');
const runMe = addon.runMe;
const async_wrap = process.binding('async_wrap');
const providers = Object.keys(async_wrap.Providers);
const fs = require('fs');
const net = require('net');
const log = process._rawDebug;
var s = net.createServer(function() { }).listen(8080, function() {
this.close();
});
fs.open(__filename, 'r', 444, function(err, fd) {
runMe(function(p, o) { log(o); });
});
fs.readFile(__filename, function onreadFile() {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment