Quick Nav
- Mode Controls
- Language Compliance
- Ecma Mode
- Plus Mode
- Fixed Mode
- Mixing Modes
- Setting Language Mode
- Compiler Checking Mode
- Standard Mode
- Strict Mode
- Setting Language
See Also
Language Modes
Ejscript is fully compliant with the JavaScript (ECMA-262 3.X) standards, but it also offers a set of enhancements and fixes designed to improve the language and correct some longstanding JavaScript design issues. Use of these enhancements and fixes is optional and controlled via opt-in configuration and script pragma directives. Language modes can be selected on a source file-by-file basis. So you can mix and match ECMA-262 pure JavaScript with enhanced Ejscript code if you wish.
Mode Controls
Ejscript provides two control mechanisms:
- A Language Compliance Mode control
- A Compiler Error Checking Mode control
Language Compliance Mode
The language compliance mode dictates what language statements and directives are available. The language mode can be configured by a use lang pragma directive in your scripts. This pragma takes effect on a file by file basis and so allows libraries and programs from various sources to select the mode that best suits their needs.
Ejscript supports three language compliance modes.
- ecma — Strict ECMA-262 3.X compliance.
- plus — Close ECMA-262 3.X compliance with enhancements.
- fixed — Close ECMA-262 3.X compliant with enhancements and some compatibility breaking fixes.
Ecma Mode
Code running in ecma mode has all the available features and capabilities of the ECMA-262 specification. No Ejscript enhancements or fixes are available. This mode is useful for JavaScript code designed for browsers or other JavaScript engines.
Plus Mode
Code running in plus mode has access to all of ECMA-262 and the Ejscript enhancements which are closely compatibile with ECMA-262. This mode is useful to migrate code from ecma mode and to gradually take advantage of Ejscript enhancements.
This mode does introduce new keywords, directives and statements into the language. The enhancements available in this mode include:
- Variable visibility controls
- Block and file scope
- Optional type annotations
- Class, extends, super directives
- Interface directive
- Module directive
- Namespaces
- Use pragmas
- Extended language library: Http, File I/O, Sockets, Stream I/O, XML, Eventing
- Cast, is operators
- E4X XML support
- Iterators and Generators
- Assertions
- Conditional compilation
- Let variable declarations
- Automatic type conversion for function arguments
- Extended try / catch syntax
- Extended operators
- Reflection
- Function rest arguments
Fixed Mode
Code running in fixed mode has access to all of ECMA-262 and all the Ejscript enhancements. Further, this mode fixes some ECMA-262 deficiencies and thus introduces some breaking changes. This mode is the most secure and capable language mode.
The fixes include:
- Assignments to undeclared variables inside functions will create local variables instead of creating unwanted globals.
- Multiple declarations of a variable name at the same scope are prohibited.
- Warn if a function call supplies insufficient actual parameters.
Mixing Modes
The language mode applies at the source file level and it is perfectly acceptable to include code in one program that uses different modes. For example: a library provided by one vendor may be designed to run in fixed mode, whereas the main program from another vendor may expect to run in ecma mode. The Ejscript compiler and VM apply the appropriate language specification depending on the function code that is being compiled or run from time-to-time.
Setting the Language Mode
The language mode control is set to a default value by the configure program when Ejscript is build from source. The default value can then be modified by command line switches to the ejs shell or ec compiler. In turn, this can be overridden by an Ejscript pragma directive in your script files.
Configure Language Switch
To set the language mode for the configure command when building Ejscript from source, use the --lang switch. The switch takes the values: ecma, plus or fixed. The default is plus. This setting defines the global default setting for the language compliance mode.
./configure --lang=ecma ./configure --lang=plus ./configure --lang=fixed
Command Line Language Switch
To set the language mode on the ejs or ec commands, use the --lang switch. The switch takes the values: ecma, plus or fixed. This will override the value defined by the configure command and defines the default setting when compiling or interpreting programs.
ejs --lang=ecma script1.es ejs --lang=plus script2.es ejs --lang=fixed scrip3t.es
Use Language Pragma
To set the language mode for a source file, utilize the use lang pragma directive. The directive takes the values: ecma, plus or fixed. This pragma will override the value defined by the configure command or by the ec or ejs commands.
/* In source file 1 */ use lang ecma /* In source file 2 */ use lang fixed /* In source file 3 */ /* No use pragma - just accept the current default */
Compiler Checking Mode
The Compiler checking mode controls how aggressively the compiler warns about potential program errors.
Ejscript supports two checking modes:
- standard — Traditional JavaScript compilation with little compile time checking
- strict — Uses strict static type checking at compile time
Strict mode is useful for libraries where detecting errors before run-time is highly desirable. Strict mode also assists the compiler to generate faster code as the compiler knows the location and type of all variables ahead of time. However, it does restrict your coding style by requiring that all variables be declared and typed — this should be used on a case-by-case basis.
For users of a module, it does not matter whether the module was created using strict or standard checking. The checking mode only impacts the module source code itself and not the users of the module.
The compiler checking mode can be controlled by a use standard or use strict pragma directive in your scripts. Like the Language Compliance pragma, this pragma takes effect on a file by file basis.
Standard Mode
Standard mode is the traditional JavaScript compilation behavior. Variables do not have to be declared ahead of time and no warnings are issued for incompatible type usage. Issues are resolved at run-time, by dynamic casting or by issuing exceptions.
Strict Mode
Strict mode applies compile time static checking and type conversions to try to handle and catch errors before run-time. In strict mode, variables must be declared before they are used. If the script code is not using ecma language compliance, variables must also be typed. Functions must define a return type and must type their parameters.
Setting the Compiler Checking Mode
The compiler mode control is set to a default value by command line switches to the ejs shell or ec compiler. This can be overridden by an Ejscript pragma directive in your script files on a file-by-file basis.
Command Line Compiler Checking Switch
To set the compiler checking mode on the ejs or ec commands, use the --strict or --standard switch. The default is --standard.
ejs --standard script1.es ejs --strict script2.es
Use Strict and Use Standard Pragmas
To set the compiler checking mode for a source file, utilize the use strict or use standard pragma directives. These pragmas will override the default value for the current source file.
/* In source file 1 */ use strict /* In source file 2 */ use standard /* In source file 3 */ /* No use pragma - just accept the current default */