JASS
Encyclopedia
JASS or JASS2 is an event driven scripting language
Scripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...

 used in Blizzard Entertainment
Blizzard Entertainment
Blizzard Entertainment, Inc. is an American video game developer and publisher founded on February 8, 1991 under the name Silicon & Synapse by three graduates of UCLA, Michael Morhaime, Allen Adham and Frank Pearce and currently owned by French company Activision Blizzard...

's Warcraft III
Warcraft III: Reign of Chaos
Warcraft III: Reign of Chaos is a real time strategy computer game released by Blizzard Entertainment on July 3, 2002 . It is the second sequel to Warcraft: Orcs & Humans, and it is the third game set in the Warcraft Universe...

game and its expansion pack
Expansion pack
An expansion pack, expansion set, or supplement is an addition to an existing role-playing game, tabletop game or video game. These add-ons usually add new game areas, weapons, objects, and/or an extended storyline to a complete and already released game...

 The Frozen Throne
Warcraft III: The Frozen Throne
Warcraft III: The Frozen Throne is a real-time strategy computer game developed for Microsoft Windows, Mac OS and Mac OS X by Blizzard Entertainment. It is the official expansion pack to Warcraft III: Reign of Chaos, requiring Reign of Chaos to play...

. Map creators can use it in the World Editor
Warcraft III World Editor
Warcraft III World Editor is the official level editor for the real-time strategy game...

 to create scripts for triggers and AI (artificial intelligence
Artificial intelligence
Artificial intelligence is the intelligence of machines and the branch of computer science that aims to create it. AI textbooks define the field as "the study and design of intelligent agents" where an intelligent agent is a system that perceives its environment and takes actions that maximize its...

) in custom maps and campaigns.

Features

The language provides an extensive API
Application programming interface
An application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...

 that gives programmers control over nearly every aspect of the game world. It can, for example, execute simple GUI functions such as giving orders to units, changing the weather and time of day, playing sounds and displaying text to the player, and manipulating the terrain. JASS can also create powerful functions such as trackables, which detect if a mouse goes over or hits a position, GetLocalPlayer, which can cause disconnects if used improperly (such as using handles with GetLocalPlayer ). It has a syntax
Syntax
In linguistics, syntax is the study of the principles and rules for constructing phrases and sentences in natural languages....

 similar to Turing
Turing programming language
Turing is a Pascal-like programming language developed in 1982 by Ric Holt and James Cordy, then of University of Toronto, Canada. Turing is a descendant of Euclid, Pascal and SP/k that features a clean syntax and precise machine-independent semantics....

 and Delphi, but unlike those languages, it is case sensitive. JASS primarily uses procedural programming
Procedural programming
Procedural programming can sometimes be used as a synonym for imperative programming , but can also refer to a programming paradigm, derived from structured programming, based upon the concept of the procedure call...

 concepts, though popular user-made modifications to Blizzard's World Editor program have since added C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

-like object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 features to the syntax of JASS.

Sample code

The following function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 creates a string
String (computer science)
In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....

 containing the message "Hello, world!" and displays it to all players:

function Trig_JASS_test_Actions takes nothing returns nothing
call DisplayTextToPlayer(GetLocalPlayer, 0, 0, "Hello, world!")
endfunction

or if you want this only for one player:

function Trig_JASS_test_Actions takes player p returns nothing
call DisplayTextToPlayer(p, 0,0, "Hello, world!")
endfunction

And if you want to print the message 90 times and show the iterator:

function Trig_JASS_test_Actions takes player p returns nothing
local integer i=0
loop
exitwhen i90
call DisplayTextToPlayer(p, 0,0, "Hello, world! "+I2S(i))
set i=i+1
endloop
endfunction

Basic syntax

Syntax
Syntax
In linguistics, syntax is the study of the principles and rules for constructing phrases and sentences in natural languages....

 of JASS is similar to Turing
Turing programming language
Turing is a Pascal-like programming language developed in 1982 by Ric Holt and James Cordy, then of University of Toronto, Canada. Turing is a descendant of Euclid, Pascal and SP/k that features a clean syntax and precise machine-independent semantics....

. It is context free
Context-free grammar
In formal language theory, a context-free grammar is a formal grammar in which every production rule is of the formwhere V is a single nonterminal symbol, and w is a string of terminals and/or nonterminals ....

