Git Product home page Git Product logo

nan's Introduction

Native Abstractions for Node.js

A header file filled with macro and utility goodness for making add-on development for Node.js easier across versions 0.8, 0.10, 0.12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 and 21.

Current version: 2.19.0

(See CHANGELOG.md for complete ChangeLog)

NPM NPM

Build Status Build status

Thanks to the crazy changes in V8 (and some in Node core), keeping native addons compiling happily across versions, particularly 0.10 to 0.12 to 4.0, 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.

News & Updates

Usage

Simply add NAN as a dependency in the package.json of your Node addon:

$ npm install --save nan

Pull in the path to NAN in your binding.gyp so that you can use #include <nan.h> in your .cpp files:

"include_dirs" : [
    "<!(node -e \"require('nan')\")"
]

This works like a -I<path-to-NAN> when compiling your addon.

Example

Just getting started with Nan? Take a look at the Node Add-on Examples.

Refer to a quick-start Nan Boilerplate for a ready-to-go project that utilizes basic Nan functionality.

For a simpler example, see the async pi estimation example 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.

Yet another example is nan-example-eol. It shows newline detection implemented as a native addon.

Also take a look at our comprehensive C++ test suite which has a plethora of code snippets for your pasting pleasure.

API

Additional to the NAN documentation below, please consult:

JavaScript-accessible methods

A template is a blueprint for JavaScript functions and objects in a context. You can use a template to wrap C++ functions and data structures within JavaScript objects so that they can be manipulated from JavaScript. See the V8 Embedders Guide section on Templates for further information.

In order to expose functionality to JavaScript via a template, you must provide it to V8 in a form that it understands. Across the versions of V8 supported by NAN, JavaScript-accessible method signatures vary widely, NAN fully abstracts method declaration and provides you with an interface that is similar to the most recent V8 API but is backward-compatible with older versions that still use the now-deceased v8::Argument type.

  • Method argument types
  • Method declarations
  • Method and template helpers

Scopes

A local handle is a pointer to an object. All V8 objects are accessed using handles, they are necessary because of the way the V8 garbage collector works.

A handle scope can be thought of as a container for any number of handles. When you've finished with your handles, instead of deleting each one individually you can simply delete their scope.

The creation of HandleScope objects is different across the supported versions of V8. Therefore, NAN provides its own implementations that can be used safely across these.

Also see the V8 Embedders Guide section on Handles and Garbage Collection.

Persistent references

An object reference that is independent of any HandleScope is a persistent reference. Where a Local handle only lives as long as the HandleScope in which it was allocated, a Persistent handle remains valid until it is explicitly disposed.

Due to the evolution of the V8 API, it is necessary for NAN to provide a wrapper implementation of the Persistent classes to supply compatibility across the V8 versions supported.

Also see the V8 Embedders Guide section on Handles and Garbage Collection.

New

NAN provides a Nan::New() helper for the creation of new JavaScript objects in a way that's compatible across the supported versions of V8.

Converters

NAN contains functions that convert v8::Values to other v8::Value types and native types. Since type conversion is not guaranteed to succeed, they return Nan::Maybe types. These converters can be used in place of value->ToX() and value->XValue() (where X is one of the types, e.g. Boolean) in a way that provides a consistent interface across V8 versions. Newer versions of V8 use the new v8::Maybe and v8::MaybeLocal types for these conversions, older versions don't have this functionality so it is provided by NAN.

Maybe Types

The Nan::MaybeLocal and Nan::Maybe types are monads that encapsulate v8::Local handles that may be empty.

Script

NAN provides v8::Script helpers as the API has changed over the supported versions of V8.

JSON

The JSON object provides the C++ versions of the methods offered by the JSON object in javascript. V8 exposes these methods via the v8::JSON object.

Refer to the V8 JSON object in the V8 documentation for more information about these methods and their arguments.

Errors

NAN includes helpers for creating, throwing and catching Errors as much of this functionality varies across the supported versions of V8 and must be abstracted.

Note that an Error object is simply a specialized form of v8::Value.

Also consult the V8 Embedders Guide section on Exceptions for more information.

Buffers

NAN's node::Buffer helpers exist as the API has changed across supported Node versions. Use these methods to ensure compatibility.

Nan::Callback

Nan::Callback makes it easier to use v8::Function handles as callbacks. A class that wraps a v8::Function handle, protecting it from garbage collection and making it particularly useful for storage and use across asynchronous execution.

Asynchronous work helpers

Nan::AsyncWorker, Nan::AsyncProgressWorker and Nan::AsyncProgressQueueWorker are helper classes that make working with asynchronous code easier.

Strings & Bytes

Miscellaneous string & byte encoding and decoding functionality provided for compatibility across supported versions of V8 and Node. Implemented by NAN to ensure that all encoding types are supported, even for older versions of Node where they are missing.

