diff --git a/lib/os.js b/lib/os.js index 4426612285e299..f412a74132e4e3 100644 --- a/lib/os.js +++ b/lib/os.js @@ -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; diff --git a/node.gyp b/node.gyp index 70c9841a89a176..11f669d8c3dcae 100644 --- a/node.gyp +++ b/node.gyp @@ -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', diff --git a/src/sizeof.cc b/src/sizeof.cc new file mode 100644 index 00000000000000..ebd78041c6c3ab --- /dev/null +++ b/src/sizeof.cc @@ -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 exports, + Handle unused, + Handle context) { + Environment* env = Environment::GetCurrent(context); + + Handle sizeof_obj = Object::New(env->isolate()); + Handle 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(sizeof(type)))); + +#define SET_ALIGNOF(name, type) \ + alignof_obj->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ + Uint32::NewFromUnsigned(env->isolate(), \ + static_cast(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)