C++ and Node.js Integration

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Handbook for creating Node.js C++ addons.

Author(s): Scott Frees, PhD
Year: 2016

Language: English
Tags: Cplusplus and nodejs, c++ and nodejs

About this book
Who is this book for?
About the author
Acknowledgments
Chapter 1 - Introduction
Why build a C++ addon?
What is a Node.js Module?
What is a Node.js Addon?
Node.js and the V8 Engine
Hello World - Returning data from C++ to JavaScript
From the bottom up - registering the module
The Isolate object, Locals, and FunctionCallbackInfo
Building Addons with node-gyp
Pre-requisites for building
Configuring the build
Calling your Addon from JavaScript
Passing Parameters from JavaScript to C++
Inspecting the arguments before using them
Calling JavaScript functions from C++
Calling callbacks with parameters
Take-aways and what's coming next…
Chapter 2 - Understanding the V8 API
JavaScript memory allocation
Isolates
Contexts
JavaScript variables, C++ Handles
HandleScope
V8 Data Types
Primitives - Strings, Numbers, and Booleans
Objects
Arrays
Other data types
Passing Local Handles and using Escapable Handles
Persistent Handles
Chapter 3 - Basic Integration Patterns
Node Versions
Integration Pattern: Data transferring
Rainfall Data Example
Organizing the source code
Rainfall C++ data models
Thinking about JavaScript
Creating the Rainfall Addon
Building the C++ addon
Mapping JavaScript objects to C++ classes
Completed Node.js File - Average Rainfall
Returning Objects - a collection of statistics
Building the JavaScript object and returning it
Calling from JavaScript
Receiving and Returning Arrays of Rainfall Data
Registering the callable addon function
Extracting the Array in C++
Building an Array to return back from C++
About efficiency
Chapter 4 - Asynchronous Addons
V8 Function API
Synchronous addons with a Callback
Moving to Asynchronous with Worker Threads
Asynchronous Memory Headaches
The C++ addon code
The worker thread
When the worker completes…
Understanding V8 Memory and Worker Threads
Handles - Local and Persistent
Bring on the worker threads!
Understanding V8 Locker objects
What about Unlocker?
Why typical async addons work
Workarounds and Compromises
Chapter 5 - Object Wrapping
Example: Polynomial Class
Adding methods
Adding properties (accessor/mutators)
Completed Wrapped Polynomial class
Wrapping existing objects
Wrapped Objects as Arguments
Chapter 6 - Native Abstractions for Node (NAN)
Basic Functionality
Build setup
Creating functions
Working with Primitives
Maybe types and ToLocalChecked
Working with Strings
Working with Objects
Working with Arrays
Callbacks and Asynchronous Patterns
Sending Progress Updates from async addons
ObjectWrap
Nan - conclusions
Chapter 7 - Streaming between Node and C++
Emitting events from C++
Building the C++ addon
Building the JS adapter
Generalizing Event-based Addons
Emitting JSON from C++
Streaming C++ output
Stopping a streaming addon
Emitting events to C++
Streaming input to C++
Summary
Chapter 8 - Publishing Addons
Review of node-gyp basics
Publishing to npm
Distributing addons that use NAN
Distributing addons that use C++11
C++11 example
Building on Linux
Building on OS X
Building on Windows
Including multiple C++ files
Pre-compiled Addons
Node-gyp Alternative: cmake-js
Appendix A - Alternatives to Addons
C++ and Node.js - your options
How to choose
Option 1 - Automation
Option 2 - Shared Library / DLL
Option 3 - Node.js Addon
A running example - Prime Numbers
Getting Started - a simple Node.js Web app
Automating a C++ program from a Node.js Web app
Prime Sieve C/C++ implementation
The Node.js Child Process API
Scenario 1: C++ Program with input from command-line arguments
Scenario 2: C++ Program that gets input from user (stdin)
Scenario 3: Automating a file-based C++ program
Calling Native C++ from Node.js as a DLL
Preparing the C++ as a Shared Library
Building the Shared Library with gyp
Calling primelib with FFI
Putting it all together
Building an Asynchronous C++ Addon for Node.js using Nan
Addon Code - blocking
Addon Code - non-blocking
Putting it all together…
Appendix B - Buffers
Example: PNG and BMP Image Processing
Setting up the addon
Passing buffers to an addon
Returning buffers from addon
Buffers as a solution to the worker thread copying problem
Closing