Shared Source JScript Compiler

The Microsoft® Shared Source implementation of the JScript compiler is very similar to the implementation in the Microsoft® .NET Framework SDK and Microsoft Visual Studio® .NET. Both implementations originated from the same source base but some options and features that rely on functionality in Microsoft® Windows® have been removed from the Shared Source implementation.

To produce an executable JScript program, use the command-line compiler, jsc.exe.  You must have your environment configured correctly.  For more information on the Shared Source CLI environment configuration, see env.html.

New for the Second Source Release

The JScript compiler now supports creating symbolic debug information via the /debug option.

Changes in the Shared Source JScript Implementation

The following option areas and related options were modified:

Complete documentation for the .NET Framework JScript compiler and tutorials are available on msdn.microsoft.com.   Other than the changes mentioned above, the functionality in the Shared Source implementation of the JScript compiler is the same as the .NET Framework implementation.

JScript Compiler Options

The JScript compiler produces executable (.exe) files and dynamic-link libraries (.dll).

JScript is a managed application.  In order to execute managed applications you must use the clix application launcher.

Every compiler option is available in two forms: -option and /option.

[Note: To avoid problems with ambiguity with filenames and / on FreeBSD the only supported option syntax is the -option format.]

Option Purpose
@response_file
Specifies a response_file which is a file that lists compiler options or source code files to compile.

The @ option lets you specify a file that contains compiler options and source code files to compile. These compiler options and source code files will be processed by the compiler just as if set out on the command line.

To specify more than one response file in a compilation, specify multiple response file options. For example:

@file1.rsp @file2.rsp

In a response file, multiple compiler options and source code files can appear on one line. A single compiler option specification must appear on one line (cannot span multiple lines).

Response files can have comments that begin with the # symbol.

Specifying compiler options from within a response file is just like making those commands on the command line.

The compiler processes command options as they are encountered, just as if set out on the command line. Therefore, the options in one response file may be incompatible with the options in another response file or the command line options. This can generate errors.

Response files cannot be nested. You cannot place @response_file inside a response file. The JScript compiler reports an error for such cases. 

Example

The following are a few lines from a sample response file:

# build the first output file
/target:exe /out:MyExe.exe source1.js source2.js
/?
Outputs compiler options to console.   Same as /help.

This option causes the compiler to display a list of compiler options along with a brief description of each option.

/autoref[+ | -]
Automatically references assemblies if they have the same name as an imported namespace or as a type annotation when declaring a variable.

Option Values

+ | -
On by default, unless /nostdlib+ is specified. Specifying /autoref+, or just /autoref, causes the compiler to automatically reference assemblies based on imported namespaces and fully qualified names.

The /autoref option instructs the compiler to reference assemblies without having to pass the assembly to /reference. When you use import to import a namespace, or you use a fully qualified type name in your code, the JScript compiler searches for an assembly that contains the type. See /lib for a discussion of how the JScript compiler searches for assemblies.

The compiler does not try to reference an assembly if it has the same name as the output file of the program you are building.

Example

The following program will compile and run when /autoref+ is in effect; the compiler will reference System.dll as a result of the type annotation when declaring a variable.

var s: System.Collections.Specialized.StringCollection = 
                   new System.Collections.Specialized.StringCollection();
print(s);

The following program will compile and run when /autoref+ is in effect; the compiler will reference System.dll as a result of the import statement.

import System;
var s = new System.Collections.Specialized.StringCollection();
print(s);

These examples also show how the compiler looks for assembly names based on type annotation or import statements. When the compiler did not find an assembly called System.Collections.Specialized.dll that contained StringCollection, it looked for System.Collections.dll. Failing to find that file, it looked for System.dll, which it did find to contain StringCollection.

/codepage:id
Specifies the code page to use for all source code files in the compilation where id is the id of the code page for all the source code files in the compilation.

If you compile one or more source code files that when created did not designate use of the default code page on your computer, you can use the /codepage option to specify which code page should be used. /codepage applies to all source code files in your compilation.

If the source code files were created with the same codepage that is in effect on your computer or if the source code files were created with UNICODE or UTF-8, you need not use /codepage.

/debug[+|-]
Emits debugging information.
/define:name1
[=value1][,name2[=value1]]
Defines preprocessor symbols where name1, name2 are the name of one or more symbols that you want to define and value1, value2 are the values for the symbols to take. These can be booleans or numbers.

The /define option defines names as symbols in your program.

You can define multiple symbols with /define by using a comma to separate symbol names. For example:

/define:DEBUG,trace=true,max_Num=100

/d is the short form of /define.

Example

Compile with /define:xx.

print("testing")
/*@cc_on @*/
/*@if (@xx)
print("xx defined")
@else @*/
print("xx not defined")
/*@end @*/
/fast[+ | -]
Produces an output file optimized for speed but that does not support certain language features from previous releases.  JScript source code from versions before the .NET Framework may need to be modified to compile with this option switched on.

Option Values

