Repository navigation
Update for buffer API changes - #1
Conversation
|
The data argument of buffers should arguably be Even better would of course be to make them templated on the type of data. |
|
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. |
|
Note there are some |
| return data; | ||
| template <typename T> | ||
| inline size_t Buffer<T>::Length() const { | ||
| if (_data == nullptr) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yes, I refactored this.
| 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); |
There was a problem hiding this comment.
space after keyword, goes for the rest of the file
sizeof(T) -> sizeof (T)
| _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); |
There was a problem hiding this comment.
Mark the field _length mutable instead.
| _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); |
There was a problem hiding this comment.
Mark the field _length mutable instead.
mhdawson
left a comment
There was a problem hiding this comment.
Subject to address existing comments from other reviewers LGTM
No description provided.