Where can I find detailed information about implementing closures (for example, in JavaScript or Schema)?

I am well acquainted with the theory of the compiler in general and with runtime environments of traditional procedural and object-oriented languages ​​such as C, Pascal and Java. I want to get a detailed idea of ​​how the runtime structure of a language such as JavaScript is implemented, in particular, how it relates to closure: how the repository is managed, when locks are allocated, how it decides when to close the repository. If you do not copy the source code for V8 or Rhino, can you point me to documents or books or tutorials that can provide such a description?

Thanks.

+7
source share
3 answers

its quite simple, it is a clone of the nested namespace, so its kind of opposite of compiler theory is that this namespace, distributed over runtime, is used to allow character searches compared to a compiled language that compiles characters into static offsets.

but basically all of this means that the search for a character comes from a linked list of namespaces, starting from the local namespace, to private namespaces (closures).

and in javascript, a namespace is just an object with reference counting of any public functions with a link to that namespace

eg.

var links = //a list of links identified set somehwere else for(var i = 0, l = 3; l--; i++){ (function(n){ links[i].onclick = function(){ alert(n); } })(i) } 

in the above code, the links [i] are used to access the array, but when I click the link, I will increase and, therefore, not the same number as the link. therefore, n is set in the parent namespace, so when it fires, the warning (n) will be accurate.

 global | |\ | +-- function(n=0) <- link0.onclick = function() |\ | +-- function(n=1) <- link1.onclick = function() \ +-- function(n=2) <- link2.onclick = function() 

this expands into 3 frames of the stack, one for each iteration i. and the function on the right side of the hte 'onclick =' expression has a link to the stack frame as the lexical parent element, where it starts searching for the character.

they will all end up in the same namespace because they have grandfather and grandmother.

some links:

https://developer.mozilla.org/en/a_re-introduction_to_javascript

this minimal scheme is much easier to read than v8 sources:

http://code.google.com/p/chibi-scheme/

+1
source

This method is called lambda lifting ; it is completely static (therefore, there are no expensive searches for the runtime namespace).

+1
source

This article explains the implementation of closures in javascript.

+1
source

All Articles