导出c++类到duktape
export c++ class to duktape
假设我有一个c++类Point
class Point {
public:
Point();
Point(float x, float y);
~Point();
float X;
float Y;
};
我想添加javascript功能,并选择duktape。
是否可以在javascript中重用该类?说
var p = new Point(1.23, 4.56);
我一直在阅读duktape文档,它只说如何在javascript中重用函数。
我个人的建议是为它创建c++绑定,就像你在JavaScript中做的那样。
唯一需要的,是将真正的c++对象保存在JavaScript对象中,为此我们使用内部属性。
你需要创建一个函数,它将作为构造函数从JavaScript中调用,然后你只需要填充它的原型并设置终结器。这并不难,但它需要大量的代码,所以你基本上想要创建包装器,使他们更容易。
#include <iostream>
#include "duktape.h"
class Point {
public:
float x;
float y;
};
/*
* This is the point destructor
*/
duk_ret_t js_Point_dtor(duk_context *ctx)
{
// The object to delete is passed as first argument instead
duk_get_prop_string(ctx, 0, "xff""xff""deleted");
bool deleted = duk_to_boolean(ctx, -1);
duk_pop(ctx);
if (!deleted) {
duk_get_prop_string(ctx, 0, "xff""xff""data");
delete static_cast<Point *>(duk_to_pointer(ctx, -1));
duk_pop(ctx);
// Mark as deleted
duk_push_boolean(ctx, true);
duk_put_prop_string(ctx, 0, "xff""xff""deleted");
}
return 0;
}
/*
* This is Point function, constructor. Note that it can be called
* as a standard function call, you may need to check for
* duk_is_constructor_call to be sure that it is constructed
* as a "new" statement.
*/
duk_ret_t js_Point_ctor(duk_context *ctx)
{
// Get arguments
float x = duk_require_number(ctx, 0);
float y = duk_require_number(ctx, 1);
// Push special this binding to the function being constructed
duk_push_this(ctx);
// Store the underlying object
duk_push_pointer(ctx, new Point{x, y});
duk_put_prop_string(ctx, -2, "xff""xff""data");
// Store a boolean flag to mark the object as deleted because the destructor may be called several times
duk_push_boolean(ctx, false);
duk_put_prop_string(ctx, -2, "xff""xff""deleted");
// Store the function destructor
duk_push_c_function(ctx, js_Point_dtor, 1);
duk_set_finalizer(ctx, -2);
return 0;
}
/*
* Basic toString method
*/
duk_ret_t js_Point_toString(duk_context *ctx)
{
duk_push_this(ctx);
duk_get_prop_string(ctx, -1, "xff""xff""data");
Point *point = static_cast<Point *>(duk_to_pointer(ctx, -1));
duk_pop(ctx);
duk_push_sprintf(ctx, "%f, %f", point->x, point->y);
return 1;
}
// methods, add more here
const duk_function_list_entry methods[] = {
{ "toString", js_Point_toString, 0 },
{ nullptr, nullptr, 0 }
};
int main(void)
{
duk_context *ctx = duk_create_heap_default();
// Create Point function
duk_push_c_function(ctx, js_Point_ctor, 2);
// Create a prototype with toString and all other functions
duk_push_object(ctx);
duk_put_function_list(ctx, -1, methods);
duk_put_prop_string(ctx, -2, "prototype");
// Now store the Point function as a global
duk_put_global_string(ctx, "Point");
if (duk_peval_string(ctx, "p = new Point(20, 40); print(p)") != 0) {
std::cerr << "error: " << duk_to_string(ctx, -1) << std::endl;
std::exit(1);
}
return 0;
}
相关文章:
- 在c++中使用nlohmann从类到json的转换
- 无法在UE4中包含BP类到CPP类
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 基类到派生类的强制转换向量
- 两个抽象类,派生自同一个基类.如何访问从一个抽象类到另一个抽象类的指针
- C++从"NameBase"类到模板"Name"类的相互关系的成语(或模式)
- 基类到派生模板类的强制转换指针,而不知道类型
- Vulkan-hpp 是reinterpret_casting非标准布局类到另一个类.这合法吗?
- 转换运算符从一个模板类到另一个相关的模板类
- 从子类到具有相同基数的抽象子类的隐式转换
- 如果派生类包含其他方法和成员,则可以static_cast从基类到派生类
- 为什么函数不能正确强制转换指针(从基类到派生类)
- 从类到双精度的隐式转换
- memcpy派生类到基类,为什么还调用基类函数
- 模板化函数指针从子类到父类的转换
- 类到另一个类的实例化:Java到C++的转换
- C++从接口类到子类的转换无效
- C++多态性:从基类到派生类
- C++中的类到类数据类型转换
- 导出c++类到duktape