为juggle添加了一个js扩展

如题

得益于一年前,重构juggle的codegen代码时,比较完善的分离了parser和codegen的模块,

在parser模块部分,把dsl脚本

module name{
   void func1();
}

解析成如下的dict:

{'name': [
    ['void', 'func1', []]
]}

dict中的一个key/value对表示一个module,其中key表示module的命名,value表示module中声明的函数

一条函数的声明用一个数组保存,

数组中的第0个关键字是函数的返回值(在早期的juggle中,函数是有返回值的,但是现在出于简化逻辑的原因,返回值被删除,函数返回值一律为void)

后面的关键字依次为函数名和函数参数类型

之后codegen模块接受parser模块解析出的关键字信息,生成对应的事件响应的代码

/*this module file is codegen by juggle for js*/
function test_module(){
    eventobj.call(this);
    Imodule.call(this, "test");

    this.test_func = function(argv0, argv1){
        this.call_event("test_func", [argv0, argv1]);
    }

}
(function(){
    var Super = function(){};
    Super.prototype = Imodule.prototype;
    test_module.prototype = new Super();
})();
test_module.prototype.constructor = test_module;
/*this caller file is codegen by juggle for js*/
function test_caller(ch){
    Icaller.call(this, "test", ch);

    this.test_func = function( argv0, argv1){
        var _argv = [argv0,argv1];
        this.call_module_method.call(this, "test_func", _argv);
    }

}
(function(){
    var Super = function(){};
    Super.prototype = Icaller.prototype;
    test_caller.prototype = new Super();
})();
test_caller.prototype.constructor = test_caller;

因为是js的版本,所以遇到了一个有趣的问题

一个是js没有c++(boost)中的signal,也没有c#中的event,所以我写了个简单的事件映射的模块

function eventobj(){
    this.events = {}

    this.add_event_listen = function(event, this_argv, mothed){
        this.events[event] = {"this_argv":this_argv, "mothed":mothed};
    }

    this.call_event = function(event, argvs){
        if (this.events[event]){
            this.events[event]["mothed"].apply(this.events[event]["this_argv"], argvs);
        }
    }
}

可以看到,生成的js module模块继承了这个eventobj 

juggle地址:https://github.com/qianqians/juggle

然后在稍后的几天,我会编写abelkhan的node.js版本

原文地址:https://www.cnblogs.com/qianqians/p/8616393.html