Monday, May 24, 2010

Spider-monkey Internals

SpiderMonkey Internals 
The JavaScript engine compiles and executes scripts containing JavaScript statements and functions. The engine handles memory allocation for the objects needed to execute scripts, and it cleans up—garbage collects—objects it no longer needs.

The word JavaScript may bring to mind features such as event handlers (like onclick), DOMobjects,, and XMLHttpRequest. But in Mozilla, all of these features are actually provided by other components, not the SpiderMonkey engine itself. SpiderMonkey provides a few core JavaScript data types—numbers, strings, Arrays, Objects, and so on—and a few methods, such as Array.push. It also makes it easy for each application to expose some of its own objects and functions to JavaScript code. Browsers expose DOM objects. 

Spidermonkey includes/implemented JavaScript functions such as eval(), charAt(), escape(), unescape(), encodeURI(), decodeURI(), etc. 

JavaScript Event Handlers (like onClick) are implemented in  the content/event/ module in Firefox. The file use to handle events is content/event/src/nsEventStateManager.cpp. is implemented in the dom/ module. The file  which implemented native function for is  dom/src/base/nsGlobalWindow::OpenJS() in Firefox. window object is not standardized in DOM specifications, therefore, it has vendor specific implementation of it. 

XMLHttpRequest() is implemented in the content/base/ module in Firefox. The file is content/base/src/nsXMLHttpRequest.cpp implementing XHR request.  

C/C++ code accesses SpiderMonkey via the JSAPI, by including the header "jsapi.h". The JSAPI provides functions for setting up the JavaScript runtime, compiling and executing scripts, creating and examining JavaScript data structures, handling errors, enabling security checks, and debugging scripts.

In order to run any JavaScript code in SpiderMonkey, an application must have three key elements: a JSRuntime, a JSContext, and a global object.

Runtimes. A JSRuntime, or runtime, is the space in which the JavaScript variables, objects, scripts, and contexts used by your application are allocated. Every JSContext and every object in an application lives within a JSRuntime. They cannot travel to other runtimes or be shared across runtimes. Most applications only need one runtime.

A program typically has only one JSRuntime, even if it has many threads.

Contexts. A JSContext, or context, is like a little machine that can do many things involving JavaScript code and objects. It can compile and execute scripts, get and set object properties, call JavaScript functions, convert JavaScript data from one type to another, create objects, and so on. Almost all JSAPI functions require a JSContext * as the first argument.

Global objects. Lastly, the global object contains all the classes, functions, and variables that are available for JavaScript code to use. Whenever JavaScript code does something like""), it is accessing a global property, in this case window. JSAPI applications have full control over what global properties scripts can see. The application starts out by creating an object and populating it with the standard JavaScript classes, like Array and Object. Then it adds whatever custom classes, functions, and variables (like window) the application wants to provide; see Custom objects below. Each time the application runs a JS script (using, for example, JS_EvaluateScript), it provides the global object for that script to use. As the script runs, it can create global functions and variables of its own. All of these functions, classes, and variables are stored as properties of the global object.


Wednesday, May 5, 2010

V8 Build Error

I downloaded v8 and followed the build instruction given at .

However, When I executed the command "scons" (without quotes) it shown following errors on my machine.

cc1plus: error: dereferencing pointer 'dest' does break strict-aliasing rules
cc1plus: error: dereferencing pointer 'dest' does break strict-aliasing rules
cc1plus: error: dereferencing pointer 'dest' does break strict-aliasing rules
src/ note: initialized from here
scons: *** [obj/release/api.o] Error 1
scons: building terminated because of errors.

This is bcoz warnings are considered as errors by the compiler.
To solve this problem,
open "SConstruct" file in the source directory of v8 and look for "V8_EXTRA_FLAGS"
Then comment the line (use # to comment line) '-Werror' in

After doing this, I was able to build it. However, I was not able to embed v8 in C++ application.
The reason was, I was running ubuntu x64 bit OS and V8 build is of 32-bit.

I ran following command to build 64-bit libaray of V8.
$ scons mode=debug arch=x64

Where mode=debug to build DEBUG version instead of by default release version. And arch=x64 to build 64-bit version instead of by-default 32-bit version.