Node-addon-api: Porting FunctionTemplate to N-API

Created on 1 Aug 2017  路  10Comments  路  Source: nodejs/node-addon-api

How do I port FunctionTemplate to N-API?

v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);

This is the best I could do be

Napi::FunctionReference tpl = Napi::FunctionReference(env, New);

But it still did not work.

Most helpful comment

Ok, then yeah, I think a conversion guide is definitely needed :-) It appears the case is covered, it's just not immediately clear how. Great work and thank you for the patience as we feel our way around

All 10 comments

Templates are a V8-specific concept. There is no equivalent in N-API. Just create a Napi::Function directly.

Napi::FunctionReference is equivalent to a v8::Persistent reference to a v8::Function.

Templates are a V8-specific concept. There is no equivalent in N-API. Just create a Napi::Function directly.

That is a very common pattern everywhere in native module authoring. It's definitely not clear how to port something like https://github.com/mcollina/native-hdr-histogram/blob/master/hdr_histogram_wrap.cc#L12-L28.

Napi::FunctionReference is equivalent to a v8::Persistent reference to a v8::Function.

The convert.js聽 automatically replaced my FunctionTemplate with a FunctionReference. Was it wrong?

FunctionTemplate is definitely something that we make fairly extensive use of within core so it's likely going to need to be something N-API supports at some point. Having a Napi::FunctionTemplate would be important, even if there is no concept of it in Chakra-Core or other VMs.

Currently I was able to port it to:

 Napi::Function tpl = Napi::Function::New(env, HdrHistogramWrap::New, "HdrHistogram");

Not sure if it's correct, but I was able to move on to the next build error.

This is where we need a conversion guide. The code you linked is defining a class with a bunch of methods. For that, you should definitely use Napi::ObjectWrap and its DefineClass() method. See for example https://github.com/jasongin/node-canvas/blob/napi/src/Canvas.cc#L36 and https://github.com/jasongin/node-canvas/blob/napi/src/Canvas.h#L52

FunctionTemplate is definitely something that we make fairly extensive use of within core so it's likely going to need to be something N-API supports at some point.

We've converted a lot of code so far (which has used v8::FunctionTemplate) and haven't really needed it. The N-API implementation uses it internally where necessary.

The convert.js聽 automatically replaced my FunctionTemplate with a FunctionReference. Was it wrong?

Maybe not. There's a common pattern where a Napi::FunctionReference can be used instead of a v8::FunctionTemplate, though not in exactly the same way. That is when the constructor function of a class is persisted for later instanceof checks.

Ok, then yeah, I think a conversion guide is definitely needed :-) It appears the case is covered, it's just not immediately clear how. Great work and thank you for the patience as we feel our way around

@mcollina just following up to see if there is still an open question here or you have the info you needed to do the conversion.

I think we can close this issue. We have the guide at https://github.com/nodejs/node-addon-api/tree/master/tools.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blagoev picture blagoev  路  6Comments

mhdawson picture mhdawson  路  4Comments

NickNaso picture NickNaso  路  8Comments

fivdi picture fivdi  路  8Comments

jfathman picture jfathman  路  3Comments