JavaScript is an interpreted, object oriented language suitable for embedding in applications and devices. Best known as the scripting language for browsers, JavaScript is general purpose language that can also be used for web server scripting and many other embedded scripting needs. Embedded JavaScript is used by Embedded Server Pages to allow access to HTTP variables and to retrieve dynamic data for presentation to the client's browser.
JavaScript was originally developed by Netscape but has subsequently been standardized as ECMAScript. The ECMA-262 standard describes JavaScript 1.5 which is the basis for Embedded JavaScript.
JavaScript is syntactically similar to C or C++. JavaScript expresssions and statements such as if/else, for, +, -, &&, || are almost identical to C. However, JavaScript is an untyped language that will automatically convert data from one type to another as expressions and statements require. This makes it much more like Perl than a strongly typed language. JavaScript also has objects and methods and resembles Java and C++ in this regard and it supports associative arrays like Pearl or TCL. Thus, JavaScript is a nice blend of some of the best features of C, C++, Java and Perl.
JavaScript is best when it is used for scripting. It was not designed for large-scale application development. For that purpose, strongly typed languages are a better choice.
This JavaScript documentation includes sections on:
For more information about JavaScript, we recommend the excellent book on JavaScript by David Flanagan, JavaScript the Definitive Guide by O'Reilly, ISBN: 0-596-00048-0, 2002.
JavaScript Quick Example
So what does JavaScript actually look like? The following code fragment demonstrates the syntax and some of the features of JavaScript:
/*
* Can use either C or C++ style comments
*/
function justForDemo(start, end) // Arguments do not have types
{
var i, j; // Declare variables with var
var objects = new Array(10); // Create an array with ten elements
for (i = start; i < 10; i++) { // Classic for loop
objects[i] = new Object(); // Store a new object
}
for (o in objects) { // Iterate over all the objects
// Can do something here to each object
}
return "Hello World";
}
justForDemo(0, 10); // Invoke the function
Embedded JavaScript Overview
Embedded JavaScript (EJS) was created as a subset of ECMAScript as specified in the ECMA-262 specification. The ECMA specification defines a complete and extensive language of which Embedded Javascript implements a well defined subset focussed on the needs of the embedded market.
The complete ECMA-262 specification has many features that are not essential for embedded use and the resultant memory footprint for the complete spec (typically > 500K) is too large for many embedded applications. Embedded JavaScript in contrast, requires approximately ~50K of code and 20K of RAM to execute depending on the size of the program.
Design Goals
The design goals for EJS were, to develop a useful and powerful subset of the JavaScript language, while minimizing the system resources required in which to execute. Redundant language features were eliminated and unnecessary features were ommitted. An architecture and implementation was then chosen for EJS to prioritize small memory size above fast execution speed. For example: a recursive descent parser was selected over a table driven parser as it requires considerably less memory.
Recursive Descent Parser
Embedded JavaScript uses a compact, recursive descent parser. This is a one-pass, parse and execute interpreter. Compared to two-pass interpreters that create in-memory representations of the script to execute, the one-pass, recursive descent parser has greatly reduced memory requirements. It is somewhat slower than two-pass interpreters that use Just-in-time compilation technologies.
Multiple Instances
EJS supports multiple independent instances at run-time. Embedded Server Pages uses this feature to allow multiple HTTP requests to execute concurrently.
Garbage Collection
Unlike C and C++ where objects are explicitly destroyed, JavaScript uses an automatic mechanism called Garbage Collectionto reclaim unused memory when objects are no longer used.
Because Embedded JavaScript may be used in embedded real-time systems, determinism in the garbage collection was an important design goal. However, the traditional JavaScript mark and sweep algorithms exhibit decidedly non-deterministic behavior. Consequently, a modified reference counting approach was chosen. This approach is very efficient and uses little CPU or memory to implement, but it can be temporarily miss reclaiming objects that have cyclical references. This is normally not a problem when implemented in systems like Embedded Server Pages that use smaller scripts that run for short durations.
Data Types
Embedded JavaScript borrows from some of the proposed JavaScript 2.0 features to provide a rich suite of types. 32-bit and 64-bit integers are provided along with floating point, boolean and string primitive types. EJS can also be configured when building from source code to select the default numeric type.
Extensible
The Embedded JavaScript capabilities may be extended by publishing new JavaScript functions. JavaScript functions may be created via the ESP C API. See the
Programmers Reference for more details.
Quick Language Overview
Embedded JavaScript implements the following standard JavaScript elements:
- Case sensitivity, white space, optional semicolons, C and C++ style comments
- Identifiers
- Data types including numbers, booleans, strings with quoted backslash characters
- Expressions / operators (< <= == != > >= + - / % << >> ++ -- && || !)
- If/else statement
- for and for/in
- Functions and return statement
- Objects and new statement
- Arrays and associative arrays
The following language elements are not implemented:
- Exceptions including try, catch and throw
- Labeled statements including: break, continue
- Control flow statements including: switch, while, do, export, import, with
- Regular expressions
- The operators === !== <<< >>>
- The prefix ++ and -- operators
- Function, array and object literals
- Standard object methods: toLocaleString, hasOwnProperty, propertyIsEnumerable, isPrototypeOf,
- Standard array methods: length, join, sort, reverse, slice, splice, push, pop, unshift, shift, toLocaleString
- Number and Date classes
- Object class prototypes and class methods
Other differences in Embedded JavaScript from the ECMAScript standard are listed below. All these differences were deliberate design decisions made in support of the design goals.
- Strings are stored in ASCII not UNICODE.
- Number types are integers by default instead of floating point. This is changable as a build time configuration option.