Skip to content

Update for buffer API changes - #1

Merged
jasongin merged 4 commits into
nodejs:masterfrom
jasongin:buffer
Mar 15, 2017
Merged

jasongin merged 4 commits into
nodejs:masterfrom
jasongin:buffer

Conversation

@jasongin

Copy link
Copy Markdown
Member

No description provided.

@kkoopa

kkoopa commented Mar 1, 2017

Copy link
Copy Markdown

The data argument of buffers should arguably be void * , using char * is a legacy oversight on Node's part. Using char * makes typecasting required for the user.

Even better would of course be to make them templated on the type of data.

@jasongin

Copy link
Copy Markdown
Member Author

I made the Buffer class templated on the data type. For testing I updated my node-canvas NAPI port to use it, and it did indeed eliminate a lot of casting.

@jasongin
jasongin requested a review from boingoing March 11, 2017 00:42
@jasongin

jasongin commented Mar 11, 2017 •

Copy link
Copy Markdown
Member Author

Note there are some reinterpret_cast<> calls in the implementation that can go away or change to static_cast<> after nodejs/abi-stable-node#135 is fixed.

@boingoing boingoing left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

Comment thread napi-inl.h Outdated
return data;
template <typename T>
inline size_t Buffer<T>::Length() const {
if (_data == nullptr) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This same initialization code is duplicated in Buffer::Length and Buffer::Data. Would it make sense to put it into a helper like a private method Buffer::EnsureCachedInfo or something to make the intent more clear and avoid having to change two places in the future? Also, might want to copy/paste this block again for another method later and inlining a small helper should translate to exactly the same code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I refactored this.

Comment thread napi-inl.h Outdated
napi_value value;
char* data;
napi_status status = napi_create_buffer(env, length, &data, &value);
napi_status status = napi_create_buffer(env, length * sizeof(T), &data, &value);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after keyword, goes for the rest of the file
sizeof(T) -> sizeof (T)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread napi-inl.h Outdated
_env, _value, const_cast<char**>(&_data), const_cast<size_t*>(&_length));
_env, _value, reinterpret_cast<char**>(const_cast<T**>(&_data)), &byteLength);
if (status != napi_ok) throw Error::New(Env());
*const_cast<size_t*>(&_length) = byteLength / sizeof(T);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark the field _length mutable instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread napi-inl.h Outdated
_env, _value, const_cast<char**>(&_data), const_cast<size_t*>(&_length));
_env, _value, reinterpret_cast<char**>(const_cast<T**>(&_data)), &byteLength);
if (status != napi_ok) throw Error::New(Env());
*const_cast<size_t*>(&_length) = byteLength / sizeof(T);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark the field _length mutable instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@mhdawson mhdawson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subject to address existing comments from other reviewers LGTM

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

Successfully merging this pull request may close these issues.

4 participants