Grades:
- I am not asking what are the differences between dynamically typed and statically typed, be they advantages or disadvantages.
Can someone tell me how dynamic typing works in relation to the memory manager?
How the memory manager (or the part in charge) works to decide if it needs another memory address etc. (to store the data), creating it or expanding the current one if possible (speculation) etc.
For example (speculation): when the variable x = 5
is initialized and assigned to it x = "dynamically"
, the Z
manager part verifies that there is space for the new type, and acts accordingly, reserving a new memory address for the new type and assigning the new address to the initial variable. This is speculation because I don't know how it works exactly "I know there will be different implementations for the same thing" depending on the interpreter/compiler/etc.
What I would like to know is the general idea of Z
-> interpreter/compiler/etc. and the code in charge of such a task.
I've looked at some projects, including Google's V8 , but it's not a small project and I haven't been able to find the part that deals with such an issue, to figure out what the idea is of how it works Z
for the case of V8
.
The answer does not have to be based on V8
it can be on another language, interpreter etc, what interests me is the idea of how it works in x case, or in most cases, (but not a speculation about how it works) , and if possible I would like a reference to the part of the code that reflects the above idea.
A possible implementation, in C, of one
variable
of the Javascript language:Without losing sight of the above, it is relatively easy to imagine the process for dynamically changing types:
The last point implies that each variable must have a scope , a container, a scope , which limits its lifetime.
It also implies that the original variable does not change , except by making an assignment to it.
If we assign a new value to the variable, then it depends. If it is a primitive (by value), it is changed and done.
If it's complex , we'd set the reference count to -1, and check if the number of references
== 0
is there, so we could free up the memory.The topic of reference counting... it's complicated , and I'm not sure I fully understand it myself...
For interpreted languages , it makes little difference whether you use static or dynamic typing; at the end, we are going to search a hash table for the variable, and create it on the heap ; For that matter, we don't care about reserving a block of memory of 20 or 100 bytes (memory reservation optimization techniques aside).
In compiled languages , things change. A variable is an alias for a particular memory address; Seeking the greatest speed, said memory address is fixed throughout the execution time of the program... ergo we cannot change the size of said block ; therefore, we cannot change the type.
There are compiled languages that support great dynamism , although they cheat : for variables that can change type, they always create them on the heap , and access them through a pointer. I recommend you take a look at the source code of
libobjc
, the Objective-C runtime support library (it comes with thegcc
); I don't know in your version2.xx
, but the version1.x
is great for learning about compiled dynamic types.This is as far as we go...for now. I await comments to expand (as far as possible) on a specific topic ... until the question is closed ;-)