在V8中,如何将原始类型(如null,undefined和boolean)存储在内存中? [英] In V8, how are primitive types such as null, undefined, and boolean stored in memory?
问题描述
boolean
是否在内存中存储为32字节整数?如果 null
值?
Is a boolean
stored as a 32-byte integer in memory? How about a null
value?
在书中使用Javascript ,
它指的是类型标记,用于指示存储在内存中的值的类型。例如 Object
类型的类型标签是000.什么是类型标签?
In the book Speaking Javascript,
it refers to a type tag being used to indicate the type of a value stored in memory. e.g. The type tag for Object
type was 000. What is a type tag?
我如何找到类型标记的值类型,例如布尔
或字符串
?
How would I find the type tag of a value type such as a boolean
or string
?
推荐答案
来自Andy Wingo的关于该主题的博客文章:
From Andy Wingo's blog post on the topic:
最初,所有JavaScript实现都使用标记指针代表JS值。这是一个老技巧,来自观察到分配的内存占用至少4或8个字节,并以最低有效位或3为零的方式对齐。
Initially, all JavaScript implementations used tagged pointers to represent JS values. This is a old trick that comes from the observation that allocated memory takes up at least 4 or 8 bytes, and are aligned in such a way that the least significant bit or three will be zero.
因此类型标签允许统一存储所有值。所有值占用一个机器字(32/64位),并且根据标记(最低有效位或位),它们被解释为指向对象的指针或根据标记的某些整数/布尔/等。
So the type tags allow for all values to be stored uniformly. All values occupy one machine word (32/64 bit) and depending on the tag (which is the least significant bit or bits) they are interpreted either as a pointer to an object or as some integer/boolean/etc depending on the tag.
布尔存储为js内存中的32字节整数?
is boolean stored as a 32-byte integer in js memory?
布尔值也占用一个单词。对于更具体的答案,我需要通过v8源代码。但如果我没记错的话, true
和 false
表示为根指针。
A boolean also occupies one word. For a more specific answer I'd need to go though the v8 source. But if I remember correctly, true
and false
are represented as root pointers.
如何获取值类型的类型标记(boolean,undefined,string,number);
how to get the type tag of a value type(boolean,undefined,string, number);
无法通过JavaScript实现。这是内部实施细节。
No way to do it from JavaScript. It's internal implementation details.
这篇关于在V8中,如何将原始类型(如null,undefined和boolean)存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!