c++ - 在 v8 中将字符串转换为函数

标签 c++ v8

我正在尝试将函数硬编码为字符串,将其转换为函数并运行它。

Local<Function> cb = "function(){ console.log('HELLO BOSS'); }";
int argc=0;
v8::Handle<v8::Value> * argv;
cb->Call(Context::GetCurrent()->Global(),argc,argv);

编译器说我不能直接这样做:

      error C2664: 'v8::Handle<T>::Handle(T *)' : cannot convert parameter 1 from 'const char *' to 'v8::Function *'
      with
      [
         T=v8::Function
      ]
      Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 
      x.cpp(15) : see reference to function template instantiation 'v8::Local<T>::Local<const char>(S *)' being compiled
      with
      [
         T=v8::Function,
         S=const char
      ]

那我该怎么做呢?另外,所有好东西(eval、new Function 等)在哪里?

最佳答案

我做到了。男孩,我喜欢这个东西。

v8::Script::Compile(v8::String::New(
    " console.log('WORKS BOSS'); "
    " console.log('Happy?'); "
    " console.log('2+2=' + (2+2)); "
))->Run();

我不知道它是否正确(如“任何相关风险”中的正确),但它似乎有效。

关于c++ - 在 v8 中将字符串转换为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11078394/

相关文章:

c++ - 加速 Qt 中的 Oracle 数据库接口(interface) (QOCI)

c++ - 错误: Cannot use parentheses when declaring variable with deduced class template specialization type

javascript - 调用堆栈和事件循环 - 为什么要等待空堆栈?

JavaScript 专家 : Do block-scopes with `{}` and anonymous functions both help garbage-collection?

c++ - 将引用从 Python 传递给用 swig 包装的 c++ 函数以获得返回值

c++ - 检查十六进制前缀后将字符串转换为 int 的更简洁方法?

c++ - 如何将 "extern template"与由同一类中的模板化成员使用的嵌套类一起使用?

node.js - 如何将数据从casperJS传递到nodeJS?

c++ - Node js 原生模块,从 Objective-C block 事件监听器触发回调不起作用

javascript - Google Apps Script V8 运行时使用哪个版本的 ECMAScript?