Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why no v8 code in AsyncWork #12

Open
dwdwdw opened this issue May 19, 2015 · 1 comment
Open

why no v8 code in AsyncWork #12

dwdwdw opened this issue May 19, 2015 · 1 comment

Comments

@dwdwdw
Copy link

dwdwdw commented May 19, 2015

I have some work to do in AsyncWork, like transforming c struct into v8::Object. So I have to write v8 code in AsyncWork.
How can I manage this?

@Talbot3
Copy link

Talbot3 commented Jul 1, 2016

just to use callback function,like this:
// addon.cc
#include <node.h>

namespace demo {

using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Null;
using v8::Object;
using v8::String;
using v8::Value;

void RunCallback(const FunctionCallbackInfo& args) {
Isolate* isolate = args.GetIsolate();
Local cb = Local::Cast(args[0]);
const unsigned argc = 1;
Local argv[argc] = { String::NewFromUtf8(isolate, "hello world") };
cb->Call(Null(isolate), argc, argv);
}

void Init(Local exports, Local module) {
NODE_SET_METHOD(module, "exports", RunCallback);
}

NODE_MODULE(addon, Init)

} // namespace demo
Note that this example uses a two-argument form of Init() that receives the full module object as the second argument. This allows the Addon to completely overwrite exports with a single function instead of adding the function as a property of exports.

To test it, run the following JavaScript:

// test.js
const addon = require('./build/Release/addon');

addon((msg) => {
console.log(msg); // 'hello world'
});
Note that, in this example, the callback function is invoked synchronously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants