为juggle添加了一个js扩展

本文介绍了一种使用DSL定义脚本并将其解析为JavaScript模块的方法。通过自定义的解析器将DSL脚本转换为字典结构,再由代码生成模块生成事件响应代码。针对JavaScript缺乏信号和事件支持的问题,设计了一个简易的事件映射模块。

如题

得益于一年前,重构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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值