nodejs addon C++自动翻译成js

运行gen.py将解析C++代码并生成一个C++导出文件和一个js文件,c++导出文件用于生成dll导出函数供js调用,js和c++接口一一对应。程序员只需编写C++代码和js调用,省去addon繁琐的接口适配。支持异步调用和delegate。


测试C++ delegate

#include 
#include 
#include 

struct TestStruct;
enum MyResult;

class Interface
{
public:
    virtual void OnTestObject(const TestStruct &ts, int a) = 0;
    virtual void OnTestString(const std::string &value) = 0;
    virtual void OnTestInt(int value) = 0;
    virtual void OnTestDouble(double value) = 0;
    virtual void OnTestEnum(MyResult value) = 0;
    virtual void OnTestBool(bool value) = 0;
    virtual void OnTestVoid() = 0;
	virtual void OnTestList(const std::list &value) = 0;
	virtual void OnTestMap(const std::map> &value) = 0;
};

class Interface2
{
public:
	virtual void OnTestInt2(int value) = 0;
	virtual void OnTestPointer(int *value) = 0;
};


测试C++ class

#include 
#include 
#include 
#include "interface.h"

enum MyResult {
	OK = 0
};

struct TestStruct
{
	int n;
	std::string s;
	double d;
	bool b;
	MyResult e;
};

class TestClass
{
public:
	TestClass(void);
	~TestClass(void);

	void SetDelegate(Interface *delegate)
	{
		_delegate = delegate;
	}

	void SetDelegate2(Interface2 *delegate)
	{
		_delegate2 = delegate;
	}

	TestStruct TestObject(const TestStruct &value, int a)
	{
		if (_delegate)
		{
			_delegate->OnTestObject(value, a);
		}

		return value;
	}

	std::string TestString(const std::string &value)
	{
		_value = value;

		if (_delegate)
		{
			_delegate->OnTestString(value);
		}

		return value;
	}

	MyResult TestEnum(MyResult value)
	{
		if (_delegate)
		{
			_delegate->OnTestEnum(value);
		}

		return value;
	}

	bool TestBool(bool value)
	{
		if (_delegate)
		{
			_delegate->OnTestBool(value);
		}

		return value;
	}

	int TestInt(int value)
	{
		if (_delegate)
		{
			_delegate->OnTestInt(value);
		}

		_si += value;

		if (_delegate2)
		{
			_delegate2->OnTestInt2(_si);
		}

		return value;
	}
	
	const int GetValue() const
	{
		return _b;
	}

	double TestDouble(double value)
	{
		if (_delegate)
		{
			_delegate->OnTestDouble(value);
		}

		return value;
	}

	void TestVoid()
	{
		if (_delegate)
		{
			_delegate->OnTestVoid();
		}
	}

	std::list TestList(const std::list &value)
	{
		if (_delegate)
		{
			_delegate->OnTestList(value);
		}

		return value;
	}

	std::map> TestMap(const std::map> &value)
	{
		if (_delegate)
		{
			_delegate->OnTestMap(value);
		}

		return value;
	}

	std::string TestClass::GetString(std::string value);

	void TestPointer(int *p)
	{

	}

	int *TestPointer1()
	{
		return 0;
	}

	int *TestPointer2(int *p)
	{
		return 0;
	}

private:
	std::string _value;
	Interface *_delegate;
	Interface2 *_delegate2;

public:
	std::string _a;
	int _b;
	std::string _c;
	float _d;
	TestStruct _s;
	static int _si;
};

js调用c++测试代码:

var delegate = {
        OnTestObject: function(ts, a) {
          log("OnTestObject : " + JSON.stringify(ts) + " " + a);
        },
        OnTestInt: function(value) {
          log("OnTestInt : " + value);
        },
        OnTestList: function(value) {
          log("OnTestList : " + JSON.stringify(value));
        },
        OnTestMap: function(value) {
          log("OnTestMap : " + JSON.stringify(value));
        }
      }
      var tc = new TestClass();
      tc.SetDelegate(delegate);
      var obj = {
        n: 125,
        s: "hello 世界",
        d: 0.5,
        b: false,
        e: 3
      }
      // 直接调用
      log(JSON.stringify(tc.TestObject(obj, 22)));
      // 异步调用,启动线程调用C++ TestObject接口,调用完成通过callback返回结果
      // 最后一个参数传递function表示异步调用
      tc.TestObject(obj, 22, function(retval) {
        log(JSON.stringify(retval));
      });
      log(tc.TestString("hello 世界!"));
      log(tc.TestInt(22));
      log(tc.TestBool(true));
      log(tc.TestDouble(5.1));
      log(tc.TestEnum(3));
      log(tc.TestVoid());
      log(JSON.stringify(tc.TestList(["hello", "world"])));
      log(JSON.stringify(tc.TestMap({
        "hello": {"he":"llo"},
        "world": {"wo":"rld"}
      })));
      
      var delegate1 = {
        OnTestObject: function(ts, a) {
          log("OnTestObject-- : " + JSON.stringify(ts) + " " + a);
        },
        OnTestInt: function(value) {
          log("OnTestInt-- : " + value);
        },
        OnTestList: function(value) {
          log("OnTestList-- : " + JSON.stringify(value));
        },
        OnTestMap: function(value) {
          log("OnTestMap-- : " + JSON.stringify(value));
        }
      }
      var delegate2 = {
        OnTestInt2: function(value) {
          log("OnTestInt2 : " + value);
        }
      }
      var tc2 = new TestClass();
      tc2.SetDelegate(delegate1);
      tc2.SetDelegate2(delegate2);
      log(tc2.TestInt(5));



你可能感兴趣的:(nodejs addon C++自动翻译成js)