11cb0ef41Sopenharmony_ci#include <v8.h>
21cb0ef41Sopenharmony_ci#include <node.h>
31cb0ef41Sopenharmony_ci#include <assert.h>
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciusing v8::Array;
61cb0ef41Sopenharmony_ciusing v8::ArrayBuffer;
71cb0ef41Sopenharmony_ciusing v8::ArrayBufferView;
81cb0ef41Sopenharmony_ciusing v8::BackingStore;
91cb0ef41Sopenharmony_ciusing v8::Context;
101cb0ef41Sopenharmony_ciusing v8::FunctionCallbackInfo;
111cb0ef41Sopenharmony_ciusing v8::Isolate;
121cb0ef41Sopenharmony_ciusing v8::Local;
131cb0ef41Sopenharmony_ciusing v8::MaybeLocal;
141cb0ef41Sopenharmony_ciusing v8::Number;
151cb0ef41Sopenharmony_ciusing v8::Object;
161cb0ef41Sopenharmony_ciusing v8::String;
171cb0ef41Sopenharmony_ciusing v8::Uint32;
181cb0ef41Sopenharmony_ciusing v8::Value;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_civoid CallWithString(const FunctionCallbackInfo<Value>& args) {
211cb0ef41Sopenharmony_ci  assert(args.Length() == 1 && args[0]->IsString());
221cb0ef41Sopenharmony_ci  if (args.Length() == 1 && args[0]->IsString()) {
231cb0ef41Sopenharmony_ci    Local<String> str = args[0].As<String>();
241cb0ef41Sopenharmony_ci    const int32_t length = str->Utf8Length(args.GetIsolate()) + 1;
251cb0ef41Sopenharmony_ci    char* buf = new char[length];
261cb0ef41Sopenharmony_ci    str->WriteUtf8(args.GetIsolate(), buf, length);
271cb0ef41Sopenharmony_ci    delete[] buf;
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_civoid CallWithArray(const FunctionCallbackInfo<Value>& args) {
321cb0ef41Sopenharmony_ci  assert(args.Length() == 1 && args[0]->IsArray());
331cb0ef41Sopenharmony_ci  if (args.Length() == 1 && args[0]->IsArray()) {
341cb0ef41Sopenharmony_ci    const Local<Array> array = args[0].As<Array>();
351cb0ef41Sopenharmony_ci    uint32_t length = array->Length();
361cb0ef41Sopenharmony_ci    for (uint32_t i = 0; i < length; i++) {
371cb0ef41Sopenharmony_ci      Local<Value> v;
381cb0ef41Sopenharmony_ci      v = array->Get(args.GetIsolate()->GetCurrentContext(),
391cb0ef41Sopenharmony_ci                     i).ToLocalChecked();
401cb0ef41Sopenharmony_ci    }
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_civoid CallWithNumber(const FunctionCallbackInfo<Value>& args) {
451cb0ef41Sopenharmony_ci  assert(args.Length() == 1 && args[0]->IsNumber());
461cb0ef41Sopenharmony_ci  if (args.Length() == 1 && args[0]->IsNumber()) {
471cb0ef41Sopenharmony_ci    args[0].As<Number>()->Value();
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_civoid CallWithObject(const FunctionCallbackInfo<Value>& args) {
521cb0ef41Sopenharmony_ci  Isolate* isolate = args.GetIsolate();
531cb0ef41Sopenharmony_ci  Local<Context> context = isolate->GetCurrentContext();
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  assert(args.Length() == 1 && args[0]->IsObject());
561cb0ef41Sopenharmony_ci  if (args.Length() == 1 && args[0]->IsObject()) {
571cb0ef41Sopenharmony_ci    Local<Object> obj = args[0].As<Object>();
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    MaybeLocal<String> map_key = String::NewFromUtf8(isolate,
601cb0ef41Sopenharmony_ci        "map", v8::NewStringType::kNormal);
611cb0ef41Sopenharmony_ci    assert(!map_key.IsEmpty());
621cb0ef41Sopenharmony_ci    MaybeLocal<Value> map_maybe = obj->Get(context,
631cb0ef41Sopenharmony_ci        map_key.ToLocalChecked());
641cb0ef41Sopenharmony_ci    assert(!map_maybe.IsEmpty());
651cb0ef41Sopenharmony_ci    Local<Value> map;
661cb0ef41Sopenharmony_ci    map = map_maybe.ToLocalChecked();
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    MaybeLocal<String> operand_key = String::NewFromUtf8(isolate,
691cb0ef41Sopenharmony_ci        "operand", v8::NewStringType::kNormal);
701cb0ef41Sopenharmony_ci    assert(!operand_key.IsEmpty());
711cb0ef41Sopenharmony_ci    MaybeLocal<Value> operand_maybe = obj->Get(context,
721cb0ef41Sopenharmony_ci        operand_key.ToLocalChecked());
731cb0ef41Sopenharmony_ci    assert(!operand_maybe.IsEmpty());
741cb0ef41Sopenharmony_ci    Local<Value> operand;
751cb0ef41Sopenharmony_ci    operand = operand_maybe.ToLocalChecked();
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci    MaybeLocal<String> data_key = String::NewFromUtf8(isolate,
781cb0ef41Sopenharmony_ci        "data", v8::NewStringType::kNormal);
791cb0ef41Sopenharmony_ci    assert(!data_key.IsEmpty());
801cb0ef41Sopenharmony_ci    MaybeLocal<Value> data_maybe = obj->Get(context,
811cb0ef41Sopenharmony_ci        data_key.ToLocalChecked());
821cb0ef41Sopenharmony_ci    assert(!data_maybe.IsEmpty());
831cb0ef41Sopenharmony_ci    Local<Value> data;
841cb0ef41Sopenharmony_ci    data = data_maybe.ToLocalChecked();
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    MaybeLocal<String> reduce_key = String::NewFromUtf8(isolate,
871cb0ef41Sopenharmony_ci        "reduce", v8::NewStringType::kNormal);
881cb0ef41Sopenharmony_ci    assert(!reduce_key.IsEmpty());
891cb0ef41Sopenharmony_ci    MaybeLocal<Value> reduce_maybe = obj->Get(context,
901cb0ef41Sopenharmony_ci        reduce_key.ToLocalChecked());
911cb0ef41Sopenharmony_ci    assert(!reduce_maybe.IsEmpty());
921cb0ef41Sopenharmony_ci    Local<Value> reduce;
931cb0ef41Sopenharmony_ci    reduce = reduce_maybe.ToLocalChecked();
941cb0ef41Sopenharmony_ci  }
951cb0ef41Sopenharmony_ci}
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_civoid CallWithTypedarray(const FunctionCallbackInfo<Value>& args) {
981cb0ef41Sopenharmony_ci  assert(args.Length() == 1 && args[0]->IsArrayBufferView());
991cb0ef41Sopenharmony_ci  if (args.Length() == 1 && args[0]->IsArrayBufferView()) {
1001cb0ef41Sopenharmony_ci    assert(args[0]->IsArrayBufferView());
1011cb0ef41Sopenharmony_ci    Local<ArrayBufferView> view = args[0].As<ArrayBufferView>();
1021cb0ef41Sopenharmony_ci    const size_t byte_offset = view->ByteOffset();
1031cb0ef41Sopenharmony_ci    const size_t byte_length = view->ByteLength();
1041cb0ef41Sopenharmony_ci    assert(byte_length > 0);
1051cb0ef41Sopenharmony_ci    assert(view->HasBuffer());
1061cb0ef41Sopenharmony_ci    Local<ArrayBuffer> buffer = view->Buffer();
1071cb0ef41Sopenharmony_ci    std::shared_ptr<BackingStore> bs = buffer->GetBackingStore();
1081cb0ef41Sopenharmony_ci    const uint32_t* data = reinterpret_cast<uint32_t*>(
1091cb0ef41Sopenharmony_ci        static_cast<uint8_t*>(bs->Data()) + byte_offset);
1101cb0ef41Sopenharmony_ci    assert(data);
1111cb0ef41Sopenharmony_ci  }
1121cb0ef41Sopenharmony_ci}
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_civoid CallWithArguments(const FunctionCallbackInfo<Value>& args) {
1151cb0ef41Sopenharmony_ci  assert(args.Length() > 1 && args[0]->IsNumber());
1161cb0ef41Sopenharmony_ci  if (args.Length() > 1 && args[0]->IsNumber()) {
1171cb0ef41Sopenharmony_ci    int32_t loop = args[0].As<Uint32>()->Value();
1181cb0ef41Sopenharmony_ci    for (int32_t i = 1; i < loop; ++i) {
1191cb0ef41Sopenharmony_ci      assert(i < args.Length());
1201cb0ef41Sopenharmony_ci      assert(args[i]->IsUint32());
1211cb0ef41Sopenharmony_ci      args[i].As<Uint32>()->Value();
1221cb0ef41Sopenharmony_ci    }
1231cb0ef41Sopenharmony_ci  }
1241cb0ef41Sopenharmony_ci}
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_civoid Initialize(Local<Object> target,
1271cb0ef41Sopenharmony_ci                Local<Value> module,
1281cb0ef41Sopenharmony_ci                void* data) {
1291cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWithString", CallWithString);
1301cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWithLongString", CallWithString);
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWithArray", CallWithArray);
1331cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWithLargeArray", CallWithArray);
1341cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWithHugeArray", CallWithArray);
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWithNumber", CallWithNumber);
1371cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWithObject", CallWithObject);
1381cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWithTypedarray", CallWithTypedarray);
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWith10Numbers", CallWithArguments);
1411cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWith100Numbers", CallWithArguments);
1421cb0ef41Sopenharmony_ci  NODE_SET_METHOD(target, "callWith1000Numbers", CallWithArguments);
1431cb0ef41Sopenharmony_ci}
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ciNODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
146