+ | -
/fast is on by default. /fast or /fast+ causes the compiler to generate an output file that is speed-optimized, however, if this option is used certain language features from previous versions will not be supported. Specifying /fast-, on the other hand, will provide for backward language compatibility, but the compiler will produce an output file that is not optimized for speed.

When /fast is in effect,

  • All variables must be declared.
  • Functions become constants and you cannot assign to them or redefine them.
  • Predefined properties of built-in objects are marked DontEnum, DontDelete, ReadOnly.
  • Properties on the built-in objects may not be expanded, other than the Global object (which is also the global scope).
  • The arguments variable is not available within function calls.
  • Assignments to read-only variables, fields, or methods generate errors.
/help
Outputs compiler options to console.   Same as /?.

This option causes the compiler to display a list of compiler options along with a brief description of each option.

/lcid:id
Specifies code page for compiler messages where id is the id of the code page to use for printing out messages from the compiler.
/lib:dir1[, dir2]
Specifies the location of assemblies referenced via /reference.

Option Values

dir1
A directory for the compiler to look in if a referenced assembly is not found in the current working directory (the directory from which the compiler is invoked) or in the CLI implementation assembly directory.
dir2
One or more additional directories for searching for assembly references. Separate additional directory names with a comma or semicolon.

The /lib option specifies the location of assemblies referenced via the /reference option.

The compiler searches for assembly references that are not fully qualified in the following order:

  1. Current working directory. This is the directory from which the compiler is invoked.
  2. The common language runtime system directory.
  3. Directories specified by /lib.
  4. Directories specified by the LIB environment variable.

Use /reference to specify an assembly reference.

/lib is additive; specifying it more than once appends to any prior values.

Example

Compile t2.js to create an .exe. The compiler will look in the working directory and in the root directory of the C drive for assembly references.

jsc /lib:c:\ /reference:t2.dll t2.js
/linkresource:filename
[,name[,public|private]]
Creates a link to a managed resource.

Option Values

filename
The resource file to link to the assembly.
name[,public|private] (optional)
The logical name for the resource; the name used to load the resource. The default is the name of the file. Optionally, you can specify whether the file is public or private in the assembly manifest. For example, /linkres:filename.res,myname.res,public. By default, filename is public in the assembly.

The /linkresource option does not embed the resource file in the output file. Use the /resource option to embed a resource file in the output file.

If filename is a managed resource created, for example, by resgen.exe  it can be accessed with members in the System.Resources namespace.

/linkres is the short form of /linkresource.

Example

Compile in.js and link to resource file rf.resource:

jsc /linkresource:rf.resource in.js
/nologo

The /nologo option suppresses display of the banner when the compiler starts.

/nostdlib[+ | -]
Does not import standard library (mscorlib.dll).

Option Values

+ | -
/nostdlib or /nostdlib+ option causes the compiler to not import mscorlib.dll. Use this option if you want to define or create your own System namespace and objects. If you do not specify /nostdlib, mscorlib.dll will be imported into your program (same as specifying /nostdlib-).

Specifying /nostdlib+ also specifies /autoref-.

Example

If you have a component called System. String (or any other name in mscorlib) the only way you could get at your component would be to use

/nostdlib /r:your_library,mscorlib 

to search your library before mscorlib. Commonly, you would not a define a namespace in your application called System.

/out:filename
Specifies output file name where filename is the name of the output file created by the compiler.

The /out option specifies the name of the output file. The compiler expects to find one or more source code files following the /out option.

If you do not specify the name of the output file:

  • An .exe will take its name from the first source code file used to build the output file.
  • A .dll will take its name from the first source code file used to build the output file.

On the command line, it is possible to specify multiple output files for a compilation. All source code files specified after an /out option will be compiled into the output file specified by that /out option.

Specify the full name and extension of the file you want to create. The extension must be either .exe or .dll. It is possible to specify a .dll extension for /t:exe projects.

Example

Compile t2.js and create output file t2.exe and build t3.js and create output file t3.exe:

jsc t2.js /out:t3.exe t3.js
/print[+ | -]
Specifies whether the print statement is available.

Option Values

+ | -
By default, /print or /print+ causes the compiler to enable the use of the print statement. An example of a print statement is,
print("hello world"); 

Specifying /print- will disable the print command.

Remarks

You would use /print- if your .dll will be loaded into an environment that doesn't have a console.

You can set Microsoft.JScript.ScriptStream.Out to be an instance of a TextWriter object to enable print to send output elsewhere.

/reference:file[;file2]
Imports the information in the metadata of the referenced assembly for use in resolving external references where file, file2 are one or more files that contains an assembly manifest. To import more than one file, separate file names with either a comma or a semicolon.

The /reference option directs the compiler to make public type information in the specified files available to the project you are currently compiling.

The file(s) you reference must be assemblies.

/reference cannot take a module as input.

Although executable files can be referenced as assemblies it is not recommended that shared, referenced code be stored in executable assemblies.  Instead, place the shared code in an assembly library.

If you reference an assembly (Assembly A), which itself references another assembly (Assembly B), you will need to reference assembly B if:

  • A type you use from Assembly A inherits from a type or implements an interface from Assembly B.
  • If you invoke a field, property, event, or method that has a return type or parameter type from Assembly B.

