Libmocha

The libmocha code governs the interactions between the core JavaScript runtime engine and Navigator. The libmocha files reside in the directory ns/lib/libmocha. The original internal development code name for JavaScript was Mocha--hence, the library name is libmocha. The function prefix is LM_ , and all of the filenames in that directory begin with LM_ .

To understand the code in libmocha, you should be reasonably familiar with the JavaScript API.

The libmocha files are responsible for reflecting client-specific objects into the JavaScript world. For example, when the user opens a new window, libmocha creates a new object to represent the window. When that window loads an HTML document, libmocha creates new objects to represent the various forms, links, and other objects on the page. These new objects are window-specific and are hooked up by libmocha as children of the containing window.

Some objects in the client-side JavaScript world are unique: only one copy is created for the entire Navigator session instead of one per window. The navigator object is the best known example of such an object.

Threading

In 2.x and 3.x versions of Navigator, libmocha and JavaScript ran in the same thread as the core browser. This shared thread caused a variety of problems. For instance, users couldn't use the Stop button to stop errant scripts. There were also structure lifetime problems (sometimes executing a script closed a window) and some LiveConnect class-loading problems. In version 4.x, these problems were solved by moving libmocha and the JavaScript core engine into its own thread, called the "mocha thread," but this thread segregation caused a new set of problems.

A new layer of code was added to support message passing between the core browser (running in the mozilla thread) and the mocha thread. The ET (EventTransfer) layer handles the passing of messages between the two threads. Code to send events from the mozilla thread to the mocha thread is in the file et_mocha.c. Code to send events from the mocha thread to the mozilla thread is in the file et_moz.c.

To avoid deadlocks between the threads, the mocha thread is allowed to block waiting for the mozilla thread in order to execute an action. By contrast, the mozilla thread is not allowed to block waiting for the mocha thread for any reason. When a running script calls window.open(), the mocha thread sends a message to the mozilla thread and then calls wait() until the mozilla thread creates a new window and sends back a handle to the window. In contrast, when an HTML document is being laid out in the mozilla thread and a script is encountered, layout is blocked. A message is sent instructing the mocha thread to evaluate the script, and the mozilla thread returns to its main event loop. When the mocha thread has finished evaluating the script, it sends a message to the mozilla thread, which unblocks layout and continues processing the document.


Copyright © 1998 Netscape Communications Corporation