kevinresol / hxgenjs

Extensible JS generator for Haxe
57 stars 16 forks source link

improve gen extern return type #45

Open sonygod opened 3 years ago

sonygod commented 3 years ago
HxExternClassGenerator.hx
    var processField = function (field:haxe.macro.ClassField, isStatic:Bool) {
            var fieldCode = "";

            if (field != null && field.type != null) {

                //trace(field.type);
                switch (field.type) {
                    case TFun(args, r):
                        var params = "";
                        var i = 0;
                        for (arg in args) {
                            if (params != "") params += ", ";
                            var name = (arg.name != "" && arg.name != null) ? arg.name : "a" + (++i);

                            switch (arg.t){
                                    case TAbstract(t, _):
                                        params += (arg.opt ? "?" : "") + name + ":" + $v{t}/*arg.t.toString ()*/;   
                                    default:    
                                     params += (arg.opt ? "?" : "") + name + ":" + "Dynamic"/*arg.t.toString ()*/;  
                            }

                        }

                        //fieldCode = ((c.type.superClass != null && hasField(field.name, c.type.superClass.t)) ? "override " : "") + (isStatic? "static " : "") + "function " + field.name + "(" + params + ")" + (field.name != "new" ? ":Dynamic" : "") + ";";
                        switch(r){
                            case TAbstract(t,_):

                                fieldCode = ((c.type.superClass != null && hasField(field.name, c.type.superClass.t)) ? "override " : "") + (isStatic? "static " : "") + "function " + field.name + "(" + params + ")" + (field.name != "new" ? ":"+$v{t} : "") + ";";
                             case TInst(t,_):
                                fieldCode = ((c.type.superClass != null && hasField(field.name, c.type.superClass.t)) ? "override " : "") + (isStatic? "static " : "") + "function " + field.name + "(" + params + ")" + (field.name != "new" ? ":"+$v{t} : "") + ";";
                                default:
                                fieldCode = ((c.type.superClass != null && hasField(field.name, c.type.superClass.t)) ? "override " : "") + (isStatic? "static " : "") + "function " + field.name + "(" + params + ")" + (field.name != "new" ? ":Dynamic" : "") + ";";
                            }
                        case TInst(t, _):

                        fieldCode = (isStatic? "static " : "") + "var " + field.name + ":"+$v{t}+";";
                    case TAbstract(t, _):
                        fieldCode = (isStatic? "static " : "") + "var " + field.name + ":"+$v{t}+";";
                    default:
                }
            }

            body += "\t" + fieldCode + "\n";
        }

before

package;

@:jsRequire("MyCustomCompontHx")
extern class MyCustomCompontHx_ex extends global.cc.Component {
    function new();
    var power:Dynamic ;
    function getPower():Dynamic;
}

after

package;

@:jsRequire("MyCustomCompontHx")
extern class MyCustomCompontHx_ex extends global.cc.Component {
    function new();
    var power:Int;
    function getPower():Int;
}
elsassph commented 3 years ago

Sounds good, why don't you do a pull request?

sonygod commented 3 years ago

@elsassph ,I'm working on cocos creator marco function. I'll up request when I had time.