Closed bernardmizzi closed 6 years ago
Hi @bernardmizzi. Meld overwrites methods on objects--probably similar to what your implementation does. For functions, it simply returns wrapped functions (since functions can't be modified). ES Proxy might be an interesting direction to look into as well, for comparison.
Hope that helps. Cheers!
I managed to get this library to work on the browser, just one last question. I know you mentioned that it overrides methods and returns wrapped functions, my library does the following:
var nameOfTheDesiredFunction = 'functionA'
var function = window[nameOfTheDesiredFunction]
window[nameOfTheDesiredFunction] = function(){
...
//before functions
var returnValue = function.apply(this, arguments)
...
//after functions
return returnValue
}
Do you think that meld and my library do the same thing?
Hey @bernardmizzi,
Very generally, yes, meld and your code snippets use the same general strategy. I like to call it "function replacement" (or "method replacement").
The code snippet you pasted seems to only work on global functions. Meld works on functions that could be declared inside closures, declared inside modules, or attached to contexts (i.e. methods). Meld also provides a mechanism to remove the advised function, replacing the original function again (but only for methods, afaict).
Thank you for your response, I'm still going to use this library to compare it to mine. Do you think that all AOP libraries have to make use of this method?
When we first started investigating AOP for JavaScript, the only other strategy we investigated (IIRC) was code injection. I think code coverage tools (like istanbul) and code tracing tools (AWS X-Ray?) use this, but I don't know any js AOP libs that do.
I am a bit dubious that there are popular libs that do this at run-time since code injection probably requires full AST parsing if you want it to safely add advice in all situations. There may be babel plugins (build-time AOP) that use code injection, but I didn't bother to look. 😃
Actually, my babel answer doesn't make sense since babel only transforms standardized (ECMAScript) features and there are no proposed standards for AOP (yet???). Still, there may be build-time AOP libs on npm, but I haven't looked.
Thanks a lot mate :)
So I managed to compare the time it took for meld and for my library. My library carries out the procedure that I wrote in one of the previous comments:
var nameOfTheDesiredFunction = 'functionA'
var function = window[nameOfTheDesiredFunction]
window[nameOfTheDesiredFunction] = function(){
...
//before functions
var returnValue = function.apply(this, arguments)
...
//after functions
return returnValue
}
It seems that my library takes shorter time than meld. Do you know what might cause this?
Hi @bernardmizzi. That's great ... as for why, I'd say you'd have to study the two implementations. Performance wasn't one of meld's primary concerns at the time it was created, and it has never really been reported as a problem. I'm sure it's possible to create more efficient implementations.
Meld does implement several features that you either may not need, or maybe haven't yet implemented, such as adding multiple advices, removing advices from anywhere in the advice stack, wildcard pointcut-like matching, joinpoint tracking, etc. I'm sure each of those things adds a bit of overhead.
Is this library sort of overriding the methods and inserting the advice function to achieve AOP? I'm asking this because I implemented an AOP library of my own and I achieved AOP by overriding the functions and adding the necessary code, and so I would like a new library that is different from my own so that I can compare them.