On Jun 10, 2:49 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I like to use Rhino to parse a JS file and insert statement at the
> beginning and end of a function.
>
> e.g
>
> myFunction() {
> doSomething();
>
> }
>
> is converted to
>
> myFunction() {
> trace.start('myFunction');
> doSomething();
> trace.end('myFunction');
>
> }
>
> Is it possible using Rhino?
>
> Thanks.
What you want is called "around" advice in Aspect Oriented
Programming. You can implement it pretty easily in JavaScript:
function doSomething(str) {
print("in doSomething(): " + str);
}
// function replaces original function with a wrapper
function wrap(funcName) {
var func = this[funcName];
this[funcName] = function() {
print("trace start");
func.apply(this, arguments);
print("trace stop");
}
}
// call original function
doSomething("foo");
// wrap
wrap("doSomething");
// call wrapped function
doSomething("foo");
Of course, Rhino allows you to do the same in Java, but I guess it's
easier in JavaScript.
hannes
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino