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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Cfront 是一个用于将 C++ 程序翻译成 C 语言的编译器前端。下面是如何使用 Cfront 将 C++ 程序翻译成 C 语言的步骤: 1. 安装 Cfront:首先,需要下载并安装 Cfront 编译器。Cfront 是一个比较古老的工具,可以在一些历史文档中找到它的源码。一旦安装完成,确保将其添加到系统环境变量中,以便能在命令行中直接访问 Cfront。 2. 准备 C++ 源代码:将要翻译成 C 语言的 C++ 源代码保存到某个文件中,确保代码正确无误并且可以通过 Cfront 进行处理。 3. 执行 Cfront 命令:使用命令行工具进入保存源代码的目录,并执行以下命令:`cfront your_cpp_file.cpp` 4. 检查生成的 C 代码:Cfront 会将翻译后的 C 代码输出到一个名为 `your_cpp_file.C` 的文件中。打开该文件,检查生成的 C 代码是否正确。 5. 编译 C 代码:使用适当的 C 语言编译器来编译生成的 C 源代码。例如,如果您使用的是 GCC 编译器,可以执行以下命令:`gcc your_cpp_file.C -o your_output_file` 6. 运行 C 代码:编译成功后,运行生成的可执行文件,以验证翻译过程是否正确。 请注意,Cfront 是一个相对较旧的工具,并且可能不支持所有 C++ 特性。因此,在将 C++ 程序翻译为 C 的过程中,可能需要进行一些手动的修改和调整,以使代码正确且能够在 C 编译器中成功构建。 ### 回答2: Cfront是一个用于将C++代码转换为C代码的转译器。下面是使用Cfront将C++代码翻译成C代码的步骤: 1. 首先,确保你的系统上已经安装了Cfront。Cfront是一个早期的C++编译器,已经很少使用了,但你可能会在一些旧的系统上找到它。 2. 将C++源代码文件(例如example.cpp)放置在一个文件夹中,以便于使用Cfront进行处理。 3. 打开终端或命令提示符,并导航到包含C++源代码文件的文件夹。 4. 在命令行中使用Cfront命令,将C++代码翻译成C代码。命令应该类似于:cfront -c example.cpp 5. Cfront将处理C++代码,并将生成一个C代码文件。这个文件的文件名通常与C++源代码文件的文件名相同,只是文件扩展名不同(.c而不是.cpp)。 6. 最后,你可以使用任何C编译器(例如gcc或clang)对生成的C代码进行编译。例如,使用gcc命令编译生成的C代码文件:gcc -o example example.c 7. 现在你可以运行生成的可执行文件(例如example)来验证翻译的C代码是否按预期工作。 需要注意的是,由于Cfront是一个早期的C++编译器,它可能无法处理一些更现代的C++特性。此外,由于Cfront已经很少使用,它可能无法在所有系统上正常工作。在使用Cfront进行转译之前,请确保仔细阅读和理解相关文档,并对源代码进行适当的测试和验证。 ### 回答3: Cfront是一个将C++代码翻译成C代码的编译器前端。Cfront的输入是C++代码,其输出是等价的C代码。 使用Cfront将C++翻译成C的主要步骤如下: 1. 安装Cfront:首先需要从合适的来源(例如GitHub)上下载并安装Cfront编译器。 2. 编写C++代码:使用任何文本编辑器编写C++代码,并保存为.cpp文件。 3. 使用Cfront进行翻译:打开命令行终端,导航到保存C++代码的目录,并运行Cfront编译器,指定需要翻译的C++文件。 4. 检查翻译结果:Cfront会将翻译结果保存为等价的C代码。打开生成的C文件,检查并验证是否正确完成了翻译。 需要注意的是,Cfront在将C++代码翻译成C代码时会尽可能保留C++的语法结构和特性。但由于C和C++的语法差异,部分C++语法和特性无法完全翻译到C中,并且Cfront并不保证生成的C代码能够正确编译和运行。 此外,由于Cfront是一个早期的C++编译器前端,目前已经不再维护和更新。因此,在实际使用中,了解更现代和广泛使用的C++到C转换工具,如Emscripten和Cheerp等,可能更为实用和可靠。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值