--- a/nashorn/samples/DOMLinkerExporter.java Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,162 +0,0 @@
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodType;
-import java.util.ArrayList;
-import java.util.List;
-import jdk.dynalink.CallSiteDescriptor;
-import jdk.dynalink.CompositeOperation;
-import jdk.dynalink.NamedOperation;
-import jdk.dynalink.Operation;
-import jdk.dynalink.StandardOperation;
-import jdk.dynalink.linker.GuardingDynamicLinker;
-import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
-import jdk.dynalink.linker.GuardedInvocation;
-import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
-import jdk.dynalink.linker.LinkRequest;
-import jdk.dynalink.linker.LinkerServices;
-import jdk.dynalink.linker.support.Guards;
-import jdk.dynalink.linker.support.Lookup;
-import org.w3c.dom.Attr;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Element;
-
-/**
- * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
- * This linker handles XML DOM Element objects specially. This linker links
- * special properties starting with "_" and treats those as child element names
- * to access. This kind of child element access makes it easy to write XML DOM
- * accessing scripts. See for example ./dom_linker_gutenberg.js.
- */
-public final class DOMLinkerExporter extends GuardingDynamicLinkerExporter {
- static {
- System.out.println("pluggable dynalink DOM linker loaded");
- }
-
- // return List of child Elements of the given Element matching the given name.
- private static List<Element> getChildElements(Element elem, String name) {
- NodeList nodeList = elem.getChildNodes();
- List<Element> childElems = new ArrayList<>();
- int len = nodeList.getLength();
- for (int i = 0; i < len; i++) {
- Node node = nodeList.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE &&
- ((Element)node).getTagName().equals(name)) {
- childElems.add((Element)node);
- }
- }
- return childElems;
- }
-
- // method that returns either unique child element matching given name
- // or a list of child elements of that name (if there are more than one matches).
- public static Object getElementsByName(Object elem, final String name) {
- List<Element> elems = getChildElements((Element)elem, name);
- return elems.size() == 1? elems.get(0) : elems;
- }
-
- // method to extract text context under a given DOM Element
- public static Object getElementText(Object elem) {
- NodeList nodeList = ((Element)elem).getChildNodes();
- int len = nodeList.getLength();
- StringBuilder text = new StringBuilder();
- for (int i = 0; i < len; i++) {
- Node node = nodeList.item(i);
- if (node.getNodeType() == Node.TEXT_NODE) {
- text.append(node.getNodeValue());
- }
- }
- return text.toString();
- }
-
- private static final MethodHandle ELEMENTS_BY_NAME;
- private static final MethodHandle ELEMENT_TEXT;
- private static final MethodHandle IS_ELEMENT;
- static {
- ELEMENTS_BY_NAME = Lookup.PUBLIC.findStatic(DOMLinkerExporter.class,
- "getElementsByName",
- MethodType.methodType(Object.class, Object.class, String.class));
- ELEMENT_TEXT = Lookup.PUBLIC.findStatic(DOMLinkerExporter.class,
- "getElementText",
- MethodType.methodType(Object.class, Object.class));
- IS_ELEMENT = Guards.isInstance(Element.class, MethodType.methodType(Boolean.TYPE, Object.class));
- }
-
- @Override
- public List<GuardingDynamicLinker> get() {
- final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
- linkers.add(new TypeBasedGuardingDynamicLinker() {
- @Override
- public boolean canLinkType(final Class<?> type) {
- return Element.class.isAssignableFrom(type);
- }
-
- @Override
- public GuardedInvocation getGuardedInvocation(LinkRequest request,
- LinkerServices linkerServices) throws Exception {
- final Object self = request.getReceiver();
- if (! (self instanceof Element)) {
- return null;
- }
-
- CallSiteDescriptor desc = request.getCallSiteDescriptor();
- Operation op = desc.getOperation();
- Object name = NamedOperation.getName(op);
- boolean getProp = CompositeOperation.contains(
- NamedOperation.getBaseOperation(op),
- StandardOperation.GET_PROPERTY);
- if (getProp && name instanceof String) {
- String nameStr = (String)name;
-
- // Treat names starting with "_" as special names.
- // Everything else is linked other dynalink bean linker!
- // This avoids collision with Java methods of org.w3c.dom.Element class
- // Assumption is that Java APIs won't start with "_" character!!
- if (nameStr.equals("_")) {
- // short-hand to get text content under a given DOM Element
- return new GuardedInvocation(ELEMENT_TEXT, IS_ELEMENT);
- } else if (nameStr.startsWith("_")) {
- return new GuardedInvocation(
- MethodHandles.insertArguments(ELEMENTS_BY_NAME, 1, nameStr.substring(1)),
- IS_ELEMENT);
- }
-
- }
-
- return null;
- }
- });
- return linkers;
- }
-}
--- a/nashorn/samples/META-INF/services/jdk.dynalink.linker.GuardingDynamicLinkerExporter Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-DOMLinkerExporter
-UnderscoreNameLinkerExporter
-MissingMethodLinkerExporter
--- a/nashorn/samples/MissingMethodExample.java Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,53 +0,0 @@
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-import java.util.ArrayList;
-
-// This is an example class that implements MissingMethodHandler
-// to receive "doesNotUnderstand" calls on 'missing methods'
-public class MissingMethodExample extends ArrayList
- implements MissingMethodHandler {
-
- @Override
- public Object doesNotUnderstand(String name, Object... args) {
- // This dummy doesNotUnderstand just prints method name and args.
- // You can put useful method routing logic here.
- System.out.println("you called " + name);
- if (args.length != 0) {
- System.out.println("arguments are: ");
- for (Object arg : args) {
- System.out.println(" " + arg);
- }
- }
- return this;
- }
-}
-
--- a/nashorn/samples/MissingMethodHandler.java Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// interface to be implemented by any Java class that wants
-// to trap "missing method" calls.
-public interface MissingMethodHandler {
- // name is "missing" method name. "args" is the array of arguments passed
- public Object doesNotUnderstand(String name, Object... args);
-}
--- a/nashorn/samples/MissingMethodLinkerExporter.java Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,190 +0,0 @@
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodType;
-import java.util.ArrayList;
-import java.util.List;
-import jdk.dynalink.CallSiteDescriptor;
-import jdk.dynalink.CompositeOperation;
-import jdk.dynalink.NamedOperation;
-import jdk.dynalink.Operation;
-import jdk.dynalink.StandardOperation;
-import jdk.dynalink.beans.BeansLinker;
-import jdk.dynalink.linker.GuardingDynamicLinker;
-import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
-import jdk.dynalink.linker.GuardedInvocation;
-import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
-import jdk.dynalink.linker.LinkRequest;
-import jdk.dynalink.linker.LinkerServices;
-import jdk.dynalink.linker.support.Guards;
-import jdk.dynalink.linker.support.Lookup;
-
-/**
- * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
- * This linker routes missing methods to Smalltalk-style doesNotUnderstand method.
- * Object of any Java class that implements MissingMethodHandler is handled by this linker.
- * For any method call, if a matching Java method is found, it is called. If there is no
- * method by that name, then MissingMethodHandler.doesNotUnderstand is called.
- */
-public final class MissingMethodLinkerExporter extends GuardingDynamicLinkerExporter {
- static {
- System.out.println("pluggable dynalink missing method linker loaded");
- }
-
- // represents a MissingMethod - just stores as name and also serves a guard type
- public static class MissingMethod {
- private final String name;
-
- public MissingMethod(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
- }
-
- // MissingMethodHandler.doesNotUnderstand method
- private static final MethodHandle DOES_NOT_UNDERSTAND;
-
- // type of MissingMethodHandler - but "this" and String args are flipped
- private static final MethodType FLIPPED_DOES_NOT_UNDERSTAND_TYPE;
-
- // "is this a MissingMethod?" guard
- private static final MethodHandle IS_MISSING_METHOD;
-
- // MissingMethod object->it's name filter
- private static final MethodHandle MISSING_METHOD_TO_NAME;
-
- static {
- DOES_NOT_UNDERSTAND = Lookup.PUBLIC.findVirtual(
- MissingMethodHandler.class,
- "doesNotUnderstand",
- MethodType.methodType(Object.class, String.class, Object[].class));
- FLIPPED_DOES_NOT_UNDERSTAND_TYPE =
- MethodType.methodType(Object.class, String.class, MissingMethodHandler.class, Object[].class);
- IS_MISSING_METHOD = Guards.isOfClass(MissingMethod.class,
- MethodType.methodType(Boolean.TYPE, Object.class));
- MISSING_METHOD_TO_NAME = Lookup.PUBLIC.findVirtual(MissingMethod.class,
- "getName", MethodType.methodType(String.class));
- }
-
- // locate the first standard operation from the call descriptor
- private static StandardOperation getFirstStandardOperation(final CallSiteDescriptor desc) {
- final Operation base = NamedOperation.getBaseOperation(desc.getOperation());
- if (base instanceof StandardOperation) {
- return (StandardOperation)base;
- } else if (base instanceof CompositeOperation) {
- final CompositeOperation cop = (CompositeOperation)base;
- for(int i = 0; i < cop.getOperationCount(); ++i) {
- final Operation op = cop.getOperation(i);
- if (op instanceof StandardOperation) {
- return (StandardOperation)op;
- }
- }
- }
- return null;
- }
-
- @Override
- public List<GuardingDynamicLinker> get() {
- final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
- final BeansLinker beansLinker = new BeansLinker();
- linkers.add(new TypeBasedGuardingDynamicLinker() {
- // only handles MissingMethodHandler and MissingMethod objects
- @Override
- public boolean canLinkType(final Class<?> type) {
- return
- MissingMethodHandler.class.isAssignableFrom(type) ||
- type == MissingMethod.class;
- }
-
- @Override
- public GuardedInvocation getGuardedInvocation(LinkRequest request,
- LinkerServices linkerServices) throws Exception {
- final Object self = request.getReceiver();
- CallSiteDescriptor desc = request.getCallSiteDescriptor();
-
- // any method call is done by two steps. Step (1) GET_METHOD and (2) is CALL
- // For step (1), we check if GET_METHOD can succeed by Java linker, if so
- // we return that method object. If not, we return a MissingMethod object.
- if (self instanceof MissingMethodHandler) {
- // Check if this is a named GET_METHOD first.
- boolean isGetMethod = getFirstStandardOperation(desc) == StandardOperation.GET_METHOD;
- Object name = NamedOperation.getName(desc.getOperation());
- if (isGetMethod && name instanceof String) {
- GuardingDynamicLinker javaLinker = beansLinker.getLinkerForClass(self.getClass());
- GuardedInvocation inv;
- try {
- inv = javaLinker.getGuardedInvocation(request, linkerServices);
- } catch (final Throwable th) {
- inv = null;
- }
-
- String nameStr = name.toString();
- if (inv == null) {
- // use "this" for just guard and drop it -- return a constant Method handle
- // that returns a newly created MissingMethod object
- MethodHandle mh = MethodHandles.constant(Object.class, new MissingMethod(nameStr));
- inv = new GuardedInvocation(
- MethodHandles.dropArguments(mh, 0, Object.class),
- Guards.isOfClass(self.getClass(), MethodType.methodType(Boolean.TYPE, Object.class)));
- }
-
- return inv;
- }
- } else if (self instanceof MissingMethod) {
- // This is step (2). We call MissingMethodHandler.doesNotUnderstand here
- // Check if this is this a CALL first.
- boolean isCall = getFirstStandardOperation(desc) == StandardOperation.CALL;
- if (isCall) {
- MethodHandle mh = DOES_NOT_UNDERSTAND;
-
- // flip "this" and method name (String)
- mh = MethodHandles.permuteArguments(mh, FLIPPED_DOES_NOT_UNDERSTAND_TYPE, 1, 0, 2);
-
- // collect rest of the arguments as vararg
- mh = mh.asCollector(Object[].class, desc.getMethodType().parameterCount() - 2);
-
- // convert MissingMethod object to it's name
- mh = MethodHandles.filterArguments(mh, 0, MISSING_METHOD_TO_NAME);
- return new GuardedInvocation(mh, IS_MISSING_METHOD);
- }
- }
-
- return null;
- }
- });
- return linkers;
- }
-}
--- a/nashorn/samples/UnderscoreNameLinkerExporter.java Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,129 +0,0 @@
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodType;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.regex.Pattern;
-import java.util.regex.Matcher;
-import jdk.dynalink.CallSiteDescriptor;
-import jdk.dynalink.CompositeOperation;
-import jdk.dynalink.NamedOperation;
-import jdk.dynalink.Operation;
-import jdk.dynalink.CompositeOperation;
-import jdk.dynalink.StandardOperation;
-import jdk.dynalink.linker.GuardingDynamicLinker;
-import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
-import jdk.dynalink.linker.GuardedInvocation;
-import jdk.dynalink.linker.LinkRequest;
-import jdk.dynalink.linker.LinkerServices;
-import jdk.dynalink.linker.support.SimpleLinkRequest;
-
-/**
- * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
- * This linker translater underscore_separated method names to CamelCase names
- * used in Java APIs.
- */
-public final class UnderscoreNameLinkerExporter extends GuardingDynamicLinkerExporter {
- static {
- System.out.println("pluggable dynalink underscore name linker loaded");
- }
-
- private static final Pattern UNDERSCORE_NAME = Pattern.compile("_(.)");
-
- // translate underscore_separated name as a CamelCase name
- private static String translateToCamelCase(String name) {
- Matcher m = UNDERSCORE_NAME.matcher(name);
- StringBuilder buf = new StringBuilder();
- while (m.find()) {
- m.appendReplacement(buf, m.group(1).toUpperCase());
- }
- m.appendTail(buf);
- return buf.toString();
- }
-
- // locate the first standard operation from the call descriptor
- private static StandardOperation getFirstStandardOperation(final CallSiteDescriptor desc) {
- final Operation base = NamedOperation.getBaseOperation(desc.getOperation());
- if (base instanceof StandardOperation) {
- return (StandardOperation)base;
- } else if (base instanceof CompositeOperation) {
- final CompositeOperation cop = (CompositeOperation)base;
- for(int i = 0; i < cop.getOperationCount(); ++i) {
- final Operation op = cop.getOperation(i);
- if (op instanceof StandardOperation) {
- return (StandardOperation)op;
- }
- }
- }
- return null;
- }
-
- @Override
- public List<GuardingDynamicLinker> get() {
- final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
- linkers.add(new GuardingDynamicLinker() {
- @Override
- public GuardedInvocation getGuardedInvocation(LinkRequest request,
- LinkerServices linkerServices) throws Exception {
- final Object self = request.getReceiver();
- CallSiteDescriptor desc = request.getCallSiteDescriptor();
- Operation op = desc.getOperation();
- Object name = NamedOperation.getName(op);
- // is this a named GET_METHOD?
- boolean isGetMethod = getFirstStandardOperation(desc) == StandardOperation.GET_METHOD;
- if (isGetMethod && name instanceof String) {
- String str = (String)name;
- if (str.indexOf('_') == -1) {
- return null;
- }
-
- String nameStr = translateToCamelCase(str);
- // create a new call descriptor to use translated name
- CallSiteDescriptor newDesc = new CallSiteDescriptor(
- desc.getLookup(),
- new NamedOperation(NamedOperation.getBaseOperation(op), nameStr),
- desc.getMethodType());
- // create a new Link request to link the call site with translated name
- LinkRequest newRequest = new SimpleLinkRequest(newDesc,
- request.isCallSiteUnstable(), request.getArguments());
- // return guarded invocation linking the translated request
- return linkerServices.getGuardedInvocation(newRequest);
- }
-
- return null;
- }
- });
- return linkers;
- }
-}
--- a/nashorn/samples/dom_linker.js Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-#! simple dom linker example
-
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// This script assumes you've built jdk9 or using latest
-// jdk9 image and put the 'bin' directory in the PATH
-
-$EXEC.throwOnError=true
-
-// compile DOMLinkerExporter
-`javac -cp ../dist/nashorn.jar DOMLinkerExporter.java`
-
-// make a jar file out of pluggable linker
-`jar cvf dom_linker.jar DOMLinkerExporter*.class META-INF/`
-
-// run a sample script that uses pluggable linker
-// but make sure classpath points to the pluggable linker jar!
-
-`jjs -cp dom_linker.jar dom_linker_gutenberg.js`
--- a/nashorn/samples/dom_linker_gutenberg.js Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,117 +0,0 @@
-#// Usage: jjs -cp dom_linker.jar -scripting dom_linker_gutenberg.js
-
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// This script depends on DOM Element dynalink linker!
-// Without that linker, this script will fail to run!
-
-// Simple example that demonstrates reading XML Rss feed
-// to generate a HTML file from script and show it by browser.
-// Uses XML DOM parser along with DOM element pluggable dynalink linker
-// for ease of navigation of DOM (child) elements by name.
-
-// Java classes used
-var DocBuilderFac = Java.type("javax.xml.parsers.DocumentBuilderFactory");
-var Node = Java.type("org.w3c.dom.Node");
-var File = Java.type("java.io.File");
-var FileWriter = Java.type("java.io.FileWriter");
-var PrintWriter = Java.type("java.io.PrintWriter");
-
-// parse XML from uri and return Document
-function parseXML(uri) {
- var docBuilder = DocBuilderFac.newInstance().newDocumentBuilder();
- return docBuilder["parse(java.lang.String)"](uri);
-}
-
-// generate HTML using here-doc and string interpolation
-function getBooksHtml() {
- var doc = parseXML("http://www.gutenberg.org/cache/epub/feeds/today.rss");
- // wrap document root Element as script convenient object
- var rss = doc.documentElement;
-
- var str = <<HEAD
-
-<html>
-<title>${rss._channel._title._}</title>
-<body>
-<h1>${rss._channel._description._}</h1>
-<p>
-Published on ${rss._channel._pubDate._}
-</p>
-
-HEAD
-
- var items = rss._channel._item;
- for each (var i in items) {
- str += <<LIST
-
-<dl>
-<dt><a href="${i._link._}">${i._title._}</a></dt>
-<dd>${i._description._}</dd>
-</dl>
-
-LIST
- }
- str += <<END
-
-</body>
-</html>
-
-END
- return str;
-}
-
-// write the string to the given file
-function writeTo(file, str) {
- var w = new PrintWriter(new FileWriter(file));
- try {
- w.print(str);
- } finally {
- w.close();
- }
-}
-
-// generate books HTML
-var str = getBooksHtml();
-
-// write to file. __DIR__ is directory where
-// this script is stored.
-var file = new File(__DIR__ + "books.html");
-writeTo(file, str);
-
-// show it by desktop browser
-try {
- var Desktop = Java.type("java.awt.Desktop");
- Desktop.desktop.browse(file.toURI());
-} catch (e) {
- print(e);
-}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/ArrayStreamLinkerExporter.java Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+import java.util.stream.Stream;
+import jdk.dynalink.CallSiteDescriptor;
+import jdk.dynalink.CompositeOperation;
+import jdk.dynalink.NamedOperation;
+import jdk.dynalink.Operation;
+import jdk.dynalink.StandardOperation;
+import jdk.dynalink.linker.GuardingDynamicLinker;
+import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
+import jdk.dynalink.linker.GuardedInvocation;
+import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
+import jdk.dynalink.linker.LinkRequest;
+import jdk.dynalink.linker.LinkerServices;
+import jdk.dynalink.linker.support.Guards;
+import jdk.dynalink.linker.support.Lookup;
+
+/**
+ * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
+ * This linker adds "stream" property to Java arrays. The appropriate Stream
+ * type object is returned for "stream" property on Java arrays. Note that
+ * the dynalink beans linker just adds "length" property and Java array objects
+ * don't have any other property. "stream" property does not conflict with anything
+ * else!
+ */
+public final class ArrayStreamLinkerExporter extends GuardingDynamicLinkerExporter {
+ static {
+ System.out.println("pluggable dynalink array stream linker loaded");
+ }
+
+ public static Object arrayToStream(Object array) {
+ if (array instanceof int[]) {
+ return IntStream.of((int[])array);
+ } else if (array instanceof long[]) {
+ return LongStream.of((long[])array);
+ } else if (array instanceof double[]) {
+ return DoubleStream.of((double[])array);
+ } else if (array instanceof Object[]) {
+ return Stream.of((Object[])array);
+ } else {
+ throw new IllegalArgumentException();
+ }
+ }
+
+ private static final MethodType GUARD_TYPE = MethodType.methodType(Boolean.TYPE, Object.class);
+ private static final MethodHandle ARRAY_TO_STREAM = Lookup.PUBLIC.findStatic(
+ ArrayStreamLinkerExporter.class, "arrayToStream",
+ MethodType.methodType(Object.class, Object.class));
+
+ @Override
+ public List<GuardingDynamicLinker> get() {
+ final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
+ linkers.add(new TypeBasedGuardingDynamicLinker() {
+ @Override
+ public boolean canLinkType(final Class<?> type) {
+ return type == Object[].class || type == int[].class ||
+ type == long[].class || type == double[].class;
+ }
+
+ @Override
+ public GuardedInvocation getGuardedInvocation(LinkRequest request,
+ LinkerServices linkerServices) throws Exception {
+ final Object self = request.getReceiver();
+ if (self == null || !canLinkType(self.getClass())) {
+ return null;
+ }
+
+ CallSiteDescriptor desc = request.getCallSiteDescriptor();
+ Operation op = desc.getOperation();
+ Object name = NamedOperation.getName(op);
+ boolean getProp = CompositeOperation.contains(
+ NamedOperation.getBaseOperation(op),
+ StandardOperation.GET_PROPERTY);
+ if (getProp && "stream".equals(name)) {
+ return new GuardedInvocation(ARRAY_TO_STREAM,
+ Guards.isOfClass(self.getClass(), GUARD_TYPE));
+ }
+
+ return null;
+ }
+ });
+ return linkers;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/BufferIndexingLinkerExporter.java Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,259 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.ArrayList;
+import java.util.List;
+import java.nio.Buffer;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.DoubleBuffer;
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+import java.nio.LongBuffer;
+import java.nio.ShortBuffer;
+import jdk.dynalink.CallSiteDescriptor;
+import jdk.dynalink.CompositeOperation;
+import jdk.dynalink.NamedOperation;
+import jdk.dynalink.Operation;
+import jdk.dynalink.StandardOperation;
+import jdk.dynalink.linker.GuardingDynamicLinker;
+import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
+import jdk.dynalink.linker.GuardedInvocation;
+import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
+import jdk.dynalink.linker.LinkRequest;
+import jdk.dynalink.linker.LinkerServices;
+import jdk.dynalink.linker.support.Guards;
+import jdk.dynalink.linker.support.Lookup;
+
+/**
+ * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
+ * This linker adds array-like indexing and "length" property to nio Buffer objects.
+ */
+public final class BufferIndexingLinkerExporter extends GuardingDynamicLinkerExporter {
+ static {
+ System.out.println("pluggable dynalink buffer indexing linker loaded");
+ }
+
+ private static final MethodHandle BUFFER_LIMIT;
+ private static final MethodHandle BYTEBUFFER_GET;
+ private static final MethodHandle BYTEBUFFER_PUT;
+ private static final MethodHandle CHARBUFFER_GET;
+ private static final MethodHandle CHARBUFFER_PUT;
+ private static final MethodHandle SHORTBUFFER_GET;
+ private static final MethodHandle SHORTBUFFER_PUT;
+ private static final MethodHandle INTBUFFER_GET;
+ private static final MethodHandle INTBUFFER_PUT;
+ private static final MethodHandle LONGBUFFER_GET;
+ private static final MethodHandle LONGBUFFER_PUT;
+ private static final MethodHandle FLOATBUFFER_GET;
+ private static final MethodHandle FLOATBUFFER_PUT;
+ private static final MethodHandle DOUBLEBUFFER_GET;
+ private static final MethodHandle DOUBLEBUFFER_PUT;
+
+ // guards
+ private static final MethodHandle IS_BUFFER;
+ private static final MethodHandle IS_BYTEBUFFER;
+ private static final MethodHandle IS_CHARBUFFER;
+ private static final MethodHandle IS_SHORTBUFFER;
+ private static final MethodHandle IS_INTBUFFER;
+ private static final MethodHandle IS_LONGBUFFER;
+ private static final MethodHandle IS_FLOATBUFFER;
+ private static final MethodHandle IS_DOUBLEBUFFER;
+
+ private static final MethodType GUARD_TYPE;
+
+ static {
+ Lookup look = Lookup.PUBLIC;
+ BUFFER_LIMIT = look.findVirtual(Buffer.class, "limit", MethodType.methodType(int.class));
+ BYTEBUFFER_GET = look.findVirtual(ByteBuffer.class, "get",
+ MethodType.methodType(byte.class, int.class));
+ BYTEBUFFER_PUT = look.findVirtual(ByteBuffer.class, "put",
+ MethodType.methodType(ByteBuffer.class, int.class, byte.class));
+ CHARBUFFER_GET = look.findVirtual(CharBuffer.class, "get",
+ MethodType.methodType(char.class, int.class));
+ CHARBUFFER_PUT = look.findVirtual(CharBuffer.class, "put",
+ MethodType.methodType(CharBuffer.class, int.class, char.class));
+ SHORTBUFFER_GET = look.findVirtual(ShortBuffer.class, "get",
+ MethodType.methodType(short.class, int.class));
+ SHORTBUFFER_PUT = look.findVirtual(ShortBuffer.class, "put",
+ MethodType.methodType(ShortBuffer.class, int.class, short.class));
+ INTBUFFER_GET = look.findVirtual(IntBuffer.class, "get",
+ MethodType.methodType(int.class, int.class));
+ INTBUFFER_PUT = look.findVirtual(IntBuffer.class, "put",
+ MethodType.methodType(IntBuffer.class, int.class, int.class));
+ LONGBUFFER_GET = look.findVirtual(LongBuffer.class, "get",
+ MethodType.methodType(long.class, int.class));
+ LONGBUFFER_PUT = look.findVirtual(LongBuffer.class, "put",
+ MethodType.methodType(LongBuffer.class, int.class, long.class));
+ FLOATBUFFER_GET = look.findVirtual(FloatBuffer.class, "get",
+ MethodType.methodType(float.class, int.class));
+ FLOATBUFFER_PUT = look.findVirtual(FloatBuffer.class, "put",
+ MethodType.methodType(FloatBuffer.class, int.class, float.class));
+ DOUBLEBUFFER_GET = look.findVirtual(DoubleBuffer.class, "get",
+ MethodType.methodType(double.class, int.class));
+ DOUBLEBUFFER_PUT = look.findVirtual(DoubleBuffer.class, "put",
+ MethodType.methodType(DoubleBuffer.class, int.class, double.class));
+
+ GUARD_TYPE = MethodType.methodType(boolean.class, Object.class);
+ IS_BUFFER = Guards.isInstance(Buffer.class, GUARD_TYPE);
+ IS_BYTEBUFFER = Guards.isInstance(ByteBuffer.class, GUARD_TYPE);
+ IS_CHARBUFFER = Guards.isInstance(CharBuffer.class, GUARD_TYPE);
+ IS_SHORTBUFFER = Guards.isInstance(ShortBuffer.class, GUARD_TYPE);
+ IS_INTBUFFER = Guards.isInstance(IntBuffer.class, GUARD_TYPE);
+ IS_LONGBUFFER = Guards.isInstance(LongBuffer.class, GUARD_TYPE);
+ IS_FLOATBUFFER = Guards.isInstance(FloatBuffer.class, GUARD_TYPE);
+ IS_DOUBLEBUFFER = Guards.isInstance(DoubleBuffer.class, GUARD_TYPE);
+ }
+
+ // locate the first standard operation from the call descriptor
+ private static StandardOperation getFirstStandardOperation(final CallSiteDescriptor desc) {
+ final Operation base = NamedOperation.getBaseOperation(desc.getOperation());
+ if (base instanceof StandardOperation) {
+ return (StandardOperation)base;
+ } else if (base instanceof CompositeOperation) {
+ final CompositeOperation cop = (CompositeOperation)base;
+ for(int i = 0; i < cop.getOperationCount(); ++i) {
+ final Operation op = cop.getOperation(i);
+ if (op instanceof StandardOperation) {
+ return (StandardOperation)op;
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public List<GuardingDynamicLinker> get() {
+ final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
+ linkers.add(new TypeBasedGuardingDynamicLinker() {
+ @Override
+ public boolean canLinkType(final Class<?> type) {
+ return Buffer.class.isAssignableFrom(type);
+ }
+
+ @Override
+ public GuardedInvocation getGuardedInvocation(LinkRequest request,
+ LinkerServices linkerServices) throws Exception {
+ final Object self = request.getReceiver();
+ if (self == null || !canLinkType(self.getClass())) {
+ return null;
+ }
+
+ CallSiteDescriptor desc = request.getCallSiteDescriptor();
+ StandardOperation op = getFirstStandardOperation(desc);
+ if (op == null) {
+ return null;
+ }
+
+ switch (op) {
+ case GET_ELEMENT:
+ return linkGetElement(self);
+ case SET_ELEMENT:
+ return linkSetElement(self);
+ case GET_PROPERTY: {
+ Object name = NamedOperation.getName(desc.getOperation());
+ if ("length".equals(name)) {
+ return linkLength();
+ }
+ }
+ }
+
+ return null;
+ }
+ });
+ return linkers;
+ }
+
+ private static GuardedInvocation linkGetElement(Object self) {
+ MethodHandle method = null;
+ MethodHandle guard = null;
+ if (self instanceof ByteBuffer) {
+ method = BYTEBUFFER_GET;
+ guard = IS_BYTEBUFFER;
+ } else if (self instanceof CharBuffer) {
+ method = CHARBUFFER_GET;
+ guard = IS_CHARBUFFER;
+ } else if (self instanceof ShortBuffer) {
+ method = SHORTBUFFER_GET;
+ guard = IS_SHORTBUFFER;
+ } else if (self instanceof IntBuffer) {
+ method = INTBUFFER_GET;
+ guard = IS_INTBUFFER;
+ } else if (self instanceof LongBuffer) {
+ method = LONGBUFFER_GET;
+ guard = IS_LONGBUFFER;
+ } else if (self instanceof FloatBuffer) {
+ method = FLOATBUFFER_GET;
+ guard = IS_FLOATBUFFER;
+ } else if (self instanceof DoubleBuffer) {
+ method = DOUBLEBUFFER_GET;
+ guard = IS_DOUBLEBUFFER;
+ }
+
+ return method != null? new GuardedInvocation(method, guard) : null;
+ }
+
+ private static GuardedInvocation linkSetElement(Object self) {
+ MethodHandle method = null;
+ MethodHandle guard = null;
+ if (self instanceof ByteBuffer) {
+ method = BYTEBUFFER_PUT;
+ guard = IS_BYTEBUFFER;
+ } else if (self instanceof CharBuffer) {
+ method = CHARBUFFER_PUT;
+ guard = IS_CHARBUFFER;
+ } else if (self instanceof ShortBuffer) {
+ method = SHORTBUFFER_PUT;
+ guard = IS_SHORTBUFFER;
+ } else if (self instanceof IntBuffer) {
+ method = INTBUFFER_PUT;
+ guard = IS_INTBUFFER;
+ } else if (self instanceof LongBuffer) {
+ method = LONGBUFFER_PUT;
+ guard = IS_LONGBUFFER;
+ } else if (self instanceof FloatBuffer) {
+ method = FLOATBUFFER_PUT;
+ guard = IS_FLOATBUFFER;
+ } else if (self instanceof DoubleBuffer) {
+ method = DOUBLEBUFFER_PUT;
+ guard = IS_DOUBLEBUFFER;
+ }
+
+ return method != null? new GuardedInvocation(method, guard) : null;
+ }
+
+ private static GuardedInvocation linkLength() {
+ return new GuardedInvocation(BUFFER_LIMIT, IS_BUFFER);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/DOMLinkerExporter.java Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.ArrayList;
+import java.util.List;
+import jdk.dynalink.CallSiteDescriptor;
+import jdk.dynalink.CompositeOperation;
+import jdk.dynalink.NamedOperation;
+import jdk.dynalink.Operation;
+import jdk.dynalink.StandardOperation;
+import jdk.dynalink.linker.GuardingDynamicLinker;
+import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
+import jdk.dynalink.linker.GuardedInvocation;
+import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
+import jdk.dynalink.linker.LinkRequest;
+import jdk.dynalink.linker.LinkerServices;
+import jdk.dynalink.linker.support.Guards;
+import jdk.dynalink.linker.support.Lookup;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Element;
+
+/**
+ * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
+ * This linker handles XML DOM Element objects specially. This linker links
+ * special properties starting with "_" and treats those as child element names
+ * to access. This kind of child element access makes it easy to write XML DOM
+ * accessing scripts. See for example ./dom_linker_gutenberg.js.
+ */
+public final class DOMLinkerExporter extends GuardingDynamicLinkerExporter {
+ static {
+ System.out.println("pluggable dynalink DOM linker loaded");
+ }
+
+ // return List of child Elements of the given Element matching the given name.
+ private static List<Element> getChildElements(Element elem, String name) {
+ NodeList nodeList = elem.getChildNodes();
+ List<Element> childElems = new ArrayList<>();
+ int len = nodeList.getLength();
+ for (int i = 0; i < len; i++) {
+ Node node = nodeList.item(i);
+ if (node.getNodeType() == Node.ELEMENT_NODE &&
+ ((Element)node).getTagName().equals(name)) {
+ childElems.add((Element)node);
+ }
+ }
+ return childElems;
+ }
+
+ // method that returns either unique child element matching given name
+ // or a list of child elements of that name (if there are more than one matches).
+ public static Object getElementsByName(Object elem, final String name) {
+ List<Element> elems = getChildElements((Element)elem, name);
+ return elems.size() == 1? elems.get(0) : elems;
+ }
+
+ // method to extract text context under a given DOM Element
+ public static Object getElementText(Object elem) {
+ NodeList nodeList = ((Element)elem).getChildNodes();
+ int len = nodeList.getLength();
+ StringBuilder text = new StringBuilder();
+ for (int i = 0; i < len; i++) {
+ Node node = nodeList.item(i);
+ if (node.getNodeType() == Node.TEXT_NODE) {
+ text.append(node.getNodeValue());
+ }
+ }
+ return text.toString();
+ }
+
+ private static final MethodHandle ELEMENTS_BY_NAME;
+ private static final MethodHandle ELEMENT_TEXT;
+ private static final MethodHandle IS_ELEMENT;
+ static {
+ ELEMENTS_BY_NAME = Lookup.PUBLIC.findStatic(DOMLinkerExporter.class,
+ "getElementsByName",
+ MethodType.methodType(Object.class, Object.class, String.class));
+ ELEMENT_TEXT = Lookup.PUBLIC.findStatic(DOMLinkerExporter.class,
+ "getElementText",
+ MethodType.methodType(Object.class, Object.class));
+ IS_ELEMENT = Guards.isInstance(Element.class, MethodType.methodType(Boolean.TYPE, Object.class));
+ }
+
+ @Override
+ public List<GuardingDynamicLinker> get() {
+ final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
+ linkers.add(new TypeBasedGuardingDynamicLinker() {
+ @Override
+ public boolean canLinkType(final Class<?> type) {
+ return Element.class.isAssignableFrom(type);
+ }
+
+ @Override
+ public GuardedInvocation getGuardedInvocation(LinkRequest request,
+ LinkerServices linkerServices) throws Exception {
+ final Object self = request.getReceiver();
+ if (! (self instanceof Element)) {
+ return null;
+ }
+
+ CallSiteDescriptor desc = request.getCallSiteDescriptor();
+ Operation op = desc.getOperation();
+ Object name = NamedOperation.getName(op);
+ boolean getProp = CompositeOperation.contains(
+ NamedOperation.getBaseOperation(op),
+ StandardOperation.GET_PROPERTY);
+ if (getProp && name instanceof String) {
+ String nameStr = (String)name;
+
+ // Treat names starting with "_" as special names.
+ // Everything else is linked other dynalink bean linker!
+ // This avoids collision with Java methods of org.w3c.dom.Element class
+ // Assumption is that Java APIs won't start with "_" character!!
+ if (nameStr.equals("_")) {
+ // short-hand to get text content under a given DOM Element
+ return new GuardedInvocation(ELEMENT_TEXT, IS_ELEMENT);
+ } else if (nameStr.startsWith("_")) {
+ return new GuardedInvocation(
+ MethodHandles.insertArguments(ELEMENTS_BY_NAME, 1, nameStr.substring(1)),
+ IS_ELEMENT);
+ }
+
+ }
+
+ return null;
+ }
+ });
+ return linkers;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/META-INF/services/jdk.dynalink.linker.GuardingDynamicLinkerExporter Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,5 @@
+DOMLinkerExporter
+UnderscoreNameLinkerExporter
+MissingMethodLinkerExporter
+ArrayStreamLinkerExporter
+BufferIndexingLinkerExporter
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/MissingMethodExample.java Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.ArrayList;
+
+// This is an example class that implements MissingMethodHandler
+// to receive "doesNotUnderstand" calls on 'missing methods'
+public class MissingMethodExample extends ArrayList
+ implements MissingMethodHandler {
+
+ @Override
+ public Object doesNotUnderstand(String name, Object... args) {
+ // This simple doesNotUnderstand just prints method name and args.
+ // You can put useful method routing logic here.
+ System.out.println("you called " + name);
+ if (args.length != 0) {
+ System.out.println("arguments are: ");
+ for (Object arg : args) {
+ System.out.println(" " + arg);
+ }
+ }
+ return this;
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/MissingMethodHandler.java Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// interface to be implemented by any Java class that wants
+// to trap "missing method" calls.
+public interface MissingMethodHandler {
+ // name is "missing" method name. "args" is the array of arguments passed
+ public Object doesNotUnderstand(String name, Object... args);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/MissingMethodLinkerExporter.java Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,190 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.ArrayList;
+import java.util.List;
+import jdk.dynalink.CallSiteDescriptor;
+import jdk.dynalink.CompositeOperation;
+import jdk.dynalink.NamedOperation;
+import jdk.dynalink.Operation;
+import jdk.dynalink.StandardOperation;
+import jdk.dynalink.beans.BeansLinker;
+import jdk.dynalink.linker.GuardingDynamicLinker;
+import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
+import jdk.dynalink.linker.GuardedInvocation;
+import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
+import jdk.dynalink.linker.LinkRequest;
+import jdk.dynalink.linker.LinkerServices;
+import jdk.dynalink.linker.support.Guards;
+import jdk.dynalink.linker.support.Lookup;
+
+/**
+ * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
+ * This linker routes missing methods to Smalltalk-style doesNotUnderstand method.
+ * Object of any Java class that implements MissingMethodHandler is handled by this linker.
+ * For any method call, if a matching Java method is found, it is called. If there is no
+ * method by that name, then MissingMethodHandler.doesNotUnderstand is called.
+ */
+public final class MissingMethodLinkerExporter extends GuardingDynamicLinkerExporter {
+ static {
+ System.out.println("pluggable dynalink missing method linker loaded");
+ }
+
+ // represents a MissingMethod - just stores as name and also serves a guard type
+ public static class MissingMethod {
+ private final String name;
+
+ public MissingMethod(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+ }
+
+ // MissingMethodHandler.doesNotUnderstand method
+ private static final MethodHandle DOES_NOT_UNDERSTAND;
+
+ // type of MissingMethodHandler - but "this" and String args are flipped
+ private static final MethodType FLIPPED_DOES_NOT_UNDERSTAND_TYPE;
+
+ // "is this a MissingMethod?" guard
+ private static final MethodHandle IS_MISSING_METHOD;
+
+ // MissingMethod object->it's name filter
+ private static final MethodHandle MISSING_METHOD_TO_NAME;
+
+ static {
+ DOES_NOT_UNDERSTAND = Lookup.PUBLIC.findVirtual(
+ MissingMethodHandler.class,
+ "doesNotUnderstand",
+ MethodType.methodType(Object.class, String.class, Object[].class));
+ FLIPPED_DOES_NOT_UNDERSTAND_TYPE =
+ MethodType.methodType(Object.class, String.class, MissingMethodHandler.class, Object[].class);
+ IS_MISSING_METHOD = Guards.isOfClass(MissingMethod.class,
+ MethodType.methodType(Boolean.TYPE, Object.class));
+ MISSING_METHOD_TO_NAME = Lookup.PUBLIC.findVirtual(MissingMethod.class,
+ "getName", MethodType.methodType(String.class));
+ }
+
+ // locate the first standard operation from the call descriptor
+ private static StandardOperation getFirstStandardOperation(final CallSiteDescriptor desc) {
+ final Operation base = NamedOperation.getBaseOperation(desc.getOperation());
+ if (base instanceof StandardOperation) {
+ return (StandardOperation)base;
+ } else if (base instanceof CompositeOperation) {
+ final CompositeOperation cop = (CompositeOperation)base;
+ for(int i = 0; i < cop.getOperationCount(); ++i) {
+ final Operation op = cop.getOperation(i);
+ if (op instanceof StandardOperation) {
+ return (StandardOperation)op;
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public List<GuardingDynamicLinker> get() {
+ final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
+ final BeansLinker beansLinker = new BeansLinker();
+ linkers.add(new TypeBasedGuardingDynamicLinker() {
+ // only handles MissingMethodHandler and MissingMethod objects
+ @Override
+ public boolean canLinkType(final Class<?> type) {
+ return
+ MissingMethodHandler.class.isAssignableFrom(type) ||
+ type == MissingMethod.class;
+ }
+
+ @Override
+ public GuardedInvocation getGuardedInvocation(LinkRequest request,
+ LinkerServices linkerServices) throws Exception {
+ final Object self = request.getReceiver();
+ CallSiteDescriptor desc = request.getCallSiteDescriptor();
+
+ // any method call is done by two steps. Step (1) GET_METHOD and (2) is CALL
+ // For step (1), we check if GET_METHOD can succeed by Java linker, if so
+ // we return that method object. If not, we return a MissingMethod object.
+ if (self instanceof MissingMethodHandler) {
+ // Check if this is a named GET_METHOD first.
+ boolean isGetMethod = getFirstStandardOperation(desc) == StandardOperation.GET_METHOD;
+ Object name = NamedOperation.getName(desc.getOperation());
+ if (isGetMethod && name instanceof String) {
+ GuardingDynamicLinker javaLinker = beansLinker.getLinkerForClass(self.getClass());
+ GuardedInvocation inv;
+ try {
+ inv = javaLinker.getGuardedInvocation(request, linkerServices);
+ } catch (final Throwable th) {
+ inv = null;
+ }
+
+ String nameStr = name.toString();
+ if (inv == null) {
+ // use "this" for just guard and drop it -- return a constant Method handle
+ // that returns a newly created MissingMethod object
+ MethodHandle mh = MethodHandles.constant(Object.class, new MissingMethod(nameStr));
+ inv = new GuardedInvocation(
+ MethodHandles.dropArguments(mh, 0, Object.class),
+ Guards.isOfClass(self.getClass(), MethodType.methodType(Boolean.TYPE, Object.class)));
+ }
+
+ return inv;
+ }
+ } else if (self instanceof MissingMethod) {
+ // This is step (2). We call MissingMethodHandler.doesNotUnderstand here
+ // Check if this is this a CALL first.
+ boolean isCall = getFirstStandardOperation(desc) == StandardOperation.CALL;
+ if (isCall) {
+ MethodHandle mh = DOES_NOT_UNDERSTAND;
+
+ // flip "this" and method name (String)
+ mh = MethodHandles.permuteArguments(mh, FLIPPED_DOES_NOT_UNDERSTAND_TYPE, 1, 0, 2);
+
+ // collect rest of the arguments as vararg
+ mh = mh.asCollector(Object[].class, desc.getMethodType().parameterCount() - 2);
+
+ // convert MissingMethod object to it's name
+ mh = MethodHandles.filterArguments(mh, 0, MISSING_METHOD_TO_NAME);
+ return new GuardedInvocation(mh, IS_MISSING_METHOD);
+ }
+ }
+
+ return null;
+ }
+ });
+ return linkers;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/README Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,51 @@
+This directory contains samples for Dynalink API (http://openjdk.java.net/jeps/276).
+These samples require a jar file to be built and such jars be placed in the
+classpath of the jjs tool. Linker samples are named with the naming pattern
+"xyz_linker.js". These scripts build dynalink linker jar from java code and exec
+another jjs process with appropriate classpath set.
+
+Note: you need to build jdk9 forest and put "images/jdk/bin" in your PATH to use
+these scripts. This is because these scripts use javac to build dynalink jar and
+exec another jjs with classpath set! Alternatively, you can also manually build
+dynalink linker jars and invoke sample scripts by putting linker jar in jjs tool's
+classpath as well.
+
+Dynalink samples:
+
+* array_stream_linker.js
+
+This sample builds ArrayStreamLinkerExporter.java and uses it in a sample script
+called "array_stream.js". This linker adds "stream" property to Java array
+objects. The "stream" property returns appropriate Stream type for the given
+Java array (IntStream, DoubleStream ...).
+
+* buffer_indexing_linker.js
+
+This sample builds BufferIndexingLinkerExporter.java and uses it in a sample script
+called "buffer_index.js". This linker adds array-like indexed access, indexed assignment
+and "length" property to Java NIO Buffer objects. Script can treat NIO Buffer objects
+as if those are just array objects.
+
+* dom_linker.js
+
+This sample builds DOMLinkerExporter.java and uses it in a sample script
+called "dom_linker_gutenberg.js". This linker handles DOM Element objects to add
+properties to access child elements of a given element by child element tag name.
+This simplifies script access of parsed XML DOM Documents.
+
+* missing_method_linker.js
+
+This sample builds MissingMethodLinkerExporter.java and uses it in a sample script
+called "missing_method.js". This linker supports Smalltalk-style "doesNotUnderstand"
+calls on Java objects. i.e., A Java class can implement MissingMethodHandler interface
+with one method named "doesNotUnderstand". When script accesses a method on such
+object and if that method does not exist in the Java class (or any of it's supertypes),
+then "doesNotUnderstand" method is invoked.
+
+* underscore_linker.js
+
+This sample builds UnderscoreNameLinkerExporter.java and uses it in a sample script
+called "underscore.js". This linker converts underscore separated names to Camel Case
+names (as used in Java APIs). You can call Java APIs using Ruby-like naming convention
+and this linker converts method names to CamelCase!
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/UnderscoreNameLinkerExporter.java Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+import jdk.dynalink.CallSiteDescriptor;
+import jdk.dynalink.CompositeOperation;
+import jdk.dynalink.NamedOperation;
+import jdk.dynalink.Operation;
+import jdk.dynalink.CompositeOperation;
+import jdk.dynalink.StandardOperation;
+import jdk.dynalink.linker.GuardingDynamicLinker;
+import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
+import jdk.dynalink.linker.GuardedInvocation;
+import jdk.dynalink.linker.LinkRequest;
+import jdk.dynalink.linker.LinkerServices;
+import jdk.dynalink.linker.support.SimpleLinkRequest;
+
+/**
+ * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
+ * This linker translater underscore_separated method names to CamelCase names
+ * used in Java APIs.
+ */
+public final class UnderscoreNameLinkerExporter extends GuardingDynamicLinkerExporter {
+ static {
+ System.out.println("pluggable dynalink underscore name linker loaded");
+ }
+
+ private static final Pattern UNDERSCORE_NAME = Pattern.compile("_(.)");
+
+ // translate underscore_separated name as a CamelCase name
+ private static String translateToCamelCase(String name) {
+ Matcher m = UNDERSCORE_NAME.matcher(name);
+ StringBuilder buf = new StringBuilder();
+ while (m.find()) {
+ m.appendReplacement(buf, m.group(1).toUpperCase());
+ }
+ m.appendTail(buf);
+ return buf.toString();
+ }
+
+ // locate the first standard operation from the call descriptor
+ private static StandardOperation getFirstStandardOperation(final CallSiteDescriptor desc) {
+ final Operation base = NamedOperation.getBaseOperation(desc.getOperation());
+ if (base instanceof StandardOperation) {
+ return (StandardOperation)base;
+ } else if (base instanceof CompositeOperation) {
+ final CompositeOperation cop = (CompositeOperation)base;
+ for(int i = 0; i < cop.getOperationCount(); ++i) {
+ final Operation op = cop.getOperation(i);
+ if (op instanceof StandardOperation) {
+ return (StandardOperation)op;
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public List<GuardingDynamicLinker> get() {
+ final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
+ linkers.add(new GuardingDynamicLinker() {
+ @Override
+ public GuardedInvocation getGuardedInvocation(LinkRequest request,
+ LinkerServices linkerServices) throws Exception {
+ final Object self = request.getReceiver();
+ CallSiteDescriptor desc = request.getCallSiteDescriptor();
+ Operation op = desc.getOperation();
+ Object name = NamedOperation.getName(op);
+ // is this a named GET_METHOD?
+ boolean isGetMethod = getFirstStandardOperation(desc) == StandardOperation.GET_METHOD;
+ if (isGetMethod && name instanceof String) {
+ String str = (String)name;
+ if (str.indexOf('_') == -1) {
+ return null;
+ }
+
+ String nameStr = translateToCamelCase(str);
+ // create a new call descriptor to use translated name
+ CallSiteDescriptor newDesc = new CallSiteDescriptor(
+ desc.getLookup(),
+ new NamedOperation(NamedOperation.getBaseOperation(op), nameStr),
+ desc.getMethodType());
+ // create a new Link request to link the call site with translated name
+ LinkRequest newRequest = new SimpleLinkRequest(newDesc,
+ request.isCallSiteUnstable(), request.getArguments());
+ // return guarded invocation linking the translated request
+ return linkerServices.getGuardedInvocation(newRequest);
+ }
+
+ return null;
+ }
+ });
+ return linkers;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/array_stream.js Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,54 @@
+# Usage: jjs -cp array_stream_linker.jar array_stream.js
+
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This script depends on array stream dynalink linker
+// to work as expected. Without that linker in jjs classpath,
+// this script will fail to run.
+
+// Object[] and then Stream
+var s = Java.to(["hello", "world"]).stream
+s.map(function(s) s.toUpperCase()).forEach(print)
+
+// IntStream
+var is = Java.to([3, 56, 4, 23], "int[]").stream
+print(is.map(function(x) x*x).sum())
+
+// DoubleStream
+var DoubleArray = Java.type("double[]")
+var arr = new DoubleArray(100)
+for (var i = 0; i < arr.length; i++)
+ arr[i] = Math.random()
+
+print(arr.stream.summaryStatistics())
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/array_stream_linker.js Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,49 @@
+#! array stream linker example
+
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This script assumes you've built jdk9 or using latest
+// jdk9 image and put the 'bin' directory in the PATH
+
+$EXEC.throwOnError=true
+
+// compile ArrayStreamLinkerExporter
+`javac -cp ../../dist/nashorn.jar ArrayStreamLinkerExporter.java`
+
+// make a jar file out of pluggable linker
+`jar cvf array_stream_linker.jar ArrayStreamLinkerExporter*.class META-INF/`
+
+// run a sample script that uses pluggable linker
+// but make sure classpath points to the pluggable linker jar!
+
+`jjs -cp array_stream_linker.jar array_stream.js`
+print($OUT)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/buffer_index.js Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,65 @@
+# Usage: jjs -cp buffer_indexing_linker.jar buffer_index.js
+
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This script depends on buffer indexing dynalink linker. Without that
+// linker in classpath, this script will fail to run properly.
+
+function str(buf) {
+ var s = ''
+ for (var i = 0; i < buf.length; i++)
+ s += buf[i] + ","
+ return s
+}
+
+var ByteBuffer = Java.type("java.nio.ByteBuffer")
+var bb = ByteBuffer.allocate(10)
+for (var i = 0; i < bb.length; i++)
+ bb[i] = i*i
+print(str(bb))
+
+var CharBuffer = Java.type("java.nio.CharBuffer")
+var cb = CharBuffer.wrap("hello world")
+print(str(cb))
+
+var RandomAccessFile = Java.type("java.io.RandomAccessFile")
+var raf = new RandomAccessFile("buffer_index.js", "r")
+var chan = raf.getChannel()
+var fileSize = chan.size()
+var buf = ByteBuffer.allocate(fileSize)
+chan.read(buf)
+chan.close()
+
+var str = ''
+for (var i = 0; i < buf.length; i++)
+ str += String.fromCharCode(buf[i])
+print(str)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/buffer_indexing_linker.js Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,49 @@
+# buffer indexing linker example
+
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This script assumes you've built jdk9 or using latest
+// jdk9 image and put the 'bin' directory in the PATH
+
+$EXEC.throwOnError=true
+
+// compile BufferIndexingLinkerExporter
+`javac -cp ../../dist/nashorn.jar BufferIndexingLinkerExporter.java`
+
+// make a jar file out of pluggable linker
+`jar cvf buffer_indexing_linker.jar BufferIndexingLinkerExporter*.class META-INF/`
+
+// run a sample script that uses pluggable linker
+// but make sure classpath points to the pluggable linker jar!
+
+`jjs -cp buffer_indexing_linker.jar buffer_index.js`
+print($OUT)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/dom_linker.js Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,48 @@
+#! simple dom linker example
+
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This script assumes you've built jdk9 or using latest
+// jdk9 image and put the 'bin' directory in the PATH
+
+$EXEC.throwOnError=true
+
+// compile DOMLinkerExporter
+`javac -cp ../../dist/nashorn.jar DOMLinkerExporter.java`
+
+// make a jar file out of pluggable linker
+`jar cvf dom_linker.jar DOMLinkerExporter*.class META-INF/`
+
+// run a sample script that uses pluggable linker
+// but make sure classpath points to the pluggable linker jar!
+
+`jjs -cp dom_linker.jar dom_linker_gutenberg.js`
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/dom_linker_gutenberg.js Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,117 @@
+#// Usage: jjs -cp dom_linker.jar -scripting dom_linker_gutenberg.js
+
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This script depends on DOM Element dynalink linker!
+// Without that linker, this script will fail to run!
+
+// Simple example that demonstrates reading XML Rss feed
+// to generate a HTML file from script and show it by browser.
+// Uses XML DOM parser along with DOM element pluggable dynalink linker
+// for ease of navigation of DOM (child) elements by name.
+
+// Java classes used
+var DocBuilderFac = Java.type("javax.xml.parsers.DocumentBuilderFactory");
+var Node = Java.type("org.w3c.dom.Node");
+var File = Java.type("java.io.File");
+var FileWriter = Java.type("java.io.FileWriter");
+var PrintWriter = Java.type("java.io.PrintWriter");
+
+// parse XML from uri and return Document
+function parseXML(uri) {
+ var docBuilder = DocBuilderFac.newInstance().newDocumentBuilder();
+ return docBuilder["parse(java.lang.String)"](uri);
+}
+
+// generate HTML using here-doc and string interpolation
+function getBooksHtml() {
+ var doc = parseXML("http://www.gutenberg.org/cache/epub/feeds/today.rss");
+ // wrap document root Element as script convenient object
+ var rss = doc.documentElement;
+
+ var str = <<HEAD
+
+<html>
+<title>${rss._channel._title._}</title>
+<body>
+<h1>${rss._channel._description._}</h1>
+<p>
+Published on ${rss._channel._pubDate._}
+</p>
+
+HEAD
+
+ var items = rss._channel._item;
+ for each (var i in items) {
+ str += <<LIST
+
+<dl>
+<dt><a href="${i._link._}">${i._title._}</a></dt>
+<dd>${i._description._}</dd>
+</dl>
+
+LIST
+ }
+ str += <<END
+
+</body>
+</html>
+
+END
+ return str;
+}
+
+// write the string to the given file
+function writeTo(file, str) {
+ var w = new PrintWriter(new FileWriter(file));
+ try {
+ w.print(str);
+ } finally {
+ w.close();
+ }
+}
+
+// generate books HTML
+var str = getBooksHtml();
+
+// write to file. __DIR__ is directory where
+// this script is stored.
+var file = new File(__DIR__ + "books.html");
+writeTo(file, str);
+
+// show it by desktop browser
+try {
+ var Desktop = Java.type("java.awt.Desktop");
+ Desktop.desktop.browse(file.toURI());
+} catch (e) {
+ print(e);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/missing_method.js Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,51 @@
+# Usage: jjs -cp missing_method_linker.js missing_method.js
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// MissingMethodExample class extends ArrayList and
+// implements MissingMethodHandler interface
+var MME = Java.type("MissingMethodExample")
+
+var obj = new MME()
+
+// can call any ArrayList method
+obj.add("hello")
+obj.add("world")
+print(obj.size())
+print(obj)
+
+// "foo", "bar" are 'missing' methods
+// these will result in doesNotUnderstand calls
+obj.foo(1, "www", true)
+obj.bar("http", "dynalink")
+
+// another valid ArrayList method call
+obj.forEach(print)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/missing_method_linker.js Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,49 @@
+#! missing method linker example
+
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This script assumes you've built jdk9 or using latest
+// jdk9 image and put the 'bin' directory in the PATH
+
+$EXEC.throwOnError=true
+
+// compile MissingMethodLinkerExporter
+`javac -cp ../../dist/nashorn.jar MissingMethodLinkerExporter.java MissingMethodHandler.java MissingMethodExample.java`
+
+// make a jar file out of pluggable linker
+`jar cvf missing_method_linker.jar MissingMethod*.class META-INF/`
+
+// run a sample script that uses pluggable linker
+// but make sure classpath points to the pluggable linker jar!
+
+`jjs -cp missing_method_linker.jar missing_method.js`
+print($OUT)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/underscore.js Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,42 @@
+#// Usage: jjs -cp underscore_linker.jar -scripting underscore.js
+
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This script depends on underscore name to CamelCase name
+// translator dynalink linker! Without that linker, this script
+// will fail to run!
+
+var S = java.util.stream.Stream
+S.of(45, 54654,546,435).for_each(print)
+print("hello".char_at(2))
+print(4354.45.value_of())
+print(java.lang.System.get_properties())
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/dynalink/underscore_linker.js Thu Dec 10 09:24:33 2015 -0800
@@ -0,0 +1,50 @@
+# underscore name translator dynalink linker example
+
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This script assumes you've built jdk9 or using latest
+// jdk9 image and put the 'bin' directory in the PATH
+
+$EXEC.throwOnError=true
+
+// compile UnderscoreNameLinkerExporter
+`javac -cp ../../dist/nashorn.jar UnderscoreNameLinkerExporter.java`
+
+// make a jar file out of pluggable linker
+`jar cvf underscore_linker.jar UnderscoreNameLinkerExporter*.class META-INF/`
+
+// run a sample script that uses pluggable linker
+// but make sure classpath points to the pluggable linker jar!
+
+`jjs -cp underscore_linker.jar underscore.js`
+print($OUT)
+
--- a/nashorn/samples/missing_method.js Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-# Usage: jjs -cp missing_method_linker.js missing_method.js
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// MissingMethodExample class extends ArrayList and
-// implements MissingMethodHandler interface
-var MME = Java.type("MissingMethodExample")
-
-var obj = new MME()
-
-// can call any ArrayList method
-obj.add("hello")
-obj.add("world")
-print(obj.size())
-print(obj)
-
-// "foo", "bar" are 'missing' methods
-// these will result in doesNotUnderstand calls
-obj.foo(1, "www", true)
-obj.bar("http", "dynalink")
-
-// another valid ArrayList method call
-obj.forEach(print)
--- a/nashorn/samples/missing_method_linker.js Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,49 +0,0 @@
-#! missing method linker example
-
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// This script assumes you've built jdk9 or using latest
-// jdk9 image and put the 'bin' directory in the PATH
-
-$EXEC.throwOnError=true
-
-// compile MissingMethodLinkerExporter
-`javac -cp ../dist/nashorn.jar MissingMethodLinkerExporter.java MissingMethodHandler.java MissingMethodExample.java`
-
-// make a jar file out of pluggable linker
-`jar cvf missing_method_linker.jar MissingMethod*.class META-INF/`
-
-// run a sample script that uses pluggable linker
-// but make sure classpath points to the pluggable linker jar!
-
-`jjs -cp missing_method_linker.jar missing_method.js`
-print($OUT)
--- a/nashorn/samples/underscore.js Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,42 +0,0 @@
-#// Usage: jjs -cp underscore_linker.jar -scripting underscore.js
-
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// This script depends on underscore name to CamelCase name
-// translator dynalink linker! Without that linker, this script
-// will fail to run!
-
-var S = java.util.stream.Stream
-S.of(45, 54654,546,435).for_each(print)
-print("hello".char_at(2))
-print(4354.45.value_of())
-print(java.lang.System.get_properties())
--- a/nashorn/samples/underscore_linker.js Thu Dec 10 08:17:13 2015 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-# underscore name translator dynalink linker example
-
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// This script assumes you've built jdk9 or using latest
-// jdk9 image and put the 'bin' directory in the PATH
-
-$EXEC.throwOnError=true
-
-// compile UnderscoreNameLinkerExporter
-`javac -cp ../dist/nashorn.jar UnderscoreNameLinkerExporter.java`
-
-// make a jar file out of pluggable linker
-`jar cvf underscore_linker.jar UnderscoreNameLinkerExporter*.class META-INF/`
-
-// run a sample script that uses pluggable linker
-// but make sure classpath points to the pluggable linker jar!
-
-`jjs -cp underscore_linker.jar underscore.js`
-print($OUT)
-
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java Thu Dec 10 08:17:13 2015 -0800
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java Thu Dec 10 09:24:33 2015 -0800
@@ -1300,7 +1300,16 @@
* @return context
*/
static Context fromClass(final Class<?> clazz) {
- final ClassLoader loader = clazz.getClassLoader();
+ ClassLoader loader = null;
+ try {
+ loader = clazz.getClassLoader();
+ } catch (SecurityException ignored) {
+ // This could fail because of anonymous classes being used.
+ // Accessing loader of anonymous class fails (for extension
+ // loader class too?). In any case, for us fetching Context
+ // from class loader is just an optimization. We can always
+ // get Context from thread local storage (below).
+ }
if (loader instanceof ScriptLoader) {
return ((ScriptLoader)loader).getContext();