Certain ECMAScript statements must be terminated with a semicolon. Such a semicolon may always appear explicitly in the source text. For pain of the compiler implementator, however, such semicolons may be omitted from the source text in certain situations. These situations are described in details in the ECMA-262 Version 2 draft 22-Apr-98 standard. Here is @email{mtr@ngs.fi}'s interpretation of the rules:
Insert semicolons as you would do in the C language. Now, you can omit them:
}
' character
The automatic semicolon insertion sets some restrictions how you can insert whitespace to you code. You can't insert line breaks:
++
or --
operator
return
and the returned Expression
if
Statementdo
...while
Statementwhile
Statementfor
Statementfor
...in
Statementcontinue
Statementbreak
Statementreturn
Statementwith
Statement
The syntax of the with
-statement is:
with (expr) statement
with (Math) { result = sin (PI); result -= tan (45); }
switch
Statementthrow
Statementtry
Statement
Extend interpreter by calling an initialization function from shared library class_spec. The argument class_spec can be given in the following formats:
library:function
library
loadClass ("libexts.so:init_all"); => call functioninit_all
from library `libexts.so' loadClass ("/usr/local/lib/libexts.so:init_all"); => call functioninit_all
from library `/usr/local/lib/libexts.so' loadClass ("/usr/local/lib/libdbexts.so"); => call functiondbexts
from library `/usr/local/lib/libexts.so'
The initialization function must be a void function that takes one argument that is a pointer to the interpreter.
void entry (JSInterpPtr interp) { Initialize extensions using normal `js.h' and `jsint.h' interfaces. }
Call method method from object object with arguments arguments.
callMethod (System.stdout, "writeln", ["Hello, world!"]); -| Hello, world!
toSource()
method is missing.
new Array()
called with the
same arguments.
Create a new array object. The first form creates a new array which length is count. All items are set to value `undefined'. The second form creates an array that contains the given items as its values.
var a = new Array (5); a.length; => 5 a.toString (); => undefined,undefined,undefined,undefined,undefined a = new Array (1, 2, "hello"); a.length; => 3 a.toString (); => 1,2,hello
var a = new Array (1, 2, 3); var b = a.concat (new Array (4, 5)); b.length; => 5; b.toString (); => 1,2,3,4,5
","
is used.
var a = new Array (1, 2, "three"); a.join (); => "1,2,three" a.join ("-"); => "1-2-three"
undefined
is returned.
a = new Array (1, 2, 3); a.pop (); => 3 a.length; => 2
a = new Array (1, 2); a.push (7); => 7 a.push (7, 8, 9); => 9 System.print (a.join (", "), "\n"); -| 1, 2, 7, 7, 8, 9
a = new Array (1, 2, 3); a.reverse (); System.print (a.join (""), "\n"); -| 321
a = new Array (1, 2, 3); a.shift (); => 1
a = new Array (1, 2, 3, 4, 5); b = a.slice (1, 4); System.print (b.join (", "), "\n"); -| 2, 3, 4 b = a.slice (1, -2); System.print (b.join (", "), "\n"); -| 2, 3 b = a.slice (2); System.print (b.join (", "), "\n"); -| 3, 4, 5
var a = new Array (1, 2, 3); a.splice (1, 1); => 1, 3 a.splice (1, 0, "new item"); => 1, "new item", 2, 3 var a = new Array (1, 2, 3, 4); a.splice (1, 2, "new item"); => 1, "new item", 4
-1
0
1
If the argument sort_function is omitted, the items are sorted to an alphabetical (lexicographical) order.
a = new Array ("Jukka-Pekka", "Jukka", "Kari", "Markku"); a.sort (); System.print (a, "\n"); -| Jukka,Jukka-Pekka,Kari,Markku a = new Array (1, 2, 10, 20, 100, 200); a.sort (); System.stdout.writeln (a.toString ()); -| 1,10,100,2,20,200
The sort method is stable in that sense that, if the comparison function returns 0 for two items, their original order in the array is preserved. For example, if a list of person objects is sorted first by their names, and second by their ages, all persons with the same age will remain sorted in an alphabetical order.
function by_age (a, b) { return a.age - b.age; } function by_name (a, b) { if (a.name < b.name) return -1; if (a.name > b.name) return 1; return 0; } function Person (name, age) { this.name = name; this.age = age; } a = new Array (new Person ("Smith", 30), new Person ("Jones", 31), new Person ("Bob", 30), new Person ("Chris", 29)); a.sort (by_name); a.sort (by_age); for (i in a) System.print (i.name, ", ", i.age, "\n"); -| Chris, 29 -| Bob, 30 -| Smith, 30 -| Jones, 31
","
.
var a = new Array (1, "foo", 2, new Array (7, 8)); a.toString (); => 1,foo,2,7,8
a = new Array (1, 2, 3); System.print (a.unshift (7, 8, 9), "\n"); -| 6
var a = new Array (1, 2); a.length; => 2 a.push (3, 4, 5); a.length; => 5
false
.
undefined
, null
, false
, ""
(an empty
string), or 0
, the value of the object will be `false'. All
other values for the argument value will set the initial value of
the object to `true'.
"true"
or "false"
according to the value of
the object.
Change permissions of file path to mode. The modes are specifeid by or'ing the following values:
04000
02000
01000
00400
00200
00100
00040
00020
00010
00004
00002
00001
Return statistics about the file file. The method returns a 13 element array containing the statistics, or `false' if the file couldn't be inspected.
The returned array contains the following items:
dev
ino
dev
and ino
.
mode
nlink
uid
gid
rdev
size
atime
mtime
ctime
blksize
blocks
fields = new Array ("dev", "ino", "mode", "nlink", "uid", "gid", "rdev", "size", "atime", "mtime", "ctime", "blksize", "blocks"); var a = File.stat ("js"); if (a) { var i; for (i = 0; i < a.length; i++) System.print (fields[i], "=", a[i], " "); System.print ("\n"); } -| dev=655368 ino=370741 mode=33261 nlink=1 uid=201 gid=200 -| rdev=2979328 size=731370 atime=893159080 mtime=893158537 -| ctime=893158537 blksize=4096 blocks=1432
The argument mode must have one of the following values:
r
[b
]
w
[b
]
a
[b
]
r+
[b
]
w+
[b
]
a+
[b
]
Flag that specifies whether the stream should automatically flush its buffers after a write.
The I/O buffer size of the stream. The buffer size can be changed at the runtime.
0
.
+0
. If the argument value is given, the
constructor returns ToNumber(value)
.
new Number (); => 0 new Number (3.1415); => 3.1415 new Number (true); => 1
10
.
System.stdout.writeln ((193).toString ()); -| 193 System.stdout.writeln ((193).toString (8)); -| 301 System.stdout.writeln ((193).toString (16)); -| c1 System.stdout.writeln ((193).toString (2)); -| 11000001
toString()
and toSource()
methods are missing.
var o = new Object ();
Create a new regular expression from string pattern. The optional argument string flags can contain the following options:
i
g
exec
method multiple times against
the string.
Note! All regular expressions are always compiled in this
implementation. This holds also for the expressions, created with the
RegExp()
constructor.
var re = new RegExp ("ab*"); re.compile ("ab*", "i");
RegExp.input
string. The method returns an array that holds the
matched portions of the expression.
var re = new RegExp ("d(b+)(d)", "ig"); var a = re.exec ("cdbBdbsbz"); a.toString (); => "dbBd,bB,d"
In the previous example, the result array `a' has the following items:
dbBd
bB
d
The option `g' of the regular expression can be used to iterate over multiple matches of the expression on at a time. For example, the following code fragment searches for the expression `a(b*)' from the string `str'. In the inial state -- when the expression is create with the constructor -- the object's `lastIndex' property is set to 0. When the expression is matched against the string, the `lastIndex' property is updated to point to the next index from which the matching should be continued. Therefore, the following example iterates over all matches in the string `str'.
var re = new RegExp ("a(b*)", "g"); var str = "abbcdefabh"; while (a = re.exec (str)) System.print ("Found ", a, ". Next match starts at ", re.lastIndex, ".\n"); -| Found abb. Next match starts at 3. -| Found ab. Next match starts at 9.
The property `regexp.lastIndex' can also be set explicitly to start the matching from a pre-defined position.
RegExp.input
string.
var re = new RegExp ("fo*bar"); re.test ("fbar"); => true re.test ("fooBar"); => false re = new RegExp ("fo*bar", "i"); re.test ("FOObAR"); => true RegExp.input = "#include <stdio.h>"; re = new RegExp ("^#"); re.test (); => true
var str = file.readln (); re.test (str); RegExp.input; => The string returned by the `file.readln()' method.
var re = new RegExp ("foo"); var str = "garbage foo tail garbage"; re.exec (str); RegExp.leftContext; => "garbage "
var re = new RegExp ("foo"); var str = "garbage foo tail garbage"; re.exec (str); RegExp.rightContext; => " tail garbage"
compile
method.
compile
method.
var str = new String ("Hello, world"); => "Hello, world!"
var str = String.fromCharCode (72, 101, 108, 108, 111, 33); => "Hello!"
Create a new string by packing values arg... to a string according to the format string format.
The format is a sequence of characters that specify the type of values as follows:
C
n
N
d
Append string string to the end of the string object. The string object must be a dynamic string, not a constant string literal.
var str = new String (""); str.append ("foo"); str.append ("-"); str.append ("bar"); => "foo-bar"
"foobar".charAt (3); => "b"
"foobar".charCodeAt (3); => 98
"foo".concat ("bar"); => "foobar"
Count a 32-bit CRC of the string.
var str = "Hello, world!"; System.print ("CRC32 of \"", str, "\" is ", str.crc32 ().toString (16), ".\n"); -| CRC32 of "Hello, world!" is e4928064.
var str = "foobar foo bar foo"; str.indexOf ("foo"); => 0 str.indexOf (" foo"); => 6 str.indexOf ("foo", 1); => 7 str.indexOf ("Foo"); => -1
var str = "foobar foo bar foo"; str.lastIndexOf ("foo"); => 15 str.lastIndexOf ("bar"); => 11 str.lastIndexOf ("foo", 14); => 7 str.lastIndexOf ("Foo"); => -1
"FoObAr".toLowerCase (); => "foobar"
"FoObAr".toUpperCase (); => "FOOBAR"
"foobar".length; => 6 var str = new String ("foo"); str.append ("bar"); str.length; => 6
false
if errors were
encountered.
System.stdout.writeln (System.canonicalHost); -| powerpc-ibm-aix4.2.1.0
System.stdout.writeln (System.canonicalHostCPU); -| powerpc
System.stdout.writeln (System.canonicalHostVendor); -| ibm
System.stdout.writeln (System.canonicalHostOS); -| aix4.2.1.0
strerror()
method of the System object.
var fp = new File ("output.txt"); if (!fp.open ("w")) System.error ("couldn't create output file `", fp, "': ", System.strerror (System.errno), "\n"); -| couldn't create output file `output.txt': Permission denied
System.stdout.writeln ("Hello!"); -| Hello! System.stdout.write ("Hello!" + System.lineBreakSequence); -| Hello!
System.stderr.writeln ("panic: must exit"); System.exit (1); -| panic: must exit
garbageCollect()
method can be used to trigger the collection
explicitly.
function recursive (n) { if (n > 5) VM.stackTrace (); else recursive (n + 1); } recursive (0); -|VM: stacktrace: stacksize=2048, used=78 -|#0 recursive(): builtin 0 -|#1 recursive(): null 1 6 -|#2 recursive(): null 1 5 -|#3 recursive(): null 1 4 -|#4 recursive(): null 1 3 -|#5 recursive(): null 1 2 -|#6 recursive(): null 1 1 -|#7 .global(): null 1 0
gcTrigger
number of bytes of memory, the
virtual machine will perform a garbage collection.
var md5 = new MD5 ();
update()
method. The method returns a 32 bytes long string that
holds the MD5 value as a hexadecimal number.
function print_md5 (str) { var md5 = new MD5 (); md5.update (str); System.print ("MD5 of \"", str, "\" is ", md5.final (), ".\n"); } print_md5 ("Hello, world!"); -| MD5 of "Hello, world!" is 6CD3556DEB0DA54BCA060B4C39479839.
update()
method.
function count_md5_for_file (stream) { var md5 = new MD5 (); while (!stream.eof ()) { var data = stream.read (1024); md5.update (data); } return md5.final (); }
Go to the first, previous, next, last section, table of contents.