KEMBAR78
Understanding Javascript Engines | PPTX
JavaScript EnginesPop the hood
z = x + y
Executing  z = x + yRead operation from memoryGet location of x and yRead values for x and y Unbox x and y.Choose meaning of “+”, perform “+”Save z to memoryDo garbage.
1. Read operation from memory…String “z = x + y” is passed into tokenizer.Webkit uses Flex (LEX)Accommodate semi colon insertion, etc.Tokenizer output fed to parser Webkit uses Bison, bottom up shift reduce parserGecko has top down parserStatement now available as Abstract Syntax Tree (AST)
2. Get locations of x and yX & Y could be number, string, object, null, undefined, array, etc. Offsets directly available for primitivesValues also depend on context of executionClosures (activation contexts)Local VariablesObject propertiesScope modifiers – eval, with, etc.
2.  Get values of x – ArrayIf x is a actually array - obj[x]Dense array have offsetsCreated using 0..N or pushGecko creates sparse array on N..0Adding obj[“name”] fails optimization
2.  Get values of x – ObjectIf X is an object property (obj.x)Looks up current object or up the prototype chainInline Cache (IC) the valueObjects have shape – {x:1} is different from {x:1,y:2}Webkit stores memory offsets in hidden classesNew shape created for every new property.IC can read from prototype without walking treeClosures only save path, still have to walk every time. OpCodes generated for each shapeObj.x ==> read shape1[member1]
3. Read boxed input …JavaScript variable assignments are un-typed. Assignments stored as boxed inputsx could be (int32 | 100) – indicating type and valueJavascript numbers are IEEE-754 floating point.Who cares, just use 32 bit to optimize. Overflow to doubles. Ways to Box values (ref)Tagging the LSBs (ref)Nan Boxing (ref) – 51 bit of NaN space for non-doubles  (Webkit)Nun Boxing (favor doubles in NAN – Mozilla pun)
4.  Unbox x and yFrom box, infer type and value, represent it in native type
Int32 x = 100;
From NunBoxed Values
0x400c 0000 | 0x0000 0000 = not a nan, so double (3.5)
0xFFFF0001 | 0x0000 0040 = Nan space, so Int32 (0x0000 0040)
From NanBoxed Values (0xFFFF80 00000040)
Mask to get pointer, shift to get double
X64 portability, fits in register, but harder to decode5.  Perform “+” : InterpreterIf (typeof x == int32 && typeof y == int32)
result = x <operator> y
If (result overflows), result = float
If (result Nan), result = NaN.
If (typeof x == int32 && typeof y === float)
result = CoarceToFloat(x) + y

Understanding Javascript Engines