annotate src/overview.html @ 2:da1e581c933b

8005403: Open-source Nashorn Reviewed-by: attila, hannesw, lagergren, sundar Contributed-by: james.laskey@oracle.com, akhil.arora@oracle.com, andreas.woess@jku.at, attila.szegedi@oracle.com, hannes.wallnoefer@oracle.com, henry.jen@oracle.com, marcus.lagergren@oracle.com, pavel.semenov@oracle.com, pavel.stepanov@oracle.com, petr.hejl@oracle.com, petr.pisl@oracle.com, sundararajan.athijegannathan@oracle.com
author jlaskey
date Fri, 21 Dec 2012 16:36:24 -0400
parents
children 5a1b0714df0e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
1 <!--
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
2 Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
4
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
5 This code is free software; you can redistribute it and/or modify it
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
6 under the terms of the GNU General Public License version 2 only, as
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
7 published by the Free Software Foundation. Oracle designates this
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
8 particular file as subject to the "Classpath" exception as provided
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
9 by Oracle in the LICENSE file that accompanied this code.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
10
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
11 This code is distributed in the hope that it will be useful, but WITHOUT
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
14 version 2 for more details (a copy is included in the LICENSE file that
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
15 accompanied this code).
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
16
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
17 You should have received a copy of the GNU General Public License version
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
18 2 along with this work; if not, write to the Free Software Foundation,
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
19 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
20
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
21 Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
22 or visit www.oracle.com if you need additional information or have any
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
23 questions.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
24 -->
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
25 <body>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
26 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
27 Nashorn is a runtime environment for programs written in ECMAScript 5.1.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
28 </p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
29 <h1>Usage</h1>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
30 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
31 The recommended way to use Nashorn is through the <a href="http://jcp.org/en/jsr/detail?id=223" target="_top">JSR-223
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
32 "Scripting for the Java Platform"</a> APIs found in the {@link javax.script} package. Usually, you'll obtain a
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
33 {@link javax.script.ScriptEngine} instance for Nashorn using:
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
34 <pre>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
35 import javax.script.*;
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
36 ...
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
37 ScriptEngine nashornEngine = new ScriptEngineManager().getEngineByName("nashorn");
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
38 </pre>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
39 and then use it just as you would any other JSR-223 script engine. See
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
40 <a href="jdk/nashorn/api/scripting/package-summary.html">{@code jdk.nashorn.api.scripting}</a> package
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
41 for details.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
42 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
43 <h1>Compatibility</h1>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
44 Nashorn is 100% compliant with the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm"
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
45 target="_top">ECMA-262 Standard, Edition 5.1</a>. It requires a Java Virtual Machine that implements the
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
46 <a href="http://jcp.org/en/jsr/detail?id=292" target="_top">JSR-292 "Supporting Dynamically Typed Languages on the Java
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
47 Platform"</a> specification (often referred to as "invokedynamic"), as well as the already mentioned JSR-223.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
48 <h1>Interoperability with the Java platform</h1>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
49 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
50 In addition to being a 100% ECMAScript 5.1 runtime, Nashorn provides features for interoperability of the ECMAScript
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
51 programs with the Java platform. In general, any Java object put into the script engine's context will be visible from
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
52 the script. In terms of the standard, such Java objects are not considered "native objects", but rather "host objects",
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
53 as defined in section 4.3.8. This distinction allows certain semantical differences in handling them compared to native
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
54 objects. For most purposes, Java objects behave just as native objects do: you can invoke their methods, get and set
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
55 their properties. In most cases, though, you can't add arbitrary properties to them, nor can you remove existing
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
56 properties.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
57 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
58 <h2>Java collection handling</h2>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
59 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
60 Native Java arrays and {@link java.util.List}s support indexed access to their elements through the property accessors,
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
61 and {@link java.util.Map}s support both property and element access through both dot and square-bracket property
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
62 accessors, with the difference being that dot operator gives precedence to object properties (its fields and properties
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
63 defined as {@code getXxx} and {@code setXxx} methods) while the square bracket operator gives precedence to map
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
64 elements. Native Java arrays expose the {@code length} property.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
65 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
66 <h2>ECMAScript primitive types</h2>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
67 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
68 ECMAScript primitive types for number, string, and boolean are represented with {@link java.lang.Number},
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
69 {@link java.lang.CharSequence}, and {@link java.lang.Boolean} objects. While the most often used number type is
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
70 {@link java.lang.Double} and the most often used string type is {@link java.lang.String}, don't rely on it as various
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
71 internal optimizations cause other subclasses of {@code Number} and internal implementations of {@code CharSequence} to
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
72 be used.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
73 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
74 <h2>Type conversions</h2>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
75 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
76 When a method on a Java object is invoked, the arguments are converted to the formal parameter types of the Java method
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
77 using all allowed ECMAScript conversions. This can be surprising, as in general, conversions from string to number will
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
78 succeed according to Standard's section 9.3 "ToNumber" and so on; string to boolean, number to boolean, Object to
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
79 number, Object to string all work. Note that if the Java method's declared parameter type is {@code java.lang.Object},
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
80 Nashorn objects are passed without any conversion whatsoever; specifically if the JavaScript value being passed is of
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
81 primitive string type, you can only rely on it being a {@code java.lang.CharSequence}, and if the value is a number, you
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
82 can only rely on it being a {@code java.lang.Number}. If the Java method declared parameter type is more specific (e.g.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
83 {@code java.lang.String} or {@code java.lang.Double}), then Nashorn will of course ensure the required type is passed.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
84 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
85 <h2>SAM types</h2>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
86 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
87 As a special extension when invoking Java methods, ECMAScript function objects can be passed in place of an argument
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
88 whose Java type is so-called "single abstract method" or "SAM" type. While this name usually covers single-method
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
89 interfaces, Nashorn is a bit more versatile, and it recognizes a type as a SAM type if all its abstract methods are
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
90 overloads of the same name, and it is either an interface, or it is an abstract class with
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
91 a no-arg constructor. The type itself must be public, while the constructor and the methods can be either public or
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
92 protected. If there are multiple abstract overloads of the same name, the single function will serve as the shared
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
93 implementation for all of them, <em>and additionally it will also override any non-abstract methods of the same name</em>.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
94 This is done to be consistent with the fact that ECMAScript does not have the concept of overloaded methods.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
95 <p>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
96 <h2>The {@code Java} object</h2>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
97 Nashorn exposes a non-standard global object named {@code Java} that is the primary API entry point into Java
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
98 platform-specific functionality. You can use it to create instances of Java classes, convert from Java arrays to native
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
99 arrays and back, and so on. The methods on the objects are directly implemented by public static methods on the class
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
100 <a href="jdk/nashorn/internal/objects/NativeJava.html">{@code NativeJava}</a>, see that class for details on what
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
101 functionality is available.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
102 <h2>Representations of Java types</h2>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
103 The method <a href="jdk/nashorn/internal/objects/NativeJava.html#type(java.lang.Object,%20java.lang.Object)">
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
104 {@code Java.type(typeName)}</a> takes a name of a type, and returns an object representing a Java type. You can
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
105 use that object to both create new instances of Java classes, as well as to access static fields and methods on them.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
106 The type object is distinct from the {@code java.lang.Class} object, which represents the reflective run-time type
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
107 identity and doesn't carry i.e. static members. Again, see the link for {@code NativeJava} above for details.
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
108 <h2>Other non-standard built-in objects</h2>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
109 In addition to {@code Java}, Nashorn also exposes some other non-standard built-in objects:
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
110 <a href="jdk/nashorn/internal/objects/NativeJSAdapter.html">{@code JSAdapter}</a>,
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
111 <a href="jdk/nashorn/internal/objects/NativeJavaImporter.html">{@code JavaImporter},
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
112 <a href="jdk/nashorn/internal/runtime/NativeJavaPackage.html">{@code Packages}.</a>
da1e581c933b 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
113 </body>