node.js-addon

How can you call an emitter callback from separate c++ thread in an addon?

不羁岁月 提交于 2020-01-04 05:08:09
问题 For context, I started with this question. I need to call the callback for the emitter in another thread. I made a minimal example but it segfaults on emit.Call({cb, result}); My first instinct is that I have a problem with the lifetimes of env or the emit function. addon.cpp #include <napi.h> #include <iostream> #include <thread> #include <memory> #include <functional> #include <chrono> std::shared_ptr<std::thread> thread; bool running = true; void generate(Napi::Env& env, Napi::Function&

V8 Multithreaded function

烈酒焚心 提交于 2019-12-29 07:16:07
问题 I'm writing a Node plugin and I'm having problems trying to call a V8 function object from a C++ worker thread. My plugin basically starts a C++ std::thread and enters a wait loop using WaitForSingleOject(), this is triggered by a different C++ app (An X-Plane plugin) writing to a bit of shared memory. I'm trying to get my Node plugin to wake up when the Windows shared event is signaled then call a JavaScript function that I've registered from the node app, which will in turn pass the data

use libuv function in node.js 0.12.x

青春壹個敷衍的年華 提交于 2019-12-23 20:30:20
问题 I have written a node.js's c++ addon,i can be complied successfully under node.js 0.10.x. But when migrate it to 0.12.x, it failed when several error,such as error C2065: “uv_work_t”:undeclared identifier .I wonder whether i can touch libuv's api in 0.12.x? The code is showed as follow: #include <node.h> #include <string> #include <v8.h> #ifdef WINDOWS_SPECIFIC_DEFINE #include <windows.h> typedef DWORD ThreadId; #else #include <unistd.h> #include <pthread.h> typedef unsigned int ThreadId;

Typescript declarations file for Node C++ Addon

蓝咒 提交于 2019-12-23 09:30:04
问题 I have a Node C++ Addon that provides a wrapped class similar to the one in the Node documentation. I can require() my addon and then get the constructor for my class to create an instance. const { MyClass } = require('myaddon'); const obj = new MyClass('data'); Now I want to use TypeScript to do the same. I can't find the right combination of .d.ts file and import statement to make this work. I guess ideally I'd like to declare my class is in the module and has a constructor that takes a

Invoke javascript callback repeatedly from C++ module in node.js

元气小坏坏 提交于 2019-12-23 02:29:35
问题 So I am writing a node.js module in C++ which processes data from a camera. I want to invoke a callback function in my app.js file whenever new data is available. What I have at the moment Right now I have in my node javascript file ( app.js ) the following code. Every second it calls a function in my C++-Module and returns the number of frames that have been processed so far: setInterval(function () { var counter = MyCPPModule.NumberOfFrames(); console.log(counter); }, 1000); In my C++ file

Node.js and C/C++ integration: how to properly implement callbacks?

懵懂的女人 提交于 2019-12-22 11:27:20
问题 I am trying to implement a C++ extension to be integrated with node.js. This extension will internally invoke some blocking calls, so it needs to provide a non-blocking interface to the node.js world. As specified in https://nodejs.org/api/addons.html, there are two ways to implement non-blocking callbacks: a) By using a simple callback to a JavaScript function. So my extension would have to spawn a thread and return immediately, and let that thread call the blocking code and then invoke the

Node Buffer to char array

泄露秘密 提交于 2019-12-20 03:52:44
问题 I have a native NodeJS addon that accepts a Buffer instance as one of it's arguments. I'm able to convert a char array into a Buffer with the following code, but looking for the other way around. static v8::Local<v8::Object> create_buffer(char *data, unsigned long length) { node::Buffer *slow_buffer = node::Buffer::New(length); memcpy(node::Buffer::Data(slow_buffer), data, length); v8::Handle<v8::Value> constructor_arguments[3] = { slow_buffer->handle_, v8::Integer::New(length), v8::Integer:

Node C++ Addon - How do I access a Typed Array (Float32Array) when it's beenn passed as an argument?

ぐ巨炮叔叔 提交于 2019-12-18 06:51:30
问题 I'd like to make use of the V8 Float32Array data structure. How can I initialise it? I'd also be interested in direct memory access to the data. How could that be done? 回答1: Updated The best way now is to use the helper Nan::TypedArrayContents. assert(args[i]->IsFloat32Array()); Local<Float32Array> myarr = args[i].As<Float32Array>(); Nan::TypedArrayContents<float> dest(myarr); // Now use dest, e.g. (*dest)[0] There's a good example of this in node-canvas. Original Answer, which shows why the

how to deliver c++ array to node.js using v8 native addon

陌路散爱 提交于 2019-12-12 13:48:57
问题 I implemented a c++ addon module that creates some buffer using unsigned char(uint8_t) memory and delivers it to node.js. ---- c++ addon.cpp ---- void get_frame_buffer(const FunctionCallbackInfo<Value>& args){ Isolate* isolate = helper.get_current_isolate(); if (!args[0]->IsUint32()&& !args[1]->IsUint32()) { isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Wrong arguments"))); return; } int width = args[0]->Uint32Value(); int height = args[1]->Uint32Value(); uint8_t

Streaming data into a Node.js C++ Addon with N-API

老子叫甜甜 提交于 2019-12-12 11:22:03
问题 I am building a C++ addon for NodeJS and I want to stream data asynchronously from C++ to Node. I have found this article, https://nodeaddons.com/streaming-data-into-a-node-js-c-addon/, however; I want to use the N-API instead of NAN. I have been searching through the NodeJS docs and examples as well as looking for other resources and examples but have not come across a resource to show me how I can accomplish this. This is my first time writing a C++ addon for NodeJS. An example that would