gecko-dev/js/rhino/docs/runtime.html

173 lines
6.6 KiB
HTML

<!-- ***** BEGIN LICENSE BLOCK *****
- Version: MPL 1.1/GPL 2.0
-
- The contents of this file are subject to the Mozilla Public License Version
- 1.1 (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.mozilla.org/MPL/
-
- Software distributed under the License is distributed on an "AS IS" basis,
- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- for the specific language governing rights and limitations under the
- License.
-
- The Original Code is Rhino code, released May 6, 1999.
-
- The Initial Developer of the Original Code is
- Netscape Communications Corporation.
- Portions created by the Initial Developer are Copyright (C) 1997-1999
- the Initial Developer. All Rights Reserved.
-
- Contributor(s):
-
- Alternatively, the contents of this file may be used under the terms of
- the GNU General Public License Version 2 or later (the "GPL"), in which
- case the provisions of the GPL are applicable instead of those above. If
- you wish to allow use of your version of this file only under the terms of
- the GPL and not to allow others to use your version of this file under the
- MPL, indicate your decision by deleting the provisions above and replacing
- them with the notice and other provisions required by the GPL. If you do
- not delete the provisions above, a recipient may use your version of this
- file under either the MPL or the GPL.
-
- ***** END LICENSE BLOCK ***** -->
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="Norris Boyd">
<meta name="GENERATOR" content="Mozilla/4.75 [en] (WinNT; U) [Netscape]">
<title>JavaScript Runtime</title>
</head>
<body bgcolor="#FFFFFF">
<center>
<h1>
The JavaScript Runtime</h1></center>
<h3>
Interpretation</h3>
Beginning with Rhino 1.4 Release 2, an interpretive mode is supported.
When scripts are compiled in interpretive mode, an internal representation
of the compiled form is created and stored rather than generating a Java
class. Execution proceeds by evaluating this compiled form using support
routines in Rhino.
<h3>
Compilation to Java Bytecodes</h3>
For improved performance, Rhino may compile JavaScript scripts to Java
bytecodes. The generated bytecodes in turn depend upon runtime support
routines. Each JavaScript script or function is compiled to a separate
class.
<p>Compilation of JavaScript source to class files is supported. It is
possible to specify the class files as well as the packages to generate
into.
<h3>
Types and Values</h3>
There are six fundamental types in JavaScript. These types are implemented
with the following Java types and values:
<br>&nbsp;
<br>&nbsp;
<center><table BORDER COLS=2 WIDTH="75%" >
<tr>
<td><i>JavaScript fundamental type</i></td>
<td><i>Java type</i></td>
</tr>
<tr>
<td>Undefined</td>
<td>A singleton object defined by <tt>Context.getUndefinedType()</tt></td>
</tr>
<tr>
<td>Null</td>
<td><tt>null</tt></td>
</tr>
<tr>
<td>Boolean</td>
<td><tt>java.lang.Boolean</tt></td>
</tr>
<tr>
<td>Number</td>
<td><tt>java.lang.Number</tt>, that is, any of <tt>java.lang.Byte</tt>,<tt>
java.lang.Short</tt>,<tt> java.lang.Integer</tt>,<tt> java.lang.Float</tt>,
or <tt>java.lang.Double. Not java.lang.Long, since a double representation
of a long may lose precision.</tt></td>
</tr>
<tr>
<td>String</td>
<td><tt>java.lang.String</tt></td>
</tr>
<tr>
<td>Object</td>
<td><tt>org.mozilla.javascript.Scriptable</tt></td>
</tr>
</table></center>
<p>In addition, ECMA refers to objects that implement [[Call]] as functions.
These object types are represented by implementing the Function interface.
<p>Since JavaScript is a dynamically typed language, the static Java type
of a JavaScript value is <tt>java.lang.Object</tt>.
<p>The behavior of the JavaScript engine is undefined if a value of any
type other than the ones described above is introduced into JavaScript.
(This caveat does not apply to scripts that use LiveConnect; the Java values
are wrapped and unwrapped as appropriate to conform to the above type constraints.)
<br>&nbsp;
<h3>
Property Access</h3>
Properties in JavaScript objects may be accessed using either string or
numeric identifiers. Conceptually, all accessors are converted to strings
in order to perform the lookup of the property in the object. However,
this is not the implementation used in practice because a number to string
conversion is too expensive to be performed on every array access.
<p>Instead, every property accessor method in <a href="apidocs/org/mozilla/javascript/Scriptable.html">Scriptable</a>
(<tt>has</tt>, <tt>get</tt>, <tt>set</tt>, <tt>remove</tt>, <tt>getAttributes</tt>,
and <tt>setAttributes</tt>) has overloaded forms that take either a <tt>String</tt>
or an <tt>int</tt> argument. It is the responsibility of the caller to
invoke the appropriate overloaded form. For example, evaluating the expression
<tt>obj["3"]</tt>
will invoke the get(int, Scriptable) method even though the property name
was presented in the script as a string. Similarly, values of numbers that
do not fix in integers (like 1.1 and 0x100000000) must be converted to
strings.
<br>&nbsp;
<h3>
Defining Host Objects</h3>
Host objects are JavaScript objects that provide special access to the
host environment. For example, in a browser environment, the Window and
Document objects are host objects.
<p>The easiest way to define new host objects is by using <a href="apidocs/org/mozilla/javascript/ScriptableObject.html#defineClass(org.mozilla.javascript.Scriptable, java.lang.Class)">ScriptableObject.defineClass()</a>.
This method defines a set of JavaScript objects using a Java class. Several
of the <a href="examples.html">examples</a> define host objects this way.
<p>If the services provided by defineClass are insufficient, try other
methods of
<a href="apidocs/org/mozilla/javascript/ScriptableObject.html">ScriptableObject</a>
and
<a href="apidocs/org/mozilla/javascript/FunctionObject.html">FunctionObject</a>,
such as <tt>defineProperty</tt> and <tt>defineFunctionProperties</tt>.
<br>&nbsp;
<br>&nbsp;
<h3>
Contexts and Threads</h3>
Every thread that executes JavaScript must have an associated Context.
Multiple threads (with multiple associated Contexts) may act upon the same
set of objects. Any host objects that are defined are responsible for any
sychronization required to run safely from multiple threads.
<br>&nbsp;
<p>
<hr WIDTH="100%">
<br><a href="index.html">back to top</a>
</body>
</html>