npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nan-marshal

v0.0.5

Published

Data type marshalling for NAN module

Downloads

96

Readme

Data type marshalling for C++ addons in Nodejs

Build Status

This project contains some helper utilities that make native addon development a bit more easier.

Example

NAN_METHOD(GetFirstNPrimes) {
    
    int numberOfPrimes = Nan::Marshal<int>(info[0]);
    std::vector<int> primes = computeNPrimes(numberOfPrimes);
    info.GetReturnValue().Set(Nan::Marshal(primes));
}

Ok, enough words, gimme the code! All source code is available on GitHub: nan-marshal.

Requirements

By tradition, native add-ons for Node are built with GYP build system. So you should install node-gyp package: npm install -g node-gyp. This module requires Nan package. If you are not using Nan already for writing C++ add-ons for Nodejs I strongly advise you to start doing that. Anyway, npm install --save nan is a right way to start.

Usage

Simply add nan-marshal as a dependency module to package.json of your Node add-on:

$ npm install --save nan-marshal

Add include directories for NAN and NAN-Marshal in your binding.gyp so that you can use #include <nan-marshal.h> in your .cpp files:

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

This works like a -I<path-to-nan-marshal> when compiling your add-on.

API

There is a single all-purpose function: Nan::Marshal. To convert from V8 object to C++ type, use it as follows: Nan::Marshal<Dst>(V8 object). To convert from C++ to V8 object: Nan::Marshal(..).

Nan::Marshal supports following types out of the box:

  • Built-in C++ types
  • std::string
  • std::vector
  • std::map
  • std::shared_ptr
  • Marshalling of used-defined types (There are intrusive and non-intrusive options available)

For built-in and STL types, use is straightforward:

Local<Value> arg1 = info[0];
// Marshal from V8 to C++ type
std::string msg = Nan::Marshal<std::string>(arg1);

// Marshal from C++ to V8
info.GetReturnValue().Set(Nan::Marshal(msg));

User-defined serialization inspired by boost::serialization approach and you will find it similar and easy-to-use. Here's quick example of non-intrusive serialization of the OpenCV data type:

namespace Nan
{
    namespace marshal
    {
        template<typename T>
        struct Serializer < cv::Rect_<T> >
        {
            template<typename InputArchive>
            static inline void load(InputArchive& ar, cv::Rect_<T>& val)
            {
                ar & make_nvp("x", val.x);
                ar & make_nvp("y", val.y);
                ar & make_nvp("width", val.width);
                ar & make_nvp("height", val.height);
            }

            template<typename OutputArchive>
            static inline void save(OutputArchive& ar, const cv::Rect_<T>& val)
            {
                ar & make_nvp("x", val.x);
                ar & make_nvp("y", val.y);
                ar & make_nvp("width", val.width);
                ar & make_nvp("height", val.height);
            }
        };
    }
}

Having a snippet above in your code lets you to return JavaScript object like { x:12, y:13, width:124, height: 144 } from C++ code. The same is true for V8 -> C++ marshalling. Nan::Marshal will convert V8 object to desired object type.

Tests

To run the tests do:

npm install
npm run-script rebuild-tests
npm test

Or just:

npm install
npm test

Limitations

This library does not perform strict checking of V8 types during conversion. There is nan-check module that serves this purpose.

Licence & copyright

Copyright (c) 2015 Ievgen Khvedchenia.

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.