nashorn/samples/BufferArray.java
author jlahoda
Fri, 12 May 2017 06:42:37 +0200
changeset 45156 001f73134346
parent 26886 18c744ab4df2
permissions -rw-r--r--
8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current> Summary: --release 9 should only allow documented modules; --add-exports/--add-reads/--patch-module should not be allowed on system modules when --release 9 is used. Reviewed-by: jjg, erikj, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     1
/*
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     3
 *
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     6
 * are met:
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     7
 *
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    10
 *
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    14
 *
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    18
 *
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    30
 */
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    31
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
    32
import java.nio.DoubleBuffer;
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    33
import jdk.nashorn.api.scripting.AbstractJSObject;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    34
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    35
/**
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    36
 * Simple class demonstrating pluggable script object
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    37
 * implementation. By implementing jdk.nashorn.api.scripting.JSObject
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    38
 * (or extending AbstractJSObject which implements it), you
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    39
 * can supply a friendly script object. Nashorn will call
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    40
 * 'magic' methods on such a class on 'obj.foo, obj.foo = 33,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    41
 * obj.bar()' etc. from script.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    42
 *
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    43
 * In this example, Java nio DoubleBuffer object is wrapped
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    44
 * as a friendly script object that provides indexed acces
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    45
 * to buffer content and also support array-like "length"
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    46
 * readonly property to retrieve buffer's capacity. This class
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    47
 * also demonstrates a function valued property called "buf".
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    48
 * On 'buf' method, we return the underlying nio buffer object
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    49
 * that is being wrapped.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    50
 */
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    51
public class BufferArray extends AbstractJSObject {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    52
    // underlying nio buffer
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    53
    private final DoubleBuffer buf;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    54
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    55
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    56
     * Constructor
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    57
     * @param size initial size
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    58
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    59
    public BufferArray(final int size) {
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    60
        buf = DoubleBuffer.allocate(size);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    61
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    62
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    63
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    64
     * Constructur
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    65
     * @param buf {@link DoubleBuffer} to link to
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    66
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    67
    public BufferArray(final DoubleBuffer buf) {
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    68
        this.buf = buf;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    69
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    70
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    71
    // called to check if indexed property exists
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    72
    @Override
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    73
    public boolean hasSlot(final int index) {
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    74
        return index > 0 && index < buf.capacity();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    75
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    76
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    77
    // get the value from that index
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    78
    @Override
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    79
    public Object getSlot(final int index) {
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    80
       return buf.get(index);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    81
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    82
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    83
    // set the value at that index
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    84
    @Override
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    85
    public void setSlot(final int index, final Object value) {
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    86
       buf.put(index, ((Number)value).doubleValue());
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    87
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    88
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    89
    // do you have a property of that given name?
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    90
    @Override
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    91
    public boolean hasMember(final String name) {
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    92
       return "length".equals(name) || "buf".equals(name);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    93
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    94
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    95
    // get the value of that named property
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    96
    @Override
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
    97
    public Object getMember(final String name) {
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    98
       switch (name) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    99
          case "length":
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   100
              return buf.capacity();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   101
          case "buf":
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   102
              // return a 'function' value for this property
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   103
              return new AbstractJSObject() {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   104
                  @Override
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 24283
diff changeset
   105
                  public Object call(final Object thiz, final Object... args) {
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   106
                      return BufferArray.this.buf;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   107
                  }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   108
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   109
                  // yes, I'm a function !
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   110
                  @Override
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   111
                  public boolean isFunction() {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   112
                      return true;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   113
                  }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   114
              };
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   115
          default:
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   116
              break;
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   117
       }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   118
       return null;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   119
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   120
}