mirror of
https://gitee.com/openharmony/napi_generator
synced 2024-11-23 08:20:01 +00:00
delete middle files
Signed-off-by: bayanxing <bayanxing@kaihongdigi.com>
This commit is contained in:
parent
137dad6e87
commit
4a67a2fd38
@ -20,8 +20,8 @@ NAPI框架代码生成工具,根据用户指定的路径下的ts接口文件可
|
||||
|
||||
#### 安装教程
|
||||
|
||||
1. 将Windows可执行程序cmd_gen-win.exe拷贝到对应目录下,不需要安装,可以在cmd命令行中直接运行。
|
||||
2. 将Linux可执行程序cmd_gen-linux拷贝到对应目录下,可以在终端下直接运行。
|
||||
1. 将output目录下Windows可执行程序cmd_gen-win.exe拷贝到对应目录下,不需要安装,可以在cmd命令行中直接运行。
|
||||
2. 将output目录下Linux可执行程序cmd_gen-linux拷贝到对应目录下,可以在终端下直接运行。
|
||||
|
||||
#### 使用说明
|
||||
|
||||
|
@ -1,29 +0,0 @@
|
||||
import("//build/ohos.gni")
|
||||
|
||||
ohos_shared_library("napitest")
|
||||
{
|
||||
sources = [
|
||||
"napitest_middle.cpp",
|
||||
"napitest.cpp",
|
||||
"x_napi_tool.cpp",
|
||||
]
|
||||
include_dirs = [
|
||||
".",
|
||||
"//third_party/node/src",
|
||||
]
|
||||
deps=[
|
||||
"//foundation/ace/napi:ace_napi",
|
||||
]
|
||||
remove_configs = [ "//build/config/compiler:no_rtti" ]
|
||||
cflags=[
|
||||
]
|
||||
cflags_cc=[
|
||||
"-frtti",
|
||||
]
|
||||
ldflags = [
|
||||
]
|
||||
|
||||
relative_install_dir = "module"
|
||||
part_name = "<your part name>"
|
||||
subsystem_name = "<your subsystem name>"
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
|
||||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "napitest",
|
||||
"sources": [
|
||||
"./napitest.cpp",
|
||||
"./napitest_middle.cpp",
|
||||
"./x_napi_tool.cpp"],
|
||||
"include_dirs": ["."],
|
||||
"cflags_cc": [ "-frtti","-std=c++17" ]
|
||||
}
|
||||
]
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
#include "napitest.h"
|
||||
|
||||
|
||||
namespace napitest {
|
||||
|
||||
bool TestClass1::if_direct(std::string &v1, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TestClass1::if_callback(std::string &v1, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TestClass1::if_async(std::string &v1, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fun2(std::string &v2, std::vector<NUMBER_TYPE_4> &numcc, Human &mancc, std::vector<Human> &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fun3(std::string &v2, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fun4(std::string &v2, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
namespace Space3 {
|
||||
|
||||
bool fun3(std::string &v3, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
namespace Space4 {
|
||||
|
||||
bool TestClass3::add(std::vector<NUMBER_TYPE_7> &v1, NUMBER_TYPE_8 &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fun3(std::string &v3, std::string &out) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
#ifndef IMPL_NAPITEST_H
|
||||
#define IMPL_NAPITEST_H
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <math.h>
|
||||
|
||||
using NUMBER_TYPE_1 = uint32_t;
|
||||
using NUMBER_TYPE_2 = uint32_t;
|
||||
using NUMBER_TYPE_3 = uint32_t;
|
||||
using NUMBER_TYPE_4 = uint32_t;
|
||||
using NUMBER_TYPE_5 = uint32_t;
|
||||
using NUMBER_TYPE_6 = uint32_t;
|
||||
using NUMBER_TYPE_7 = uint32_t;
|
||||
using NUMBER_TYPE_8 = uint32_t;
|
||||
|
||||
|
||||
|
||||
namespace napitest {
|
||||
|
||||
class Human {
|
||||
public:
|
||||
std::string name;
|
||||
NUMBER_TYPE_1 age;
|
||||
};
|
||||
class TestClass1 {
|
||||
public:
|
||||
Human ahuman;
|
||||
NUMBER_TYPE_2 num1;
|
||||
std::string str1;
|
||||
std::vector<NUMBER_TYPE_3> nums;
|
||||
std::vector<std::string> strs;
|
||||
std::vector<Human> mans;
|
||||
bool if_direct(std::string &v1, std::string &out);
|
||||
bool if_callback(std::string &v1, std::string &out);
|
||||
bool if_async(std::string &v1, std::string &out);
|
||||
};
|
||||
bool fun2(std::string &v2, std::vector<NUMBER_TYPE_4> &numcc, Human &mancc, std::vector<Human> &out);
|
||||
bool fun3(std::string &v2, std::string &out);
|
||||
bool fun4(std::string &v2, std::string &out);
|
||||
namespace Space3 {
|
||||
|
||||
class TestClass2 {
|
||||
public:
|
||||
NUMBER_TYPE_5 haha;
|
||||
};
|
||||
bool fun3(std::string &v3, std::string &out);
|
||||
namespace Space4 {
|
||||
|
||||
class TestClass3 {
|
||||
public:
|
||||
NUMBER_TYPE_6 hoho;
|
||||
bool add(std::vector<NUMBER_TYPE_7> &v1, NUMBER_TYPE_8 &out);
|
||||
};
|
||||
bool fun3(std::string &v3, std::string &out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // IMPL_NAPITEST_H
|
@ -1,792 +0,0 @@
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <node_api.h>
|
||||
#include "x_napi_tool.h"
|
||||
#include "napitest.h"
|
||||
|
||||
#define NUMBER_JS_2_C(napi_v, type, dest) \
|
||||
if (typeid(type) == typeid(int32_t)) \
|
||||
dest = pxt->SwapJs2CInt32(napi_v); \
|
||||
else if (typeid(type) == typeid(uint32_t)) \
|
||||
dest = pxt->SwapJs2CUint32(napi_v); \
|
||||
else if (typeid(type) == typeid(int64_t)) \
|
||||
dest = pxt->SwapJs2CInt64(napi_v); \
|
||||
else if (typeid(type) == typeid(double_t)) \
|
||||
dest = pxt->SwapJs2CDouble(napi_v);
|
||||
|
||||
napi_value number_c_to_js(XNapiTool *pxt, const std::type_info &n, void *num)
|
||||
{
|
||||
if (n == typeid(int32_t))
|
||||
return pxt->SwapC2JsInt32(*(int32_t *)num);
|
||||
else if (n == typeid(uint32_t))
|
||||
return pxt->SwapC2JsUint32(*(uint32_t *)num);
|
||||
else if (n == typeid(int64_t))
|
||||
return pxt->SwapC2JsInt64(*(int64_t *)num);
|
||||
else if (n == typeid(double_t))
|
||||
return pxt->SwapC2JsDouble(*(double_t *)num);
|
||||
return nullptr;
|
||||
}
|
||||
#define NUMBER_C_2_JS(pxt, n) \
|
||||
number_c_to_js(pxt, typeid(n), &n)
|
||||
|
||||
|
||||
namespace napitest {
|
||||
|
||||
class Human_middle {
|
||||
public:
|
||||
static napi_value constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = new XNapiTool(env, info);
|
||||
|
||||
Human *p = new Human();
|
||||
// printf("static constructor %x\n", p);
|
||||
|
||||
napi_value thisvar = pxt->WrapInstance(p, release);
|
||||
|
||||
return thisvar;
|
||||
}
|
||||
static void release(void *p)
|
||||
{
|
||||
// printf("test2 released\n");
|
||||
Human *p2 = (Human *)p;
|
||||
delete p2;
|
||||
}
|
||||
|
||||
static napi_value getvalue_name(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
Human *p = (Human *)pxt->UnWarpInstance();
|
||||
napi_value result;
|
||||
result = pxt->SwapC2JsUtf8(p->name.c_str());
|
||||
delete pxt;
|
||||
return result;
|
||||
}
|
||||
static napi_value setvalue_name(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
|
||||
Human *p = (Human *)pxt->UnWarpInstance();
|
||||
pxt->SwapJs2CUtf8(pxt->GetArgv(0), p->name);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static napi_value getvalue_age(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
Human *p = (Human *)pxt->UnWarpInstance();
|
||||
napi_value result;
|
||||
result = NUMBER_C_2_JS(pxt, p->age);
|
||||
delete pxt;
|
||||
return result;
|
||||
}
|
||||
static napi_value setvalue_age(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
|
||||
Human *p = (Human *)pxt->UnWarpInstance();
|
||||
NUMBER_JS_2_C(pxt->GetArgv(0),NUMBER_TYPE_1,p->age);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
};
|
||||
class TestClass1_middle {
|
||||
public:
|
||||
static napi_value constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = new XNapiTool(env, info);
|
||||
|
||||
TestClass1 *p = new TestClass1();
|
||||
// printf("static constructor %x\n", p);
|
||||
|
||||
napi_value thisvar = pxt->WrapInstance(p, release);
|
||||
|
||||
return thisvar;
|
||||
}
|
||||
static void release(void *p)
|
||||
{
|
||||
// printf("test2 released\n");
|
||||
TestClass1 *p2 = (TestClass1 *)p;
|
||||
delete p2;
|
||||
}
|
||||
|
||||
static napi_value getvalue_ahuman(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
napi_value result;
|
||||
{
|
||||
napi_value tnv1 = nullptr;
|
||||
tnv1 = pxt->SwapC2JsUtf8(p->ahuman.name.c_str());
|
||||
pxt->SetValueProperty(result,"name",tnv1);
|
||||
}{
|
||||
napi_value tnv1 = nullptr;
|
||||
tnv1 = NUMBER_C_2_JS(pxt, p->ahuman.age);
|
||||
pxt->SetValueProperty(result,"age",tnv1);
|
||||
}
|
||||
delete pxt;
|
||||
return result;
|
||||
}
|
||||
static napi_value setvalue_ahuman(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
napi_value tnv1 = pxt->GetValueProperty(pxt->GetArgv(0), "name");
|
||||
if(tnv1!=nullptr){pxt->SwapJs2CUtf8(tnv1,p->ahuman.name);}napi_value tnv2 = pxt->GetValueProperty(pxt->GetArgv(0), "age");
|
||||
if(tnv2!=nullptr){NUMBER_JS_2_C(tnv2,NUMBER_TYPE_1,p->ahuman.age);}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static napi_value getvalue_num1(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
napi_value result;
|
||||
result = NUMBER_C_2_JS(pxt, p->num1);
|
||||
delete pxt;
|
||||
return result;
|
||||
}
|
||||
static napi_value setvalue_num1(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
NUMBER_JS_2_C(pxt->GetArgv(0),NUMBER_TYPE_2,p->num1);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static napi_value getvalue_str1(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
napi_value result;
|
||||
result = pxt->SwapC2JsUtf8(p->str1.c_str());
|
||||
delete pxt;
|
||||
return result;
|
||||
}
|
||||
static napi_value setvalue_str1(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
pxt->SwapJs2CUtf8(pxt->GetArgv(0), p->str1);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static napi_value getvalue_nums(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
napi_value result;
|
||||
uint32_t len1=p->nums.size();
|
||||
for(uint32_t i=0;i<len1;i++) {
|
||||
napi_value tnv1 = nullptr;
|
||||
tnv1 = NUMBER_C_2_JS(pxt,p->nums[i]);
|
||||
pxt->SetArrayElement(result, i, tnv1);
|
||||
}
|
||||
delete pxt;
|
||||
return result;
|
||||
}
|
||||
static napi_value setvalue_nums(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
uint32_t len3=pxt->GetArrayLength(pxt->GetArgv(0));
|
||||
for(uint32_t i3=0;i3<len3;i3++) {
|
||||
NUMBER_TYPE_3 tt3;
|
||||
NUMBER_JS_2_C(pxt->GetArrayElement(pxt->GetArgv(0),i3),NUMBER_TYPE_3,tt3);
|
||||
p->nums.push_back(tt3);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static napi_value getvalue_strs(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
napi_value result;
|
||||
uint32_t len1=p->strs.size();
|
||||
for(uint32_t i=0;i<len1;i++) {
|
||||
napi_value tnv1 = nullptr;
|
||||
tnv1 = pxt->SwapC2JsUtf8(p->strs[i].c_str());
|
||||
pxt->SetArrayElement(result, i, tnv1);
|
||||
}
|
||||
delete pxt;
|
||||
return result;
|
||||
}
|
||||
static napi_value setvalue_strs(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
uint32_t len4=pxt->GetArrayLength(pxt->GetArgv(0));
|
||||
for(uint32_t i4=0;i4<len4;i4++) {
|
||||
std::string tt4;
|
||||
pxt->SwapJs2CUtf8(pxt->GetArrayElement(pxt->GetArgv(0),i4), tt4);
|
||||
p->strs.push_back(tt4);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static napi_value getvalue_mans(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
napi_value result;
|
||||
uint32_t len1=p->mans.size();
|
||||
for(uint32_t i=0;i<len1;i++) {
|
||||
napi_value tnv1 = nullptr;
|
||||
{
|
||||
napi_value tnv2 = nullptr;
|
||||
tnv2 = pxt->SwapC2JsUtf8(p->mans[i].name.c_str());
|
||||
pxt->SetValueProperty(tnv1,"name",tnv2);
|
||||
}{
|
||||
napi_value tnv2 = nullptr;
|
||||
tnv2 = NUMBER_C_2_JS(pxt, p->mans[i].age);
|
||||
pxt->SetValueProperty(tnv1,"age",tnv2);
|
||||
}
|
||||
pxt->SetArrayElement(result, i, tnv1);
|
||||
}
|
||||
delete pxt;
|
||||
return result;
|
||||
}
|
||||
static napi_value setvalue_mans(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
|
||||
TestClass1 *p = (TestClass1 *)pxt->UnWarpInstance();
|
||||
uint32_t len5=pxt->GetArrayLength(pxt->GetArgv(0));
|
||||
for(uint32_t i5=0;i5<len5;i5++) {
|
||||
Human tt5;
|
||||
napi_value tnv6 = pxt->GetValueProperty(pxt->GetArrayElement(pxt->GetArgv(0),i5), "name");
|
||||
if(tnv6!=nullptr){pxt->SwapJs2CUtf8(tnv6,tt5.name);}napi_value tnv7 = pxt->GetValueProperty(pxt->GetArrayElement(pxt->GetArgv(0),i5), "age");
|
||||
if(tnv7!=nullptr){NUMBER_JS_2_C(tnv7,NUMBER_TYPE_1,tt5.age);}
|
||||
p->mans.push_back(tt5);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct if_direct_value_struct {
|
||||
std::string in0;
|
||||
|
||||
std::string out;
|
||||
};
|
||||
|
||||
static napi_value if_direct_middle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
if (pxt->IsFailed())
|
||||
{
|
||||
napi_value err = pxt->GetError();
|
||||
delete pxt;
|
||||
return err;
|
||||
}
|
||||
TestClass1 *pInstance = (TestClass1 *)pxt->UnWarpInstance();
|
||||
|
||||
struct if_direct_value_struct *vio=new if_direct_value_struct();
|
||||
|
||||
pxt->SwapJs2CUtf8(pxt->GetArgv(0), vio->in0);
|
||||
|
||||
pInstance->if_direct(vio->in0, vio->out);
|
||||
|
||||
napi_value result = nullptr;
|
||||
result = pxt->SwapC2JsUtf8(vio->out.c_str());
|
||||
|
||||
delete vio;
|
||||
if (pxt->IsFailed())
|
||||
result = pxt->GetError();
|
||||
delete pxt;// release
|
||||
return result;
|
||||
}
|
||||
struct if_callback_value_struct {
|
||||
std::string in0;
|
||||
|
||||
std::string out;
|
||||
};
|
||||
|
||||
static napi_value if_callback_middle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
if (pxt->IsFailed())
|
||||
{
|
||||
napi_value err = pxt->GetError();
|
||||
delete pxt;
|
||||
return err;
|
||||
}
|
||||
TestClass1 *pInstance = (TestClass1 *)pxt->UnWarpInstance();
|
||||
|
||||
struct if_callback_value_struct *vio=new if_callback_value_struct();
|
||||
|
||||
pxt->SwapJs2CUtf8(pxt->GetArgv(0), vio->in0);
|
||||
|
||||
pInstance->if_callback(vio->in0, vio->out);
|
||||
|
||||
napi_value result = nullptr;
|
||||
result = pxt->SwapC2JsUtf8(vio->out.c_str());
|
||||
|
||||
{
|
||||
napi_value args[1] = {result};
|
||||
pxt->SyncCallBack(pxt->GetArgv(1), 1, args);
|
||||
}
|
||||
result = pxt->UndefinedValue();
|
||||
|
||||
delete vio;
|
||||
if (pxt->IsFailed())
|
||||
result = pxt->GetError();
|
||||
delete pxt;// release
|
||||
return result;
|
||||
}
|
||||
struct if_async_value_struct {
|
||||
std::string in0;
|
||||
|
||||
std::string out;
|
||||
};
|
||||
|
||||
static void if_async_execute(XNapiTool *pxt, void *data)
|
||||
{
|
||||
if_async_value_struct *vio = (if_async_value_struct *)data;
|
||||
TestClass1 *pInstance = (TestClass1 *)pxt->GetAsyncInstance();
|
||||
|
||||
pInstance->if_async(vio->in0, vio->out);
|
||||
}
|
||||
|
||||
static void if_async_complete(XNapiTool *pxt, void *data)
|
||||
{
|
||||
if_async_value_struct *vio = (if_async_value_struct *)data;
|
||||
|
||||
napi_value result = nullptr;
|
||||
result = pxt->SwapC2JsUtf8(vio->out.c_str());
|
||||
|
||||
{
|
||||
napi_value args[1] = {result};
|
||||
pxt->FinishAsync(1, args);
|
||||
}
|
||||
|
||||
delete vio;
|
||||
}
|
||||
|
||||
static napi_value if_async_middle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
if (pxt->IsFailed())
|
||||
{
|
||||
napi_value err = pxt->GetError();
|
||||
delete pxt;
|
||||
return err;
|
||||
}
|
||||
pxt->SetAsyncInstance(pxt->UnWarpInstance());
|
||||
|
||||
struct if_async_value_struct *vio=new if_async_value_struct();
|
||||
|
||||
pxt->SwapJs2CUtf8(pxt->GetArgv(0), vio->in0);
|
||||
|
||||
|
||||
napi_value result = pxt->StartAsync(if_async_execute, vio, if_async_complete, pxt->GetArgc() == 2 ? pxt->GetArgv(1) : nullptr);
|
||||
|
||||
if (pxt->IsFailed())
|
||||
result = pxt->GetError();
|
||||
return result;
|
||||
}
|
||||
};
|
||||
struct fun2_value_struct {
|
||||
std::string in0;
|
||||
std::vector<NUMBER_TYPE_4> in1;
|
||||
Human in2;
|
||||
|
||||
std::vector<Human> out;
|
||||
};
|
||||
|
||||
napi_value fun2_middle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
if (pxt->IsFailed())
|
||||
{
|
||||
napi_value err = pxt->GetError();
|
||||
delete pxt;
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
struct fun2_value_struct *vio=new fun2_value_struct();
|
||||
|
||||
pxt->SwapJs2CUtf8(pxt->GetArgv(0), vio->in0); uint32_t len8=pxt->GetArrayLength(pxt->GetArgv(1));
|
||||
for(uint32_t i8=0;i8<len8;i8++) {
|
||||
NUMBER_TYPE_4 tt8;
|
||||
NUMBER_JS_2_C(pxt->GetArrayElement(pxt->GetArgv(1),i8),NUMBER_TYPE_4,tt8);
|
||||
vio->in1.push_back(tt8);
|
||||
}napi_value tnv9 = pxt->GetValueProperty(pxt->GetArgv(2), "name");
|
||||
if(tnv9!=nullptr){pxt->SwapJs2CUtf8(tnv9,vio->in2.name);}napi_value tnv10 = pxt->GetValueProperty(pxt->GetArgv(2), "age");
|
||||
if(tnv10!=nullptr){NUMBER_JS_2_C(tnv10,NUMBER_TYPE_1,vio->in2.age);}
|
||||
|
||||
fun2(vio->in0, vio->in1, vio->in2, vio->out);
|
||||
|
||||
napi_value result = nullptr;
|
||||
uint32_t len1=vio->out.size();
|
||||
for(uint32_t i=0;i<len1;i++) {
|
||||
napi_value tnv1 = nullptr;
|
||||
{
|
||||
napi_value tnv2 = nullptr;
|
||||
tnv2 = pxt->SwapC2JsUtf8(vio->out[i].name.c_str());
|
||||
pxt->SetValueProperty(tnv1,"name",tnv2);
|
||||
}{
|
||||
napi_value tnv2 = nullptr;
|
||||
tnv2 = NUMBER_C_2_JS(pxt, vio->out[i].age);
|
||||
pxt->SetValueProperty(tnv1,"age",tnv2);
|
||||
}
|
||||
pxt->SetArrayElement(result, i, tnv1);
|
||||
}
|
||||
|
||||
delete vio;
|
||||
if (pxt->IsFailed())
|
||||
result = pxt->GetError();
|
||||
delete pxt;// release
|
||||
return result;
|
||||
}
|
||||
struct fun3_value_struct {
|
||||
std::string in0;
|
||||
|
||||
std::string out;
|
||||
};
|
||||
|
||||
napi_value fun3_middle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
if (pxt->IsFailed())
|
||||
{
|
||||
napi_value err = pxt->GetError();
|
||||
delete pxt;
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
struct fun3_value_struct *vio=new fun3_value_struct();
|
||||
|
||||
pxt->SwapJs2CUtf8(pxt->GetArgv(0), vio->in0);
|
||||
|
||||
fun3(vio->in0, vio->out);
|
||||
|
||||
napi_value result = nullptr;
|
||||
result = pxt->SwapC2JsUtf8(vio->out.c_str());
|
||||
|
||||
{
|
||||
napi_value args[1] = {result};
|
||||
pxt->SyncCallBack(pxt->GetArgv(1), 1, args);
|
||||
}
|
||||
result = pxt->UndefinedValue();
|
||||
|
||||
delete vio;
|
||||
if (pxt->IsFailed())
|
||||
result = pxt->GetError();
|
||||
delete pxt;// release
|
||||
return result;
|
||||
}
|
||||
struct fun4_value_struct {
|
||||
std::string in0;
|
||||
|
||||
std::string out;
|
||||
};
|
||||
|
||||
void fun4_execute(XNapiTool *pxt, void *data)
|
||||
{
|
||||
fun4_value_struct *vio = (fun4_value_struct *)data;
|
||||
|
||||
|
||||
fun4(vio->in0, vio->out);
|
||||
}
|
||||
|
||||
void fun4_complete(XNapiTool *pxt, void *data)
|
||||
{
|
||||
fun4_value_struct *vio = (fun4_value_struct *)data;
|
||||
|
||||
napi_value result = nullptr;
|
||||
result = pxt->SwapC2JsUtf8(vio->out.c_str());
|
||||
|
||||
{
|
||||
napi_value args[1] = {result};
|
||||
pxt->FinishAsync(1, args);
|
||||
}
|
||||
|
||||
delete vio;
|
||||
}
|
||||
|
||||
napi_value fun4_middle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
if (pxt->IsFailed())
|
||||
{
|
||||
napi_value err = pxt->GetError();
|
||||
delete pxt;
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
struct fun4_value_struct *vio=new fun4_value_struct();
|
||||
|
||||
pxt->SwapJs2CUtf8(pxt->GetArgv(0), vio->in0);
|
||||
|
||||
|
||||
napi_value result = pxt->StartAsync(fun4_execute, vio, fun4_complete, pxt->GetArgc() == 2 ? pxt->GetArgv(1) : nullptr);
|
||||
|
||||
if (pxt->IsFailed())
|
||||
result = pxt->GetError();
|
||||
return result;
|
||||
}
|
||||
namespace Space3 {
|
||||
|
||||
class TestClass2_middle {
|
||||
public:
|
||||
static napi_value constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = new XNapiTool(env, info);
|
||||
|
||||
TestClass2 *p = new TestClass2();
|
||||
// printf("static constructor %x\n", p);
|
||||
|
||||
napi_value thisvar = pxt->WrapInstance(p, release);
|
||||
|
||||
return thisvar;
|
||||
}
|
||||
static void release(void *p)
|
||||
{
|
||||
// printf("test2 released\n");
|
||||
TestClass2 *p2 = (TestClass2 *)p;
|
||||
delete p2;
|
||||
}
|
||||
|
||||
static napi_value getvalue_haha(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
TestClass2 *p = (TestClass2 *)pxt->UnWarpInstance();
|
||||
napi_value result;
|
||||
result = NUMBER_C_2_JS(pxt, p->haha);
|
||||
delete pxt;
|
||||
return result;
|
||||
}
|
||||
static napi_value setvalue_haha(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
|
||||
TestClass2 *p = (TestClass2 *)pxt->UnWarpInstance();
|
||||
NUMBER_JS_2_C(pxt->GetArgv(0),NUMBER_TYPE_5,p->haha);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
};
|
||||
struct fun3_value_struct {
|
||||
std::string in0;
|
||||
|
||||
std::string out;
|
||||
};
|
||||
|
||||
napi_value fun3_middle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
if (pxt->IsFailed())
|
||||
{
|
||||
napi_value err = pxt->GetError();
|
||||
delete pxt;
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
struct fun3_value_struct *vio=new fun3_value_struct();
|
||||
|
||||
pxt->SwapJs2CUtf8(pxt->GetArgv(0), vio->in0);
|
||||
|
||||
fun3(vio->in0, vio->out);
|
||||
|
||||
napi_value result = nullptr;
|
||||
result = pxt->SwapC2JsUtf8(vio->out.c_str());
|
||||
|
||||
delete vio;
|
||||
if (pxt->IsFailed())
|
||||
result = pxt->GetError();
|
||||
delete pxt;// release
|
||||
return result;
|
||||
}
|
||||
namespace Space4 {
|
||||
|
||||
class TestClass3_middle {
|
||||
public:
|
||||
static napi_value constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = new XNapiTool(env, info);
|
||||
|
||||
TestClass3 *p = new TestClass3();
|
||||
// printf("static constructor %x\n", p);
|
||||
|
||||
napi_value thisvar = pxt->WrapInstance(p, release);
|
||||
|
||||
return thisvar;
|
||||
}
|
||||
static void release(void *p)
|
||||
{
|
||||
// printf("test2 released\n");
|
||||
TestClass3 *p2 = (TestClass3 *)p;
|
||||
delete p2;
|
||||
}
|
||||
|
||||
static napi_value getvalue_hoho(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
TestClass3 *p = (TestClass3 *)pxt->UnWarpInstance();
|
||||
napi_value result;
|
||||
result = NUMBER_C_2_JS(pxt, p->hoho);
|
||||
delete pxt;
|
||||
return result;
|
||||
}
|
||||
static napi_value setvalue_hoho(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
|
||||
TestClass3 *p = (TestClass3 *)pxt->UnWarpInstance();
|
||||
NUMBER_JS_2_C(pxt->GetArgv(0),NUMBER_TYPE_6,p->hoho);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct add_value_struct {
|
||||
std::vector<NUMBER_TYPE_7> in0;
|
||||
|
||||
NUMBER_TYPE_8 out;
|
||||
};
|
||||
|
||||
static napi_value add_middle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
if (pxt->IsFailed())
|
||||
{
|
||||
napi_value err = pxt->GetError();
|
||||
delete pxt;
|
||||
return err;
|
||||
}
|
||||
TestClass3 *pInstance = (TestClass3 *)pxt->UnWarpInstance();
|
||||
|
||||
struct add_value_struct *vio=new add_value_struct();
|
||||
|
||||
uint32_t len11=pxt->GetArrayLength(pxt->GetArgv(0));
|
||||
for(uint32_t i11=0;i11<len11;i11++) {
|
||||
NUMBER_TYPE_7 tt11;
|
||||
NUMBER_JS_2_C(pxt->GetArrayElement(pxt->GetArgv(0),i11),NUMBER_TYPE_7,tt11);
|
||||
vio->in0.push_back(tt11);
|
||||
}
|
||||
|
||||
pInstance->add(vio->in0, vio->out);
|
||||
|
||||
napi_value result = nullptr;
|
||||
result = NUMBER_C_2_JS(pxt, vio->out);
|
||||
|
||||
delete vio;
|
||||
if (pxt->IsFailed())
|
||||
result = pxt->GetError();
|
||||
delete pxt;// release
|
||||
return result;
|
||||
}
|
||||
};
|
||||
struct fun3_value_struct {
|
||||
std::string in0;
|
||||
|
||||
std::string out;
|
||||
};
|
||||
|
||||
napi_value fun3_middle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
|
||||
if (pxt->IsFailed())
|
||||
{
|
||||
napi_value err = pxt->GetError();
|
||||
delete pxt;
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
struct fun3_value_struct *vio=new fun3_value_struct();
|
||||
|
||||
pxt->SwapJs2CUtf8(pxt->GetArgv(0), vio->in0);
|
||||
|
||||
fun3(vio->in0, vio->out);
|
||||
|
||||
napi_value result = nullptr;
|
||||
result = pxt->SwapC2JsUtf8(vio->out.c_str());
|
||||
|
||||
delete vio;
|
||||
if (pxt->IsFailed())
|
||||
result = pxt->GetError();
|
||||
delete pxt;// release
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
napi_value init(napi_env env, napi_value exports)
|
||||
{
|
||||
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, exports);
|
||||
|
||||
{
|
||||
std::map<const char *,std::map<const char *,napi_callback>> valueList;
|
||||
valueList["name"]["getvalue"]=napitest::Human_middle::getvalue_name;
|
||||
valueList["name"]["setvalue"]=napitest::Human_middle::setvalue_name;
|
||||
valueList["age"]["getvalue"]=napitest::Human_middle::getvalue_age;
|
||||
valueList["age"]["setvalue"]=napitest::Human_middle::setvalue_age;
|
||||
std::map<const char *, napi_callback> funcList;
|
||||
pxt->DefineClass("Human", napitest::Human_middle::constructor, valueList ,funcList);
|
||||
}
|
||||
{
|
||||
std::map<const char *,std::map<const char *,napi_callback>> valueList;
|
||||
valueList["ahuman"]["getvalue"]=napitest::TestClass1_middle::getvalue_ahuman;
|
||||
valueList["ahuman"]["setvalue"]=napitest::TestClass1_middle::setvalue_ahuman;
|
||||
valueList["num1"]["getvalue"]=napitest::TestClass1_middle::getvalue_num1;
|
||||
valueList["num1"]["setvalue"]=napitest::TestClass1_middle::setvalue_num1;
|
||||
valueList["str1"]["getvalue"]=napitest::TestClass1_middle::getvalue_str1;
|
||||
valueList["str1"]["setvalue"]=napitest::TestClass1_middle::setvalue_str1;
|
||||
valueList["nums"]["getvalue"]=napitest::TestClass1_middle::getvalue_nums;
|
||||
valueList["nums"]["setvalue"]=napitest::TestClass1_middle::setvalue_nums;
|
||||
valueList["strs"]["getvalue"]=napitest::TestClass1_middle::getvalue_strs;
|
||||
valueList["strs"]["setvalue"]=napitest::TestClass1_middle::setvalue_strs;
|
||||
valueList["mans"]["getvalue"]=napitest::TestClass1_middle::getvalue_mans;
|
||||
valueList["mans"]["setvalue"]=napitest::TestClass1_middle::setvalue_mans;
|
||||
std::map<const char *, napi_callback> funcList;
|
||||
funcList["if_direct"] = napitest::TestClass1_middle::if_direct_middle;
|
||||
funcList["if_callback"] = napitest::TestClass1_middle::if_callback_middle;
|
||||
funcList["if_async"] = napitest::TestClass1_middle::if_async_middle;
|
||||
pxt->DefineClass("TestClass1", napitest::TestClass1_middle::constructor, valueList ,funcList);
|
||||
}
|
||||
pxt->DefineFunction("fun2", napitest::fun2_middle);
|
||||
pxt->DefineFunction("fun3", napitest::fun3_middle);
|
||||
pxt->DefineFunction("fun4", napitest::fun4_middle);
|
||||
{
|
||||
napi_value Space3=pxt->CreateSubObject(exports,"Space3");
|
||||
{
|
||||
std::map<const char *,std::map<const char *,napi_callback>> valueList;
|
||||
valueList["haha"]["getvalue"]=napitest::Space3::TestClass2_middle::getvalue_haha;
|
||||
valueList["haha"]["setvalue"]=napitest::Space3::TestClass2_middle::setvalue_haha;
|
||||
std::map<const char *, napi_callback> funcList;
|
||||
pxt->DefineClass("TestClass2", napitest::Space3::TestClass2_middle::constructor, valueList ,funcList, Space3);
|
||||
}
|
||||
pxt->DefineFunction("fun3", napitest::Space3::fun3_middle, Space3);
|
||||
{
|
||||
napi_value Space4=pxt->CreateSubObject(Space3,"Space4");
|
||||
{
|
||||
std::map<const char *,std::map<const char *,napi_callback>> valueList;
|
||||
valueList["hoho"]["getvalue"]=napitest::Space3::Space4::TestClass3_middle::getvalue_hoho;
|
||||
valueList["hoho"]["setvalue"]=napitest::Space3::Space4::TestClass3_middle::setvalue_hoho;
|
||||
std::map<const char *, napi_callback> funcList;
|
||||
funcList["add"] = napitest::Space3::Space4::TestClass3_middle::add_middle;
|
||||
pxt->DefineClass("TestClass3", napitest::Space3::Space4::TestClass3_middle::constructor, valueList ,funcList, Space4);
|
||||
}
|
||||
pxt->DefineFunction("fun3", napitest::Space3::Space4::fun3_middle, Space4);
|
||||
}}
|
||||
|
||||
return exports;
|
||||
}
|
||||
|
||||
static napi_module g_napitest_Module = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = init,
|
||||
.nm_modname = "",
|
||||
.nm_priv = ((void *)0),
|
||||
.reserved = {(void *)0},
|
||||
};
|
||||
|
||||
extern "C" __attribute__((constructor)) void Register_napitest_Module(void)
|
||||
{
|
||||
napi_module_register(&g_napitest_Module);
|
||||
}
|
@ -1 +0,0 @@
|
||||
node-gyp configure build && sleep 0.5 && node --expose-gc test.js
|
@ -1,437 +0,0 @@
|
||||
|
||||
|
||||
#include "x_napi_tool.h"
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
#define CC_ASSERT(btrue) \
|
||||
if (!(btrue)) \
|
||||
{ \
|
||||
} \
|
||||
assert(btrue);
|
||||
|
||||
XNapiTool::XNapiTool(napi_env env, napi_callback_info info)
|
||||
{
|
||||
env_ = env;
|
||||
bFailed_ = false;
|
||||
executeFunction_ = nullptr;
|
||||
completeFunction_ = nullptr;
|
||||
valueData_ = nullptr;
|
||||
asyncNeedRelease_ = false;
|
||||
asyncMode_ = AsyncMode::NONE;
|
||||
pInstance_ = nullptr;
|
||||
releaseInstance_ = nullptr;
|
||||
wrapper_ = nullptr;
|
||||
|
||||
argc_ = 8;
|
||||
|
||||
napi_status result_status = napi_get_cb_info(env, info, &argc_, argv_, &thisVar_, &data_);
|
||||
CheckFailed(result_status == napi_ok, "get args fail");
|
||||
}
|
||||
|
||||
XNapiTool::XNapiTool(napi_env env, napi_value exports)
|
||||
{
|
||||
env_ = env;
|
||||
exports_ = exports;
|
||||
|
||||
asyncMode_ = AsyncMode::NONE;
|
||||
wrapper_ = nullptr;
|
||||
}
|
||||
|
||||
XNapiTool::~XNapiTool()
|
||||
{
|
||||
if (asyncMode_ == AsyncMode::PROMISE)
|
||||
{
|
||||
napi_status result_status = napi_delete_async_work(env_, work_);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
}
|
||||
if (asyncMode_ == AsyncMode::CALLBACK)
|
||||
{
|
||||
napi_status result_status = napi_delete_reference(env_, callbackFunc_);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
result_status = napi_delete_async_work(env_, work_);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
}
|
||||
if (wrapper_ != nullptr)
|
||||
{
|
||||
napi_status result_status = napi_delete_reference(env_, wrapper_);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
}
|
||||
/*printf("----------------release XNapiTool\n");*/
|
||||
}
|
||||
|
||||
napi_value XNapiTool::GetArgv(uint32_t p)
|
||||
{
|
||||
if (CheckFailed(p < argc_, "GetArgv失败"))
|
||||
return error_;
|
||||
|
||||
return argv_[p];
|
||||
}
|
||||
|
||||
uint32_t XNapiTool::GetArgc()
|
||||
{
|
||||
return argc_;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::GetValueProperty(napi_value value, const char *propertyName)
|
||||
{
|
||||
napi_value result;
|
||||
napi_status result_status = napi_get_named_property(env_, value, propertyName, &result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::SetValueProperty(napi_value &value, const char *propertyName, napi_value property)
|
||||
{
|
||||
napi_status result_status;
|
||||
if (value == nullptr)
|
||||
{
|
||||
result_status = napi_create_object(env_, &value);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
}
|
||||
result_status = napi_set_named_property(env_, value, propertyName, property);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32_t XNapiTool::GetArrayLength(napi_value value)
|
||||
{
|
||||
uint32_t ret;
|
||||
napi_status result_status = napi_get_array_length(env_, value, &ret);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return ret;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::GetArrayElement(napi_value value, uint32_t p)
|
||||
{
|
||||
napi_value result;
|
||||
napi_status result_status = napi_get_element(env_, value, p, &result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::SetArrayElement(napi_value &value, uint32_t p, napi_value ele)
|
||||
{
|
||||
napi_status result_status;
|
||||
if (value == nullptr)
|
||||
{
|
||||
result_status = napi_create_array(env_, &value);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
}
|
||||
result_status = napi_set_element(env_, value, p, ele);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return value;
|
||||
}
|
||||
|
||||
bool XNapiTool::CheckFailed(bool b, const char *errStr)
|
||||
{
|
||||
if (bFailed_)
|
||||
return true;
|
||||
if (b)
|
||||
return false;
|
||||
|
||||
napi_value errCode = nullptr;
|
||||
napi_value errMessage = nullptr;
|
||||
|
||||
napi_create_string_utf8(env_, "x_tool", strlen("x_tool"), &errCode);
|
||||
napi_create_string_utf8(env_, errStr, strlen(errStr), &errMessage);
|
||||
napi_create_error(env_, errCode, errMessage, &error_);
|
||||
printf("x_napi_tool err : %s\n", errStr);
|
||||
|
||||
bFailed_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
int32_t XNapiTool::SwapJs2CInt32(napi_value value)
|
||||
{
|
||||
int32_t result;
|
||||
napi_status result_status = napi_get_value_int32(env_, value, &result);
|
||||
if (CheckFailed(result_status == napi_ok, "swap_js_2_c_int32 fail"))
|
||||
return -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t XNapiTool::SwapJs2CUint32(napi_value value)
|
||||
{
|
||||
uint32_t result;
|
||||
napi_status result_status = napi_get_value_uint32(env_, value, &result);
|
||||
if (CheckFailed(result_status == napi_ok, "swap_js_2_c_uint32 fail"))
|
||||
return -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
int64_t XNapiTool::SwapJs2CInt64(napi_value value)
|
||||
{
|
||||
int64_t result;
|
||||
napi_status result_status = napi_get_value_int64(env_, value, &result);
|
||||
if (CheckFailed(result_status == napi_ok, "swap_js_2_c_int32 fail"))
|
||||
return -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
double_t XNapiTool::SwapJs2CDouble(napi_value value)
|
||||
{
|
||||
double_t result;
|
||||
napi_status result_status = napi_get_value_double(env_, value, &result);
|
||||
if (CheckFailed(result_status == napi_ok, "swap_js_2_c_int32 fail"))
|
||||
return -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t XNapiTool::SwapJs2CUtf8(napi_value value, std::string &str)
|
||||
{
|
||||
char buf[1024];
|
||||
size_t result;
|
||||
napi_status result_status = napi_get_value_string_utf8(env_, value, buf, 1024, &result);
|
||||
if (CheckFailed(result_status == napi_ok, "napi_get_value_string_utf8 fail"))
|
||||
return -1;
|
||||
str = buf;
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::SwapC2JsInt32(int32_t value)
|
||||
{
|
||||
napi_value result;
|
||||
napi_status result_status = napi_create_int32(env_, value, &result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::SwapC2JsUint32(uint32_t value)
|
||||
{
|
||||
napi_value result;
|
||||
napi_status result_status = napi_create_uint32(env_, value, &result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::SwapC2JsInt64(int64_t value)
|
||||
{
|
||||
napi_value result;
|
||||
napi_status result_status = napi_create_int64(env_, value, &result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::SwapC2JsDouble(double_t value)
|
||||
{
|
||||
napi_value result;
|
||||
napi_status result_status = napi_create_double(env_, value, &result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::SwapC2JsUtf8(const char *value)
|
||||
{
|
||||
napi_value result;
|
||||
napi_status result_status = napi_create_string_utf8(env_, value, NAPI_AUTO_LENGTH, &result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool XNapiTool::CheckValueType(napi_value value, napi_valuetype type)
|
||||
{
|
||||
napi_valuetype value_type;
|
||||
napi_status result_status = napi_typeof(env_, value, &value_type);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
if (CheckFailed(value_type == type, "传入参数类型不是回调函数"))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::SyncCallBack(napi_value func, size_t argc, napi_value *args)
|
||||
{
|
||||
napi_value cb_result;
|
||||
napi_status result_status = napi_call_function(env_, thisVar_, func, argc, args, &cb_result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return cb_result;
|
||||
}
|
||||
|
||||
void XNapiTool::AsyncExecuteFunction()
|
||||
{
|
||||
if (executeFunction_ != nullptr)
|
||||
{
|
||||
executeFunction_(this, valueData_);
|
||||
}
|
||||
}
|
||||
void XNapiTool::AsyncExecute(napi_env env, void *p)
|
||||
{
|
||||
XNapiTool *pxt = (XNapiTool *)p;
|
||||
pxt->AsyncExecuteFunction();
|
||||
}
|
||||
void XNapiTool::AsyncCompleteFunction()
|
||||
{
|
||||
if (completeFunction_ != nullptr)
|
||||
{
|
||||
completeFunction_(this, valueData_);
|
||||
}
|
||||
}
|
||||
void XNapiTool::AsyncComplete(napi_env env, napi_status status, void *p)
|
||||
{
|
||||
XNapiTool *pxt = (XNapiTool *)p;
|
||||
pxt->AsyncCompleteFunction();
|
||||
delete pxt;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::StartAsync(CallbackFunction pe, void *data, CallbackFunction pc, napi_value func)
|
||||
{
|
||||
napi_value result;
|
||||
napi_status result_status;
|
||||
|
||||
if (func == nullptr)
|
||||
{ // promise
|
||||
result_status = napi_create_promise(env_, &deferred_, &result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
asyncMode_ = AsyncMode::PROMISE;
|
||||
}
|
||||
else
|
||||
{ // callback
|
||||
result_status = napi_create_reference(env_, func, 1, &callbackFunc_);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
asyncMode_ = AsyncMode::CALLBACK;
|
||||
result = UndefinedValue(env_);
|
||||
}
|
||||
|
||||
napi_value resourceName = nullptr;
|
||||
result_status = napi_create_string_utf8(env_, "x_napi_tool", NAPI_AUTO_LENGTH, &resourceName);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
result_status = napi_create_async_work(env_, nullptr, resourceName, XNapiTool::AsyncExecute, XNapiTool::AsyncComplete, this, &work_);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
result_status = napi_queue_async_work(env_, work_);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
|
||||
asyncNeedRelease_ = true;
|
||||
executeFunction_ = pe;
|
||||
completeFunction_ = pc;
|
||||
valueData_ = data;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void XNapiTool::FinishAsync(size_t argc, napi_value *args)
|
||||
{
|
||||
if (asyncMode_ == AsyncMode::PROMISE)
|
||||
{
|
||||
if (argc > 0)
|
||||
{
|
||||
napi_resolve_deferred(env_, deferred_, args[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
napi_reject_deferred(env_, deferred_, SwapC2JsUtf8("promise fail"));
|
||||
}
|
||||
return;
|
||||
}
|
||||
napi_value result = 0;
|
||||
napi_value cb = 0;
|
||||
|
||||
napi_status result_status = napi_get_reference_value(env_, callbackFunc_, &cb);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
result_status = napi_call_function(env_, thisVar_, cb, argc, args, &result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
}
|
||||
|
||||
napi_value XNapiTool::UndefinedValue(napi_env env)
|
||||
{
|
||||
napi_value result;
|
||||
napi_get_undefined(env, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::UndefinedValue()
|
||||
{
|
||||
napi_value result;
|
||||
napi_get_undefined(env_, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value XNapiTool::CreateSubObject(napi_value parent, const char *name)
|
||||
{
|
||||
napi_value result;
|
||||
napi_status result_status = napi_create_object(env_, &result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
|
||||
result_status = napi_set_named_property(env_, parent, name, result);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void XNapiTool::DefineFunction(const char *funcName, napi_callback callback, napi_value dest)
|
||||
{
|
||||
if (dest == nullptr)
|
||||
dest = exports_;
|
||||
napi_property_descriptor descriptor[] = {
|
||||
{funcName, 0, callback, 0, 0, 0, napi_default, 0}};
|
||||
|
||||
napi_status result_status = napi_define_properties(env_, dest, 1, descriptor);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
}
|
||||
|
||||
void XNapiTool::DefineClass(const char *className, napi_callback constructorFunc, std::map<const char *, std::map<const char *, napi_callback>> &valueList, std::map<const char *, napi_callback> &funcList, napi_value dest)
|
||||
{
|
||||
if (dest == nullptr)
|
||||
dest = exports_;
|
||||
napi_value tmpClass = nullptr;
|
||||
napi_property_descriptor funcs[funcList.size() + valueList.size()];
|
||||
|
||||
uint32_t p = 0;
|
||||
for (auto it = valueList.begin(); it != valueList.end(); it++)
|
||||
{
|
||||
funcs[p++] = {it->first, 0, 0, it->second["getvalue"], it->second["setvalue"], 0, napi_default, 0}; // get,set
|
||||
}
|
||||
for (auto it = funcList.begin(); it != funcList.end(); it++)
|
||||
{
|
||||
funcs[p++] = {it->first, 0, it->second, 0, 0, 0, napi_default, 0};
|
||||
}
|
||||
|
||||
napi_status result_status = napi_define_class(env_, className, NAPI_AUTO_LENGTH, constructorFunc, nullptr, p, funcs, &tmpClass);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
|
||||
result_status = napi_set_named_property(env_, dest, className, tmpClass);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
}
|
||||
|
||||
void XNapiTool::WrapFinalize(napi_env env, void *data, void *hint)
|
||||
{
|
||||
// (void)env;
|
||||
// (void)hint;
|
||||
XNapiTool *pxt = (XNapiTool *)data;
|
||||
pxt->ReleaseInstance();
|
||||
delete pxt;
|
||||
}
|
||||
|
||||
void XNapiTool::ReleaseInstance()
|
||||
{
|
||||
if (releaseInstance_ != nullptr)
|
||||
{
|
||||
releaseInstance_(pInstance_);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value XNapiTool::WrapInstance(void *instance, RELEASE_INSTANCE ri)
|
||||
{
|
||||
pInstance_ = instance;
|
||||
releaseInstance_ = ri;
|
||||
napi_status result_status = napi_wrap(env_, thisVar_, this, WrapFinalize, nullptr, &wrapper_);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return thisVar_;
|
||||
}
|
||||
|
||||
void *XNapiTool::UnWarpInstance()
|
||||
{
|
||||
XNapiTool *p;
|
||||
napi_status result_status = napi_unwrap(env_, thisVar_, (void **)&p);
|
||||
CC_ASSERT(result_status == napi_ok);
|
||||
return p->pInstance_;
|
||||
}
|
||||
|
||||
void XNapiTool::SetAsyncInstance(void *p)
|
||||
{
|
||||
asyncInstance_ = p;
|
||||
}
|
||||
|
||||
void *XNapiTool::GetAsyncInstance()
|
||||
{
|
||||
return asyncInstance_;
|
||||
}
|
@ -1,122 +0,0 @@
|
||||
#ifndef CC_TOOL_H
|
||||
#define CC_TOOL_H
|
||||
|
||||
#include <node_api.h>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
class XNapiTool
|
||||
{
|
||||
public:
|
||||
using CallbackFunction = void (*)(XNapiTool *pxt, void *data);
|
||||
using RELEASE_INSTANCE = void (*)(void *p);
|
||||
static napi_value UndefinedValue(napi_env env);
|
||||
napi_value UndefinedValue();
|
||||
|
||||
napi_value CreateSubObject(napi_value parent, const char *name);
|
||||
void DefineFunction(const char *funcName, napi_callback callback, napi_value dest = nullptr);
|
||||
void DefineClass(const char *className, napi_callback constructorFunc, std::map<const char *, std::map<const char *, napi_callback>> &valueList, std::map<const char *, napi_callback> &funcList, napi_value dest = nullptr);
|
||||
|
||||
XNapiTool(napi_env env, napi_callback_info info);
|
||||
XNapiTool(napi_env env, napi_value exports);
|
||||
~XNapiTool();
|
||||
|
||||
int32_t SwapJs2CInt32(napi_value value);
|
||||
uint32_t SwapJs2CUint32(napi_value value);
|
||||
int64_t SwapJs2CInt64(napi_value value);
|
||||
double_t SwapJs2CDouble(napi_value value);
|
||||
size_t SwapJs2CUtf8(napi_value value, std::string &str);
|
||||
|
||||
napi_value SwapC2JsInt32(int32_t value);
|
||||
napi_value SwapC2JsUint32(uint32_t value);
|
||||
napi_value SwapC2JsInt64(int64_t value);
|
||||
napi_value SwapC2JsDouble(double_t value);
|
||||
napi_value SwapC2JsUtf8(const char *value);
|
||||
|
||||
napi_value GetArgv(uint32_t p);
|
||||
uint32_t GetArgc();
|
||||
|
||||
napi_value GetValueProperty(napi_value value, const char *propertyName);
|
||||
napi_value SetValueProperty(napi_value &value, const char *propertyName, napi_value property);
|
||||
|
||||
uint32_t GetArrayLength(napi_value value);
|
||||
napi_value GetArrayElement(napi_value value, uint32_t p);
|
||||
napi_value SetArrayElement(napi_value &value, uint32_t p, napi_value ele);
|
||||
|
||||
napi_value SyncCallBack(napi_value func, size_t argc, napi_value *args);
|
||||
|
||||
napi_value StartAsync(CallbackFunction pe, void *data, CallbackFunction pc, napi_value func = nullptr);
|
||||
void FinishAsync(size_t argc, napi_value *args);
|
||||
|
||||
bool IsFailed()
|
||||
{
|
||||
return bFailed_;
|
||||
}
|
||||
napi_value GetError()
|
||||
{
|
||||
return error_;
|
||||
}
|
||||
napi_env GetEnv()
|
||||
{
|
||||
return env_;
|
||||
}
|
||||
|
||||
napi_value tmp_value;
|
||||
|
||||
private:
|
||||
napi_env env_;
|
||||
napi_value exports_;
|
||||
|
||||
//解析参数
|
||||
napi_value argv_[8];
|
||||
size_t argc_;
|
||||
napi_value thisVar_;
|
||||
void *data_;
|
||||
|
||||
//错误信息
|
||||
napi_value error_;
|
||||
bool bFailed_;
|
||||
bool CheckFailed(bool b, const char *errStr);
|
||||
bool CheckValueType(napi_value value, napi_valuetype type);
|
||||
|
||||
//异步调用相关代码
|
||||
static void AsyncExecute(napi_env env, void *p);
|
||||
void AsyncExecuteFunction();
|
||||
static void AsyncComplete(napi_env env, napi_status status, void *p);
|
||||
void AsyncCompleteFunction();
|
||||
napi_ref callbackFunc_;
|
||||
napi_async_work work_;
|
||||
bool asyncNeedRelease_;
|
||||
CallbackFunction executeFunction_;
|
||||
CallbackFunction completeFunction_;
|
||||
void *valueData_;
|
||||
napi_deferred deferred_;
|
||||
enum class AsyncMode
|
||||
{
|
||||
NONE,
|
||||
CALLBACK,
|
||||
PROMISE,
|
||||
};
|
||||
AsyncMode asyncMode_;
|
||||
|
||||
//创建类相关代码
|
||||
public:
|
||||
static void WrapFinalize(napi_env env, void *data, void *hint);
|
||||
void ReleaseInstance();
|
||||
napi_value WrapInstance(void *instance, RELEASE_INSTANCE ri);
|
||||
void *UnWarpInstance();
|
||||
|
||||
void SetAsyncInstance(void *p);
|
||||
void *GetAsyncInstance();
|
||||
|
||||
private:
|
||||
napi_ref wrapper_;
|
||||
void *pInstance_;
|
||||
RELEASE_INSTANCE releaseInstance_;
|
||||
void *asyncInstance_;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user