#include #include #include namespace { inline void RandomBytes(const v8::FunctionCallbackInfo& info) { assert(info[0]->IsArrayBufferView()); auto view = info[0].As(); auto byte_offset = view->ByteOffset(); auto byte_length = view->ByteLength(); assert(view->HasBuffer()); auto buffer = view->Buffer(); auto contents = buffer->GetContents(); auto data = static_cast(contents.Data()) + byte_offset; assert(RAND_poll()); auto rval = RAND_bytes(data, static_cast(byte_length)); info.GetReturnValue().Set(rval > 0); } inline void Initialize(v8::Local exports, v8::Local module, v8::Local context) { auto isolate = context->GetIsolate(); auto key = v8::String::NewFromUtf8(isolate, "randomBytes"); auto value = v8::FunctionTemplate::New(isolate, RandomBytes)->GetFunction(); assert(exports->Set(context, key, value).IsJust()); } } // anonymous namespace NODE_MODULE_CONTEXT_AWARE(binding, Initialize)