-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
[Discussion] FFI - export os.sizeof
and os.alignof
mappings
#1759
[Discussion] FFI - export os.sizeof
and os.alignof
mappings
#1759
Conversation
@@ -54,3 +54,6 @@ if (binding.isBigEndian) | |||
exports.endianness = function() { return 'BE'; }; | |||
else | |||
exports.endianness = function() { return 'LE'; }; | |||
|
|||
exports.sizeof = process.binding('sizeof'); | |||
exports.alignof = process.binding('alignof'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to cache a return result here? (Or is that already what the c++ does?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The C++ layer already caches process.binding()
results.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok cool, so long as this doesn't have to go through c++ each time it is accessed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lib/os.js
also is only executed once (and the exports are cached), so yes, the values should basically be cached indefinitely.
What about |
@@ -0,0 +1,68 @@ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specific reason why this is in a new file rather than src/node_os.cc
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No particularly good reason, but I was planning on using process.binding('sizeof').pointer
within buffer.js in order to add correct offset validation to the read/writePointer() functions. We could easily just require('os').sizeof.pointer
instead though. I'm open either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay! I don't have much experience with the C++ anyways, but perhaps someone else may have an opinion on it.
#define SET_ALIGNOF(name, type) \ | ||
struct s_##name { type a; }; \ | ||
exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name ), \ | ||
Uint32::NewFromUnsigned(env->isolate(), static_cast<uint32_t>(__alignof__(struct s_##name)))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you use alignof(type)
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forget we're in C++ land sometimes :) Does that mean I can ditch the struct thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, long line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, no need for the struct.
I would suggest merging the two .cc files. That would avoid most of the repetition and the potential for oversights when adding new types later on. |
Aside: I'm not sure if 'os' is a good place for this because it's not really specific to the operating system. Maybe introduce a new 'ffi' or 'ctypes' module? |
SET_ALIGNOF(size_t, size_t); | ||
|
||
// alignment of a Persistent handle to a JS object | ||
SET_ALIGNOF(Object, Persistent<Object>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain the need for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove it (copy and paste oversight).
But as an explanation, in ref
I have readObject()
and writeObject()
, which allow you to create and write the memory address of a Persistent<Object>
handle to a Buffer instance, for later retrieval. It was more black magic / experimental though, and probably unsafe for use in core. At least for now. It's not necessary for the main "FFI in core" mission anyways, so we could revisit the idea once all this big stuff is landed.
Will be merged with sizeof.cc
This makes it easier in the future to add more types, since they're now located in one location.
Closing due to @bnoordhuis recommendation of moving this to the |
Related to #1750.
This PR adds
os.sizeof
andos.alignof
Object mappings. For example, on a 64-bit OS:Hopefully pretty uncontroversial, but still needs docs and tests. Wanted to get notes/comments on implementation first though.