. Examples of basic syntax are shown below:

function syntax_Example_Sum takes integer i, real r returns real //function declaration must include: the keyword "function",
//the function name, parameters (if any) and return type (if
//it returns something)
return i + r //return statements must begin with the keyword "return"
endfunction //the keyword "endfunction" signals the end of a function block

function syntax_Example takes nothing returns nothing
local integer i //declaring a local variable requires the modifier "local", the variable's data type, and the variable name
local real r = 5.0 //local variable declarations must come before anything else in a function and variables may be
//initialized on declaration

//separated statements MUST be placed on separate lines

set i = 6 //the keyword "set" is used to rebind variables
call syntax_Example_Sum( i, r ) //function calls must be preceded by the keyword "call"
set r = syntax_Example_Sum( i, r ) //the "call" keyword is omitted when accessing a function's return value
endfunction

Types

JASS is statically-typed, and its types
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...

 can be separated into two classes: natives and handles.
The native types are:
  • integer (32-bit signed)
  • real (32-bit floating point numbers, similar to the float type in Java
    Java (programming language)
    Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

    )
  • string (limit is 1023 signs, actually to provide save/load compatibility it's required to use only 1013 signs)
  • boolean
  • code


All other types are considered non-native. The native types behave very similarly to primitive types
Primitive type
In computer science, primitive data type is either of the following:* a basic type is a data type provided by a programming language as a basic building block...

 in other programming languages. Handle types, however, behave more like objects
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

. Handle types often represent an "object" within the game (units, players, special effects, etc.). Similarly to how Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 treats Objects, all variables and parameters in JASS of handle types are treated as values, but in reality those values are nothing but references
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...

 to the handle objects. This becomes important when dealing with garbage collection
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...

 because handles, if not properly cleaned up, can cause significant performance issues. Additionally, local variables do not properly dereference handles when they go out of scope. If they are not nullified properly, handle indices will not be garbage collected and will eventually leak. Also, any references to handles themselves take up some memory space. Users may experience reduced performance if they are not nullified, though on a much smaller scale.

function garbage_Collection_Example takes string effectPath, real x, real y returns nothing
local effect specialEffect = AddSpecialEffect( effectPath, x, y ) //uncleaned handle types will continue to take up system resources
endfunction

function garbage_Collection_Example2 takes string effectPath, real x, real y returns nothing
local effect specialEffect = AddSpecialEffect( effectPath, x, y )

set specialEffect = null //setting the variable to null is not enough, since it is only a reference to the handle;
//the handle still exists
endfunction

function garbage_Collection_Example3 takes string effectPath, real x, real y returns nothing
local effect specialEffect = AddSpecialEffect( effectPath, x, y )

call DestroyEffect( specialEffect ) //we destroy (clean up) the handle to free up memory
set specialEffect = null
endfunction

function garbage_Collection_Example4 takes effect e returns nothing
//do stuff
call DestroyEffect( e )
//parameters do not have to be nullified
endfunction

Another property of handle types worth noting is that all handle types are treated as if they were children of the "handle" type. Some of these children types have their own children types, and so on. Handle variables may reference its own specific handle type or any children type. For example:

function Trig_JASS_handle_Example_Child takes widget w, widget w2 returns nothing
//do stuff
endfunction

function handle_Example takes real x, real y returns nothing
local widget w //widget is a handle type with children type unit and destructible
local unit u = CreateUnit( 'hfoo', x, y )
local destructible d = CreateDestructible( 'ATtr', x, y )

set w = u //acceptable
call Trig_JASS_handle_Example_Child( w, d ) //acceptable
endfunction

agent

Since patch 1.24b there is a new handle-based type called "agent" which has been introduced to separate handle types of which objects has to be deleted manually (Dynamic memory allocation) and handle types of which objects are deleted automatically (Stack-based memory allocation
Stack-based memory allocation
Stacks in computing architectures are regions of memory where data is added or removed in a last-in-first-out manner.In most modern computer systems, each thread has a reserved region of memory referred to as its stack. When a function executes, it may add some of its state data to the top of the...

).
For example types "unit", "rect" or "destructable" which refer to dynamic allocated objects do extend type "agent" now whereas types such as "race" or "alliancetype" which actually are only some kind of wrappers for the native type "integer" and can be compared to enumerated type
Enumerated type
In computer programming, an enumerated type is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language...

s do still extend type "handle".

Type casting

Of the primitive types, type casting
Type conversion
In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations...

 between integer, real, and string is officially supported by the language. JASS supports both implicit and explicit type casting.

Implicit casting only occurs from real to integer. For example:

function type_Casting_Example takes nothing returns real
local integer i = 4
local real r = 5 //implicit cast of 5 to real to satisfy variable type

if ( i < r ) then //implicit cast of i to real
set r = r / i //implicit cast of i to real in order to carry out real division
endif

return i //XXX: NOT allowed; JASS does not allow implicit casting from integer to real to satisfy return types
endfunction

The JASS library provides several functions for explicit type casting:
  • I2R: casts integer to real
  • R2I: casts real to integer
  • I2S: casts integer to string
  • R2S: casts real to string
  • S2I: casts string to integer


An important property of handle types related to type casting is that since all variables of handles are just references
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...

, they can all be treated (and are treated) as integers. Each instance of a handle is assigned a unique integer value that essentially acts as an identifier for the handle. Therefore, type casting from handles to integers, although technically not supported by JASS, is possible in practice because implicit casting from handle types to integer can and will occur if the code is written in a certain way, for example:

function H2I takes handle h returns integer
return h
endfunction

If the game ever reached the line "return h", it would in fact actually cast the handle to an integer and return the value. However, Blizzard
Blizzard Entertainment
Blizzard Entertainment, Inc. is an American video game developer and publisher founded on February 8, 1991 under the name Silicon & Synapse by three graduates of UCLA, Michael Morhaime, Allen Adham and Frank Pearce and currently owned by French company Activision Blizzard...

 never intended for JASS to be used this way, and so the JASS compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

 will actually throw an error, warning the programmer that the function isn't returning the correct type. However, JASS programmers have found and exploited a now famous bug in the JASS compiler's syntax checker: the so-called "return bug". Essentially, the compiler will only make sure that the last return statement in a function returns the correct type. Therefore, the following code compiles without error and can be used to cast handles to integers

function H2I takes handle h returns integer
return h //the function will stop executing after the first return statement, i.e. this one
return 0 //the compiler will only check this statement for syntax accuracy, but it is in reality unreachable code
endfunction

This bug has been fixed in patch 1.23b, although it was not completely fixed until patch 1.24b. Users have to use new hashtable natives instead of their return bug counterparts.
While this bug has been fixed in patch 1.24b, another return bug has been discovered by users, known as the Return Nothing bug. The Return Nothing bug has been fixed by Blizzard
Blizzard Entertainment
Blizzard Entertainment, Inc. is an American video game developer and publisher founded on February 8, 1991 under the name Silicon & Synapse by three graduates of UCLA, Michael Morhaime, Allen Adham and Frank Pearce and currently owned by French company Activision Blizzard...

 in patch 1.23c.

The return nothing bug lets the user get the last value returned by any function, even as another type. To properly utilize the bug, the desired return must be made in a separate function and a return in the calling function should be made impossible.

function ReturnHandle takes handle h returns handle
return h
endfunction

function H2I takes handle h returns integer
call ReturnHandle(h) //This sets the last returned value to 'h'.
if false then
return 0 //This can never occur, so the game uses the last returned value as this function's returned value instead.
//It will even return the last returned value as a different type, in this case an integer.
endif
endfunction

Arrays

JASS supports one-dimensional arrays of any type (excluding code). The syntax to declare arrays and access members in an array is outlined in the code below.

function array_Example takes nothing returns nothing
//arrays cannot be initialized at declaration and their members will hold arbitrary values
local integer array numbers
local unit array units
local boolean array booleans
local integer i = 1

set numbers[0] = 1 //the syntax to initialize an array member is identical to that for any other variable, only a set of
//brackets: [] must immediately follow the variable name with the index value of the specific member
//to initialize inside the brackets
set units[0] = CreateUnit( 'hfoo', 0, 0 ) //indexes for arrays always start at 0

loop
exitwhen ( i > 8191 ) //the maximum size for arrays in JASS is 8192, which means that the last index of any array can
//only be 8191, to provide save/load compatibility it's required to use maximum index 8190
set numbers[i] = i //variables can substitute for constants when specifying the index value in the brackets
set units[numbers[i]] = null //array members can also substitute
set booleans[i-1] = true //arithmetic (or functional) operations are also acceptable
set i = i + 1 //increments the index value
endloop

if ( booleans[200] true ) then //to access an array member, again the syntax is the same as for normal variables, only
//with the addition of the brackets with the index value inside
set numbers[200] = -200 //you can re-assign members of the array to different values
endif
endfunction

One limitation of arrays in JASS is that they cannot be returned by functions or passed as parameters to other functions, though array members may be returned (in a function that returns a unit, u[0] may be returned if u is an array of type unit).

Patch 1.24 and return bug

Blizzard Entertainment has decided to deprecate any use of return bugs with Warcraft III patch 1.24. This is done as an attempt to fix certain security vulnerabilities. Once the patch is deployed, any map that contains the script code with returns bugs will not run properly. All maps using return bugs must use newly available hashtable natives to be compatible with newer versions.

The patch is currently applied on all Battle.net servers.

vJass

vJass is a set of user-made extensions to JASS. It introduces object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 features to the language, such as structs
Object composition
In computer science, object composition is a way to combine simple objects or data types into more complex ones...

, encapsulation
Information hiding
In computer science, information hiding is the principle of segregation of the design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed...

, and polymorphism
Polymorphism in object-oriented programming
Subtype polymorphism, almost universally called just polymorphism in the context of object-oriented programming, is the ability to create a variable, a function, or an object that has more than one form. The word derives from the Greek "πολυμορφισμός" meaning "having multiple forms"...

. Strictly speaking, vJass does not add anything to the JASS library but instead mostly uses JASS arrays and integers used as their indices. The extension relies on a custom-made compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

 that compiles vJass code to strict JASS code. In this manner, no additional mods
Mod (computer gaming)
Mod or modification is a term generally applied to personal computer games , especially first-person shooters, role-playing games and real-time strategy games. Mods are made by the general public or a developer, and can be entirely new games in themselves, but mods are not standalone software and...

 for either the World Editor program or Warcraft III are required, and maps made using vJass code are fully compatible with any copy of the game, even those without the compiler.
The most convenient Tool for Map-Developers which want to use vJass is currently the JassNewGen-Pack, which includes several enhancements for the Warcraft III-World-Editor (including a vJass precompiler).

Known issues

The JASS interpreter of Warcraft III up to version 1.23 doesn't check memory region boundaries. This allows execution of arbitrary bytecode through a map, meaning that practically anything, including malware (viruses, trojans, etc.), can be engineered into a map to be executed and infect a computer. Blizzard Entertainment is aware of this issue and applied a temporary workaround to games hosted on Battle.net. They are also preparing a permanent fix for LAN and single-player games. This issue was addressed with the release of version 1.24.

Preload Bug

In 2010, the Warcraft III Jass community found a way to exploit the I/O capabilities of the Preload native (Commonly used to preload files during initialization to prevent lag), by using it to run malicious code on a players computer. There are several maps that exploit these capabilities by forcing your computer to download files from the internet. The Preload native allows you to download files over the internet, create files on your computer, move files on your computer, delete files and much more. You must only play maps hosted by a source that you trust.

Documentation


Scripts


Tools

  • JassCraftEditor
    Source code editor
    A source code editor is a text editor program designed specifically for editing source code of computer programs by programmers. It may be a standalone application or it may be built into an integrated development environment ....

     for JASS scripts.
  • JASS Tools – Includes a syntax checker.
  • Jass NewGenPack – A World Editor
    Warcraft III World Editor
    Warcraft III World Editor is the official level editor for the real-time strategy game...

     mod
    Mod (computer gaming)
    Mod or modification is a term generally applied to personal computer games , especially first-person shooters, role-playing games and real-time strategy games. Mods are made by the general public or a developer, and can be entirely new games in themselves, but mods are not standalone software and...

     that allows vJass compilation and adds debugging capabilities among other features.
  • JassHelper – A vJass to Jass compiler.
  • AdicHelper - A cJass to vJass/Jass compiler.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK