阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js N-API napi_new_instance

      Node.js N-API napi_new_instance

      新增于: v8.0.0 N-API version: 1 napi_status napi_new_instance(napi_env env, napi_value cons, size_t argc, napi_value* argv, napi_value* result) [in] env: The environment that the API is invoked under. [in] cons: napi_value representing the JavaScript function to be invoked as a constructor. [in] argc: The count of elements in the argv array. [in] argv: Array of JavaScript values as napi_value representing the arguments to the constructor. [out] result: napi_value representing the JavaScript object returned, which in this case is the constructed object. This method is used to instantiate a new JavaScript value using a given napi_value that represents the constructor for the object. For example, consider the following snippet:

      function MyObject(param) { this.param = param; }

      const arg = ‘hello’; const value = new MyObject(arg); The following can be approximated in N-API using the following snippet:

      // Get the constructor function MyObject napi_value global, constructor, arg, value; napi_status status = napi_get_global(env, &global); if (status != napi_ok) return;

      status = napi_get_named_property(env, global, “MyObject”, &constructor); if (status != napi_ok) return;

      // const arg = “hello” status = napi_create_string_utf8(env, “hello”, NAPI_AUTO_LENGTH, &arg); if (status != napi_ok) return;

      napi_value* argv = &arg; size_t argc = 1;

      // const value = new MyObject(arg) status = napi_new_instance(env, constructor, argc, argv, &value); Returns napi_ok if the API succeeded.


      更多内容请参考:Node.js N-API,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录