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.