Upgrading to GitLab 11.10.0. Expect errors and see debian-infrastructure-announce@lists.debian.org for further information.

Commit 407e9da2 authored by Mike Gabriel's avatar Mike Gabriel

Imported Upstream version 0.3.2

parents
build/
node_modules/
\ No newline at end of file
//noop
\ No newline at end of file
test/
examples/
.travis.yml
.npmignore
\ No newline at end of file
language: node_js
node_js:
- "0.8"
- "0.10"
- "0.11"
notifications:
email:
- rod@vagg.org
before_install: "cd test; npm install"
script: "cd test; npm test"
Copyright 2013, NAN contributors:
- Rod Vagg <https://github.com/rvagg>
- Benjamin Byholm <https://github.com/kkoopa>
- Trevor Norris <https://github.com/trevnorris>
(the "Original Author")
All rights reserved.
MIT +no-false-attribs License
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
Distributions of all or part of the Software intended to be used
by the recipients as they would use the unmodified Software,
containing modifications that substantially alter, remove, or
disable functionality of the Software, outside of the documented
configuration mechanisms provided by the Software, shall be
modified such that the Original Author's bug reporting email
addresses and urls are either replaced with the contact information
of the parties responsible for the changes, or removed entirely.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Except where noted, this license applies to any and all software
programs and associated documentation files created by the
Original Author, when distributed with the Software.
This diff is collapsed.
In this directory run `node-gyp rebuild` and then `node ./addon.js`
\ No newline at end of file
#include <node.h>
#include "../../nan.h"
#include "sync.h"
#include "async.h"
using namespace v8;
// Expose synchronous and asynchronous access to our
// Estimate() function
void InitAll(Handle<Object> exports) {
exports->Set(NanSymbol("calculateSync"),
FunctionTemplate::New(CalculateSync)->GetFunction());
exports->Set(NanSymbol("calculateAsync"),
FunctionTemplate::New(CalculateAsync)->GetFunction());
}
NODE_MODULE(addon, InitAll)
var addon = require('./build/Release/addon');
var calculations = process.argv[2] || 100000000;
function printResult(type, pi, ms) {
console.log(type, 'method:')
console.log('\tπ ≈ ' + pi
+ ' (' + Math.abs(pi - Math.PI) + ' away from actual)')
console.log('\tTook ' + ms + 'ms');
console.log()
}
function runSync () {
var start = Date.now();
// Estimate() will execute in the current thread,
// the next line won't return until it is finished
var result = addon.calculateSync(calculations);
printResult('Sync', result, Date.now() - start)
}
function runAsync () {
// how many batches should we split the work in to?
var batches = process.argv[3] || 16;
var ended = 0;
var total = 0;
var start = Date.now();
function done (err, result) {
total += result;
// have all the batches finished executing?
if (++ended == batches) {
printResult('Async', total / batches, Date.now() - start)
}
}
// for each batch of work, request an async Estimate() for
// a portion of the total number of calculations
for (var i = 0; i < batches; i++) {
addon.calculateAsync(calculations / batches, done);
}
}
runSync()
runAsync()
#include <node.h>
#include "../../nan.h"
#include "pi_est.h"
#include "async.h"
using namespace v8;
class PiWorker : public NanAsyncWorker {
public:
PiWorker(NanCallback *callback, int points)
: NanAsyncWorker(callback), points(points) {}
~PiWorker() {}
// Executed inside the worker-thread.
// It is not safe to access V8, or V8 data structures
// here, so everything we need for input and output
// should go on `this`.
void Execute () {
estimate = Estimate(points);
}
// Executed when the async work is complete
// this function will be run inside the main event loop
// so it is safe to use V8 again
void HandleOKCallback () {
NanScope();
Local<Value> argv[] = {
Local<Value>::New(Null())
, Number::New(estimate)
};
callback->Call(2, argv);
};
private:
int points;
double estimate;
};
// Asynchronous access to the `Estimate()` function
NAN_METHOD(CalculateAsync) {
NanScope();
int points = args[0]->Uint32Value();
NanCallback *callback = new NanCallback(args[1].As<Function>());
NanAsyncQueueWorker(new PiWorker(callback, points));
NanReturnUndefined();
}
#include <node.h>
#include "../../nan.h"
NAN_METHOD(CalculateAsync);
{
"targets": [
{
"target_name": "addon",
"sources": [
"addon.cc",
"pi_est.cc",
"sync.cc",
"async.cc"
]
}
]
}
{
"name": "async_work",
"version": "0.0.0",
"description": "Node.js Addons Example #9",
"main": "addon.js",
"private": true,
"gypfile": true
}
\ No newline at end of file
#include <cstdlib>
#include "pi_est.h"
/*
Estimate the value of π by using a Monte Carlo method.
Take `points` samples of random x and y values on a
[0,1][0,1] plane. Calculating the length of the diagonal
tells us whether the point lies inside, or outside a
quarter circle running from 0,1 to 1,0. The ratio of the
number of points inside to outside gives us an
approximation of π/4.
See https://en.wikipedia.org/wiki/File:Pi_30K.gif
for a visualization of how this works.
*/
double Estimate (int points) {
int i = points;
int inside = 0;
// unique seed for each run, for threaded use
unsigned int seed = rand();
double x, y;
while (i-- > 0) {
// rand_r() is used to avoid thread locking
x = rand_r(&seed) / (double)RAND_MAX;
y = rand_r(&seed) / (double)RAND_MAX;
// x & y and now values between 0 and 1
// now do a pythagorean diagonal calculation
// `1` represents our 1/4 circle
if ((x * x) + (y * y) <= 1)
inside++;
}
// calculate ratio and multiply by 4 for π
return (inside / (double)points) * 4;
}
double Estimate (int points);
#include <node.h>
#include "../../nan.h"
#include "pi_est.h"
#include "sync.h"
using namespace v8;
// Simple synchronous access to the `Estimate()` function
NAN_METHOD(CalculateSync) {
NanScope();
// expect a number as the first argument
int points = args[0]->Uint32Value();
double est = Estimate(points);
NanReturnValue(Number::New(est));
}
#include <node.h>
#include "../../nan.h"
NAN_METHOD(CalculateSync);
This diff is collapsed.
{
"name": "nan",
"version": "0.3.2",
"description": "Native Abstractions for Node.js: C++ header for Node 0.8->0.12 compatibility",
"main": ".index.js",
"repository": {
"type": "git",
"url": "git://github.com/rvagg/nan.git"
},
"contributors": [
"Rod Vagg <r@va.gg> (https://github.com/rvagg)",
"Benjamin Byholm <bbyholm@abo.fi> (https://github.com/kkoopa/)",
"Trevor Norris <trev.norris@gmail.com> (https://github.com/trevnorris)"
],
"license": "MIT"
}
\ No newline at end of file
{
"predef": [ ]
, "bitwise": false
, "camelcase": false
, "curly": false
, "eqeqeq": false
, "forin": false
, "immed": false
, "latedef": false
, "noarg": true
, "noempty": true
, "nonew": true
, "plusplus": false
, "quotmark": true
, "regexp": false
, "undef": true
, "unused": true
, "strict": false
, "trailing": true
, "maxlen": 120
, "asi": true
, "boss": true
, "debug": true
, "eqnull": true
, "esnext": true
, "evil": true
, "expr": true
, "funcscope": false
, "globalstrict": false
, "iterator": false
, "lastsemic": true
, "laxbreak": true
, "laxcomma": true
, "loopfunc": true
, "multistr": false
, "onecase": false
, "proto": false
, "regexdash": false
, "scripturl": true
, "smarttabs": false
, "shadow": false
, "sub": true
, "supernew": false
, "validthis": true
, "browser": true
, "couch": false
, "devel": false
, "dojo": false
, "mootools": false
, "node": true
, "nonstandard": true
, "prototypejs": false
, "rhino": false
, "worker": true
, "wsh": false
, "nomen": false
, "onevar": true
, "passfail": false
}
\ No newline at end of file
{"targets": [
{
"target_name" : "symbols"
, "sources" : [ "cpp/symbols.cpp" ]
}
, {
"target_name" : "fromv8string"
, "sources" : [ "cpp/fromv8string.cpp" ]
}
, {
"target_name" : "optionvalues"
, "sources" : [ "cpp/optionvalues.cpp" ]
}
, {
"target_name" : "multifile"
, "sources" : [ "cpp/multifile1.cpp", "cpp/multifile2.cpp" ]
}
, {
"target_name" : "returnvalue"
, "sources" : [ "cpp/returnvalue.cpp" ]
}
, {
"target_name" : "returnundefined"
, "sources" : [ "cpp/returnundefined.cpp" ]
}
, {
"target_name" : "returnnull"
, "sources" : [ "cpp/returnnull.cpp" ]
}
, {
"target_name" : "returnemptystring"
, "sources" : [ "cpp/returnemptystring.cpp" ]
}
, {
"target_name" : "asyncworker"
, "sources" : [ "cpp/asyncworker.cpp" ]
}
]}
#include <unistd.h>
#include <node.h>
#include "../../nan.h"
class SleepWorker : public NanAsyncWorker {
public:
SleepWorker(NanCallback *callback, int milliseconds)
: NanAsyncWorker(callback), milliseconds(milliseconds) {}
~SleepWorker() {}
void Execute () {
usleep(milliseconds * 1000);
}
private:
int milliseconds;
};
NAN_METHOD(Sleep) {
NanScope();
NanCallback *callback = new NanCallback(args[1].As<v8::Function>());
NanAsyncQueueWorker(new SleepWorker(callback, args[0]->Uint32Value()));
NanReturnUndefined();
}
void Init (v8::Handle<v8::Object> exports) {
exports->Set(NanSymbol("a"), v8::FunctionTemplate::New(Sleep)->GetFunction());
}
NODE_MODULE(asyncworker, Init)
#include <node.h>
#include "../../nan.h"
NAN_METHOD(ReturnString) {
NanScope();
Nan::Encoding enc = Nan::UTF8;
size_t bc;
unsigned int flags = v8::String::HINT_MANY_WRITES_EXPECTED | v8::String::NO_NULL_TERMINATION;
if (args[1]->IsUint32()) {
enc = (Nan::Encoding) args[1]->Uint32Value();
}
if (args[2]->IsUint32()) {
flags = args[2]->Uint32Value();
}
char *s = NanFromV8String(args[0].As<v8::Object>(), enc, &bc, NULL, 0, flags);
if (enc == Nan::UCS2) {
NanReturnValue(v8::String::New(reinterpret_cast<uint16_t *>(s), flags & v8::String::NO_NULL_TERMINATION ? bc / 2 : - 1));
} else {
NanReturnValue(v8::String::New(s, flags & v8::String::NO_NULL_TERMINATION ? bc : -1));
}
}
void Init (v8::Handle<v8::Object> target) {
target->Set(
NanSymbol("r")
, v8::FunctionTemplate::New(ReturnString)->GetFunction()
);
}
NODE_MODULE(fromv8string, Init)
#include <node.h>
#include "../../nan.h"
#include "multifile2.h"
void Init (v8::Handle<v8::Object> target) {
target->Set(
NanSymbol("r")
, v8::FunctionTemplate::New(ReturnString)->GetFunction()
);
}
NODE_MODULE(multifile, Init)
#include <node.h>
#include "../../nan.h"
NAN_METHOD(ReturnString) {
NanScope();
NanReturnValue(v8::String::New(NanFromV8String(args[0].As<v8::Object>())));
}
#include <node.h>
#include "../../nan.h"
NAN_METHOD(ReturnString);
#include <node.h>
#include "../../nan.h"
NAN_METHOD(OptionValues) {
NanScope();
v8::Local<v8::Object> inobj = args[0].As<v8::Object>();
v8::Local<v8::Object> outobj = v8::Object::New();
bool boolt = NanBooleanOptionValue(inobj, NanSymbol("true"));
bool boolf = NanBooleanOptionValue(inobj, NanSymbol("false"));
bool booldt = NanBooleanOptionValue(inobj, NanSymbol("dt"), true);
bool booldf = NanBooleanOptionValue(inobj, NanSymbol("df"), false);
uint32_t uint32i = NanUInt32OptionValue(inobj, NanSymbol("i"), 0);
uint32_t uint32f = NanUInt32OptionValue(inobj, NanSymbol("f"), 0);
uint32_t uint32di = NanUInt32OptionValue(inobj, NanSymbol("di"), 111);
outobj->Set(NanSymbol("true"), v8::Boolean::New(boolt));
outobj->Set(NanSymbol("false"), v8::Boolean::New(boolf));
outobj->Set(NanSymbol("dt"), v8::Boolean::New(booldt));
outobj->Set(NanSymbol("df"), v8::Boolean::New(booldf));
outobj->Set(NanSymbol("i"), v8::Number::New(uint32i));
outobj->Set(NanSymbol("f"), v8::Number::New(uint32f));
outobj->Set(NanSymbol("di"), v8::Number::New(uint32di));
NanReturnValue(outobj);
}
void Init (v8::Handle<v8::Object> target) {
target->Set(
NanSymbol("o")
, v8::FunctionTemplate::New(OptionValues)->GetFunction()
);
}
NODE_MODULE(optionvalues, Init)
\ No newline at end of file
#include <node.h>
#include "../../nan.h"
NAN_METHOD(ReturnEmptyString) {
NanScope();
NanReturnEmptyString();
}
void Init (v8::Handle<v8::Object> target) {
target->Set(
NanSymbol("r")
, v8::FunctionTemplate::New(ReturnEmptyString)->GetFunction()
);
}
NODE_MODULE(returnemptystring, Init)
#include <node.h>
#include "../../nan.h"
NAN_METHOD(ReturnNull) {
NanScope();
NanReturnNull();
}
void Init (v8::Handle<v8::Object> target) {
target->Set(
NanSymbol("r")
, v8::FunctionTemplate::New(ReturnNull)->GetFunction()
);
}
NODE_MODULE(returnnull, Init)
#include <node.h>
#include "../../nan.h"
NAN_METHOD(ReturnUndefined) {
NanScope();
NanReturnUndefined();
}
void Init (v8::Handle<v8::Object> target) {
target->Set(
NanSymbol("r")
, v8::FunctionTemplate::New(ReturnUndefined)->GetFunction()
);
}
NODE_MODULE(returnundefined, Init)
#include <node.h>
#include "../../nan.h"
NAN_METHOD(ReturnValue) {
NanScope();
if (args.Length() == 1) {
NanReturnValue(args[0]);
} else {
NanReturnValue(v8::String::New("default"));
}
}
void Init (v8::Handle<v8::Object> target) {
target->Set(
NanSymbol("r")
, v8::FunctionTemplate::New(ReturnValue)->GetFunction()
);
}
NODE_MODULE(returnvalue, Init)
#include <node.h>
#include "../../nan.h"
void Init (v8::Handle<v8::Object> target) {
target->Set(NanSymbol("key"), v8::String::New("a property"));
}
NODE_MODULE(symbols, Init)
const test = require('tap').test
, bindings = require('bindings')
test('asyncworker', function (t) {
var worker = bindings('asyncworker').a
t.type(worker, 'function')
var ticks = 0
, called = false
function tick () {
ticks++
if (!called)
setTimeout(tick, 0)
}
setTimeout(tick, 0)
worker(200, function () {
called = true
t.ok(ticks > 50, 'got plenty of ticks! (' + ticks + ')')
t.end()
})
})
\ No newline at end of file
const test = require('tap').test
, bindings = require('bindings');
test('fromv8string', function (t) {
const ASCII = 0,
UTF8 = 1,
BASE64 = 2,
UCS2 = 3,
BINARY = 4,
HEX = 5,
BUFFER = 6;
t.plan(17);
t.type(bindings('fromv8string').r, 'function');
t.equal(bindings('fromv8string').r('an ascii string', ASCII), 'an ascii string');
t.equal(bindings('fromv8string').r('an utf8 strïng', UTF8), 'an utf8 strïng');
t.equal(bindings('fromv8string').r('YSBiYXNlNjQgc3RyaW5n', BASE64), 'a base64 string');
t.equal(bindings('fromv8string').r('an ucs2 strïng', UCS2), 'an ucs2 strïng');
t.equal(bindings('fromv8string').r('a binary string', BINARY), 'a binary string');
t.equal(bindings('fromv8string').r('612068657820737472696E67', HEX), 'a hex string');
t.equal(bindings('fromv8string').r('a buffer string', BUFFER), 'a buffer string');
t.equal(bindings('fromv8string').r(new Buffer('an actual buffer'), BUFFER), 'an actual buffer');
t.equal(bindings('fromv8string').r('an ascii string', ASCII, 1), 'an ascii string');
t.equal(bindings('fromv8string').r('an utf8 strïng', UTF8, 1), 'an utf8 strïng');
t.equal(bindings('fromv8string').r('YSBiYXNlNjQgc3RyaW5n', BASE64, 1), 'a base64 string');
t.equal(bindings('fromv8string').r('an ucs2 strïng', UCS2, 1), 'an ucs2 strïng');
t.equal(bindings('fromv8string').r('a binary string', BINARY, 1), 'a binary string');
t.equal(bindings('fromv8string').r('612068657820737472696E67', HEX, 1), 'a hex string');
t.equal(bindings('fromv8string').r('a buffer string', BUFFER, 1), 'a buffer string');
t.equal(bindings('fromv8string').r(new Buffer('an actual buffer'), BUFFER, 1), 'an actual buffer');
});
const test = require('tap').test
, bindings = require('bindings');
// same as fromv8string test but split over multiple compile units
// just to test that there aren't any conflicts from including nan.h
// more than once in project files
test('multifile', function (t) {
t.plan(2);
t.type(bindings('multifile').r, 'function');
t.equal(bindings('multifile').r('a string value'), 'a string value');
});
const test = require('tap').test
, bindings = require('bindings')
, xtend = require('xtend');
test('optionvalues', function (t) {
t.plan(2);
var obj = {
'true' : true
, 'false' : false
, i : 100
, f : 200.2
}
, expobj = xtend(obj, {
dt : true // default true
, df : false // default false
, f : 200 // truncate
, di : 111
})
, actobj;
t.type(bindings('optionvalues').o, 'function');
actobj = bindings('optionvalues').o(obj);
t.deepEqual(actobj, expobj);
});
const test = require('tap').test
, bindings = require('bindings');
test('returnemptystring', function (t) {
t.plan(3);
t.type(bindings('returnemptystring').r, 'function');
t.equal(bindings('returnemptystring').r('a string value'), '');
t.equal(bindings('returnemptystring').r(), '');
});
const test = require('tap').test
, bindings = require('bindings');
test('returnnull', function (t) {
t.plan(3);
t.type(bindings('returnnull').r, 'function');