December 19, 2014
Hot Topics:

What's New in JScript .NET?

  • December 21, 2001
  • By Essam Ahmed
  • Send Email »
  • More Articles »

JScript .NET introduces several new features:

  • Compiled code
  • New Data Types
  • New Statements
  • New Directives
  • Support for New Types of Applications

This article introduces you to each of these new features and provides background information where necessary. This article assumes that you're familiar with JavaScript, JScript, or ECMAScript. You should be familiar with keywords like while, for, function, and String, and how to use them in scripts.

Compiled Code

JScript .NET is now a compiled language. To appreciate the benefit that this provides, you first need to understand how the code you write executes when it executes in an environment like Internet Explorer or in an Active Server Page (ASP).

JavaScript and JScript code executes in a host environment. When you create a Web page that contains JavaScript code and open it using a browser, like Internet Explorer, the browser acts as the host for your code. Besides displaying your Web page, the host converts your code into instructions the computer understands and then submits the converted code to your computer's processor to carry out the instructions. The host environment converts each line of the code you write into executable instructions on a just in time basis; in other words, the host and the system's processor don't have any idea of what your code will do next - they have only enough information to carry out the instructions your code provides on a line-by-line basis and only when the code is executing. The entity that performs the conversion is called an interpreter, which is usually a component of either the host environment or the host itself.

