A browser's JavaScript engine can seem like a magical black box. During this session, we'll show you how they work from 10,000 feet and give you the tricks to compile all the popular engines out there including JavaScriptCore, V8, and SpiderMonkey). We'll inspect the internals of the engine, and debug+profile your favorite code snippets. Armed with just a little extra knowledge about this black box, you will be ready to take a new look at JavaScript apps.
5. JavaScript Engines
SpiderMonkey Opera Carakan
JavaScriptCore Microsoft JScript
V8 ...
Open Closed
Monday, November 29, 2010
6. JavaScript Engines
SpiderMonkey
JavaScriptCore
V8
Open
Monday, November 29, 2010
7. SpiderMonkey
First JavaScript engine
created by Brendan Eich
Written in C
Mostly used in Mozilla, Firefox, ...
License: MPL/LGPL/GPL
Monday, November 29, 2010
8. JavaScriptCore (JSC)
Built into WebKit
Forked from KDE’s KJS a long time ago
License: LGPL
Monday, November 29, 2010
9. Other Names of JSC
SquirrelFish
byte-code interpreter
SquirrelFish Extreme (SFX)
native/machine code
Nitro, Nitro Extreme
Apple’s marketing terms
Monday, November 29, 2010
10. Google V8
Written in C++
License: BSD
Used in Chromium (Google Chrome)
Monday, November 29, 2010
11. Qt Script
Uses JSC as the back-end
Does not power any web browser
Powerful bindings, debugger
Useful to make applications scriptable
Monday, November 29, 2010
12. Let’s Go
UNDER THE HOOD
Monday, November 29, 2010
13. Let’s Go Platform +
Compiler
UNDER THE HOOD
Monday, November 29, 2010
39. From Code to Execution
var answer = 42;
Declare a local object
Call it “answer”
Create a (small integer) number 42
Assign it to “answer”
Monday, November 29, 2010
40. Traverse and Run
Variable Declaration
Identifier
Literal Constant
answer 42
Monday, November 29, 2010
41. Bytecode
Serialize tree traversal into
a list of “actions”
Monday, November 29, 2010
42. Machine Code
Compile the bytecodes into
machine instructions
Monday, November 29, 2010
43. Machine Code
Compile the bytecodes into
machine instructions
JIT (=just-in-time) compile
Monday, November 29, 2010
44. Machine Code on V8
Global
shell_g --print-code
When needed
shell_g --expose-debug-as deb
Monday, November 29, 2010
45. Machine Code on V8
Global
shell_g --print-code
When needed
deb.Debug.disassemble(f)
shell_g --expose-debug-as deb
Monday, November 29, 2010
46. “Lazy” Approach
foobar = function(x, y, z)
{
....
}
foobar(x, y, z);
Monday, November 29, 2010
47. “Lazy” Approach
foobar = function(x, y, z) Analyze the syntax
{
....
Mark the position of
} function ‘foobar’
foobar(x, y, z);
Monday, November 29, 2010
48. “Lazy” Approach
foobar = function(x, y, z) Analyze the syntax
{
....
Mark the position of
} function ‘foobar’
foobar(x, y, z);
Compile and run the
function ‘foobar’
Monday, November 29, 2010
79. Get SpiderMonkey
hg clone http://hg.mozilla.org/mozilla-central/
cd mozilla-central/js/src
Monday, November 29, 2010
80. Get JavaScriptCore
svn checkout http://svn.webkit.org/repository/
webkit/trunk webkit
cd webkit/JavaScriptCore
git clone git://git.webkit.org/WebKit.git
cd WebKit/JavaScriptCore
Monday, November 29, 2010
81. Build SpiderMonkey
autoconf213
./configure --disable-debug --enable-optimize
make
Monday, November 29, 2010
82. Build JavaScriptCore
JavaScriptCore/JavaScriptCore.xcodeproj
JavaScriptCore
jsc
Monday, November 29, 2010
85. Build JavaScriptCore
qmake -r DerivedSources.pro
cd JavaScriptCore
make -f Makefile.DerivedSources
qmake && make
qmake jsc.pro && make
Monday, November 29, 2010