2014
2014

Reputation: 103

how to convert node v8 string to c++ string

i am new to write node c++ addon.
i want to change params string from node to change c++ string,
but i got error, and i try this issue,
How to convert std::string to v8's Local<string> it didn't work.
i have tried these function, but none is work.

void Method(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();
    // Local<Value> layer_name = Local<String>::Cast(args[0]);
    // Local<Value> imei = Local<String>::Cast(args[1]);
    // Local<String> layer_name = args[0]->ToString();
    // Local<String> imei = args[1]->ToString();
    // v8::String::Utf8Value layer_name(args[0]);

    // v8::String::Utf8Value param1(args[0]);
    // std::string from = std::string(*param1);

    // std::string encoded_val = "test";
    // Local<String> returned_str = Local<String>::Cast(args[0]);
    // encoded_val.c_str() = returned_str;

    // v8::String::Utf8Value s(args[0]);
//     Local<String> s = v8::String::NewFromUtf8(isolate, args[0], 
// NewStringType::kNormal).ToLocalChecked();
//     std::string str(*s);

    // std::string str(v8::String::Utf8Value(args[0].As<String>());

    // v8::String::Utf8Value str(isolate, args[0]);
    // std::string layer_name(*str);

    // v8::String::Utf8Value layer_name(args[0]); 
    // char* str = ToCString(str); 
    // std::string str(*layer_name);

    std::string result;
    int age = 0;
    result = std::to_string(age);
    int32_t res = IMEIHash(result, 10000);
    args.GetReturnValue().Set(res);
}```
so how to change v8 args[0] to c++ string, in node v12.16.0 ?

Upvotes: 2

Views: 2331

Answers (1)

Satyan
Satyan

Reputation: 1406

Try this

void MyV8ToCppStr(const v8::FunctionCallbackInfo<v8::Value> &args)
{

  v8::Isolate* isolate = args.GetIsolate();
  v8::String::Utf8Value str(isolate, args[0]);
  std::string cppStr(*str);
}

Upvotes: 2

Related Questions