While interpreting code as it executes offers advantages like providing developers with instant feedback with regards to their code's execution, the primary drawback is performance. Performance becomes a key factor when you use JScript (or VBScript) in ASP on Microsoft's Internet Information Server (IIS). Since scripting languages are the only means of implementing ASPs, developers have been stuck with relatively poor performance (when compared to the performance of languages like Visual Basic or C++). Performance is not as critical when working with client-side code (code that executes in a user's browser).

The secret to the performance gains developers gain as a result of using Visual Basic, for example, is that a program called a compiler compiles Visual Basic code before it actually executes. A compiler's function is similar to that of the interpreter I described at the beginning of this article, except that the compiler does its work before you execute your program; as a result, Visual Basic code needs to be processed by the Visual Basic compiler before you can execute it.

The JScript .NET compiler translates the JScript .NET code you write into executable instructions. Figure 1 demonstrates how a compiler works.

Figure 1 - How a Compiler Works

Similar to an interpreter, the compiler reviews your code on a line-by-line basis. Also similar to the interpreter, the compiler converts your code into executable instructions - that, however, is where the similarity ends. A compiler has two outputs: an executable program file and a list of errors. As the compiler reviews your code it keeps track of any errors it encounters and continues to convert your code. If the compiler encounters any serious errors in your code, it does not produce an executable file; however, it provides you with a listing of errors that includes the location of the error (the line number in your code) and a description of the error. The primary advantage that a compiler provides over an interpreter is that it provides a list of errors all in one place and before the program executes, making it possible for the compiler to catch a broad range of errors that would otherwise get reported as runtime errors by an interpreter.

Unlike traditional compilers, which emit code that a system's processor can directly execute, .NET compilers - including the JScript .NET compiler - emit code in another language called Microsoft Intermediate Language (MSIL, or simply IL). The compiler stores the IL it generates in a file with a .EXE or .DLL extension (depending on what options you specify to the compiler). An entity, called the Just-In-Time (JIT) compiler (part of the .NET Framework) compiles the IL into executable instructions that the system's processor carries out. The difference between the JIT and an interpreter is that, once code is processed by the JIT it is cached and is usable across all .NET languages while an interpreter re-interprets code each time it executes and discards it as soon as it executes. Figure 2 illustrates, using a sequence diagram, the steps that JScript .NET code goes through to the point it executes (read the diagram from left to right, time flows from the top of the diagram to the bottom).

Figure 2 - Steps JScript .NET code goes through from source to execution

The JScript .NET compiler is called JSC.EXE - it resides in the %SystemRoot%\Microsoft\Framework\version directory. version is the version number of the .NET Framework installed on your system, for example v1. 0. 2914. As of the .NET Framework that's available at the time of this writing (Beta 2), Visual Studio .NET does not provide support for creating or compiling JScript .NET applications. As a result, the only way to compile JScript .NET applications is to invoke the JScript .NET compiler at a command prompt. If you have the .NET Framework installed on your system, type jsc at a command prompt - it should respond with a screen full of information about the options you can use with it. If it does not, open a special command prompt window that the Visual Studio .NET setup creates for you called the Visual Studio .NET Command Prompt, and try again. Access the Visual Studio .NET Command Prompt using the following options from your system's Start menu: Start -> Microsoft Visual Studio.NET -> Visual Studio.NET Tools -> Visual Studio.NET Command Prompt.

The JScript .NET compiler supports a number of options; however, most of the time compiling an application simply involves providing jsc with the name of the file you want to compile. Refer to the MSDN documentation for a complete list of available options. I'll also demonstrate various uses of the compiler as this series progresses.

New Data Types

JScript has a limited set of data types: Boolean, String, Number, and Object. The limited set of data types makes it difficult to determine what parameters a function expects - for example, consider the following line of code:

   a += b;

What data type is a, and what data type is b? Since JScript has a relaxed type system and does a lot of work on behalf of the developer to ensure that the code executes, it is difficult to tell what's going on. Variables a and b could be completely different types, yet the code would still execute. For example, consider this listing:

  a = 1;
  b = "1";
  a += b;

What is the value of a? If you guessed 11, you're 99% correct! The answer is actually "11" - the string that's the result of converting the value of a (the number one) to a String and appending the value of b (the string, "1") to it. This behavior is by design and is the source of lots of bugs.

JScript .NET introduces several new data types and, as a result of compiling JScript .NET code before it executes, offers a means of automatically finding problems like the one shown above, thereby improving developer productivity by eliminating a whole class of problems. The JScript .NET data types are compatible with data types in the Common Type System (CTS) data types, described in this series' last article, ensuring interoperability with other .NET applications. Here is a list of the new data types that JScript .NET supports:

  • boolean
  • byte
  • char
  • decimal
  • double
  • float
  • int
  • long
  • sbyte
  • short
  • String
  • uint
  • ulong
  • ushort

The next article in this series describes the new data types in more detail, how to use them, and how to use the JScript .NET compiler to detect common programming errors with respect to assigning inappropriate values to some JScript .NET types.

New Statements

JScript .NET is now a fully object-oriented programming language; as a result, it supports several new statements. JScript .NET also provides direct support for expressing constant variables and enumerations; Table 1 summarizes the new statements.

Table 1 - Summary of New JScript .NET Statements

Statement Description
class Enables developers to create new types that encapsulate data and behavior. This statement has many options that allow developers to reuse implementations and interfaces.
interface Allows developers to define a common interface for related classes
package Provides a means of logically grouping related classes, interfaces, and data (a package is also known as a <em>namespace</em>)
import This statement allows developers to introduce and incorporate other packages into their applications
const Provides a means of defining a variable whose value does not change
enum This statement allows developers to associate named values with values, making code easier to read and understand

The following listing demonstrates how to use some of the new statements:

// introduce the classes in the System package
// into this application so that the application
// has access to the System classes
import System;

// define a new package
package HumanResources { 

 // create an enumeration for all departments
 enum departmentEnum : int { 
   Accounting,
   Shipping,
   ClientDelivery,
   Operations
  }

 // The person class represents most properties but 
 //  employees have additional properties like
 // salary - this class adds new attributes
 // and functions to the person class
 class employee extends person {

  public var salary : decimal;
  private var dept : departmentEnum;
  private var fullName : String;
 
  // The following is an accessor function - it allows developers
  // to encapsulate private data within a class while at the same
  // time providing controlled access to it through a property
  // as shown below. The function allows users to set a department
  // but changes the value of the department if users specify
  // the Operations member of the departmentEnum 
  // enumeration
  public function set Department(theDept : departmentEnum) : void {
   if(theDept == Operations)
     dept = departmentEnum.ClientDelivery;
   else
     dept = theDept;
  }

  // Similar to the set function above, this accessor allows users
  // to read the value of a class's private data through a funciton
  public function get Department () : departmentEnum {
   return dept;
  }
 
  // the final keyword prevents 
  public final function displayName() : void {
   Console.WriteLine(fullName);
  }

 } // end of class declaration
} // end of package

New Directives

JScript .NET introduces three new directives that are useful for improving your application's performance, assist in debugging, and allow developers to define blocks of code you want other developers to ignore.

You work with directives using the following notation:

@set @[name of option] ([value])

All directives begin with the @set statement, followed by a space, the @ symbol and the name of the directive. Directives have settings you specify in parentheses following the name of the directive.

The @option(fast) directive

The @option(fast) directive causes the JScript .NET compiler to emit code that's faster than it normally emits - thereby by leading to potentially better application performance. To emit the faster code, the compiler requires that developers follow certain rules in their code including:

  • All variables must be declared
  • Function calls must have the correct number of arguments
  • ou cannot redefine functions at runtime
  • You cannot use dynamic (expando) properties

This option is, by default, disabled for code that resides outside of a package; as a result, this directive is useful for smaller applications which, typically, do not use packages.

The @debug directive

This directive allows developers to selectively enable debugging features for sections of their code. Debugging features allow developers to interactively debug their code using the Visual Studio .NET debugger. This directive is useful in scenarios where you're not interested in debugging all of an application's code (such as code that a code generator writes). Use the directive as shown:

@set @debug(on)
ftpServer = new FTPObject("ftp://designs2solutions.com");
ftpResult = ftpServer.PutFile("c:\documents\sample.doc");
@set @debug(off)
textBoxResult.Text = ftpResult;
// ...

The setting of the @debug option remains in effect until it changes.

The @position directive

This directive is useful for code generators, which emit source code that developers can use in their own applications. This directive resets the line counter the compiler uses to report error or warning messages. For example, consider the following code:

var customer : Cust;
customer = new Cust("walk-in");
/* some code that was generated by a design tool 

...implementation omitted for brevity...

*/
@set @position(line = 3)
shippingAddress  customer.ShipToAddress;

// the above line is missing an equals symbol 
// that should appear after the shippingAddress variable

// The compiler reports an error at line 3 since the 
// @position directive changes the value of the 
// compiler's line counter

The listing shows some code that includes code from another source that the developer isn't interested in. When the compiler encounters the last (non-comment) line in the listing, it reports an error on line three instead of some other line that would have been reported since a design tool injected some code in along with the developer's.

Support for New Types of Applications

JScript and JavaScript developers rely on the host environment their code executes in to interact with end users and the underlying operating system. For example, JScript code that executes within Internet Explorer manipulates the DOM (Document Object Model) to change the values of controls on an HTML page and has very limited access to the underlying operating system. Code that executes with an ASP uses statements like Response.Write to generate output.

JScript .NET enables developers to create traditional Windows applications that have menus, interactive dialog boxes, and support multiple documents (MDI applications). JScript .NET also allows developers to create Windows Services (applications that operate in the background while users perform other tasks), XML Web Services, Windows Forms applications, and console applications.

Summary

This article introduced the new features of the JScript .NET programming language in the context of comparing it with is predecessors, JScript and JavaScript. The next installment in this series discusses how JScript .NET types map to types in the Common Type System (CTS), describes how to use the new data types along with how to get the compiler to detect common errors, and how to create your own data types using the new class keyword.

Essam Ahmed is the author of "JScript .NET Programming" (ISBN 0764548689, Published by Hungry Minds September 2001), many articles (including some at CodeGuru.com) and book reviews (also available at CodeGuru.com). Contact Essam at essam@designs2solutions.com, or at his Web site

# # #






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel