Node. js Addons translation (C/C ++ extension), node. jsaddons

Source: Internet
Author: User

Node. js Addons translation (C/C ++ extension), node. jsaddons

PS: Upgrade Node 6.2.1 first, and the Node upgrade command npm install-g n; n stable. NOde. js extension is a dynamic link library written through C/C ++, and through Node. the js function require () function is loaded, just like using a common Node. js module. It provides interfaces between Node and C/C ++ libraries.

In this way, if a method or function is implemented through Node extension, it becomes quite complex and involves several modules and interfaces:

• V8: a javascript. V8 implemented through the C ++ Library provides the object creation mechanism and callback function. Most of the V8API documentation is in the v8.h header file. Click my v8 online document

• Libuv: a c library that implements the event loop of Node. js working threads and platforms that are out-of-the-box. It also acts as a cross-platform abstract library that allows you to easily access many common tasks in all mainstream operating system systems using POSIX-like, for example, interaction with file systems, sockets, timers, and system events. Libuv also provides an abstract pthreads-like thread for more complex Asynchronization. Node. js C/C ++ extensions must go beyond standard event loops. The plug-in authors encourage them to think about how to avoid blocking the I/O Event loop and perform task-intensive work through libuv non-blocking system operations, working threads, and custom threads.

• Node. js built-in Library: Node. js uses a large number of C/C ++ extended APIs. The most important class node: ObjectWrap during C/C ++ Extension

• Many static link libraries of Node. js, such as OpenSSL: other libraries of Node. js, are stored in the deps directory under the source code directory. For more information, see Node. js's own dependencies for additional information. Click my Node. js official extension library example. This may be the starting point for you to compile a C/C ++ extension library for Node. js. Only V8 and OpenSSL classes are frequently used in Node C/C ++ extensions.

Node C/C ++ expansion first-latest example Hello World

In this example, the Node. js version is V5.0 or later.

// hello.jsconst addon = require('./build/Release/addon');console.log(addon.hello()); // 'world'// hello.cc#include <node.h>#include <v8.h>namespace demo {using v8::FunctionCallbackInfo;using v8::Isolate;using v8::Local;using v8::Object;using v8::String;using v8::Value;void Method(const FunctionCallbackInfo<Value>& args) {Isolate* isolate = args.GetIsolate();args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));}void init(Local<Object> exports) {NODE_SET_METHOD(exports, "hello", Method);}NODE_MODULE(addon, init)} // namespace demo // binding.gyp{"targets": [{"target_name": "addon","sources": [ "hello.cc" ]}]} 

Node-gyp command

Copy codeThe Code is as follows:
Node-gyp configure build

The above is a small part of the Node. js Addons translation (C/C ++ extension) related knowledge, hope to help you, if you have any questions, please leave a message, xiaobian will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.