Last active
August 29, 2015 14:20
-
-
Save trevnorris/0e9db826789dda8b837e to your computer and use it in GitHub Desktop.
Compiles against io.js v2.0
This file contains hidden or 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
| { | |
| "targets": [{ | |
| "target_name": "addon", | |
| "sources": [ "main.cc" ] | |
| }] | |
| } |
This file contains hidden or 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
| #include <v8.h> | |
| #include <node.h> | |
| using namespace v8; | |
| void RunMe(const FunctionCallbackInfo<Value>& args) { | |
| uint32_t l = args[0]->Uint32Value(); | |
| Local<ArrayBuffer> ab = ArrayBuffer::New(args.GetIsolate(), l); | |
| Local<Uint8Array> u8 = Uint8Array::New(ab, 0, l); | |
| u8->SetPrototype(args[1]); | |
| args.GetReturnValue().Set(u8); | |
| } | |
| void init(Handle<Object> exports) { | |
| NODE_SET_METHOD(exports, "runMe", RunMe); | |
| } | |
| NODE_MODULE(addon, init) |
This file contains hidden or 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
| 'use strict'; | |
| const addon = require('./build/Release/addon'); | |
| const runMe = addon.runMe; | |
| const Buffer = require('buffer').Buffer; | |
| const bp = Buffer.prototype; | |
| Buffer.__proto__ = Uint8Array; | |
| Buffer.prototype.__proto__ = Uint8Array.prototype; | |
| function Nuffer(n) { | |
| return runMe(n, bp); | |
| } | |
| Nuffer.__proto__ = Buffer; | |
| Nuffer.prototype.__proto__ = Buffer.prototype; | |
| /* | |
| var n = new Nuffer(5); | |
| console.log(n, n.length); | |
| /* */ | |
| var t = process.hrtime(); | |
| for (var i = 0; i < 1e7; i++) { | |
| new Nuffer(1); | |
| } | |
| t = process.hrtime(t); | |
| t = t[0] * 1e9 + t[1]; | |
| console.log((t / 1e7).toFixed(1) + ' ns/op'); | |
| /* */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment