cheerp 如何使用对象的回答

本文介绍了在Cheerp环境中如何使用JavaScript对象,包括在C++中定义映射类,使用get_和set_方法访问和修改JS对象属性,以及Cheerp如何优化代码和内存模型。

by 钟元 agent.zy@aliyun.com

如何使用js传递过来的对象?

首先你应该确定你的程序的类型,在cheerp的理念里是不支持出现动态字段的,因为不利于前期推导类型优化。

如果你想使用js的对象, 请在使用之前在client的命名空间下定义一个类的映射。

namespace client {
	class ManiFest {
	public:
	int get_endList();
	int get_allowCache();
	int get_targetDuration();
	int get_mediaSequence();
	int get_discontinuitySequence();
	Array * get_segments();
	Array * get_discontinuityStarts();
	void set_length(long int);
};}

获取操作使用get_方法, cheerp会自动将js的动态属性映射成一个内联方法,通过对象的指针获取。 我们在使用的时候 ManiFest * m = XXX(); m->get_allowCache() /// m.allowCache

设置对象属性我们可以使用set_方法, 同样的道理。 m->set_length(0); /// m.length = 0

这样编译器会在整个上下文和生命周期里,知道在什么时候可以优化我们的代码,如何交织最小的内存模型。

uMaxmaxmaximus opened this issue on 9 Feb 2017 · 2 comments Comments Assignees No one assigned Labels None yet Projects None yet Milestone No milestone Notifications You’re receiving notifications because you commented. 3 participants @uMaxmaxmaximus @alexp-sssup @cncmd @uMaxmaxmaximus

uMaxmaxmaximus commented on 9 Feb 2017 • class [[cheerp::jsexport]] Cat { public: int age; Cat(int age) : age(age) {} };

int webMain() { Cat *cat = new Cat(10); // create C++ object

Object *catObj = new Object(cat); // wrap it to javascript object
window._set("cat", catObj); // set it to window "cat" propperty
return 0;

} in javascript:

cat.age // 10 How can I make something like that? Or I can not? So let's figure out how to implement these things. This is critical for my project, I think not only for my project.

@alexp-sssup Contributor alexp-sssup commented on 13 Feb 2017 This is currently unsupported but it will be possible in the future with some limitations.

@cncmd

cncmd commented a minute from now First of all, you should determine the type of your program. In the concept of cheerp, there is no support for the emergence of dynamic fields, because it is not conducive to pre-derivation type optimization. If you want to use js objects, define a class mapping under the client namespace before using it.

namespace client { class ManiFest { public: int get_endList(); int get_allowCache(); int get_targetDuration(); int get_mediaSequence(); int get_discontinuitySequence(); Array * get_segments(); Array * get_discontinuityStarts(); void set_length(long int); }; }

Getting operations use get_method, cheerp automatically maps the dynamic attributes of JS into an inline method, and obtains them through pointers of objects.

When we use it like this: ManiFest * m = XXX(); m->get_allowCache() /// m.allowCache

We can use the set_method to set object attributes. The same like tthis. m->set_length(0); /// m.length = 0

This way the compiler knows when to optimize our code and how to interweave the smallest memory model throughout the context and lifecycle.

转载于:https://my.oschina.net/littlemonkeyc/blog/2999031

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值