In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to look at the implementation of Napi from the perspective of C++ Addon". In daily operation, I believe many people have doubts about how to see the implementation of Napi from the perspective of C++ Addon. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to look at the realization of Napi from the perspective of C++ Addon". Next, please follow the editor to study!
1 function exported to js for use # include NAPI_MODULE (NODE_GYP_MODULE_NAME, Init)
The above code is the general pattern when using napi, we just need to implement the Init function (you can call it another name, of course). Next, let's look at the implementation of Init.
Napi_value Init (napi_env env, napi_value exports) {napi_value func; / / create a function and set it to the value napi_create_function of the getArray property of the exports object (env, NULL, NAPI_AUTO_LENGTH, newArray, NULL, & func) Napi_set_named_property (env, exports, "getArray", func); return exports;}
Napi_create_function is also the api provided by napi, and its role is to create a function, which can be found in the documentation of napi. Then export this function to js for use with the name getArray. The newArray function is executed when js executes getArray.
2 implementation of newArray static napi_value newArray (napi_env env, napi_callback_info info) {size_t argc = 1; napi_value args [1]; / / get the input parameters of the js layer, here is a napi_get_cb_info (env, info, & argc, args, NULL, NULL); int len / / js is passed in a number, and v8 is converted into an object. Here, the input parameter is again converted to int napi_get_value_int32 (env, args [0], & len); napi_value ret; / / creates an array napi_create_array (env, & ret); / / sets the initial value of the array for (int I = 0; I) according to the js input parameter
< len; i++) { napi_value num; napi_create_int32(env, i, &num); napi_set_element(env, ret, i, num); } return ret; }3 使用c++ addonconst { getArray } = require('./build/Release/test.node'); console.log(getArray(20)); 执行上面代码最后输出 [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ]4 分析 上面的代码并不复杂,本文主要是分析napi提供的api,看看napi到底做了什么。很多api的原理是类似的,这里只以数组的api为例子。因为v8的api中,使用的参数基本都是v8提供的对象。napi做的事情其实就是帮我们处理这些对象的转换。我们首先看看napi_create_array的实现。 // 创建一个数组,对应js的数组 napi_status napi_create_array(napi_env env, napi_value* result) { // 调用v8接口v8::Array::New创建一个数组对象,然后转成napi的类型,并设置返回值 *result = v8impl::JsValueFromV8LocalValue(v8::Array::New(env->Isolate); return napi_clear_last_error (env);}
We see that the implementation of napi_create_array is very simple, which is to encapsulate the v8 interface, then convert it to the type of napi, and finally clear the error message. This is the typical api usage of napi. It mainly includes the following
1 input parameter needs to pass an env object and a second-level pointer napi_value * to save the returned value of the API. The return value of napi is not returned through the return of the function body, but the return returns the execution status of api (success or failure).
2 dealing with api of v8
3 clear or return error message every time the api provided by napi is executed, if there is an error, it will be set to env through napi_set_last_error and the error code will be returned; if not, the error message will be cleared through napi_clear_last_error and napi_ok will be returned. Let's take a look at the implementation
/ / set the error message static inline napi_status napi_set_last_error (napi_env env, napi_status error_code, uint32_t engine_error_code = 0, void* engine_reserved = nullptr) {env- > last_error.error_code = error_code; env- > last_error.engine_error_code = engine_error_code for the current function call. Env- > last_error.engine_reserved = engine_reserved; return error_code;} / / clear the last call error message static inline napi_status napi_clear_last_error (napi_env env) {env- > last_error.error_code = napi_ok; / / TODO (boingoing): Should this be a callback? Env- > last_error.engine_error_code = 0; env- > last_error.engine_reserved = nullptr; return napi_ok;}
After calling api, if an error occurs, the caller can obtain the error message of executing api through the napi_get_last_error_info interface.
/ / get the error message of the last function called napi_status napi_get_last_error_info (napi_env env, const napi_extended_error_info** result) {/ / initialized to illegal value const int last_status = napi_detachable_arraybuffer_expected / / set the error description information according to the error code (the result of each call to api is stored in env) env- > last_error.error_message = error_ messages [env-> last_error.error_code]; * result = & (env- > last_error); return napi_ok;}
To get to the point, after calling napi_create_array, we get a return value, such as the following ret.
Napi_value ret; napi_create_array (env, & ret)
I have previously analyzed that napi_value is essentially a first-level pointer. Then let's see how to use the array we got from napi. We can set the contents of the array through napi_set_element.
/ / ret is an array, I is an index, and num is a napi_value variable, which is essentially a v8 object, that is, the value napi_set_element (env, ret, I, num) corresponding to the index.
Let's take a look at the implementation of napi_set_element.
/ / set the value corresponding to key. Key is the digital napi_status napi_set_element (napi_env env, napi_value object, uint32_t index, napi_value value) {v8::Local context = env- > context (); v8::Local obj / / convert napi_value object to v8 Object, and the array inherits Object CHECK_TO_OBJECT (env, context, obj, object); / / converts the value napi_value value to v8 object v8::Local val = v8impl::V8LocalValueFromJsValue (value); / / calls the Set method of v8 Object object to set the object's properties, that is, the array element auto set_maybe = obj- > Set (context, index, val) / / execute result processing RETURN_STATUS_IF_FALSE (env, set_maybe.FromMaybe (false), napi_generic_failure); return GET_RETURN_STATUS (env);}
From the above analysis, we can roughly see some rules in the napi implementation. The logic of get's api is to call the v8 API to get the v8 type object, and then convert it to the napi_value type and return it to the caller. The api of set is passed in the napi_value type, and then converted to the v8 type object.
At this point, the study on "how to look at the implementation of Napi from the perspective of C++ Addon" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.