在V8 JavaScript引擎中,如何将FunctionTemplate添加为另一个带有C ++ API的FunctionTemplate的属性? [英] In the V8 Javascript Engine, how do you add a FunctionTemplate as an attribute of another FunctionTemplate with the C++ API?

查看:804
本文介绍了在V8 JavaScript引擎中,如何将FunctionTemplate添加为另一个带有C ++ API的FunctionTemplate的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数设置为返回一个包装的C ++对象时调用

I have a function set up to return a wrapped C++ object when called as

new MyClass();

但我也想说

MyClass.do_something();

我知道如何做我想要的纯javascript:

I know how to do what I want in pure javascript:

MyClass.prototype = { do_something: function(){}};

但是如何在C ++中做同样的事情?

but how do I do the same in C++?

我知道v8 :: FunctionTemplate上的InstanceTemplate()和PrototypeTemplate()方法,但是这些方法似乎只用于创建 new MyClass() 被调用。如何获得实际函数的原型?

I'm aware of the InstanceTemplate() and PrototypeTemplate() methods on v8::FunctionTemplate, but those seem to only be used in the creation of the new object returned when new MyClass() is called. How do I get at the actual function's prototype?

谢谢。

我看到了这篇文章,不确定是否相关:添加功能模板到v8中的全局对象原型

I saw this post, but am not sure if it's relevant: Add a function template to a global object prototype in v8

推荐答案

结果我在某种程度上反思了问题。

Turns out I was way overthinking the problem.

您只需在v8 :: FunctionTemplate上调用.Set(),并传入另一个v8 :: FunctionTemplate作为值。

You simply call .Set() on your v8::FunctionTemplate and pass in another v8::FunctionTemplate as the value.

my_constructor_function_template.Set("static_method_name", static_method_function_template);

这篇关于在V8 JavaScript引擎中,如何将FunctionTemplate添加为另一个带有C ++ API的FunctionTemplate的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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