Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ if (binding.isBigEndian)
exports.endianness = function() { return 'BE'; };
else
exports.endianness = function() { return 'LE'; };

exports.sizeof = process.binding('sizeof').sizeof;
exports.alignof = process.binding('sizeof').alignof;
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
'src/node_i18n.cc',
'src/pipe_wrap.cc',
'src/signal_wrap.cc',
'src/sizeof.cc',
'src/smalloc.cc',
'src/spawn_sync.cc',
'src/string_bytes.cc',
Expand Down
82 changes: 82 additions & 0 deletions src/sizeof.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

#include "env.h"
#include "env-inl.h"
#include "node.h"
#include "node_internals.h"
#include "v8-profiler.h"
#include "v8.h"

namespace node {
namespace _sizeof {

using v8::Context;
using v8::Handle;
using v8::Object;
using v8::Uint32;
using v8::Value;


void Initialize(Handle<Object> exports,
Handle<Value> unused,
Handle<Context> context) {
Environment* env = Environment::GetCurrent(context);

Handle<Object> sizeof_obj = Object::New(env->isolate());
Handle<Object> alignof_obj = Object::New(env->isolate());

#define SET_SIZEOF(name, type) \
sizeof_obj->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
Uint32::NewFromUnsigned(env->isolate(), \
static_cast<uint32_t>(sizeof(type))));

#define SET_ALIGNOF(name, type) \
alignof_obj->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
Uint32::NewFromUnsigned(env->isolate(), \
static_cast<uint32_t>(alignof(type))));

#define SET_TYPE(name, type) \
SET_SIZEOF(name, type) \
SET_ALIGNOF(name, type)

// fixed sizes
SET_TYPE(int8, int8_t);
SET_TYPE(uint8, uint8_t);
SET_TYPE(int16, int16_t);
SET_TYPE(uint16, uint16_t);
SET_TYPE(int32, int32_t);
SET_TYPE(uint32, uint32_t);
SET_TYPE(int64, int64_t);
SET_TYPE(uint64, uint64_t);
SET_TYPE(float, float);
SET_TYPE(double, double);

// (potentially) variable sizes
SET_TYPE(bool, bool);
SET_TYPE(byte, unsigned char);
SET_TYPE(char, char);
SET_TYPE(uchar, unsigned char);
SET_TYPE(short, short);
SET_TYPE(ushort, unsigned short);
SET_TYPE(int, int);
SET_TYPE(uint, unsigned int);
SET_TYPE(long, long);
SET_TYPE(ulong, unsigned long);
SET_TYPE(longlong, long long);
SET_TYPE(ulonglong, unsigned long long);
SET_TYPE(longdouble, long double);
SET_TYPE(pointer, char*);
SET_TYPE(size_t, size_t);

#undef SET_SIZEOF
#undef SET_ALIGNOF
#undef SET_TYPE

exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "sizeof"), sizeof_obj);
exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "alignof"), alignof_obj);
}


} // namespace _sizeof
} // namespace node

NODE_MODULE_CONTEXT_AWARE_BUILTIN(sizeof, node::_sizeof::Initialize)