pax_global_header00006660000000000000000000000064122101126460014505gustar00rootroot0000000000000052 comment=24cda1744b533babd6958ec0cc915d0dcf4ea5a5 nan-0.3.2/000077500000000000000000000000001221011264600122635ustar00rootroot00000000000000nan-0.3.2/.gitignore000066400000000000000000000000241221011264600142470ustar00rootroot00000000000000build/ node_modules/nan-0.3.2/.index.js000066400000000000000000000000061221011264600140020ustar00rootroot00000000000000//noopnan-0.3.2/.npmignore000066400000000000000000000000461221011264600142620ustar00rootroot00000000000000test/ examples/ .travis.yml .npmignorenan-0.3.2/.travis.yml000066400000000000000000000002511221011264600143720ustar00rootroot00000000000000language: 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" nan-0.3.2/LICENSE000066400000000000000000000036471221011264600133020ustar00rootroot00000000000000Copyright 2013, NAN contributors: - Rod Vagg - Benjamin Byholm - Trevor Norris (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. nan-0.3.2/README.md000066400000000000000000000633101221011264600135450ustar00rootroot00000000000000Native Abstractions for Node.js =============================== **A header file filled with macro and utility goodness for making addon development for Node.js easier across versions 0.8, 0.10 and 0.11, and eventually 0.12.** ***Current version: 0.3.2*** *(See [nan.h](https://github.com/rvagg/nan/blob/master/nan.h) for changelog)* [![NPM](https://nodei.co/npm/nan.png?downloads=true&stars=true)](https://nodei.co/npm/nan/) [![NPM](https://nodei.co/npm-dl/nan.png?months=6)](https://nodei.co/npm/nan/) Thanks to the crazy changes in V8 (and some in Node core), keeping native addons compiling happily across versions, particularly 0.10 to 0.11/0.12, is a minor nightmare. The goal of this project is to store all logic necessary to develop native Node.js addons without having to inspect `NODE_MODULE_VERSION` and get yourself into a macro-tangle. This project also contains some helper utilities that make addon development a bit more pleasant. * **[Usage](#usage)** * **[Example](#example)** * **[API](#api)** ## Usage Simply add **NAN** as a dependency in the *package.json* of your Node addon: ```js "dependencies": { ... "nan" : "~0.3.1" ... } ``` Pull in the path to **NAN** in your *binding.gyp* so that you can use `#include "nan.h"` in your *.cpp*: ```js "include_dirs" : [ ... "` when compiling your addon. ## Example See **[LevelDOWN](https://github.com/rvagg/node-leveldown/pull/48)** for a full example of **NAN** in use. For a simpler example, see the **[async pi estimation example](https://github.com/rvagg/nan/tree/master/examples/async_pi_estimate)** in the examples directory for full code and an explanation of what this Monte Carlo Pi estimation example does. Below are just some parts of the full example that illustrate the use of **NAN**. Compare to the current 0.10 version of this example, found in the [node-addon-examples](https://github.com/rvagg/node-addon-examples/tree/master/9_async_work) repository and also a 0.11 version of the same found [here](https://github.com/kkoopa/node-addon-examples/tree/5c01f58fc993377a567812597e54a83af69686d7/9_async_work). Note that there is no embedded version sniffing going on here and also the async work is made much simpler, see below for details on the `NanAsyncWorker` class. ```c++ // addon.cc #include #include "nan.h" // ... using namespace v8; void InitAll(Handle exports) { exports->Set(NanSymbol("calculateSync"), FunctionTemplate::New(CalculateSync)->GetFunction()); exports->Set(NanSymbol("calculateAsync"), FunctionTemplate::New(CalculateAsync)->GetFunction()); } NODE_MODULE(addon, InitAll) ``` ```c++ // sync.h #include #include "nan.h" NAN_METHOD(CalculateSync); ``` ```c++ // sync.cc #include #include "nan.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)); } ``` ```c++ // async.cc #include #include "nan.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 argv[] = { Local::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()); NanAsyncQueueWorker(new PiWorker(callback, points)); NanReturnUndefined(); } ``` ## API * NAN_METHOD * NAN_GETTER * NAN_SETTER * NAN_PROPERTY_GETTER * NAN_PROPERTY_SETTER * NAN_PROPERTY_ENUMERATOR * NAN_PROPERTY_DELETER * NAN_PROPERTY_QUERY * NAN_WEAK_CALLBACK * NanReturnValue * NanReturnUndefined * NanReturnNull * NanReturnEmptyString * NanScope * NanLocker * NanUnlocker * NanGetInternalFieldPointer * NanSetInternalFieldPointer * NanObjectWrapHandle * NanMakeWeak * NanSymbol * NanGetPointerSafe * NanSetPointerSafe * NanFromV8String * NanBooleanOptionValue * NanUInt32OptionValue * NanThrowError, NanThrowTypeError, NanThrowRangeError, NanThrowError(Handle), NanThrowError(Handle, int) * NanNewBufferHandle(char *, size_t, FreeCallback, void *), NanNewBufferHandle(char *, uint32_t), NanNewBufferHandle(uint32_t) * NanBufferUse(char *, uint32_t) * NanNewContextHandle * NanHasInstance * NanPersistentToLocal * NanDispose * NanAssignPersistent * NanInitPersistent * NanCallback * NanAsyncWorker * NanAsyncQueueWorker ### NAN_METHOD(methodname) Use `NAN_METHOD` to define your V8 accessible methods: ```c++ // .h: class Foo : public node::ObjectWrap { ... static NAN_METHOD(Bar); static NAN_METHOD(Baz); } // .cc: NAN_METHOD(Foo::Bar) { ... } NAN_METHOD(Foo::Baz) { ... } ``` The reason for this macro is because of the method signature change in 0.11: ```c++ // 0.10 and below: Handle name(const Arguments& args) // 0.11 and above void name(const FunctionCallbackInfo& args) ``` The introduction of `FunctionCallbackInfo` brings additional complications: ### NAN_GETTER(methodname) Use `NAN_GETTER` to declare your V8 accessible getters. You get a `Local` `property` and an appropriately typed `args` object that can act like the `args` argument to a `NAN_METHOD` call. You can use `NanReturnNull()`, `NanReturnEmptyString()`, `NanReturnUndefined()` and `NanReturnValue()` in a `NAN_GETTER`. ### NAN_SETTER(methodname) Use `NAN_SETTER` to declare your V8 accessible setters. Same as `NAN_GETTER` but you also get a `Local` `value` object to work with. You can use `NanReturnNull()`, `NanReturnEmptyString()`, `NanReturnUndefined()` and `NanReturnValue()` in a `NAN_SETTER`. ### NAN_PROPERTY_GETTER(cbname) Use `NAN_PROPERTY_GETTER` to declare your V8 accessible property getters. You get a `Local` `property` and an appropriately typed `args` object that can act similar to the `args` argument to a `NAN_METHOD` call. You can use `NanReturnNull()`, `NanReturnEmptyString()`, `NanReturnUndefined()` and `NanReturnValue()` in a `NAN_PROPERTY_GETTER`. ### NAN_PROPERTY_SETTER(cbname) Use `NAN_PROPERTY_SETTER` to declare your V8 accessible property setters. Same as `NAN_PROPERTY_GETTER` but you also get a `Local` `value` object to work with. You can use `NanReturnNull()`, `NanReturnEmptyString()`, `NanReturnUndefined()` and `NanReturnValue()` in a `NAN_PROPERTY_SETTER`. ### NAN_PROPERTY_ENUMERATOR(cbname) Use `NAN_PROPERTY_ENUMERATOR` to declare your V8 accessible property enumerators. You get an appropriately typed `args` object like the `args` argument to a `NAN_PROPERTY_GETTER` call. You can use `NanReturnNull()`, `NanReturnEmptyString()`, `NanReturnUndefined()` and `NanReturnValue()` in a `NAN_PROPERTY_ENUMERATOR`. ### NAN_PROPERTY_DELETER(cbname) Use `NAN_PROPERTY_DELETER` to declare your V8 accessible property deleters. Same as `NAN_PROPERTY_GETTER`. You can use `NanReturnNull()`, `NanReturnEmptyString()`, `NanReturnUndefined()` and `NanReturnValue()` in a `NAN_PROPERTY_DELETER`. ### NAN_PROPERTY_QUERY(cbname) Use `NAN_PROPERTY_QUERY` to declare your V8 accessible property queries. Same as `NAN_PROPERTY_GETTER`. You can use `NanReturnNull()`, `NanReturnEmptyString()`, `NanReturnUndefined()` and `NanReturnValue()` in a `NAN_PROPERTY_QUERY`. ### NAN_WEAK_CALLBACK(type, cbname) Use `NAN_WEAK_CALLBACK` to declare your V8 WeakReference callbacks. There is an object argument accessible through `NAN_WEAK_CALLBACK_OBJECT`. The `type` argument gives the type of the `data` argument, accessible through `NAN_WEAK_CALLBACK_DATA(type)`. ```c++ static NAN_WEAK_CALLBACK(BufferReference*, WeakCheck) { if (NAN_WEAK_CALLBACK_DATA(BufferReference*)->noLongerNeeded_) { delete NAN_WEAK_CALLBACK_DATA(BufferReference*); } else { // Still in use, revive, prevent GC NanMakeWeak(NAN_WEAK_CALLBACK_OBJECT, NAN_WEAK_CALLBACK_DATA(BufferReference*), &WeakCheck); } } ``` ### NanReturnValue(Handle<Value>) Use `NanReturnValue` when you want to return a value from your V8 accessible method: ```c++ NAN_METHOD(Foo::Bar) { ... NanReturnValue(String::New("FooBar!")); } ``` No `return` statement required. ### NanReturnUndefined() Use `NanReturnUndefined` when you don't want to return anything from your V8 accessible method: ```c++ NAN_METHOD(Foo::Baz) { ... NanReturnUndefined(); } ``` ### NanReturnNull() Use `NanReturnNull` when you want to return `Null` from your V8 accessible method: ```c++ NAN_METHOD(Foo::Baz) { ... NanReturnNull(); } ``` ### NanReturnEmptyString() Use `NanReturnEmptyString` when you want to return an empty `String` from your V8 accessible method: ```c++ NAN_METHOD(Foo::Baz) { ... NanReturnEmptyString(); } ``` ### NanScope() The introduction of `isolate` references for many V8 calls in Node 0.11 makes `NanScope()` necessary, use it in place of `HandleScope scope`: ```c++ NAN_METHOD(Foo::Bar) { NanScope(); NanReturnValue(String::New("FooBar!")); } ``` ### NanLocker() The introduction of `isolate` references for many V8 calls in Node 0.11 makes `NanLocker()` necessary, use it in place of `Locker locker`: ```c++ NAN_METHOD(Foo::Bar) { NanLocker(); ... NanUnlocker(); } ``` ### NanUnlocker() The introduction of `isolate` references for many V8 calls in Node 0.11 makes `NanUnlocker()` necessary, use it in place of `Unlocker unlocker`: ```c++ NAN_METHOD(Foo::Bar) { NanLocker(); ... NanUnlocker(); } ``` ### void * NanGetInternalFieldPointer(Handle<Object>, int) Gets a pointer to the internal field with at `index` from a V8 `Object` handle. ```c++ Local obj; ... NanGetInternalFieldPointer(obj, 0); ``` ### void NanSetInternalFieldPointer(Handle<Object>, int, void *) Sets the value of the internal field at `index` on a V8 `Object` handle. ```c++ static Persistent dataWrapperCtor; ... Local wrapper = NanPersistentToLocal(dataWrapperCtor)->NewInstance(); NanSetInternalFieldPointer(wrapper, 0, this); ``` ### Local<Object> NanObjectWrapHandle(Object) When you want to fetch the V8 object handle from a native object you've wrapped with Node's `ObjectWrap`, you should use `NanObjectWrapHandle`: ```c++ NanObjectWrapHandle(iterator)->Get(String::NewSymbol("end")) ``` ### NanMakeWeak(Persistent<T>, parameter, callback) Make a persistent reference weak. ### String NanSymbol(char *) This isn't strictly about compatibility, it's just an easier way to create string symbol objects (i.e. `String::NewSymbol(x)`), for getting and setting object properties, or names of objects. ```c++ bool foo = false; if (obj->Has(NanSymbol("foo"))) foo = optionsObj->Get(NanSymbol("foo"))->BooleanValue() ``` ### Type NanGetPointerSafe(Type *[, Type]) A helper for getting values from optional pointers. If the pointer is `NULL`, the function returns the optional default value, which defaults to `0`. Otherwise, the function returns the value the pointer points to. ```c++ char *plugh(uint32_t *optional) { char res[] = "xyzzy"; uint32_t param = NanGetPointerSafe(optional, 0x1337); switch (param) { ... } NanSetPointerSafe(optional, 0xDEADBEEF); } ``` ### bool NanSetPointerSafe(Type *, Type) A helper for setting optional argument pointers. If the pointer is `NULL`, the function simply return `false`. Otherwise, the value is assigned to the variable the pointer points to. ```c++ const char *plugh(size_t *outputsize) { char res[] = "xyzzy"; if !(NanSetPointerSafe(outputsize, strlen(res) + 1)) { ... } ... } ``` ### char* NanFromV8String(Handle<Value>[, enum Nan::Encoding, size_t *, char *, size_t, int]) When you want to convert a V8 `String` to a `char*` use `NanFromV8String`. It is possible to define an encoding that defaults to `Nan::UTF8` as well as a pointer to a variable that will be assigned the number of bytes in the returned string. It is also possible to supply a buffer and its length to the function in order not to have a new buffer allocated. The final argument allows optionally setting `String::WriteOptions`, which default to `String::HINT_MANY_WRITES_EXPECTED | String::NO_NULL_TERMINATION`. Just remember that you'll end up with an object that you'll need to `delete[]` at some point unless you supply your own buffer: ```c++ size_t count; char* name = NanFromV8String(args[0]); char* decoded = NanFromV8String(args[1], Nan::BASE64, &count, NULL, 0, String::HINT_MANY_WRITES_EXPECTED); char param_copy[count]; memcpy(param_copy, decoded, count); delete[] decoded; ``` ### bool NanBooleanOptionValue(Handle<Value>, Handle<String>[, bool]) When you have an "options" object that you need to fetch properties from, boolean options can be fetched with this pair. They check first if the object exists (`IsEmpty`), then if the object has the given property (`Has`) then they get and convert/coerce the property to a `bool`. The optional last parameter is the *default* value, which is `false` if left off: ```c++ // `foo` is false unless the user supplies a truthy value for it bool foo = NanBooleanOptionValue(optionsObj, NanSymbol("foo")); // `bar` is true unless the user supplies a falsy value for it bool bar = NanBooleanOptionValueDefTrue(optionsObj, NanSymbol("bar"), true); ``` ### uint32_t NanUInt32OptionValue(Handle<Value>, Handle<String>, uint32_t) Similar to `NanBooleanOptionValue`, use `NanUInt32OptionValue` to fetch an integer option from your options object. Can be any kind of JavaScript `Number` and it will be coerced to an unsigned 32-bit integer. Requires all 3 arguments as a default is not optional: ```c++ uint32_t count = NanUInt32OptionValue(optionsObj, NanSymbol("count"), 1024); ``` ### NanThrowError(message), NanThrowTypeError(message), NanThrowRangeError(message), NanThrowError(Local<Value>), NanThrowError(Local<Value>, int) For throwing `Error`, `TypeError` and `RangeError` objects. You should `return` this call: ```c++ return NanThrowError("you must supply a callback argument"); ``` Can also handle any custom object you may want to throw. If used with the error code argument, it will add the supplied error code to the error object as a property called `code`. ### Local<Object> NanNewBufferHandle(char *, uint32_t), Local<Object> NanNewBufferHandle(uint32_t) The `Buffer` API has changed a little in Node 0.11, this helper provides consistent access to `Buffer` creation: ```c++ NanNewBufferHandle((char*)value.data(), value.size()); ``` Can also be used to initialize a `Buffer` with just a `size` argument. Can also be supplied with a `NAN_WEAK_CALLBACK` and a hint for the garbage collector, when dealing with weak references. ### Local<Object> NanBufferUse(char*, uint32_t) `Buffer::New(char*, uint32_t)` prior to 0.11 would make a copy of the data. While it was possible to get around this, it required a shim by passing a callback. So the new API `Buffer::Use(char*, uint32_t)` was introduced to remove needing to use this shim. `NanBufferUse` uses the `char*` passed as the backing data, and will free the memory automatically when the weak callback is called. Keep this in mind, as careless use can lead to "double free or corruption" and other cryptic failures. ### bool NanHasInstance(Persistent<FunctionTemplate>&, Handle<Value>) Can be used to check the type of an object to determine it is of a particular class you have already defined and have a `Persistent` handle for. ### Local<Type> NanPersistentToLocal(Persistent<Type>&) Aside from `FunctionCallbackInfo`, the biggest and most painful change to V8 in Node 0.11 is the many restrictions now placed on `Persistent` handles. They are difficult to assign and difficult to fetch the original value out of. Use `NanPersistentToLocal` to convert a `Persistent` handle back to a `Local` handle. ```c++ Local handle = NanPersistentToLocal(persistentHandle); ``` ### Local<Context> NanNewContextHandle([ExtensionConfiguration*, Handle<ObjectTemplate>, Handle<Value>]) Creates a new `Local` handle. ```c++ Local ftmpl = FunctionTemplate::New(); Local otmpl = ftmpl->InstanceTemplate(); Local ctx = NanNewContextHandle(NULL, otmpl); ``` ### void NanDispose(Persistent<T> &) Use `NanDispose` to dispose a `Persistent` handle. ```c++ NanDispose(persistentHandle); ``` ### NanAssignPersistent(type, handle, object) Use `NanAssignPersistent` to assign a non-`Persistent` handle to a `Persistent` one. You can no longer just declare a `Persistent` handle and assign directly to it later, you have to `Reset` it in Node 0.11, so this makes it easier. In general it is now better to place anything you want to protect from V8's garbage collector as properties of a generic `Object` and then assign that to a `Persistent`. This works in older versions of Node also if you use `NanAssignPersistent`: ```c++ Persistent persistentHandle; ... Local obj = Object::New(); obj->Set(NanSymbol("key"), keyHandle); // where keyHandle might be a Local NanAssignPersistent(Object, persistentHandle, obj) ``` ### NanInitPersistent(type, name, object) User `NanInitPersistent` to declare and initialize a new `Persistent` with the supplied object. The assignment operator for `Persistent` is no longer public in Node 0.11, so this macro makes it easier to declare and initializing a new `Persistent`. See NanAssignPersistent for more information. ```c++ Local obj = Object::New(); obj->Set(NanSymbol("key"), keyHandle); // where keyHandle might be a Local NanInitPersistent(Object, persistentHandle, obj); ``` ### NanCallback Because of the difficulties imposed by the changes to `Persistent` handles in V8 in Node 0.11, creating `Persistent` versions of your `Local` handles is annoyingly tricky. `NanCallback` makes it easier by taking your `Local` handle, making it persistent until the `NanCallback` is deleted and even providing a handy `Call()` method to fetch and execute the callback `Function`. ```c++ Local callbackHandle = callback = args[0].As(); NanCallback *callback = new NanCallback(callbackHandle); // pass `callback` around and it's safe from GC until you: delete callback; ``` You can execute the callback like so: ```c++ // no arguments: callback->Call(0, NULL); // an error argument: Local argv[] = { Exception::Error(String::New("fail!")) }; callback->Call(1, argv); // a success argument: Local argv[] = { Local::New(Null()), String::New("w00t!") }; callback->Call(2, argv); ``` `NanCallback` also has a `Local GetCallback()` method that you can use to fetch a local handle to the underlying callback function if you need it. ### NanAsyncWorker `NanAsyncWorker` is an abstract class that you can subclass to have much of the annoying async queuing and handling taken care of for you. It can even store arbitrary V8 objects for you and have them persist while the async work is in progress. See a rough outline of the implementation: ```c++ class NanAsyncWorker { public: NanAsyncWorker (NanCallback *callback); // Clean up persistent handles and delete the *callback virtual ~NanAsyncWorker (); // Check the `char *errmsg` property and call HandleOKCallback() // or HandleErrorCallback depending on whether it has been set or not virtual void WorkComplete (); // You must implement this to do some async work. If there is an // error then allocate `errmsg` to to a message and the callback will // be passed that string in an Error object virtual void Execute (); protected: // Set this if there is an error, otherwise it's NULL const char *errmsg; // Save a V8 object in a Persistent handle to protect it from GC void SavePersistent(const char *key, Local &obj); // Fetch a stored V8 object (don't call from within `Execute()`) Local GetFromPersistent(const char *key); // Default implementation calls the callback function with no arguments. // Override this to return meaningful data virtual void HandleOKCallback (); // Default implementation calls the callback function with an Error object // wrapping the `errmsg` string virtual void HandleErrorCallback (); }; ``` ### NanAsyncQueueWorker(NanAsyncWorker *) `NanAsyncQueueWorker` will run a `NanAsyncWorker` asynchronously via libuv. Both the *execute* and *after_work* steps are taken care of for you—most of the logic for this is embedded in `NanAsyncWorker`. ### Contributors NAN is only possible due to the excellent work of the following contributors:
Rod VaggGitHub/rvaggTwitter/@rvagg
Benjamin ByholmGitHub/kkoopa
Trevor NorrisGitHub/trevnorrisTwitter/@trevnorris
Licence & copyright ----------------------- Copyright (c) 2013 Rod Vagg & NAN contributors (listed above). Native Abstractions for Node.js is licensed under an MIT +no-false-attribs license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details. nan-0.3.2/examples/000077500000000000000000000000001221011264600141015ustar00rootroot00000000000000nan-0.3.2/examples/async_pi_estimate/000077500000000000000000000000001221011264600176015ustar00rootroot00000000000000nan-0.3.2/examples/async_pi_estimate/README.md000066400000000000000000000001031221011264600210520ustar00rootroot00000000000000In this directory run `node-gyp rebuild` and then `node ./addon.js`nan-0.3.2/examples/async_pi_estimate/addon.cc000066400000000000000000000007031221011264600211750ustar00rootroot00000000000000#include #include "../../nan.h" #include "sync.h" #include "async.h" using namespace v8; // Expose synchronous and asynchronous access to our // Estimate() function void InitAll(Handle exports) { exports->Set(NanSymbol("calculateSync"), FunctionTemplate::New(CalculateSync)->GetFunction()); exports->Set(NanSymbol("calculateAsync"), FunctionTemplate::New(CalculateAsync)->GetFunction()); } NODE_MODULE(addon, InitAll) nan-0.3.2/examples/async_pi_estimate/addon.js000066400000000000000000000022141221011264600212230ustar00rootroot00000000000000var 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() nan-0.3.2/examples/async_pi_estimate/async.cc000066400000000000000000000021771221011264600212340ustar00rootroot00000000000000#include #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 argv[] = { Local::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()); NanAsyncQueueWorker(new PiWorker(callback, points)); NanReturnUndefined(); } nan-0.3.2/examples/async_pi_estimate/async.h000066400000000000000000000001061221011264600210640ustar00rootroot00000000000000#include #include "../../nan.h" NAN_METHOD(CalculateAsync); nan-0.3.2/examples/async_pi_estimate/binding.gyp000066400000000000000000000002531221011264600217340ustar00rootroot00000000000000{ "targets": [ { "target_name": "addon", "sources": [ "addon.cc", "pi_est.cc", "sync.cc", "async.cc" ] } ] } nan-0.3.2/examples/async_pi_estimate/package.json000066400000000000000000000002321221011264600220640ustar00rootroot00000000000000{ "name": "async_work", "version": "0.0.0", "description": "Node.js Addons Example #9", "main": "addon.js", "private": true, "gypfile": true }nan-0.3.2/examples/async_pi_estimate/pi_est.cc000066400000000000000000000020531221011264600213730ustar00rootroot00000000000000#include #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; } nan-0.3.2/examples/async_pi_estimate/pi_est.h000066400000000000000000000000361221011264600212340ustar00rootroot00000000000000double Estimate (int points); nan-0.3.2/examples/async_pi_estimate/sync.cc000066400000000000000000000005441221011264600210670ustar00rootroot00000000000000#include #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)); } nan-0.3.2/examples/async_pi_estimate/sync.h000066400000000000000000000001051221011264600207220ustar00rootroot00000000000000#include #include "../../nan.h" NAN_METHOD(CalculateSync); nan-0.3.2/nan.h000066400000000000000000000730411221011264600132150ustar00rootroot00000000000000/********************************************************************************** * NAN - Native Abstractions for Node.js * * Copyright (c) 2013 NAN contributors: * - Rod Vagg * - Benjamin Byholm * - Trevor Norris * * MIT +no-false-attribs License * * Version 0.3.2 (current Node unstable: 0.11.6, Node stable: 0.10.17) * * ChangeLog: * * 0.3.2 Aug 30 2013 * - Fix missing scope declaration in GetFromPersistent() and SaveToPersistent * in NanAsyncWorker * * * 0.3.1 Aug 20 2013 * - fix "not all control paths return a value" compile warning on some platforms * * * 0.3.0 Aug 19 2013 * - Made NAN work with NPM * - Lots of fixes to NanFromV8String, pulling in features from new Node core * - Changed node::encoding to Nan::Encoding in NanFromV8String to unify the API * - Added optional error number argument for NanThrowError() * - Added NanInitPersistent() * - Added NanReturnNull() and NanReturnEmptyString() * - Added NanLocker and NanUnlocker * - Added missing scopes * - Made sure to clear disposed Persistent handles * - Changed NanAsyncWorker to allocate error messages on the heap * - Changed NanThrowError(Local) to NanThrowError(Handle) * - Fixed leak in NanAsyncWorker when errmsg is used * * * 0.2.2 Aug 5 2013 * - Fixed usage of undefined variable with node::BASE64 in NanFromV8String() * * * 0.2.1 Aug 5 2013 * - Fixed 0.8 breakage, node::BUFFER encoding type not available in 0.8 for * NanFromV8String() * * * 0.2.0 Aug 5 2013 * - Added NAN_PROPERTY_GETTER, NAN_PROPERTY_SETTER, NAN_PROPERTY_ENUMERATOR, * NAN_PROPERTY_DELETER, NAN_PROPERTY_QUERY * - Extracted _NAN_METHOD_ARGS, _NAN_GETTER_ARGS, _NAN_SETTER_ARGS, * _NAN_PROPERTY_GETTER_ARGS, _NAN_PROPERTY_SETTER_ARGS, * _NAN_PROPERTY_ENUMERATOR_ARGS, _NAN_PROPERTY_DELETER_ARGS, * _NAN_PROPERTY_QUERY_ARGS * - Added NanGetInternalFieldPointer, NanSetInternalFieldPointer * - Added NAN_WEAK_CALLBACK, NAN_WEAK_CALLBACK_OBJECT, * NAN_WEAK_CALLBACK_DATA, NanMakeWeak * - Renamed THROW_ERROR to _NAN_THROW_ERROR * - Added NanNewBufferHandle(char*, size_t, node::smalloc::FreeCallback, void*) * - Added NanBufferUse(char*, uint32_t) * - Added NanNewContextHandle(v8::ExtensionConfiguration*, * v8::Handle, v8::Handle) * - Fixed broken NanCallback#GetFunction() * - Added optional encoding and size arguments to NanFromV8String() * - Added NanGetPointerSafe() and NanSetPointerSafe() * - Added initial test suite (to be expanded) * - Allow NanUInt32OptionValue to convert any Number object * * * 0.1.0 Jul 21 2013 * - Added `NAN_GETTER`, `NAN_SETTER` * - Added `NanThrowError` with single Local argument * - Added `NanNewBufferHandle` with single uint32_t argument * - Added `NanHasInstance(Persistent&, Handle)` * - Added `Local NanCallback#GetFunction()` * - Added `NanCallback#Call(int, Local[])` * - Deprecated `NanCallback#Run(int, Local[])` in favour of Call * * See https://github.com/rvagg/nan for the latest update to this file **********************************************************************************/ #ifndef NAN_H #define NAN_H #include #include #include // some generic helpers template static inline bool NanSetPointerSafe(T *var, T val) { if (var) { *var = val; return true; } else { return false; } } template static inline T NanGetPointerSafe( T *var, T fallback = reinterpret_cast(0)) { if (var) { return *var; } else { return fallback; } } #define NanSymbol(value) v8::String::NewSymbol(value) static inline bool NanBooleanOptionValue( v8::Local optionsObj , v8::Handle opt, bool def) { if (def) { return optionsObj.IsEmpty() || !optionsObj->Has(opt) || optionsObj->Get(opt)->BooleanValue(); } else { return !optionsObj.IsEmpty() && optionsObj->Has(opt) && optionsObj->Get(opt)->BooleanValue(); } } static inline bool NanBooleanOptionValue( v8::Local optionsObj , v8::Handle opt) { return NanBooleanOptionValue(optionsObj, opt, false); } static inline uint32_t NanUInt32OptionValue( v8::Local optionsObj , v8::Handle opt , uint32_t def) { return !optionsObj.IsEmpty() && optionsObj->Has(opt) && optionsObj->Get(opt)->IsNumber() ? optionsObj->Get(opt)->Uint32Value() : def; } #if (NODE_MODULE_VERSION > 0x000B) // Node 0.11+ (0.11.3 and below won't compile with these) static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent(); # define _NAN_METHOD_ARGS const v8::FunctionCallbackInfo& args # define NAN_METHOD(name) void name(_NAN_METHOD_ARGS) # define _NAN_GETTER_ARGS const v8::PropertyCallbackInfo& args # define NAN_GETTER(name) \ void name(v8::Local property, _NAN_GETTER_ARGS) # define _NAN_SETTER_ARGS const v8::PropertyCallbackInfo& args # define NAN_SETTER(name) \ void name( \ v8::Local property \ , v8::Local value \ , _NAN_SETTER_ARGS) # define _NAN_PROPERTY_GETTER_ARGS \ const v8::PropertyCallbackInfo& args # define NAN_PROPERTY_GETTER(name) \ void name(v8::Local property \ , _NAN_PROPERTY_GETTER_ARGS) # define _NAN_PROPERTY_SETTER_ARGS \ const v8::PropertyCallbackInfo& args # define NAN_PROPERTY_SETTER(name) \ void name(v8::Local property \ , v8::Local value \ , _NAN_PROPERTY_SETTER_ARGS) # define _NAN_PROPERTY_ENUMERATOR_ARGS \ const v8::PropertyCallbackInfo& args # define NAN_PROPERTY_ENUMERATOR(name) \ void name(_NAN_PROPERTY_ENUMERATOR_ARGS) # define _NAN_PROPERTY_DELETER_ARGS \ const v8::PropertyCallbackInfo& args # define NAN_PROPERTY_DELETER(name) \ void name( \ v8::Local property \ , _NAN_PROPERTY_DELETER_ARGS) # define _NAN_PROPERTY_QUERY_ARGS \ const v8::PropertyCallbackInfo& args # define NAN_PROPERTY_QUERY(name) \ void name(v8::Local property, _NAN_PROPERTY_QUERY_ARGS) # define NanGetInternalFieldPointer(object, index) \ object->GetAlignedPointerFromInternalField(index) # define NanSetInternalFieldPointer(object, index, value) \ object->SetAlignedPointerInInternalField(index, value) # define NAN_WEAK_CALLBACK(type, name) \ void name( \ v8::Isolate* isolate, \ v8::Persistent* object, \ type data) # define NAN_WEAK_CALLBACK_OBJECT (*object) # define NAN_WEAK_CALLBACK_DATA(type) ((type) data) # define NanScope() v8::HandleScope scope(nan_isolate) # define NanLocker() v8::Locker locker(nan_isolate) # define NanUnlocker() v8::Unlocker unlocker(nan_isolate) # define NanReturnValue(value) return args.GetReturnValue().Set(value) # define NanReturnUndefined() return # define NanReturnNull() return args.GetReturnValue().SetNull() # define NanReturnEmptyString() return args.GetReturnValue().SetEmptyString() # define NanAssignPersistent(type, handle, obj) handle.Reset(nan_isolate, obj) # define NanInitPersistent(type, name, obj) \ v8::Persistent name(nan_isolate, obj) # define NanObjectWrapHandle(obj) obj->handle() # define NanMakeWeak(handle, parameter, callback) \ handle.MakeWeak(nan_isolate, parameter, callback) # define _NAN_THROW_ERROR(fun, errmsg) \ do { \ NanScope(); \ v8::ThrowException(fun(v8::String::New(errmsg))); \ } while (0); inline static void NanThrowError(const char* errmsg) { _NAN_THROW_ERROR(v8::Exception::Error, errmsg); } inline static void NanThrowError(v8::Handle error) { NanScope(); v8::ThrowException(error); } inline static void NanThrowError(const char *msg, const int errorNumber) { v8::Local err = v8::Exception::Error(v8::String::New(msg)); v8::Local obj = err.As(); obj->Set(v8::String::New("code"), v8::Int32::New(errorNumber)); NanThrowError(err); } inline static void NanThrowTypeError(const char* errmsg) { _NAN_THROW_ERROR(v8::Exception::TypeError, errmsg); } inline static void NanThrowRangeError(const char* errmsg) { _NAN_THROW_ERROR(v8::Exception::RangeError, errmsg); } template static inline void NanDispose(v8::Persistent &handle) { handle.Dispose(nan_isolate); handle.Clear(); } static inline v8::Local NanNewBufferHandle ( char *data, size_t length, node::smalloc::FreeCallback callback, void *hint) { return node::Buffer::New(data, length, callback, hint); } static inline v8::Local NanNewBufferHandle ( char *data, uint32_t size) { return node::Buffer::New(data, size); } static inline v8::Local NanNewBufferHandle (uint32_t size) { return node::Buffer::New(size); } static inline v8::Local NanBufferUse(char* data, uint32_t size) { return node::Buffer::Use(data, size); } template inline v8::Local NanPersistentToLocal( const v8::Persistent& persistent) { if (persistent.IsWeak()) { return v8::Local::New(nan_isolate, persistent); } else { return *reinterpret_cast*>( const_cast*>(&persistent)); } } inline bool NanHasInstance( v8::Persistent& function_template , v8::Handle value) { return NanPersistentToLocal(function_template)->HasInstance(value); } static inline v8::Local NanNewContextHandle( v8::ExtensionConfiguration* extensions = NULL, v8::Handle tmpl = v8::Handle(), v8::Handle obj = v8::Handle()) { return v8::Local::New(nan_isolate, v8::Context::New( nan_isolate, extensions, tmpl, obj)); } #else // Node 0.8 and 0.10 # define _NAN_METHOD_ARGS const v8::Arguments& args # define NAN_METHOD(name) v8::Handle name(_NAN_METHOD_ARGS) # define _NAN_GETTER_ARGS const v8::AccessorInfo &args # define NAN_GETTER(name) \ v8::Handle name(v8::Local property, _NAN_GETTER_ARGS) # define _NAN_SETTER_ARGS const v8::AccessorInfo &args # define NAN_SETTER(name) \ void name( \ v8::Local property \ , v8::Local value \ , _NAN_SETTER_ARGS) # define _NAN_PROPERTY_GETTER_ARGS const v8::AccessorInfo& args # define NAN_PROPERTY_GETTER(name) \ v8::Handle name(v8::Local property \ , _NAN_PROPERTY_GETTER_ARGS) # define _NAN_PROPERTY_SETTER_ARGS const v8::AccessorInfo& args # define NAN_PROPERTY_SETTER(name) \ v8::Handle name(v8::Local property \ , v8::Local value \ , _NAN_PROPERTY_SETTER_ARGS) # define _NAN_PROPERTY_ENUMERATOR_ARGS const v8::AccessorInfo& args # define NAN_PROPERTY_ENUMERATOR(name) \ v8::Handle name(_NAN_PROPERTY_ENUMERATOR_ARGS) # define _NAN_PROPERTY_DELETER_ARGS const v8::AccessorInfo& args # define NAN_PROPERTY_DELETER(name) \ v8::Handle name( \ v8::Local property \ , _NAN_PROPERTY_DELETER_ARGS) # define _NAN_PROPERTY_QUERY_ARGS const v8::AccessorInfo& args # define NAN_PROPERTY_QUERY(name) \ v8::Handle name( \ v8::Local property \ , _NAN_PROPERTY_QUERY_ARGS) # define NanGetInternalFieldPointer(object, index) \ object->GetPointerFromInternalField(index) # define NanSetInternalFieldPointer(object, index, value) \ object->SetPointerInInternalField(index, value) # define NAN_WEAK_CALLBACK(type, name) void name( \ v8::Persistent object, \ void *data) # define NAN_WEAK_CALLBACK_OBJECT object # define NAN_WEAK_CALLBACK_DATA(type) ((type) data) # define NanScope() v8::HandleScope scope # define NanLocker() v8::Locker locker # define NanUnlocker() v8::Unlocker unlocker # define NanReturnValue(value) return scope.Close(value) # define NanReturnUndefined() return v8::Undefined() # define NanReturnNull() return v8::Null() # define NanReturnEmptyString() return v8::String::Empty() # define NanInitPersistent(type, name, obj) \ v8::Persistent name = v8::Persistent::New(obj) # define NanAssignPersistent(type, handle, obj) \ handle = v8::Persistent::New(obj) # define NanObjectWrapHandle(obj) obj->handle_ # define NanMakeWeak(handle, parameters, callback) \ handle.MakeWeak(parameters, callback) # define _NAN_THROW_ERROR(fun, errmsg) \ do { \ NanScope(); \ return v8::ThrowException(fun(v8::String::New(errmsg))); \ } while (0); inline static v8::Handle NanThrowError(const char* errmsg) { _NAN_THROW_ERROR(v8::Exception::Error, errmsg); } inline static v8::Handle NanThrowError( v8::Handle error) { NanScope(); return v8::ThrowException(error); } inline static v8::Handle NanThrowError( const char *msg, const int errorNumber) { v8::Local err = v8::Exception::Error(v8::String::New(msg)); v8::Local obj = err.As(); obj->Set(v8::String::New("code"), v8::Int32::New(errorNumber)); return NanThrowError(err); } inline static v8::Handle NanThrowTypeError(const char* errmsg) { _NAN_THROW_ERROR(v8::Exception::TypeError, errmsg); } inline static v8::Handle NanThrowRangeError(const char* errmsg) { _NAN_THROW_ERROR(v8::Exception::RangeError, errmsg); } template static inline void NanDispose(v8::Persistent &handle) { handle.Dispose(); handle.Clear(); } static inline v8::Local NanNewBufferHandle ( char *data, size_t length, node::Buffer::free_callback callback, void *hint) { return v8::Local::New( node::Buffer::New(data, length, callback, hint)->handle_); } static inline v8::Local NanNewBufferHandle ( char *data, uint32_t size) { return v8::Local::New(node::Buffer::New(data, size)->handle_); } static inline v8::Local NanNewBufferHandle (uint32_t size) { return v8::Local::New(node::Buffer::New(size)->handle_); } static inline void FreeData(char *data, void *hint) { delete[] data; } static inline v8::Local NanBufferUse(char* data, uint32_t size) { return v8::Local::New( node::Buffer::New(data, size, FreeData, NULL)->handle_); } template inline v8::Local NanPersistentToLocal( const v8::Persistent& persistent) { if (persistent.IsWeak()) { return v8::Local::New(persistent); } else { return *reinterpret_cast*>( const_cast*>(&persistent)); } } inline bool NanHasInstance( v8::Persistent& function_template , v8::Handle value) { return function_template->HasInstance(value); } static inline v8::Local NanNewContextHandle( v8::ExtensionConfiguration* extensions = NULL , v8::Handle tmpl = v8::Handle() , v8::Handle obj = v8::Handle() ) { v8::Persistent ctx = v8::Context::New(extensions, tmpl, obj); v8::Local lctx = v8::Local::New(ctx); ctx.Dispose(); return lctx; } #endif // node version class NanCallback { public: NanCallback(const v8::Local &fn) { NanScope(); v8::Local obj = v8::Object::New(); obj->Set(NanSymbol("callback"), fn); NanAssignPersistent(v8::Object, handle, obj); } ~NanCallback() { if (handle.IsEmpty()) return; handle.Dispose(); handle.Clear(); } inline v8::Local GetFunction () { return NanPersistentToLocal(handle)->Get(NanSymbol("callback")) .As(); } // deprecated void Run(int argc, v8::Local argv[]) { Call(argc, argv); } void Call(int argc, v8::Local argv[]) { NanScope(); v8::Local callback = NanPersistentToLocal(handle)-> Get(NanSymbol("callback")).As(); v8::TryCatch try_catch; callback->Call(v8::Context::GetCurrent()->Global(), argc, argv); if (try_catch.HasCaught()) { node::FatalException(try_catch); } } private: v8::Persistent handle; }; /* abstract */ class NanAsyncWorker { public: NanAsyncWorker (NanCallback *callback) : callback(callback) { request.data = this; errmsg = NULL; } virtual ~NanAsyncWorker () { NanScope(); if (!persistentHandle.IsEmpty()) NanDispose(persistentHandle); if (callback) delete callback; if (errmsg) delete errmsg; } virtual void WorkComplete () { NanScope(); if (errmsg == NULL) HandleOKCallback(); else HandleErrorCallback(); delete callback; callback = NULL; } virtual void Execute () =0; uv_work_t request; protected: v8::Persistent persistentHandle; NanCallback *callback; const char *errmsg; void SavePersistent(const char *key, v8::Local &obj) { NanScope(); v8::Local handle = NanPersistentToLocal(persistentHandle); handle->Set(NanSymbol(key), obj); } v8::Local GetFromPersistent(const char *key) { NanScope(); v8::Local handle = NanPersistentToLocal(persistentHandle); return handle->Get(NanSymbol(key)).As(); } virtual void HandleOKCallback () { NanScope(); callback->Call(0, NULL); }; virtual void HandleErrorCallback () { NanScope(); v8::Local argv[] = { v8::Exception::Error(v8::String::New(errmsg)) }; callback->Call(1, argv); } }; inline void NanAsyncExecute (uv_work_t* req) { NanAsyncWorker *worker = static_cast(req->data); worker->Execute(); } inline void NanAsyncExecuteComplete (uv_work_t* req) { NanAsyncWorker* worker = static_cast(req->data); worker->WorkComplete(); delete worker; } inline void NanAsyncQueueWorker (NanAsyncWorker* worker) { uv_queue_work( uv_default_loop() , &worker->request , NanAsyncExecute , (uv_after_work_cb)NanAsyncExecuteComplete ); } //// Base 64 //// #define _nan_base64_encoded_size(size) ((size + 2 - ((size + 2) % 3)) / 3 * 4) // Doesn't check for padding at the end. Can be 1-2 bytes over. static inline size_t _nan_base64_decoded_size_fast(size_t size) { size_t remainder = size % 4; size = (size / 4) * 3; if (remainder) { if (size == 0 && remainder == 1) { // special case: 1-byte input cannot be decoded size = 0; } else { // non-padded input, add 1 or 2 extra bytes size += 1 + (remainder == 3); } } return size; } template static size_t _nan_base64_decoded_size(const TypeName* src, size_t size) { if (size == 0) return 0; if (src[size - 1] == '=') size--; if (size > 0 && src[size - 1] == '=') size--; return _nan_base64_decoded_size_fast(size); } // supports regular and URL-safe base64 static const int _nan_unbase64_table[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; #define _nan_unbase64(x) _nan_unbase64_table[(uint8_t)(x)] template static size_t _nan_base64_decode(char* buf, size_t len, const TypeName* src, const size_t srcLen) { char a, b, c, d; char* dst = buf; char* dstEnd = buf + len; const TypeName* srcEnd = src + srcLen; while (src < srcEnd && dst < dstEnd) { int remaining = srcEnd - src; while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--; if (remaining == 0 || *src == '=') break; a = _nan_unbase64(*src++); while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--; if (remaining <= 1 || *src == '=') break; b = _nan_unbase64(*src++); *dst++ = (a << 2) | ((b & 0x30) >> 4); if (dst == dstEnd) break; while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--; if (remaining <= 2 || *src == '=') break; c = _nan_unbase64(*src++); *dst++ = ((b & 0x0F) << 4) | ((c & 0x3C) >> 2); if (dst == dstEnd) break; while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--; if (remaining <= 3 || *src == '=') break; d = _nan_unbase64(*src++); *dst++ = ((c & 0x03) << 6) | (d & 0x3F); } return dst - buf; } //// HEX //// template unsigned _nan_hex2bin(TypeName c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'A' && c <= 'F') return 10 + (c - 'A'); if (c >= 'a' && c <= 'f') return 10 + (c - 'a'); return static_cast(-1); } template static size_t _nan_hex_decode(char* buf, size_t len, const TypeName* src, const size_t srcLen) { size_t i; for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) { unsigned a = _nan_hex2bin(src[i * 2 + 0]); unsigned b = _nan_hex2bin(src[i * 2 + 1]); if (!~a || !~b) return i; buf[i] = a * 16 + b; } return i; } static bool _NanGetExternalParts( v8::Handle val , const char** data , size_t* len) { if (node::Buffer::HasInstance(val)) { *data = node::Buffer::Data(val.As()); *len = node::Buffer::Length(val.As()); return true; } assert(val->IsString()); v8::Local str = v8::Local::New(val.As()); if (str->IsExternalAscii()) { const v8::String::ExternalAsciiStringResource* ext; ext = str->GetExternalAsciiStringResource(); *data = ext->data(); *len = ext->length(); return true; } else if (str->IsExternal()) { const v8::String::ExternalStringResource* ext; ext = str->GetExternalStringResource(); *data = reinterpret_cast(ext->data()); *len = ext->length(); return true; } return false; } namespace Nan { enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; } static inline char* NanFromV8String( v8::Handle from , enum Nan::Encoding encoding = Nan::UTF8 , size_t *datalen = NULL , char *buf = NULL , size_t buflen = 0 , int flags = v8::String::NO_NULL_TERMINATION | v8::String::HINT_MANY_WRITES_EXPECTED) { NanScope(); size_t sz_; size_t term_len = !(flags & v8::String::NO_NULL_TERMINATION); char *data = NULL; size_t len; bool is_extern = _NanGetExternalParts( from , const_cast(&data) , &len); if (is_extern && !term_len) { NanSetPointerSafe(datalen, len); return data; } v8::Local toStr = from->ToString(); char *to = buf; v8::String::AsciiValue value(toStr); switch(encoding) { case Nan::ASCII: #if NODE_MODULE_VERSION < 0x0C sz_ = toStr->Length(); if (to == NULL) { to = new char[sz_ + term_len]; } else { assert(buflen >= sz_ + term_len && "too small buffer"); } NanSetPointerSafe( datalen , toStr->WriteAscii(to, 0, sz_ + term_len, flags)); return to; #endif case Nan::BINARY: case Nan::BUFFER: sz_ = toStr->Length(); if (to == NULL) { to = new char[sz_ + term_len]; } else { assert(buflen >= sz_ + term_len && "too small buffer"); } #if NODE_MODULE_VERSION < 0x0C // TODO(isaacs): THIS IS AWFUL!!! // AGREE(kkoopa) { uint16_t* twobytebuf = new uint16_t[sz_ + term_len]; size_t len = toStr->Write(twobytebuf, 0, sz_ + term_len, flags); for (size_t i = 0; i < sz_ + term_len && i < len + term_len; i++) { unsigned char *b = reinterpret_cast(&twobytebuf[i]); to[i] = *b; } NanSetPointerSafe(datalen, len); delete[] twobytebuf; return to; } #else NanSetPointerSafe( datalen, toStr->WriteOneByte( reinterpret_cast(to) , 0 , sz_ + term_len , flags)); return to; #endif case Nan::UTF8: sz_ = toStr->Utf8Length(); if (to == NULL) { to = new char[sz_ + term_len]; } else { assert(buflen >= sz_ + term_len && "too small buffer"); } NanSetPointerSafe( datalen , toStr->WriteUtf8(to, sz_ + term_len, NULL, flags) - term_len); return to; case Nan::BASE64: sz_ = _nan_base64_decoded_size(*value, toStr->Length()); if (to == NULL) { to = new char[sz_ + term_len]; } else { assert(buflen >= sz_ + term_len); } NanSetPointerSafe( datalen , _nan_base64_decode(to, sz_, *value, value.length())); if (term_len) { to[sz_] = '\0'; } return to; case Nan::UCS2: { sz_ = toStr->Length(); if (to == NULL) { to = new char[(sz_ + term_len) * 2]; } else { assert(buflen >= (sz_ + term_len) * 2 && "too small buffer"); } int bc = 2 * toStr->Write( reinterpret_cast(to) , 0 , sz_ + term_len , flags); NanSetPointerSafe(datalen, bc); return to; } case Nan::HEX: sz_ = toStr->Length(); assert(!(sz_ & 1) && "bad hex data"); if (to == NULL) { to = new char[sz_ / 2 + term_len]; } else { assert(buflen >= sz_ / 2 + term_len && "too small buffer"); } NanSetPointerSafe( datalen , _nan_hex_decode(to, sz_ / 2, *value, value.length())); if (term_len) { to[sz_ / 2] = '\0'; } return to; default: assert(0 && "unknown encoding"); } return to; } #endif nan-0.3.2/package.json000066400000000000000000000007521221011264600145550ustar00rootroot00000000000000{ "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 (https://github.com/rvagg)", "Benjamin Byholm (https://github.com/kkoopa/)", "Trevor Norris (https://github.com/trevnorris)" ], "license": "MIT" }nan-0.3.2/test/000077500000000000000000000000001221011264600132425ustar00rootroot00000000000000nan-0.3.2/test/.jshintrc000066400000000000000000000021721221011264600150710ustar00rootroot00000000000000{ "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 }nan-0.3.2/test/binding.gyp000066400000000000000000000016661221011264600154060ustar00rootroot00000000000000{"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" ] } ]} nan-0.3.2/test/cpp/000077500000000000000000000000001221011264600140245ustar00rootroot00000000000000nan-0.3.2/test/cpp/asyncworker.cpp000066400000000000000000000013071221011264600171000ustar00rootroot00000000000000#include #include #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()); NanAsyncQueueWorker(new SleepWorker(callback, args[0]->Uint32Value())); NanReturnUndefined(); } void Init (v8::Handle exports) { exports->Set(NanSymbol("a"), v8::FunctionTemplate::New(Sleep)->GetFunction()); } NODE_MODULE(asyncworker, Init) nan-0.3.2/test/cpp/fromv8string.cpp000066400000000000000000000016071221011264600172040ustar00rootroot00000000000000#include #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(), enc, &bc, NULL, 0, flags); if (enc == Nan::UCS2) { NanReturnValue(v8::String::New(reinterpret_cast(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 target) { target->Set( NanSymbol("r") , v8::FunctionTemplate::New(ReturnString)->GetFunction() ); } NODE_MODULE(fromv8string, Init) nan-0.3.2/test/cpp/multifile1.cpp000066400000000000000000000003641221011264600166060ustar00rootroot00000000000000#include #include "../../nan.h" #include "multifile2.h" void Init (v8::Handle target) { target->Set( NanSymbol("r") , v8::FunctionTemplate::New(ReturnString)->GetFunction() ); } NODE_MODULE(multifile, Init) nan-0.3.2/test/cpp/multifile2.cpp000066400000000000000000000002441221011264600166040ustar00rootroot00000000000000#include #include "../../nan.h" NAN_METHOD(ReturnString) { NanScope(); NanReturnValue(v8::String::New(NanFromV8String(args[0].As()))); } nan-0.3.2/test/cpp/multifile2.h000066400000000000000000000001041221011264600162440ustar00rootroot00000000000000#include #include "../../nan.h" NAN_METHOD(ReturnString); nan-0.3.2/test/cpp/optionvalues.cpp000066400000000000000000000024151221011264600172620ustar00rootroot00000000000000#include #include "../../nan.h" NAN_METHOD(OptionValues) { NanScope(); v8::Local inobj = args[0].As(); v8::Local 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 target) { target->Set( NanSymbol("o") , v8::FunctionTemplate::New(OptionValues)->GetFunction() ); } NODE_MODULE(optionvalues, Init)nan-0.3.2/test/cpp/returnemptystring.cpp000066400000000000000000000004651221011264600203620ustar00rootroot00000000000000#include #include "../../nan.h" NAN_METHOD(ReturnEmptyString) { NanScope(); NanReturnEmptyString(); } void Init (v8::Handle target) { target->Set( NanSymbol("r") , v8::FunctionTemplate::New(ReturnEmptyString)->GetFunction() ); } NODE_MODULE(returnemptystring, Init) nan-0.3.2/test/cpp/returnnull.cpp000066400000000000000000000004311221011264600167400ustar00rootroot00000000000000#include #include "../../nan.h" NAN_METHOD(ReturnNull) { NanScope(); NanReturnNull(); } void Init (v8::Handle target) { target->Set( NanSymbol("r") , v8::FunctionTemplate::New(ReturnNull)->GetFunction() ); } NODE_MODULE(returnnull, Init) nan-0.3.2/test/cpp/returnundefined.cpp000066400000000000000000000004551221011264600177350ustar00rootroot00000000000000#include #include "../../nan.h" NAN_METHOD(ReturnUndefined) { NanScope(); NanReturnUndefined(); } void Init (v8::Handle target) { target->Set( NanSymbol("r") , v8::FunctionTemplate::New(ReturnUndefined)->GetFunction() ); } NODE_MODULE(returnundefined, Init) nan-0.3.2/test/cpp/returnvalue.cpp000066400000000000000000000006011221011264600171010ustar00rootroot00000000000000#include #include "../../nan.h" NAN_METHOD(ReturnValue) { NanScope(); if (args.Length() == 1) { NanReturnValue(args[0]); } else { NanReturnValue(v8::String::New("default")); } } void Init (v8::Handle target) { target->Set( NanSymbol("r") , v8::FunctionTemplate::New(ReturnValue)->GetFunction() ); } NODE_MODULE(returnvalue, Init) nan-0.3.2/test/cpp/symbols.cpp000066400000000000000000000002641221011264600162220ustar00rootroot00000000000000#include #include "../../nan.h" void Init (v8::Handle target) { target->Set(NanSymbol("key"), v8::String::New("a property")); } NODE_MODULE(symbols, Init) nan-0.3.2/test/js/000077500000000000000000000000001221011264600136565ustar00rootroot00000000000000nan-0.3.2/test/js/asyncworker-test.js000066400000000000000000000006761221011264600175510ustar00rootroot00000000000000const 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() }) })nan-0.3.2/test/js/fromv8string-test.js000066400000000000000000000033131221011264600176410ustar00rootroot00000000000000const 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'); }); nan-0.3.2/test/js/multifile-test.js000066400000000000000000000006401221011264600171630ustar00rootroot00000000000000const 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'); }); nan-0.3.2/test/js/optionvalues-test.js000066400000000000000000000011161221011264600177200ustar00rootroot00000000000000const 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); }); nan-0.3.2/test/js/returnemptystring-test.js000066400000000000000000000004611221011264600210170ustar00rootroot00000000000000const 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(), ''); }); nan-0.3.2/test/js/returnnull-test.js000066400000000000000000000004311221011264600174010ustar00rootroot00000000000000const test = require('tap').test , bindings = require('bindings'); test('returnnull', function (t) { t.plan(3); t.type(bindings('returnnull').r, 'function'); t.equal(bindings('returnnull').r('a string value'), null); t.equal(bindings('returnnull').r(), null); }); nan-0.3.2/test/js/returnundefined-test.js000066400000000000000000000004671221011264600204010ustar00rootroot00000000000000const test = require('tap').test , bindings = require('bindings'); test('returnundefined', function (t) { t.plan(3); t.type(bindings('returnundefined').r, 'function'); t.equal(bindings('returnundefined').r('a string value'), undefined); t.equal(bindings('returnundefined').r(), undefined); }); nan-0.3.2/test/js/returnvalue-test.js000066400000000000000000000004561221011264600175520ustar00rootroot00000000000000const test = require('tap').test , bindings = require('bindings'); test('returnvalue', function (t) { t.plan(3); t.type(bindings('returnvalue').r, 'function'); t.equal(bindings('returnvalue').r('a string value'), 'a string value'); t.equal(bindings('returnvalue').r(), 'default'); }); nan-0.3.2/test/js/symbols-test.js000066400000000000000000000003331221011264600166600ustar00rootroot00000000000000const test = require('tap').test , bindings = require('bindings'); test('symbols', function (t) { t.plan(2); t.type(bindings('symbols').key, 'string'); t.equal(bindings('symbols').key, 'a property'); }); nan-0.3.2/test/package.json000066400000000000000000000004141221011264600155270ustar00rootroot00000000000000{ "name": "nan-tests", "version": "0.0.0", "description": "", "private": true, "gypfile": true, "scripts": { "test": "node-gyp build; tap js/*-test.js" }, "dependencies": { "tap": "~0.4.3", "bindings": "~1.1.1", "xtend": "~2.0.6" } }