Sorry for all of the RKelly updates, but thats what I've been doing in my free time. I mean, besides J-School. But I don't think anyone wants to read my poor Japanese!
I wanted to make my ASTs easily searchable, so I added an AOP style interface to my AST. The AST will now let you pointcut your javascript and give you back a list of all of the points it finds. Take this javascript for example:
Element.update('another_id',"blah blah blah");
Element.update(10,"blah blah blah");
} catch (e) { }
Lets say you wanted to find every place that Element.update() was called with 2 arguments, and mess with those arguments. You can pointcut the ast, then modify the arguments by giving the AST a pattern to match on like so:
ast = parser.parse(DATA.read)
ast.pointcut("Element.update(Object, Object)").matches.each do |m|
m.arguments.value.each { |x|
x.value = rand(20)
}
end
puts ast.to_ecma
But lets say you only want to update the call that was made with a number as the first parameter. No problem! Just change your pattern to use a number, like so:
ast = parser.parse(DATA.read)
ast.pointcut("Element.update(Number, Object)").matches.each do |m|
m.arguments.value.each { |x|
x.value = rand(20)
}
end
puts ast.to_ecma
You can even get more specific and match the arguments exactly. For example, matching just the function call where the first argument is 'another_id':
ast = parser.parse(DATA.read)
ast.pointcut("Element.update('another_id', Object)").matches.each do |m|
m.arguments.value.each { |x|
x.value = rand(20)
}
end
Maybe you don't care the update is being called on an Element, but you want to match all places that update is being called on something. The pointcut will match on node type too, so this is a perfectly valid pattern:
ast = parser.parse(DATA.read)
ast.pointcut("ResolveNode.update('another_id', Object)").matches.each do |m|
m.arguments.value.each { |x|
x.value = rand(20)
}
end
Hopefully you get the picture. This feature isn't full tested yet, but I think I might do my first RKelly release after I finish testing.
One Comment
I had a dream last night that I deleted your blog because right before bed last night, I deleted all my WP stuff and then I woke up in a sweaty panic and checked tried to check http://tenderlovemaking.com and that stupid yellow caution icon and cannot find this file cannot connect to the server thing kept appearing on the screen and then i went to the ftp and it was doing the same thing so I thought maybe that I had deleted the entire ftp then Steven Segal appeared and was like … where’s the server WHERE’S THE SERVER and I was like I don’t know.. it was here when I went to bed and he was like YOU’RE A LIAR and then I woke up AGAIN (double nightmare) and checked and now I am going to tell you about it.
Pretty sure I’m going to make a movie about this.