leaningtech / cheerp-meta

Cheerp - a C/C++ compiler for Web applications - compiles to WebAssembly and JavaScript
https://labs.leaningtech.com/cheerp
Other
1.03k stars 51 forks source link

How to write a callback like Function.apply and Function.call ? #67

Closed feifeigd closed 5 years ago

feifeigd commented 7 years ago

sample javascript code:

__proto.run=function(widthClear){
                    var caller=this.caller;
                    if (caller && caller.destroyed)return this.clear();
                    var method=this.method;
                    var args=this.args;
                    widthClear && this.clear();
                    if (method==null)return;
                    args ? method.apply(caller,args):method.call(caller);
                }

method is a Function

smvv commented 7 years ago

Can you provide a C++ example of what you want to achieve?

You can use [[cheerp::jsexport]] to export C++ methods to JavaScript. See also: https://github.com/leaningtech/cheerp-meta/wiki/JavaScript-interoperability

cncmd commented 5 years ago

The question of how to use the callback method is well understood. Asynchronous programming, event messages or callbacks are now supported in more fashionable languages.

This should not be elegant, in cheerp you can use the same thinking as javascript, all of which compilers have handled for us very well.

Callback can use cheerp:: Callback ()

You can also use getter/setter to use callbacks using dynamic decoration, or you can write Ramda expressions.

PID = setTimeout (cheerp:: Callback ([]() - > void {

///hook video events install

}, TICK_T);

The good thing is that the bottom of cheerp can already satisfy some programs that rely heavily on JS libraries.

JS - > C + + - > WASM mixed programming is realized.

by 钟元 agent.zy@aliyun.com