如何将std :: string转换为v8的本地< string> [英] How to convert std::string to v8's Local<string>

查看:2412
本文介绍了如何将std :: string转换为v8的本地< string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它接受一个字符串,然后传递给我的c ++函数 add_node()

I have a function which is ment to take in a string and then pass it to my c++ function add_node()

Handle<Value> Graph::add_node(const v8::Arguments& args)
{
  HandleScope scope;

  Graph* graph = ObjectWrap::Unwrap<Graph>(args.This());
  graph->add_node( args[0]->ToString() );

  std::cout << "In add node \n";
}

但是我有麻烦,因为我的所有参数都是v8的排序或另一个,我不能弄清楚如何在两者之间切换。 文档也未明确说明。

However I'm having trouble because all of my arguments are v8 templetes of some sort or another and I cant figure out how to switch between the two. The documentation doesn't state it clearly either.

编译器给我这个错误

../graph/binding.cc:52:10: error: no matching member function for call to
      'add_node'
  graph->add_node( args[0]->ToString() );
  ~~~~~~~^~~~~~~~
../graph/directed_graph.h:27:7: note: candidate function not viable: no known
      conversion from 'Local<v8::String>' to 'std::string &' (aka
      'basic_string<char> &') for 1st argument;
        void add_node( std::string & currency );

如何在 Local< v8 :: String> std :: string&

推荐答案

这似乎很好工作

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

如果你只是试图转换 std :: string v8 :: String 然后执行

and if your just trying to convert a std::string to a v8::String then do

std::string something("hello world"); 
Handle<Value> something_else = String::New( something.c_str() );

这篇关于如何将std :: string转换为v8的本地&lt; string&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