src/overview.html

changeset 3
da1e581c933b
child 7
5a1b0714df0e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/overview.html	Fri Dec 21 16:36:24 2012 -0400
     1.3 @@ -0,0 +1,113 @@
     1.4 +<!-- 
     1.5 + Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 +
     1.8 + This code is free software; you can redistribute it and/or modify it
     1.9 + under the terms of the GNU General Public License version 2 only, as
    1.10 + published by the Free Software Foundation.  Oracle designates this
    1.11 + particular file as subject to the "Classpath" exception as provided
    1.12 + by Oracle in the LICENSE file that accompanied this code.
    1.13 +
    1.14 + This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + version 2 for more details (a copy is included in the LICENSE file that
    1.18 + accompanied this code).
    1.19 +
    1.20 + You should have received a copy of the GNU General Public License version
    1.21 + 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 +
    1.24 + Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + or visit www.oracle.com if you need additional information or have any
    1.26 + questions.
    1.27 +-->
    1.28 +<body>
    1.29 +<p>
    1.30 +Nashorn is a runtime environment for programs written in ECMAScript 5.1.
    1.31 +</p>
    1.32 +<h1>Usage</h1>
    1.33 +<p>
    1.34 +The recommended way to use Nashorn is through the <a href="http://jcp.org/en/jsr/detail?id=223" target="_top">JSR-223
    1.35 +"Scripting for the Java Platform"</a> APIs found in the {@link javax.script} package. Usually, you'll obtain a
    1.36 +{@link javax.script.ScriptEngine} instance for Nashorn using:
    1.37 +<pre>
    1.38 +import javax.script.*;
    1.39 +...
    1.40 +ScriptEngine nashornEngine = new ScriptEngineManager().getEngineByName("nashorn");
    1.41 +</pre>
    1.42 +and then use it just as you would any other JSR-223 script engine. See
    1.43 +<a href="jdk/nashorn/api/scripting/package-summary.html">{@code jdk.nashorn.api.scripting}</a> package
    1.44 +for details.
    1.45 +<p>
    1.46 +<h1>Compatibility</h1>
    1.47 +Nashorn is 100% compliant with the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm"
    1.48 +target="_top">ECMA-262 Standard, Edition 5.1</a>. It requires a Java Virtual Machine that implements the
    1.49 +<a href="http://jcp.org/en/jsr/detail?id=292" target="_top">JSR-292 "Supporting Dynamically Typed Languages on the Java
    1.50 +Platform"</a> specification (often referred to as "invokedynamic"), as well as the already mentioned JSR-223.
    1.51 +<h1>Interoperability with the Java platform</h1>
    1.52 +<p>
    1.53 +In addition to being a 100% ECMAScript 5.1 runtime, Nashorn provides features for interoperability of the ECMAScript
    1.54 +programs with the Java platform. In general, any Java object put into the script engine's context will be visible from
    1.55 +the script. In terms of the standard, such Java objects are not considered "native objects", but rather "host objects",
    1.56 +as defined in section 4.3.8. This distinction allows certain semantical differences in handling them compared to native
    1.57 +objects. For most purposes, Java objects behave just as native objects do: you can invoke their methods, get and set
    1.58 +their properties. In most cases, though, you can't add arbitrary properties to them, nor can you remove existing
    1.59 +properties.
    1.60 +<p>
    1.61 +<h2>Java collection handling</h2>
    1.62 +<p>
    1.63 +Native Java arrays and {@link java.util.List}s support indexed access to their elements through the property accessors,
    1.64 +and {@link java.util.Map}s support both property and element access through both dot and square-bracket property
    1.65 +accessors, with the difference being that dot operator gives precedence to object properties (its fields and properties
    1.66 +defined as {@code getXxx} and {@code setXxx} methods) while the square bracket operator gives precedence to map
    1.67 +elements. Native Java arrays expose the {@code length} property.
    1.68 +<p>
    1.69 +<h2>ECMAScript primitive types</h2>
    1.70 +<p>
    1.71 +ECMAScript primitive types for number, string, and boolean are represented with {@link java.lang.Number},
    1.72 +{@link java.lang.CharSequence}, and {@link java.lang.Boolean} objects. While the most often used number type is
    1.73 +{@link java.lang.Double} and the most often used string type is {@link java.lang.String}, don't rely on it as various
    1.74 +internal optimizations cause other subclasses of {@code Number} and internal implementations of {@code CharSequence} to
    1.75 +be used.
    1.76 +<p>
    1.77 +<h2>Type conversions</h2>
    1.78 +<p>
    1.79 +When a method on a Java object is invoked, the arguments are converted to the formal parameter types of the Java method
    1.80 +using all allowed ECMAScript conversions. This can be surprising, as in general, conversions from string to number will
    1.81 +succeed according to Standard's section 9.3 "ToNumber" and so on; string to boolean, number to boolean, Object to
    1.82 +number, Object to string all work. Note that if the Java method's declared parameter type is {@code java.lang.Object},
    1.83 +Nashorn objects are passed without any conversion whatsoever; specifically if the JavaScript value being passed is of
    1.84 +primitive string type, you can only rely on it being a {@code java.lang.CharSequence}, and if the value is a number, you
    1.85 +can only rely on it being a {@code java.lang.Number}. If the Java method declared parameter type is more specific (e.g.
    1.86 +{@code java.lang.String} or {@code java.lang.Double}), then Nashorn will of course ensure the required type is passed.
    1.87 +<p>
    1.88 +<h2>SAM types</h2>
    1.89 +<p>
    1.90 +As a special extension when invoking Java methods, ECMAScript function objects can be passed in place of an argument
    1.91 +whose Java type is so-called "single abstract method" or "SAM" type. While this name usually covers single-method
    1.92 +interfaces, Nashorn is a bit more versatile, and it recognizes a type as a SAM type if all its abstract methods are
    1.93 +overloads of the same name, and it is either an interface, or it is an abstract class with
    1.94 +a no-arg constructor. The type itself must be public, while the constructor and the methods can be either public or
    1.95 +protected. If there are multiple abstract overloads of the same name, the single function will serve as the shared
    1.96 +implementation for all of them, <em>and additionally it will also override any non-abstract methods of the same name</em>.
    1.97 +This is done to be consistent with the fact that ECMAScript does not have the concept of overloaded methods.
    1.98 +<p>
    1.99 +<h2>The {@code Java} object</h2>
   1.100 +Nashorn exposes a non-standard global object named {@code Java} that is the primary API entry point into Java
   1.101 +platform-specific functionality. You can use it to create instances of Java classes, convert from Java arrays to native
   1.102 +arrays and back, and so on. The methods on the objects are directly implemented by public static methods on the class
   1.103 +<a href="jdk/nashorn/internal/objects/NativeJava.html">{@code NativeJava}</a>, see that class for details on what
   1.104 +functionality is available.
   1.105 +<h2>Representations of Java types</h2>
   1.106 +The method <a href="jdk/nashorn/internal/objects/NativeJava.html#type(java.lang.Object,%20java.lang.Object)">
   1.107 +{@code Java.type(typeName)}</a> takes a name of a type, and returns an object representing a Java type. You can
   1.108 +use that object to both create new instances of Java classes, as well as to access static fields and methods on them.
   1.109 +The type object is distinct from the {@code java.lang.Class} object, which represents the reflective run-time type
   1.110 +identity and doesn't carry i.e. static members. Again, see the link for {@code NativeJava} above for details.
   1.111 +<h2>Other non-standard built-in objects</h2>
   1.112 +In addition to {@code Java}, Nashorn also exposes some other non-standard built-in objects:
   1.113 +<a href="jdk/nashorn/internal/objects/NativeJSAdapter.html">{@code JSAdapter}</a>,
   1.114 +<a href="jdk/nashorn/internal/objects/NativeJavaImporter.html">{@code JavaImporter},
   1.115 +<a href="jdk/nashorn/internal/runtime/NativeJavaPackage.html">{@code Packages}.</a>
   1.116 +</body>
   1.117 \ No newline at end of file

mercurial