c++ - node.js 插件如何检查参数类型

标签 c++ node.js v8

我用了一段时间的node.js,现在我需要写一个插件,但我是c++的新手。

在 node.js 中,我可以将可选参数传递给函数,并检查它们的类型。

function hello(arg, options, callback){
  if(!callback){
    if(typeof options === 'function'){
       callback = options;
       options = {};
    }
  }
  console.log(typeof arg);
}

但是在插件中。

Handle<Value> hello(const Arguments &args) {
    HandleScope scope;
    printf("%d\n", args.Length());
    // how to check type of args[i]
    return String::New("world");
}

最佳答案

您应该查看 http://v8.googlecode.com/svn/trunk/include/v8.h 中的 API .您感兴趣的大多数函数都在 Value 类中。这里有在线文档,http://bespin.cz/~ondras/html/classv8_1_1Value.html但这看起来只是一些随机的人上传的文档版本。不确定他们是否在其他地方在线。

类似这样的事情应该与您的 JS 片段做同样的事情。

Handle<Value> hello(const Arguments &args) {
  HandleScope scope;
  Local<Value> arg(args[0]);
  Local<Value> options(args[1]);
  Local<Value> callback(args[2]);

  if (callback.equals(False())) {
    if (options->IsFunction()) {
      callback = options;
      options = Object::New();
    }
  }

  // ...
}

关于c++ - node.js 插件如何检查参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8551665/

相关文章:

properties - V8 会为用作关联数组的对象生成隐藏类吗? (大量特性)

c++ - C++ 中的简单曲线拟合实现(SVD 最小二乘拟合或类似的)

c++ - stat(char[], stat) 当 string.c_str() = "c:/"时返回 -1

c++ - 内联函数重新定义链接错误

javascript - 错误: opencv4nodejs linux for missing module

node.js - 如何在node.js客户端中进行Auth

c++ - ffmpeg YUV420 到 RGB24 只转换一行

javascript - 如何在 promise 拒绝时停止执行?

javascript - 如何将 DOM API 合并或实现到 v8?

javascript - 如何使用 C++ 中的 V8 同时运行一堆 JS 代码?