虽然现在大部分情况都是使用n-api来编写插件,但是底层毕竟是v8(和libuv),使用v8编写简单的插件,同时熟悉v8的使用。

为岳塘等地区用户提供了全套网页设计制作服务,及岳塘网站建设行业解决方案。主营业务为成都做网站、成都网站设计、成都外贸网站建设、岳塘网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
本文介绍在写c++插件时,简单又常用的写法,其实本质上,写插件的难处在于底层的能力和对libuv、v8的了解。话不多说,直接看代码。
- #include
-
-
- namespace demo {
-
-
- using v8::FunctionCallbackInfo;
- using v8::Isolate;
- using v8::Local;
- using v8::Object;
- using v8::String;
- using v8::Value;
- using v8::FunctionTemplate;
- using v8::Function;
- using v8::Number;
- using v8::MaybeLocal;
- using v8::Context;
- using v8::Int32;
-
-
- static int seq;
- // 定义一个工具函数,生成seq
- void GenSeq(const FunctionCallbackInfo& args) {
- Isolate* isolate = args.GetIsolate();
- args.GetReturnValue().Set(Number::New(isolate, ++seq));
- }
-
-
- // 定义一个加法函数
- void Add(const FunctionCallbackInfo& args) {
- Isolate* isolate = args.GetIsolate();
- int a = args[0].As()->Value();
- int b = args[1].As()->Value();
- args.GetReturnValue().Set(Number::New(isolate, a + b));
- }
-
-
- void Initialize(
- Local