Use /lib to specify the directory in which one or more of your assembly references is located.

/r is the short form of /reference.

Example

Compile source file input.js and import metadata from metad1.dll and metad2.dll to produce out.exe:

jsc /reference:metad1.dll;metad2.dll /out:out.exe input.js
/resource:filename
[,name[,public|private]]
Embeds a managed resource in an assembly.

Option Values

filename
The resource file you want to embed in the output file.
name[,public|private] (optional)
The logical name for the resource; the name used to load the resource. The default is the name of the file.
Optionally, you can specify whether the file is public or private in the assembly manifest. For example, /res:filename.res,myname.res,public. By default, filename is public in the assembly.

Use the /resource option to link a resource to an assembly and not place the resource file in the output file.

If filename is a managed resource file created, for example, by resgen.exe  it can be accessed with members in the System.Resources namespace (see System.Resources.ResourceManager for more information).

/res is the short form of /resource.

Example

Compile in.js and attach resource file rf.resource:

jsc /res:rf.resource in.js
/target:output_type
 

/target causes a .NET Framework assembly manifest to be placed in an output file.

If you create an assembly, you can indicate that all or part of your code is CLS compliant with the CLSCompliantAttribute Class attribute.

import System;
[assembly:System.CLSCompliant(true)]   // specify assembly compliance

System.CLSCompliant(true) class TestClass   // specify ompliance for element
{
   var i: int;
}
output_type specifies the format of the output file using one of three options:
/target:exe
Creates a console .exe file.

The /target:exe option causes the compiler to create an executable (EXE), console application. The /target:exe option is in effect by default. The executable file will be created with the .exe extension.

Unless otherwise specified with the /out option, the output file name takes the name of the first source code file in the compilation for the each output file.

When specified at the command line, all files up to the next /out or /target:library option are used to create the .exe. The /target:exe option is in effect for all files since the previous /out or /target:library option.

/target:library
Creates a (.dll) code library.

 

The /target:library option causes the compiler to create a DLL rather than an executable file (EXE). The DLL will be created with the .dll extension.

Unless otherwise specified with the /out option, the output file name takes the name of the first input file.

When specified at the command line, all source files up to the next /out or /target:exe option are used to create the .dll.

/target:winexe
Creates an executable (EXE) Windows GUI application. The executable file will be created with the .exe extension. This sets the subsystem information in the PE file which is used by Microsoft Windows to determine some execution characteristics of the application.

This information is ignored by the Shared Source implementation of the CLI.  However, if the application is executed under the Microsoft .NET Framework this option will have an impact. 

For more information on the console or Windows subsystem setting see msdn.microsoft.com and search for "console subsystem".

Use /target:exe to create a console application.

/utf8output[+ | -]
Displays compiler output using UTF-8 encoding.

Option Values

+ | -
By default /utf8output- displays output directly on the console. Specifying /utf8output or /utf8output+ redirects compiler output to a file.

In some international configurations, compiler output cannot correctly be displayed in the console. In these configurations, use /utf8output and redirect compiler output to a file.

The default for this option is /utf8output-.

Specifying /utf8output is the same as specifying /utf8output+.

/versionsafe[+ | -]
Ensures that all overrides are explicit.

Option Values

+ | -
By default, /versionsafe- is in effect and the compiler will not generate an error if it finds an implicit method override. /versionsafe+, which is the same as /versionsafe, causes the compiler to generate errors for implicit method overrides.

Remarks

Use the hide or override keywords to explicitly indicate the override status of a method. For example, the following code will generate an error when compiled with /versionsafe:

class c
{
function f()
{
}
}
class d extends c 
{
function f()
{
}
}
/warn:option
Sets warning level where option is the minimum warning level you want displayed for the build.

The /warn option specifies the warning level for the compiler to display.
Use /warnaserror to treat all warnings as errors up to the warning level specified. Higher-level warnings are ignored. 

The compiler always displays errors. 

/w is the short form of /warn.

Valid values are 0-4:

Warning level Meaning
0 Turns off emission of all warning messages; display errors only.
1 Displays errors and severe warning messages.
2 Displays all errors and level 1 warnings plus certain, less-severe warnings, such as warnings about hiding class members.
3 Displays errors, level 1 and 2 warnings, plus certain, less-severe warnings, such as warnings about expressions that always evaluate to true or false.
4 Displays all errors, level 1-3 warnings, plus informational warnings. This is the default warning level at the command line.
/warnaserror[+ | -]
Promotes warnings to errors.

Any messages that would ordinarily be reported as warnings are instead reported as errors. No output files are created. The build continues in order to identify as many errors/warnings as possible.

By default, /warnaserror- is in effect, which causes warnings to not prevent the generation of an output file. /warnaserror, which is the same as /warnaserror+, causes warnings to be treated as errors.

Use /warn to specify the level of warnings that you want the compiler to display.

Sample Command Lines

Copyright (c) 2002 Microsoft Corporation