Sam Kong wrote:
JavaScript hides its memory structure.
It doesn't officially expose it to the language tools, that would be
more correct to say :-) - but yes, it hides it.
I know that numbers, booleans, null and undefined are value types
(value is directed saved in a variable).
That is hardly possible in a loosely typed language.
When you declare
var myVar;
the engine has no idea what data will be stored in myVar; even if you
instantiate and assign a value to it right away:
var myVar = true;
then it improves the code readability but it doesn't help too much to
the engine, because right the next statement can change the data type:
var myVar = true;
myVar = new Object; // or something else
(The right above is not a good way of course to benefit from the loose
typing: yet the engine has to be ready for such flip around at any
system tick for any variable).
That is the end of generic considerations for *a* script engine:
further it is the question of how *this* engine or *that* engine is
dealing with the challenge.
- How JavaScript distinguishes value types from reference types by
seeing the variable content?
- How many bytes are allocated in memory when I declare the following?
var i;
I'm not ready to comment on the Gecko engine. In the old Netscape (very
fragmentary described in this aspect in ECMAScript specs) and in all
versions of Internet Explorer it is solved by creating a structure of a
kind of a database record with a MEMO field. So it's a fixed size
structure yet able to keep proprietary long data in it by having it
outside of the record and only referenced in the record itself.
So say Internet Explorer on statement
var i;
internally creates new VARIANT object (not that Variant which is in
VBA, but that VARIANT which is VT in C++)
This VARIANT object has a data member for the value (this is an
anonymous C++ union) and a data member indicating the type of
information stored in the union. That is why I used the analogy with a
database record / MEMO field (and surely made very upset all
professional C++ programmers :-)
This VARIANT object also has data member with pointer to IDispatchEx
interface extending the base IDispatch interface: that lets your object
to be a reference to something else or to be a member of some other
object.
It also has a data member with flag set by Garbage Collector. Once for
a while Garbage Collector comes, studies the object and if no in-scope
references found, it raises this flag ("that is a garbage to remove").
There is a few other fields (like a pointer to IUnknown), but the above
members are the most essential for the core functionality.
All this structure in application to the script engine is called
scavenger, and it is not to mix with a heap, because the value itself
is not here but only referenced to.
(The mythological "native ECMAScript object with internal [[values]]"
is an allegoric decription of the scavenger in the way of how ECMA's
free-lancers saw it in 1999 on Netscape 4.x).
>From this point forward your variable is ready to be whatever
programmer's unlimited fantasy :-) will make it be at any moment: a
primitive value, an object reference, a function, an object member - it
will not catch the engine on cold.
The byte-exact size of the structure depends on the current OS and on
the bit-base of the OS (16bit, 32bit, 64bit). You may look at C++ specs
for VARIANT to find out.
If you are really interested in the "night life" of the JScript engine
and if you have some knowledge of C++ programming, search MSDN for
IDispatch and IDispatchEx methods. Then you can write an util to
connect to the engine to see scavengers appearing, GC roaming from
scavenger to scavenger, references changing, the garbage removed.
P.S. JScript description above is not directly applicable to
JScript.NET
The latter is a rather different beast.
P.P.S. From the optimization point of view it is tempting to create at
least short-living primitive values as just primitive values in hope to
the programmer's reasonnable behavior; and in case of type change
dereference the primitive and and to create a VARIANT with the same
name. A pure speculation from my side but maybe such mechanics is
implemented in some engines.