Object Wrappers

The ObjectWrap class can be used to make wrapped C++ objects and a factory of wrapped objects.

V8 internals

The hooks to access V8 internals—including GC and statistics—are different across the supported versions of V8, therefore NAN provides its own hooks that call the appropriate V8 methods.

Miscellaneous V8 Helpers

Miscellaneous Node Helpers

Tests

To run the NAN tests do:

npm install
npm run-script rebuild-tests
npm test

Or just:

npm install
make test

Known issues

Compiling against Node.js 0.12 on OSX

With new enough compilers available on OSX, the versions of V8 headers corresponding to Node.js 0.12 do not compile anymore. The error looks something like:

❯   CXX(target) Release/obj.target/accessors/cpp/accessors.o
In file included from ../cpp/accessors.cpp:9:
In file included from ../../nan.h:51:
In file included from /Users/ofrobots/.node-gyp/0.12.18/include/node/node.h:61:
/Users/ofrobots/.node-gyp/0.12.18/include/node/v8.h:5800:54: error: 'CreateHandle' is a protected member of 'v8::HandleScope'
  return Handle<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
                                        ~~~~~~~~~~~~~^~~~~~~~~~~~

This can be worked around by patching your local versions of v8.h corresponding to Node 0.12 to make v8::Handle a friend of v8::HandleScope. Since neither Node.js not V8 support this release line anymore this patch cannot be released by either project in an official release.

For this reason, we do not test against Node.js 0.12 on OSX in this project's CI. If you need to support that configuration, you will need to either get an older compiler, or apply a source patch to the version of V8 headers as a workaround.

Governance & Contributing

NAN is governed by the Node.js Addon API Working Group

Addon API Working Group (WG)

The NAN project is jointly governed by a Working Group which is responsible for high-level guidance of the project.

Members of the WG are also known as Collaborators, there is no distinction between the two, unlike other Node.js projects.

The WG has final authority over this project including:

  • Technical direction
  • Project governance and process (including this policy)
  • Contribution policy
  • GitHub repository hosting
  • Maintaining the list of additional Collaborators

For the current list of WG members, see the project README.md.

Individuals making significant and valuable contributions are made members of the WG and given commit-access to the project. These individuals are identified by the WG and their addition to the WG is discussed via GitHub and requires unanimous consensus amongst those WG members participating in the discussion with a quorum of 50% of WG members required for acceptance of the vote.

Note: If you make a significant contribution and are not considered for commit-access log an issue or contact a WG member directly.

For the current list of WG members / Collaborators, see the project README.md.

Consensus Seeking Process

The WG follows a Consensus Seeking decision making model.

Modifications of the contents of the NAN repository are made on a collaborative basis. Anybody with a GitHub account may propose a modification via pull request and it will be considered by the WG. All pull requests must be reviewed and accepted by a WG member with sufficient expertise who is able to take full responsibility for the change. In the case of pull requests proposed by an existing WG member, an additional WG member is required for sign-off. Consensus should be sought if additional WG members participate and there is disagreement around a particular modification.

