appcelerator-archive / titanium-code-processor

Other
29 stars 18 forks source link

Transform #167

Closed hoonto closed 11 years ago

hoonto commented 11 years ago

Hi there, I am thrilled to see this work, it's really a great leap forward and handles a lot of cases that are incredibly difficult if not impossible in static analysis.

But I've come across a problem, I can't seem to find a straightforward way of performing an AST transform either inside of a plugin or inside of a Rule. I've given it a few shots, such as performing an uglify2 transform on "this" inside of a rule like so:

AST.registerRuleProcessor('AST_This', function processRule() {
    var globalcontext = Runtime.getGlobalObject();
    var executioncontext = Runtime.getCurrentContext();
    var isglobal = ! executioncontext.thisBinding._closure.lexicalEnvironment.outer;
    result = executioncontext.thisBinding; 
    if(isglobal){
        var tt = new uglify.TreeTransformer(function(node, descend){
            return new uglify.AST_Symbol({
                name: 'window'
            });
        };
        var new_ast = this.transform(tt);
        console.log(this.print_to_string());       // "this"
        console.log(new_ast.print_to_string()); // "window"
    }
    return result;
}

(I'm not clear on what the return of thisBinding is doing yet, nor am I totally clear on how the preProcess, fireRuleEvent, and postProcess are ultimately affecting things in the existing rules or new rules I might create either - so any clarifications there are very much appreciated as well.)

Thanks in advance, and thanks also for the great work - and thanks Appcelerator for releasing it as well!

Matt

hoonto commented 11 years ago

Not an issue - just needed to run the transform against the top level of the ast. Now just trying to figure out how to re-evaluate the transformed sections, or if this will cause the potential re-entrant issues. Worst case scenario, disposing of the process entirely and re-running though that seems a little heavy handed. Curious if re-entrance is on the roadmap? I did try disposing of the CodeProcessor and Runtime objects and deleting them from the require cache, and re-requiring but that did not seem to work in my limited tests.