#include #include #include using v8::Isolate; using v8::Context; using v8::Local; using v8::MaybeLocal; using v8::Value; using v8::Number; using v8::String; using v8::Object; using v8::Array; using v8::ArrayBufferView; using v8::ArrayBuffer; using v8::FunctionCallbackInfo; void CallWithString(const FunctionCallbackInfo& args) { assert(args.Length() == 1 && args[0]->IsString()); if (args.Length() == 1 && args[0]->IsString()) { Local str = args[0].As(); const int32_t length = str->Utf8Length(args.GetIsolate()) + 1; char* buf = new char[length]; str->WriteUtf8(args.GetIsolate(), buf, length); delete [] buf; } } void CallWithArray(const FunctionCallbackInfo& args) { assert(args.Length() == 1 && args[0]->IsArray()); if (args.Length() == 1 && args[0]->IsArray()) { const Local array = args[0].As(); uint32_t length = array->Length(); for (uint32_t i = 0; i < length; ++ i) { Local v; v = array->Get(i); } } } void CallWithNumber(const FunctionCallbackInfo& args) { assert(args.Length() == 1 && args[0]->IsNumber()); if (args.Length() == 1 && args[0]->IsNumber()) { args[0].As()->Value(); } } void CallWithObject(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); Local context = isolate->GetCurrentContext(); assert(args.Length() == 1 && args[0]->IsObject()); if (args.Length() == 1 && args[0]->IsObject()) { Local obj = args[0].As(); MaybeLocal map_key = String::NewFromUtf8(isolate, "map", v8::NewStringType::kNormal); assert(!map_key.IsEmpty()); MaybeLocal map_maybe = obj->Get(context, map_key.ToLocalChecked()); assert(!map_maybe.IsEmpty()); Local map; map = map_maybe.ToLocalChecked(); MaybeLocal operand_key = String::NewFromUtf8(isolate, "operand", v8::NewStringType::kNormal); assert(!operand_key.IsEmpty()); MaybeLocal operand_maybe = obj->Get(context, operand_key.ToLocalChecked()); assert(!operand_maybe.IsEmpty()); Local operand; operand = operand_maybe.ToLocalChecked(); MaybeLocal data_key = String::NewFromUtf8(isolate, "data", v8::NewStringType::kNormal); assert(!data_key.IsEmpty()); MaybeLocal data_maybe = obj->Get(context, data_key.ToLocalChecked()); assert(!data_maybe.IsEmpty()); Local data; data = data_maybe.ToLocalChecked(); MaybeLocal reduce_key = String::NewFromUtf8(isolate, "reduce", v8::NewStringType::kNormal); assert(!reduce_key.IsEmpty()); MaybeLocal reduce_maybe = obj->Get(context, reduce_key.ToLocalChecked()); assert(!reduce_maybe.IsEmpty()); Local reduce; reduce = reduce_maybe.ToLocalChecked(); } } void CallWithTypedarray(const FunctionCallbackInfo& args) { assert(args.Length() == 1 && args[0]->IsArrayBufferView()); if (args.Length() == 1 && args[0]->IsArrayBufferView()) { assert(args[0]->IsArrayBufferView()); Local view = args[0].As(); const size_t byte_offset = view->ByteOffset(); const size_t byte_length = view->ByteLength(); assert(byte_length > 0); assert(view->HasBuffer()); Local buffer; buffer = view->Buffer(); ArrayBuffer::Contents contents; contents = buffer->GetContents(); const uint32_t* data = reinterpret_cast( static_cast(contents.Data()) + byte_offset); assert(data); } } void CallWithArguments(const FunctionCallbackInfo& args) { assert(args.Length() > 1 && args[0]->IsNumber()); if (args.Length() > 1 && args[0]->IsNumber()) { int32_t loop = args[0].As()->Value(); for (int32_t i = 1; i < loop; ++i) { assert(i < args.Length()); assert(args[i]->IsUint32()); args[i].As()->Value(); } } } void Initialize(Local target) { NODE_SET_METHOD(target, "callWithString", CallWithString); NODE_SET_METHOD(target, "callWithLongString", CallWithString); NODE_SET_METHOD(target, "callWithArray", CallWithArray); NODE_SET_METHOD(target, "callWithLargeArray", CallWithArray); NODE_SET_METHOD(target, "callWithHugeArray", CallWithArray); NODE_SET_METHOD(target, "callWithNumber", CallWithNumber); NODE_SET_METHOD(target, "callWithObject", CallWithObject); NODE_SET_METHOD(target, "callWithTypedarray", CallWithTypedarray); NODE_SET_METHOD(target, "callWith10Numbers", CallWithArguments); NODE_SET_METHOD(target, "callWith100Numbers", CallWithArguments); NODE_SET_METHOD(target, "callWith1000Numbers", CallWithArguments); } NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)