If a change proposal cannot reach a consensus, a WG member can call for a vote amongst the members of the WG. Simple majority wins.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

  • (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or

  • (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or

  • (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

  • (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved.

WG Members / Collaborators

Rod VaggGitHub/rvaggTwitter/@rvagg
Benjamin ByholmGitHub/kkoopa-
Trevor NorrisGitHub/trevnorrisTwitter/@trevnorris
Nathan RajlichGitHub/TooTallNateTwitter/@TooTallNate
Brett LawsonGitHub/brett19Twitter/@brett19x
Ben NoordhuisGitHub/bnoordhuisTwitter/@bnoordhuis
David SiegelGitHub/agnatTwitter/@agnat
Michael Ira KrufkyGitHub/mkrufkyTwitter/@mkrufky

Licence & copyright

Copyright (c) 2018 NAN WG Members / Collaborators (listed above).

Native Abstractions for Node.js is licensed under an MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.

nan's People

Contributors

0xflotus avatar 3p3r avatar addaleax avatar agnat avatar bnoordhuis avatar brett19 avatar caasi avatar codebytere avatar flarna avatar gagern avatar jjrv avatar jsdevel avatar kkoopa avatar linusu avatar marshallofsound avatar mathiask88 avatar mkrufky avatar mmomtchev avatar mscdex avatar nataliewolfe avatar ofrobots avatar rpetrich avatar rush avatar rvagg avatar striezel avatar sunoru avatar thefourtheye avatar tootallnate avatar vertedinde avatar westy92 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nan's Issues

compatible profiler api

I reviewed changes of v8 and node today, and found many changes at v8-profiler.h between with 0.10 and 0.11. Then did we need a bundle of functions to do the compatible works? If yes, I prefer to do this ASAP, if not, I'm so sad to hear that, and do this in library-land.

:)

libuv changes

There have been some changes to libuv as well. uv_idle_cb and others have had the status argument removed. Might be other breaking changes as well. Should we address this somehow?

getting following compile errors

../node_modules/nan/nan.h: In function `v8::Local<v8::Object> NanNewBufferHandle(char*, size_t, void (*)(char*, void*), void*)':
../node_modules/nan/nan.h:1320: error: call of overloaded `NanNew(v8::Persistent<v8::Object>&)' is ambiguous
../node_modules/nan/nan.h:923: note: candidates are: v8::Local<T> NanNew(v8::Handle<T>) [with T = v8::Object]
../node_modules/nan/nan.h:944: note:                 v8::Local<T> NanNew(const v8::Persistent<S>&) [with T = v8::Object]
../node_modules/nan/nan.h:949: note:                 v8::Local<T> NanNew(P) [with T = v8::Object, P = v8::Persistent<v8::Object>]
../node_modules/nan/nan.h: In function `v8::Local<v8::Object> NanNewBufferHandle(const char*, uint32_t)':
../node_modules/nan/nan.h:1328: error: call of overloaded `NanNew(v8::Persistent<v8::Object>&)' is ambiguous
../node_modules/nan/nan.h:923: note: candidates are: v8::Local<T> NanNew(v8::Handle<T>) [with T = v8::Object]
../node_modules/nan/nan.h:944: note:                 v8::Local<T> NanNew(const v8::Persistent<S>&) [with T = v8::Object]
../node_modules/nan/nan.h:949: note:                 v8::Local<T> NanNew(P) [with T = v8::Object, P = v8::Persistent<v8::Object>]
../node_modules/nan/nan.h: In function `v8::Local<v8::Object> NanNewBufferHandle(uint32_t)':
../node_modules/nan/nan.h:1336: error: call of overloaded `NanNew(v8::Persistent<v8::Object>&)' is ambiguous
../node_modules/nan/nan.h:923: note: candidates are: v8::Local<T> NanNew(v8::Handle<T>) [with T = v8::Object]
../node_modules/nan/nan.h:944: note:                 v8::Local<T> NanNew(const v8::Persistent<S>&) [with T = v8::Object]
../node_modules/nan/nan.h:949: note:                 v8::Local<T> NanNew(P) [with T = v8::Object, P = v8::Persistent<v8::Object>]
../node_modules/nan/nan.h: In function `v8::Local<v8::Object> NanBufferUse(char*, uint32_t)':
../node_modules/nan/nan.h:1348: error: call of overloaded `NanNew(v8::Persistent<v8::Object>&)' is ambiguous
../node_modules/nan/nan.h:923: note: candidates are: v8::Local<T> NanNew(v8::Handle<T>) [with T = v8::Object]
../node_modules/nan/nan.h:944: note:                 v8::Local<T> NanNew(const v8::Persistent<S>&) [with T = v8::Object]
../node_modules/nan/nan.h:949: note:                 v8::Local<T> NanNew(P) [with T = v8::Object, P = v8::Persistent<v8::Object>]
../node_modules/nan/nan.h: In function `v8::Local<v8::Context> NanNewContextHandle(v8::ExtensionConfiguration*, v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Value>)':
../node_modules/nan/nan.h:1364: error: call of overloaded `NanNew(v8::Persistent<v8::Context>&)' is ambiguous
../node_modules/nan/nan.h:923: note: candidates are: v8::Local<T> NanNew(v8::Handle<T>) [with T = v8::Context]
../node_modules/nan/nan.h:944: note:                 v8::Local<T> NanNew(const v8::Persistent<S>&) [with T = v8::Context]
../node_modules/nan/nan.h:949: note:                 v8::Local<T> NanNew(P) [with T = v8::Context, P = v8::Persistent<v8::Context>]
../node_modules/nan/nan.h: In function `bool _NanGetExternalParts(v8::Handle<v8::Value>, const char**, size_t*)':
../node_modules/nan/nan.h:1726: error: call of overloaded `NanNew(v8::Handle<v8::String>)' is ambiguous
../node_modules/nan/nan.h:923: note: candidates are: v8::Local<T> NanNew(v8::Handle<T>) [with T = v8::String]
../node_modules/nan/nan.h:949: note:                 v8::Local<T> NanNew(P) [with T = v8::String, P = v8::Handle<v8::String>]

Correct documentation of NanUInt32OptionValue

In README.md, NanUInt32OptionValue is documented as:

uint32_t NanUInt32OptionValue(v8::Handle<v8::Value>, v8::Handle<v8::String>[, uint32_t])

Similar to NanBooleanOptionValue, use NanUInt32OptionValue to fetch an integer option from your options object. Requires all 3 arguments as a default is not optional.

As the default is not optional, shouldn't it be documented as
uint32_t NanUInt32OptionValue(v8::Handle<v8::Value>, v8::Handle<v8::String>, uint32_t)?

travis build matrix

FYI I've updated the travis config and now have a built matrix for various versions of Node. Currently the latest 0.8, the last 5 versions of 0.5 and 0.11.4, 0.11.5 and 0.11.6. unfortunately 0.11.7 and 0.11.8 come with a consistently buggy npm that causes the builds to fail so I've removed them for now. Perhaps 0.11.9 will sort that out. We'll have to manually add new versions in as they are released.

Contributing testable example

I would like to contribute an example addon to nan, maybe more complex than adding numbers and possibly useful in other circumstances (if it was faster or v8 was slower:P)

Please review my code:
https://github.com/CodeCharmLtd/nan-example-eol
I am aware of its deficiencies (too complex code in places) but it is testable and may be useful for testing regressions in nan/v8/node. Just do node-gyp rebuild && npm test.

I initially wrote it out of curiosity, whether it would be faster than carrier and well .. after feature-parity it is the same speed! Totally not worth it but still ... uses Nan's buffers, and other stuff. Could be also extended to doing stuff in threads etc.

perf hit from NanFromV8String

Two things are going to kick you:

  1. Always expecting the string to be utf8 is going to kill performance. I might allow users to pass enum encoding in node.h to allow a specific encoding output.
  2. If the string is external one or two byte then all you'll need is a memcpy.

A lot of this is properly addressed in node::StringBytes::Encode. But since that doesn't exist pre v0.11, maybe just use it for a reference.

Question: `NanPersistentToLocal()->NewInstance()` is not exists?

../src/response.cc:99:71: error: no member named 'NewInstance' in
      'v8::FunctionTemplate'; did you mean 'HasInstance'?
  ...= NanPersistentToLocal(responseTemplate)->NewInstance();
                                               ^~~~~~~~~~~
                                               HasInstance

Hi, I'm using nan to improve libetpan.node to v0.11, but I get this error at this function: https://github.com/dinhviethoa/libetpan.node/blob/master/src/response.cc#L65-L86, I don't know clearly what is the culprit of this problem :(

/cc @rvagg

Successive calls to NanFromV8String.

Hi,

I'm using nan and it's great :)

But today, I got stuck a couple of hours, before understanding that it came from NanFromV8String.

When calling twice in a row NanFromV8String with a second string shorter than the first one, the second string gets appended with old characters from the first one, as if it uses the same buffer, without null terminating the string correctly.

For example:

char* str = NanFromV8String(String::New("hello"));
printf("%s ", str);
delete[] str;
str = NanFromV8String(String::New("hell"));
printf("%s ", str);
delete[] str;

Gives:

hello hello

However, by passing explicit parameters, this works fine:

char* str = NanFromV8String(v8Str, Nan::UTF8, NULL, NULL, 0, v8::String::HINT_MANY_WRITES_EXPECTED);
printf("%s ", str);
delete[] str;
str = NanFromV8String(v8Str, Nan::UTF8, NULL, NULL, 0, v8::String::HINT_MANY_WRITES_EXPECTED);
printf("%s ", str);
delete[] str;

If this is not the default behavior, it should be I guess, or at least be specified in the docs to avoid missunderstanding.
If there is something I do wrong, please also tell me.

Thanks!

Can we remove "type" arguments?

This API really bothers me:

# define NAN_WEAK_CALLBACK(type, name) \
void name( \
v8::Isolate* isolate \
, v8::Persistent<v8::Object>* object \
, type data)
# define NAN_WEAK_CALLBACK_OBJECT (*object)
# define NAN_WEAK_CALLBACK_DATA(type) ((type) data)

...

# define NanAssignPersistent(type, handle, obj) handle.Reset(nan_isolate, obj)
# define NanInitPersistent(type, name, obj) \
v8::Persistent<type> name(nan_isolate, obj)

Supplying a "type" argument is a style-smell. As far as I'm aware there isn't a clever way to make a macro accept a generic type parameter so perhaps these things should be turned into proper functions? Of the template<class T> static NAN_INLINE(...) variety.

Thoughts? It'd mean API breakage but it feels odd whenever I have to put the type as an argument like that.

How to create an External with NaN

Basically I'm trying to figure out how to avoid the below here.

#if (NODE_MODULE_VERSION > 0x000B) /* node 0.11 */
  instance->SetInternalField(0, External::New(v8::Isolate::GetCurrent(), this));
#else
  instance->SetInternalField(0, External::New(this));
#endif

As you can see External::New takes an Isolate* for node 0.11 but not before.
I couldn't find anywhere how nan allows me to wrap this.

If it's not there yet, but would be good to have I'd be happy to PR to add something like NanExternalNew() somehow.

How about NanSetPrototypeTemplate()?

Hi,
first, this project is awesome! I'm a hobbyist programmer, writing a node addon. What do you think about a NanSetPrototypeTemplate() helper function?

  NAN_INLINE void NanSetPrototypeTemplate(
      v8::Handle<v8::Template> templ
    , const char *name
    , v8::Handle<v8::Data> value) {
    templ->PrototypeTemplate()->Set(name, value);
  }

compile warning on osx for _nan_base64_decoded_size

Just thought I'd fire up an osx vm and try that node-snappy problem and came across this warning:

In file included from ../src/binding.cc:21:
In file included from ../src/./binding.h:26:
../src/nan.h:706:46: warning: variable 'sz_' is uninitialized when used here
      [-Wuninitialized]
      sz_ = _nan_base64_decoded_size(*value, sz_);
                                             ^~~
../src/nan.h:673:13: note: initialize the variable 'sz_' to silence this warning
  size_t sz_;
            ^
             = 0
1 warning generated.

Should we `delete errmsg` in `NanAsyncWorker`?

errmsg is defined as const char* here, yet we call delete on it here. That's okay, but I think that in most cases errmsg is set like errmsg = "Not found", in which case trying to free it causes:

*** Error in `node': free(): invalid pointer: 0x00007f4b1e2dccc7 ***

If this shouldn't be deleted, mmalecki/nan@30c9c66 is a fix.

Using existing buffer without automatically deleting backing data

Hi,

I'm currently writing bindings for a C++ library which has a class that I want to expose to JS land, and this class has a char* buffer that I would also like to expose.

This is a problem currently because if the user does something like this:

var object = new MyPrimitiveObject();
var buffer = object.getBuffer();
buffer = null;

Then this will delete data that is owned by an object that is still active.

Would it make sense to provide a version of NanBufferUse that does not automatically free the backing data when the weak callback is called?

Document new NanFromV8String arguments

@kkoopa would you mind adding documentation for the new NanFromV8String() arguments please? I'm pretty sure you have a much better handle on what's going on there than I. Also, short docs for NanGetPointerSafe() and NanSetPointerSafe() would be nice too.

I'm tidying up 0.2.0 and will merge to master in a moment for a "release", so just do it straight in master when that's done.

V8 3.24

The upgrade to V8 3.24 just landed in joyent/node (good) and that means nan needs to upgrade as well (bad). The main changes:

  • Persistent<T>::MakeWeak() is replaced with Persistent<T>::SetWeak() (and the callback takes a single argument.)
  • Array::New(), FunctionTemplate::New(), Object::New() take an isolate as their first argument.
  • Integer::New(value, isolate) is now Integer::New(isolate, value) (arguments have been swapped)
  • String::New(s) is gone, replaced with String::NewFromUtf8Value(isolate, s).
  • Probably some other things I'm forgetting right now.

Thoughts on how to upgrade? Do we want to keep on supporting older node.js v0.11 (a.k.a. V8 3.22) versions?

How to convert a V8 buffer to a char?

Hi,

I am looking at passing a V8 Buffer args[1].As<v8::Object>() to a char cannot seem to find an easy way to do so.

I am fairly new to working with v8 and libuv so I assume that this would be a valid use case.

I would appreciate any insight into this,
Thanks

NanDispose() -> NanDisposePersistent()

The inconsistency here bugs me a little, what do you guys think about deprecating NanDispose() in favour of NanDisposePersistent()? It can stay in the code for a while but marked NAN_DEPRECATED.

Very good (but still disgusting)

I like what you are doing here. Unfortunately there is still a huge amount of cruft one has to write manually to do simple things. Do you think there is a will to have some kind of high level wrapper over all those APIs to do common tasks like exposing native objects?
I have been going from leveldown codebase and I am amazed you are able to even maintain it because there is more v8 bindings then actual code. :) I was thinking one could do much better with wrapping around those APIs, particularly utilizing C++11. Did you have any thoughts in this area?

bufferworkerpersistent.js test naming

Is it on purpose, that bufferworkerpersistent.js test is excluded from testing? Due to tap --gc js/*-test.js in package.json the suffix -test is missing.

Symbol not found with NanCallback

Hi,

I'm trying to use nan for sophia binary addon. I use NanCallback with async workers and have been running into the following crash:

dyld: lazy symbol binding failed: Symbol not found: __ZN6sophia3SetEPvPKvmS2_mP11NanCallback
  Referenced from: /Users/maciej/dev/js/sophia/build/Debug/sophia.node
  Expected in: dynamic lookup

dyld: Symbol not found: __ZN6sophia3SetEPvPKvmS2_mP11NanCallback
  Referenced from: /Users/maciej/dev/js/sophia/build/Debug/sophia.node
  Expected in: dynamic lookup

Trace/BPT trap: 5

Piece of code which (I think) is responsible for that:

    sophia::Set(
      wrap->db,
      key,
      strlen(key),
      value,
      strlen(value),
      new NanCallback(callback)
    );
    NanReturnUndefined();

Which is weird, since I use the exact same invocation of NanCallback somewhere else in the same file and it works.

Full code is here, the piece of code in question is here.

Way to reproduce (works on node 0.10):

git clone https://github.com/mmalecki/node-sophia.git
cd node-sophia
git checkout put
npm i
node test/simple-test.js

please tag 0.3.2

Please tag release 0.3.2 (if it is already released). I am currently preparing this project as a Debian package and for proper upstream versioning in Debian I rely on Git tags of upstream projects pulled from Github.

Thanks!
Mike (aka [email protected])

NanEscapeScope with Handle?

I had such code that was previously working okay:

Handle<Object> obj; = NanNew<Object>();
return obj;

Now I wanted to transform it to the new NanEscapeScope as advised by @kkoopa:

NanEscapableScope();
Handle<Object> obj; = NanNew<Object>();
return NanEscapeScope(obj);

but this triggers an errors because NanEscapeScope takes only Local handles.

I had to do:

NanEscapableScope();
Local<Object> obj; = NanNew<Object>();
return NanEscapeScope(obj);

Shouldn't NanEscapeScope take Handle? It will automatically work for Local as well.

create async loops

I want to execute the NanAsyncWorker again after HandleOKCallback, to do a async loop.
For example to read from a device ;-)

How would you recommend to do that? Sorry I'm not really good in C++... :-(

PS. thanks for nan, it's cool...

doxygen of v8

Hi,

I generate the v8 documentation locally. Would there be a place where nodejs and other projects could link to v8 documentation online for discussion sake?

nan.h error on node v0.10.22

This is error was found in module geoip, the original issue can be found in here.

Quoted from original issue

We are experiencing a new error (very infrequently) since upgrading to node v0.10.22. The following message is displayed and the process exits immediately.

node: /opt/pre-publish/star/node_modules/orion/node_modules/geoip/node_modules/nan/nan.h:809: bool _NanGetExternalParts(v8::Handle<v8::Value>, const char**, size_t*): Assertion `val->IsString()' failed.
Aborted

I suspect it may have something to do with the infamous WalMart memory leak on closed handles fix.

Release 1.2.0

I think we have enough for a new release. It ought to be 1.2.0 due to the addition of NanSetPrototypeTemplate. Nothing else added, just fixes and improvements.

Debian renamed node for some reason

Apparently Debian decided it was a good idea to rename node to nodejs. This causes problems with the current include-voodoo.

"include_dirs" : [
    "<!(node -e \"require('nan')\")"
]

serialport/node-serialport#301

Based on crypto-utils/keygrip#7, it seems a possible fix is something like

"[ -x /usr/bin/nodejs ] && /usr/bin/nodejs ./install.js || node ./install.js"

But, I suspect that will not work with Windows. So, how to make a cross-platform solution?

Node 0.11.12 Missing Symbols

I assume these missing symbols are due to upstream still releasing broken builds.

/home/kkoopa/dev/node-v0.11.12-linux-x64/bin/node: symbol lookup error: /home/kkoopa/dev/nan/test/build/Release/asyncworker.node: undefined symbol: _ZN4node12MakeCallbackEN2v86HandleINS0_6ObjectEEENS1_INS0_8FunctionEEEiPNS1_INS0_5ValueEEE

not ok js/asyncworker-test.js ........................... 0/1
    Command: "/home/kkoopa/dev/node-v0.11.12-linux-x64/bin/node --expose-gc asyncworker-test.js"
    TAP version 13
    not ok 1 js/asyncworker-test.js
      ---
        exit:    127
        stderr:  /home/kkoopa/dev/node-v0.11.12-linux-x64/bin/node: symbol lookup error: /home/kkoopa/dev/nan/test/build/Release/asyncworker.node: undefined symbol: _ZN4node12MakeCallbackEN2v86HandleINS0_6ObjectEEENS1_INS0_8FunctionEEEiPNS1_INS0_5ValueEEE
        command: "/home/kkoopa/dev/node-v0.11.12-linux-x64/bin/node --expose-gc asyncworker-test.js"
      ...

    1..1
    # tests 1
    # fail  1

Semi-Automatic Porting of Existing Code to NAN

For some months, I've been thinking of writing a small tool to do semi-automatic porting of existing code to NAN, but I have not yet found the time or motivation. I have, however, constructed a set of regular expression substitutions, which I use myself when porting code, among with a good text editor. These substitutions deal with most of the common cases that can be done without much or any manual intervention.

The regular expressions need to be evaluated in the order provided and some of them (the scope.Close() thing) may produce false positives and are better done interactively. The observant reader notices that some of the expressions contain explicit parameter names, like the NAN_METHOD substitution. This is intentional, as NAN assumes standard argument names, and safe renaming of parameters may require contextual knowledge.

(v8\:\:)?Handle<(v8\:\:)?Value>\s*(\S+)\s*\(\s*const\s+(v8\:\:)?Arguments\s*&\s*args\s*\)
NAN_METHOD(\3)

(v8\:\:)?Handle<(v8\:\:)?Value>\s*(\S+)\s*\(\s*(v8\:\:)?Local<(v8\:\:)?String>\s*property\s*,\s*const\s*(v8\:\:)?AccessorInfo\s*&\s*info\s*\)
NAN_GETTER(\3)

void\s*(\S+)\s*\(\s*(v8\:\:)?Local<(v8\:\:)?String>\s*property\s*,\s*(v8\:\:)?Local<(v8\:\:)?Value>\s*value\s*,\s*const\s*(v8\:\:)?AccessorInfo\s*&\s*info\s*\)
NAN_SETTER(\1)

(v8\:\:)?HandleScope\s+scope
NanScope()

(v8\:\:)?String\:\:NewSymbol
NanNew

(v8\:\:)?Null\(\)
NanNull()

(v8\:\:)?Undefined\(\)
NanUndefined()

(v8\:\:)?True\(\)
NanTrue()

(v8\:\:)?False\(\)
NanFalse()

return\s+(v8\:\:)?ThrowException\s*\(\s*(v8\:\:)?Exception::Error\s*\(\s*(v8\:\:)?String::New\s*\((.*)\)\)\)
return NanThrowError(\4)

return\s+(v8\:\:)?ThrowException\s*\(\s*(v8\:\:)?Exception::TypeError\s*\(\s*(v8\:\:)?String::New\s*\((.*)\)\)\)
return NanThrowTypeError(\4)

return\s+(v8\:\:)?ThrowException\s*\(\s*(v8\:\:)?Exception::RangeError\s*\(\s*(v8\:\:)?String::New\s*\((.*)\)\)\)
return NanThrowRangeError(\4)

return\s+scope.Close\((v8\:\:)?ThrowException\((v8\:\:)?String::New\((.*)\)\)\)
return NanThrowError(\3)

return scope.Close
NanReturnValue

(\S+)\.Dispose\(\)
NanDisposePersistent(\1)

(\S+)\s*=\s*(v8\:\:)?Persistent<(v8\:\:)?(\S+)>::New\((.+)\)
NanAssignPersistent(\3\4, \1, \5)

(v8\:\:)?Local\s*<\s*(\S*)>\s*\:\:\s*New\s*\(
NanNew<\2>(

(v8\:\:)?(\S+)\:\:\s*New\s*\(
NanNew<\2>(

_NanWeakCallbackInfo<T, P>::Callback prototype mismatch

#include "nan.h"
#include "v8.h"

NAN_WEAK_CALLBACK(WeakCallback) {
}

void Test() {
  NanMakeWeakPersistent(NanNew<v8::Object>(), "", WeakCallback);
}

Compiles okay with node.js master but fails with v0.10:

$ g++-4.2 -Wall -Wextra -Wno-unused-parameter -I$PWD -I../v0.10/src -I../v0.10/deps/uv/include -I../v0.10/deps/v8/include -c tmp/t.cc
tmp/t.cc: In function ‘void Test()’:
tmp/t.cc:8: error: no matches converting function ‘WeakCallback’ to type ‘void (*)(struct v8::Persistent<v8::Value>, void*)’
tmp/t.cc:4: error: candidates are: template<class T, class P> void WeakCallback(v8::Persistent<v8::Value>, void*)

$ g++-4.8 -Wall -Wextra -Wno-unused-parameter -I$PWD -I../v0.10/src -I../v0.10/deps/uv/include -I../v0.10/deps/v8/include -c tmp/t.cc
tmp/t.cc: In function 'void Test()':
tmp/t.cc:8:63: error: no matches converting function 'WeakCallback' to type '_NanWeakCallbackInfo<v8::Object, const char>::Callback {aka void (*)(class v8::Persistent<v8::Value>, void*)}'
   NanMakeWeakPersistent(NanNew<v8::Object>(), "", WeakCallback);
                                                               ^
In file included from tmp/t.cc:1:0:
tmp/t.cc:4:19: note: candidate is: template<class T, class P> void WeakCallback(v8::Persistent<v8::Value>, void*)
 NAN_WEAK_CALLBACK(WeakCallback) {
                   ^
/Users/bnoordhuis/src/nan/nan.h:1393:17: note: in definition of macro 'NAN_WEAK_CALLBACK'
     static void name(                                                          \

Affects 1.0.0 and master. I suggest doing a 1.0.1 point release for this.

On a related note, opinions on making _Nan_Weak_Callback_ ## name static in 1.0.x, like it is in master? You currently can't use NAN_WEAK_CALLBACK() inside a class.

Node 0.11.11 errors

0.11.11 was just released so I added it to .dntrc and ran the tests ... then this ... I don't even ...

WHAAAA??

I don't have time to even begin to understand what's going on with this or whether it's even a NAN problem.

The change to using MakeCallback breaks Node 0.6

I know it's not officially supported, but everything seemed to work nicely previously. I've figured out that MakeCallback used to be a whole lot different in 0.6. Can we add something to rectify this deviation, at least until 0.12 is released?

void MakeCallback(Handle<Object> object,
                  const char* method,
                  int argc,
                  Handle<Value> argv[]) {
  HandleScope scope;

  Local<Value> callback_v = object->Get(String::New(method));
  if (!callback_v->IsFunction()) {
    fprintf(stderr, "method = %s", method);
  }
  assert(callback_v->IsFunction());
  Local<Function> callback = Local<Function>::Cast(callback_v);

  // TODO Hook for long stack traces to be made here.

  TryCatch try_catch;

  callback->Call(object, argc, argv);

  if (try_catch.HasCaught()) {
    FatalException(try_catch);
  }
}

Release 1.1

Assuming everything works consistently, it is time to release.

Object.observe doesn't call NAN_METHOD functions

I'm not sure if this is a NAN issue or something in v8, but I have the following problem on node v0.11.9.

I am defining a method through NAN as follows (pretty standard):

#include <node.h>
#include "nan.h"

NAN_METHOD(my_function)
{
  printf("Ran my function.\n");
}

void Init(Handle<Object> exports)
{
  exports->Set(NanSymbol("my_function"),
               FunctionTemplate::New(my_function)->GetFunction());
}

NODE_MODULE(my_module, Init)

Now, if I load this module and use it as the callback to Object.observe, I get the following somewhat weird behavior:

var my_module = require('my_module');
console.log(my_module.my_function); // Prints "[Function]", as expected
my_module.my_function(); // Prints "Ran my function."

// Create a dummy object
var foo = {}

// Let's hook up an observer to my object
Object.observe(foo, my_module.my_function);
foo.bar = true; // change object
// Nothing happens! my_function isn't called, and no exception is thrown

// Ok, let's just wrap the same function in an anonymous function
Object.observe(foo, function(changes) { my_module.my_function(changes); } );
foo.baz = true; // change object
// Prints "Ran my function."

Other than Object.observe, anything else I can think to do with my_function works normally. Any ideas what this could be?

Right way to assign Local from Handle

This fails for me on Nan 1.0.0 and Node 0.10 (it does not cause problems on Node 0.11)
Could you give me any tip?

(...)
v8::Local<v8::Function> fun = funTpl->GetFunction();
(...)
fun = NanMakeCallback(fun, bind, 1, argv).As<v8::Function>();
error: no match for ‘operator=’ (operand types are ‘v8::Local<v8::Function>’ and ‘v8::Handle<v8::Function>’)
       fun = NanMakeCallback(fun, bind, 1, argv).As<v8::Function>();

add converters "to" and "from" V8

Kind of related to #29, as NanFromV8String is the only converter. This could be helpful to have converters from and to V8 for primitives in both worlds.

Taking example from: https://github.com/rectalogic/v8-webgl/blob/420977691cb07efd956146443d61f23f5b5dcf53/src/converters.h, it could use of templates.

For the string case, this could be for c-strings:

char* str = NanFromV8<char*>(v8Str);
// opposite
v8::Handle<v8::Value> v8Str = NanToV8(str);

and this for std::string:

std::string = NanFromV8<std::string>(v8Str);
// opposite
v8::Handle<v8::Value> v8Str = NanToV8(str);

Templates would be available for all useful primitives: char, int, uint8_t, uint64_t, ...

Would you be interested in a pull request for that?

feature: add NanCallback::IsEmpty()

It's not really possible right now to find out if a callback has been assigned. Suggestion: add a bool NanCallback::IsEmpty() const method

I say 'not really' because while in theory you can do nan_callback->GetFunction()->IsFunction(), that will assert in debug builds because of the cast from Undefined to Function.

cross-compatibility for tests

asyncworker.cpp, asyncworkererror.cpp, bufferworkerpersistent.cpp are currently not cross-compatible due to usleep() function. Nothing important I think, but maybe this can be fixed for the next release.

Escaping NanScope without value

This seems to work for me:

    uv_async_init(uv_default_loop(), &async_handle, [](uv_async_t* handle) {
      NanScope();
      realCallback(); // call things calling node.js
      uv_close((uv_handle_t*)&async_handle, NULL);
    });

but this function should not return anything and I see no other way to close the scope without value. Can I simply leave it up to HandleScope's destructor?

NanMakeWeakPersistent elaboration

I am looking at example of NanMakeWeakPersistent but I fail to see the exact purpose... why and when would one need it? I have made a lot of progress in node-cbind but I yet to touch asynchronous stuff - maybe this is the use case?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.