Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to analyze HeapNumber for js engine v8 source code

Shulou Source: shulou.com Published: 2022-06-01 16:06:55 09月19日 Update

In this issue, the editor will bring you about how to carry out js engine v8 source code analysis HeapNumber. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

A HeapNumber is an object that holds a large plastic shape. Smi saves plastic surgery in V8, but it only has 31 bits, and those with more than 31 bits need to use HeapNumber.

/ / A heap object with stored numbers

Class HeapNumber: public HeapObject {

Public:

Inline double value ()

Inline void set_value (double value)

Static inline HeapNumber* cast (Object* obj)

Object* HeapNumberToBoolean ()

/ / Layout description.

/ / the space before kSize stores pointers to map objects

Static const int kValueOffset = HeapObject::kSize

/ / the value of the number stored between kValueOffset-kSize.

Static const int kSize = kValueOffset + kDoubleSize

Private:

DISALLOW_IMPLICIT_CONSTRUCTORS (HeapNumber)

}

The following is implemented.

1 access value

/ / returns the value of type double

Double HeapNumber::value () {

Return READ_DOUBLE_FIELD (this, kValueOffset)

}

/ / write double value to object

Void HeapNumber::set_value (double value) {

WRITE_DOUBLE_FIELD (this, kValueOffset, value)

}

2 digits are converted to Boolean values. 0 and NAN are false, and the rest are true.

Object* HeapNumber::HeapNumberToBoolean () {

/ / NaN, + 0, and-0 should return the false object

Switch (fpclassify (value () {

Case FP_NAN: / / fall through

Case FP_ZERO: return Heap::false_value ()

Default: return Heap::true_value ()

}

}

/ / refer to the definition of floating point number in ieee.

Int fpclassify (double x) {

/ / Use the MS-specific _ fpclass () for classification.

Int flags = _ fpclass (x)

/ / non-0 positive or non-0 negative

If (flags & (_ FPCLASS_PN | _ FPCLASS_NN)) return FP_NORMAL

/ / positive or negative 0

If (flags & (_ FPCLASS_PZ | _ FPCLASS_NZ)) return FP_ZERO

/ / non-standardized positive or negative values

If (flags & (_ FPCLASS_PD | _ FPCLASS_ND)) return FP_SUBNORMAL

/ / positive and negative infinity

If (flags & (_ FPCLASS_PINF | _ FPCLASS_NINF)) return FP_INFINITE

/ / All cases should be covered by the code above.

/ / not a numeric value

ASSERT (flags & (_ FPCLASS_SNAN | _ FPCLASS_QNAN))

Return FP_NAN

}

The above is the editor for you to share how to carry out js engine v8 source code analysis HeapNumber, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Analysis objects numbers storage engines source code content numerical values negative values professional small and medium between rich in content only pointers articles more standards positive numbers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Docker Shulou Tech Info macOS Apple