将c ++类导出到duktape [英] export c++ class to duktape

查看:126
本文介绍了将c ++类导出到duktape的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个c ++类Point

say I have a c++ class Point

class Point {
public:
    Point();
    Point(float x, float y);
    ~Point();

    float X;
    float Y;

};

我想添加javascript功能并选择duktape。

I'd like to add javascript functionality to it and chose duktape.

是否可以在javascript中重用此类?
say

is it possible to reuse this class in javascript? say

var p = new Point(1.23, 4.56);

我一直在阅读duktape文档,它只说明如何在javascript中重用函数。

I have been reading the duktape documentation and it only says how to reuse functions inside javascript.

推荐答案

我个人的建议是为它创建C ++绑定,就像在JavaScript中一样。

My personal advice is to create C++ bindings for it just like you would do in JavaScript.

唯一需要的是将真实的C ++对象保存在JavaScript对象中,我们使用内部属性为此目的。

The only need, is to save the real C++ object in the JavaScript object, we use internal properties for that purpose.

您需要创建一个将从JavaScript作为构造函数调用的函数,然后您只需要填充其原型并设置终结者。这并不难,但它需要很多代码,所以你基本上想要创建包装器以使它们更容易。

You need to create a function that will be called from JavaScript as a constructor function, then you just have to fill its prototype and set a finalizer. It's not hard but it required lot of code so you basically want to create wrapper to make them easier.

#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 ++类导出到duktape的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