src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/scripting/ScriptObjectMirror.java
author sundar
Thu, 28 Jun 2018 18:04:19 +0530
changeset 50878 fb7800b66c92
parent 47216 71c04702a3d5
permissions -rw-r--r--
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs Reviewed-by: jlaskey, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.api.scripting;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
23078
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
    28
import java.nio.ByteBuffer;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    29
import java.security.AccessControlContext;
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
    30
import java.security.AccessController;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    31
import java.security.Permissions;
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
    32
import java.security.PrivilegedAction;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    33
import java.security.ProtectionDomain;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.util.AbstractMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import java.util.ArrayList;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import java.util.Collection;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import java.util.Collections;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    38
import java.util.Iterator;
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
    39
import java.util.LinkedHashSet;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
import java.util.List;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
import java.util.Map;
28785
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 28577
diff changeset
    42
import java.util.Objects;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
import java.util.Set;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
import java.util.concurrent.Callable;
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
    45
import javax.script.Bindings;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
    46
import jdk.nashorn.internal.objects.Global;
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
    47
import jdk.nashorn.internal.runtime.ConsString;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
import jdk.nashorn.internal.runtime.Context;
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
    49
import jdk.nashorn.internal.runtime.ECMAException;
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
    50
import jdk.nashorn.internal.runtime.JSONListAdapter;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
    51
import jdk.nashorn.internal.runtime.JSType;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
import jdk.nashorn.internal.runtime.ScriptFunction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
import jdk.nashorn.internal.runtime.ScriptObject;
16177
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    54
import jdk.nashorn.internal.runtime.ScriptRuntime;
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24772
diff changeset
    55
import jdk.nashorn.internal.runtime.arrays.ArrayData;
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26235
diff changeset
    56
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
/**
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
    59
 * Mirror object that wraps a given Nashorn Script object.
28317
0aeeae75e696 8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents: 26765
diff changeset
    60
 *
50878
fb7800b66c92 8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents: 47216
diff changeset
    61
 * @deprecated Nashorn JavaScript script engine and APIs, and the jjs tool
fb7800b66c92 8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents: 47216
diff changeset
    62
 * are deprecated with the intent to remove them in a future release.
fb7800b66c92 8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents: 47216
diff changeset
    63
 *
28317
0aeeae75e696 8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents: 26765
diff changeset
    64
 * @since 1.8u40
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
 */
50878
fb7800b66c92 8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents: 47216
diff changeset
    66
@Deprecated(since="11", forRemoval=true)
21453
b48953eede53 8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents: 21450
diff changeset
    67
public final class ScriptObjectMirror extends AbstractJSObject implements Bindings {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    68
    private static AccessControlContext getContextAccCtxt() {
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    69
        final Permissions perms = new Permissions();
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    70
        perms.add(new RuntimePermission(Context.NASHORN_GET_CONTEXT));
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    71
        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    72
    }
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    73
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    74
    private static final AccessControlContext GET_CONTEXT_ACC_CTXT = getContextAccCtxt();
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    75
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
    private final ScriptObject sobj;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
    77
    private final Global  global;
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
    78
    private final boolean strict;
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
    79
    private final boolean jsonCompatible;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
    public boolean equals(final Object other) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
        if (other instanceof ScriptObjectMirror) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
            return sobj.equals(((ScriptObjectMirror)other).sobj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
    public int hashCode() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
        return sobj.hashCode();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
16177
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    95
    @Override
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    96
    public String toString() {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    97
        return inGlobal(new Callable<String>() {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    98
            @Override
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    99
            public String call() {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
   100
                return ScriptRuntime.safeToString(sobj);
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
   101
            }
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
   102
        });
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
   103
    }
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
   104
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
    // JSObject methods
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   106
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
    @Override
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   108
    public Object call(final Object thiz, final Object... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   109
        final Global oldGlobal = Context.getGlobal();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
        final boolean globalChanged = (oldGlobal != global);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   112
        try {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   113
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   114
                Context.setGlobal(global);
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   115
            }
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   116
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   117
            if (sobj instanceof ScriptFunction) {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   118
                final Object[] modArgs = globalChanged? wrapArrayLikeMe(args, oldGlobal) : args;
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   119
                final Object self = globalChanged? wrapLikeMe(thiz, oldGlobal) : thiz;
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   120
                return wrapLikeMe(ScriptRuntime.apply((ScriptFunction)sobj, unwrap(self, global), unwrapArray(modArgs, global)));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   123
            throw new RuntimeException("not a function: " + toString());
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   124
        } catch (final NashornException ne) {
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   125
            throw ne.initEcmaError(global);
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   126
        } catch (final RuntimeException | Error e) {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   127
            throw e;
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   128
        } catch (final Throwable t) {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   129
            throw new RuntimeException(t);
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   130
        } finally {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   131
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   132
                Context.setGlobal(oldGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
            }
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   134
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
    @Override
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   138
    public Object newObject(final Object... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   139
        final Global oldGlobal = Context.getGlobal();
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   140
        final boolean globalChanged = (oldGlobal != global);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   141
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   142
        try {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   143
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   144
                Context.setGlobal(global);
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   145
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   146
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   147
            if (sobj instanceof ScriptFunction) {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   148
                final Object[] modArgs = globalChanged? wrapArrayLikeMe(args, oldGlobal) : args;
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   149
                return wrapLikeMe(ScriptRuntime.construct((ScriptFunction)sobj, unwrapArray(modArgs, global)));
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   150
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   151
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   152
            throw new RuntimeException("not a constructor: " + toString());
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   153
        } catch (final NashornException ne) {
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   154
            throw ne.initEcmaError(global);
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   155
        } catch (final RuntimeException | Error e) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   156
            throw e;
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   157
        } catch (final Throwable t) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   158
            throw new RuntimeException(t);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   159
        } finally {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   160
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   161
                Context.setGlobal(oldGlobal);
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   162
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   163
        }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   164
    }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   165
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   166
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
    public Object eval(final String s) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
        return inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
            public Object call() {
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   171
                final Context context = AccessController.doPrivileged(
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   172
                        new PrivilegedAction<Context>() {
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   173
                            @Override
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   174
                            public Context run() {
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   175
                                return Context.getContext();
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   176
                            }
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
   177
                        }, GET_CONTEXT_ACC_CTXT);
31192
1e019aeea9b5 8087211: Indirect evals should be strict with -strict option
sundar
parents: 31094
diff changeset
   178
                return wrapLikeMe(context.eval(global, s, sobj, null));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   183
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   184
     * Call member function
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   185
     * @param functionName function name
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   186
     * @param args         arguments
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   187
     * @return return value of function
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   188
     */
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   189
    public Object callMember(final String functionName, final Object... args) {
28785
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 28577
diff changeset
   190
        Objects.requireNonNull(functionName);
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   191
        final Global oldGlobal = Context.getGlobal();
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   192
        final boolean globalChanged = (oldGlobal != global);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   193
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   194
        try {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   195
            if (globalChanged) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   196
                Context.setGlobal(global);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   197
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   198
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   199
            final Object val = sobj.get(functionName);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   200
            if (val instanceof ScriptFunction) {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   201
                final Object[] modArgs = globalChanged? wrapArrayLikeMe(args, oldGlobal) : args;
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   202
                return wrapLikeMe(ScriptRuntime.apply((ScriptFunction)val, sobj, unwrapArray(modArgs, global)));
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   203
            } else if (val instanceof JSObject && ((JSObject)val).isFunction()) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   204
                return ((JSObject)val).call(sobj, args);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   205
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   206
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   207
            throw new NoSuchMethodException("No such function " + functionName);
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   208
        } catch (final NashornException ne) {
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   209
            throw ne.initEcmaError(global);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   210
        } catch (final RuntimeException | Error e) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   211
            throw e;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   212
        } catch (final Throwable t) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   213
            throw new RuntimeException(t);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   214
        } finally {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   215
            if (globalChanged) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   216
                Context.setGlobal(oldGlobal);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   217
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   218
        }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   219
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   220
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   221
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
    public Object getMember(final String name) {
28785
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 28577
diff changeset
   223
        Objects.requireNonNull(name);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   224
        return inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   225
            @Override public Object call() {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   226
                return wrapLikeMe(sobj.get(name));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   227
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   228
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
    public Object getSlot(final int index) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   233
        return inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   234
            @Override public Object call() {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   235
                return wrapLikeMe(sobj.get(index));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   236
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   237
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
    @Override
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   241
    public boolean hasMember(final String name) {
28785
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 28577
diff changeset
   242
        Objects.requireNonNull(name);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   243
        return inGlobal(new Callable<Boolean>() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   244
            @Override public Boolean call() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   245
                return sobj.has(name);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   246
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   247
        });
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   248
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   249
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   250
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   251
    public boolean hasSlot(final int slot) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   252
        return inGlobal(new Callable<Boolean>() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   253
            @Override public Boolean call() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   254
                return sobj.has(slot);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   255
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   256
        });
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   257
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   258
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   259
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
    public void removeMember(final String name) {
31094
7a9fa21da791 8085810: Return value of Objects.requireNonNull call can be used
sundar
parents: 30977
diff changeset
   261
        remove(Objects.requireNonNull(name));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
    public void setMember(final String name, final Object value) {
31094
7a9fa21da791 8085810: Return value of Objects.requireNonNull call can be used
sundar
parents: 30977
diff changeset
   266
        put(Objects.requireNonNull(name), value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
    public void setSlot(final int index, final Object value) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   271
        inGlobal(new Callable<Void>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   272
            @Override public Void call() {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26235
diff changeset
   273
                sobj.set(index, unwrap(value, global), getCallSiteFlags());
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   274
                return null;
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   275
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   276
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
23078
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   279
    /**
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   280
     * Nashorn extension: setIndexedPropertiesToExternalArrayData.
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   281
     * set indexed properties be exposed from a given nio ByteBuffer.
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   282
     *
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   283
     * @param buf external buffer - should be a nio ByteBuffer
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   284
     */
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   285
    public void setIndexedPropertiesToExternalArrayData(final ByteBuffer buf) {
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   286
        inGlobal(new Callable<Void>() {
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   287
            @Override public Void call() {
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   288
                sobj.setArray(ArrayData.allocate(buf));
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   289
                return null;
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   290
            }
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   291
        });
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   292
    }
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   293
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   294
    @Override
36141
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   295
    public boolean isInstance(final Object instance) {
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   296
        if (! (instance instanceof ScriptObjectMirror)) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   297
            return false;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   298
        }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   299
36141
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   300
        final ScriptObjectMirror mirror = (ScriptObjectMirror)instance;
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   301
        // if not belongs to my global scope, return false
36141
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   302
        if (global != mirror.global) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   303
            return false;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   304
        }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   305
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   306
        return inGlobal(new Callable<Boolean>() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   307
            @Override public Boolean call() {
36141
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   308
                return sobj.isInstance(mirror.sobj);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   309
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   310
        });
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   311
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   312
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   313
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   314
    public String getClassName() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   315
        return sobj.getClassName();
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   316
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   317
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   318
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   319
    public boolean isFunction() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   320
        return sobj instanceof ScriptFunction;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   321
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   322
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   323
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   324
    public boolean isStrictFunction() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   325
        return isFunction() && ((ScriptFunction)sobj).isStrict();
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   326
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   327
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   328
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   329
    public boolean isArray() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   330
        return sobj.isArray();
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   331
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   332
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   333
    // javax.script.Bindings methods
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   334
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   335
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   336
    public void clear() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   337
        inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
            @Override public Object call() {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
   339
                sobj.clear(strict);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   340
                return null;
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   341
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   342
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   343
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   344
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   345
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
    public boolean containsKey(final Object key) {
28577
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   347
        checkKey(key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   348
        return inGlobal(new Callable<Boolean>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   349
            @Override public Boolean call() {
28577
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   350
                return sobj.containsKey(key);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   351
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   352
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   353
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   354
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   356
    public boolean containsValue(final Object value) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   357
        return inGlobal(new Callable<Boolean>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
            @Override public Boolean call() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
                return sobj.containsValue(unwrap(value, global));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   360
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   361
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   362
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   365
    public Set<Map.Entry<String, Object>> entrySet() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   366
        return inGlobal(new Callable<Set<Map.Entry<String, Object>>>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   367
            @Override public Set<Map.Entry<String, Object>> call() {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
                final Iterator<String>               iter    = sobj.propertyIterator();
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   369
                final Set<Map.Entry<String, Object>> entries = new LinkedHashSet<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   370
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
                while (iter.hasNext()) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   372
                    final String key   = iter.next();
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   373
                    final Object value = translateUndefined(wrapLikeMe(sobj.get(key)));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
                    entries.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
                return Collections.unmodifiableSet(entries);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   379
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   381
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   382
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
    public Object get(final Object key) {
28577
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   384
        checkKey(key);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   385
        return inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   386
            @Override public Object call() {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   387
                return translateUndefined(wrapLikeMe(sobj.get(key)));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   388
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   389
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   393
    public boolean isEmpty() {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   394
        return inGlobal(new Callable<Boolean>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   395
            @Override public Boolean call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   396
                return sobj.isEmpty();
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   397
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   398
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   402
    public Set<String> keySet() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   403
        return inGlobal(new Callable<Set<String>>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   404
            @Override public Set<String> call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   405
                final Iterator<String> iter   = sobj.propertyIterator();
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   406
                final Set<String>      keySet = new LinkedHashSet<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   408
                while (iter.hasNext()) {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   409
                    keySet.add(iter.next());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   410
                }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   411
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   412
                return Collections.unmodifiableSet(keySet);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
            }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   414
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   415
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   416
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   417
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   418
    public Object put(final String key, final Object value) {
28577
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   419
        checkKey(key);
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   420
        final ScriptObject oldGlobal = Context.getGlobal();
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   421
        final boolean globalChanged = (oldGlobal != global);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   422
        return inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   423
            @Override public Object call() {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   424
                final Object modValue = globalChanged? wrapLikeMe(value, oldGlobal) : value;
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   425
                return translateUndefined(wrapLikeMe(sobj.put(key, unwrap(modValue, global), strict)));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   426
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   427
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   429
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   430
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   431
    public void putAll(final Map<? extends String, ? extends Object> map) {
31094
7a9fa21da791 8085810: Return value of Objects.requireNonNull call can be used
sundar
parents: 30977
diff changeset
   432
        Objects.requireNonNull(map);
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   433
        final ScriptObject oldGlobal = Context.getGlobal();
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   434
        final boolean globalChanged = (oldGlobal != global);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   435
        inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   436
            @Override public Object call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   437
                for (final Map.Entry<? extends String, ? extends Object> entry : map.entrySet()) {
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   438
                    final Object value = entry.getValue();
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   439
                    final Object modValue = globalChanged? wrapLikeMe(value, oldGlobal) : value;
28577
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   440
                    final String key = entry.getKey();
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   441
                    checkKey(key);
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   442
                    sobj.set(key, unwrap(modValue, global), getCallSiteFlags());
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   443
                }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   444
                return null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
            }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   446
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   450
    public Object remove(final Object key) {
28577
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   451
        checkKey(key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
        return inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
            @Override public Object call() {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   454
                return translateUndefined(wrapLikeMe(sobj.remove(key, strict)));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   459
    /**
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   460
     * Delete a property from this object.
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   461
     *
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   462
     * @param key the property to be deleted
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   463
     *
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   464
     * @return if the delete was successful or not
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   465
     */
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   466
    public boolean delete(final Object key) {
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   467
        return inGlobal(new Callable<Boolean>() {
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   468
            @Override public Boolean call() {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
   469
                return sobj.delete(unwrap(key, global), strict);
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   470
            }
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   471
        });
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   472
    }
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   473
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   474
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   475
    public int size() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
        return inGlobal(new Callable<Integer>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
            @Override public Integer call() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
                return sobj.size();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   480
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   481
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   482
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
    public Collection<Object> values() {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   485
        return inGlobal(new Callable<Collection<Object>>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   486
            @Override public Collection<Object> call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   487
                final List<Object>     values = new ArrayList<>(size());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   488
                final Iterator<Object> iter   = sobj.valueIterator();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   489
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   490
                while (iter.hasNext()) {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   491
                    values.add(translateUndefined(wrapLikeMe(iter.next())));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   492
                }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   493
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   494
                return Collections.unmodifiableList(values);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   495
            }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   496
        });
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   497
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   498
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   499
    // Support for ECMAScript Object API on mirrors
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   500
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   501
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   502
     * Return the __proto__ of this object.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   503
     * @return __proto__ object.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   504
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   505
    public Object getProto() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   506
        return inGlobal(new Callable<Object>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   507
            @Override public Object call() {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   508
                return wrapLikeMe(sobj.getProto());
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   509
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   510
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   511
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   512
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   513
    /**
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   514
     * Set the __proto__ of this object.
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   515
     * @param proto new proto for this object
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   516
     */
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   517
    public void setProto(final Object proto) {
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   518
        inGlobal(new Callable<Void>() {
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   519
            @Override public Void call() {
24772
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24770
diff changeset
   520
                sobj.setPrototypeOf(unwrap(proto, global));
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   521
                return null;
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   522
            }
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   523
        });
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   524
    }
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   525
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   526
    /**
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   527
     * ECMA 8.12.1 [[GetOwnProperty]] (P)
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   528
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   529
     * @param key property key
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   530
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   531
     * @return Returns the Property Descriptor of the named own property of this
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   532
     * object, or undefined if absent.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   533
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   534
    public Object getOwnPropertyDescriptor(final String key) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   535
        return inGlobal(new Callable<Object>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   536
            @Override public Object call() {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   537
                return wrapLikeMe(sobj.getOwnPropertyDescriptor(key));
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   538
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   539
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   540
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   541
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   542
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   543
     * return an array of own property keys associated with the object.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   544
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   545
     * @param all True if to include non-enumerable keys.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   546
     * @return Array of keys.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   547
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   548
    public String[] getOwnKeys(final boolean all) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   549
        return inGlobal(new Callable<String[]>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   550
            @Override public String[] call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   551
                return sobj.getOwnKeys(all);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   552
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   553
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   554
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   555
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   556
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   557
     * Flag this script object as non extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   558
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   559
     * @return the object after being made non extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   560
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   561
    public ScriptObjectMirror preventExtensions() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   562
        return inGlobal(new Callable<ScriptObjectMirror>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   563
            @Override public ScriptObjectMirror call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   564
                sobj.preventExtensions();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   565
                return ScriptObjectMirror.this;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   566
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   567
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   568
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   569
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   570
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   571
     * Check if this script object is extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   572
     * @return true if extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   573
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   574
    public boolean isExtensible() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   575
        return inGlobal(new Callable<Boolean>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   576
            @Override public Boolean call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   577
                return sobj.isExtensible();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   578
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   579
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   580
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   581
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   582
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   583
     * ECMAScript 15.2.3.8 - seal implementation
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   584
     * @return the sealed script object
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   585
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   586
    public ScriptObjectMirror seal() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   587
        return inGlobal(new Callable<ScriptObjectMirror>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   588
            @Override public ScriptObjectMirror call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   589
                sobj.seal();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   590
                return ScriptObjectMirror.this;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   591
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   592
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   593
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   594
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   595
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   596
     * Check whether this script object is sealed
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   597
     * @return true if sealed
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   598
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   599
    public boolean isSealed() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   600
        return inGlobal(new Callable<Boolean>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   601
            @Override public Boolean call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   602
                return sobj.isSealed();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   603
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   604
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   605
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   606
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   607
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   608
     * ECMA 15.2.39 - freeze implementation. Freeze this script object
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   609
     * @return the frozen script object
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   610
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   611
    public ScriptObjectMirror freeze() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   612
        return inGlobal(new Callable<ScriptObjectMirror>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   613
            @Override public ScriptObjectMirror call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   614
                sobj.freeze();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   615
                return ScriptObjectMirror.this;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   616
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   617
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   618
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   619
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   620
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   621
     * Check whether this script object is frozen
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   622
     * @return true if frozen
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   623
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   624
    public boolean isFrozen() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   625
        return inGlobal(new Callable<Boolean>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   626
            @Override public Boolean call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   627
                return sobj.isFrozen();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   628
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   629
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   630
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   631
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   632
    /**
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   633
     * Utility to check if given object is ECMAScript undefined value
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   634
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   635
     * @param obj object to check
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   636
     * @return true if 'obj' is ECMAScript undefined value
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   637
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   638
    public static boolean isUndefined(final Object obj) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   639
        return obj == ScriptRuntime.UNDEFINED;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   640
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   641
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   642
    /**
28577
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   643
     * Utility to convert this script object to the given type.
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   644
     *
24639
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents: 23375
diff changeset
   645
     * @param <T> destination type to convert to
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   646
     * @param type destination type to convert to
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   647
     * @return converted object
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   648
     */
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   649
    public <T> T to(final Class<T> type) {
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   650
        return inGlobal(new Callable<T>() {
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   651
            @Override
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   652
            public T call() {
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   653
                return type.cast(ScriptUtils.convert(sobj, type));
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   654
            }
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   655
        });
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   656
    }
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   657
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   658
    /**
36141
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   659
     * Make a script object mirror on given object if needed.
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   660
     *
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   661
     * @param obj object to be wrapped/converted
36141
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   662
     * @param homeGlobal global to which this object belongs.
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   663
     * @return wrapped/converted object
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   664
     */
21688
90bb8dc029c7 8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents: 21686
diff changeset
   665
    public static Object wrap(final Object obj, final Object homeGlobal) {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   666
        return wrap(obj, homeGlobal, false);
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   667
    }
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   668
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   669
    /**
36141
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   670
     * Make a script object mirror on given object if needed. The created wrapper will implement
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   671
     * the Java {@code List} interface if {@code obj} is a JavaScript {@code Array} object;
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   672
     * this is compatible with Java JSON libraries expectations. Arrays retrieved through its
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   673
     * properties (transitively) will also implement the list interface.
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   674
     *
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   675
     * @param obj object to be wrapped/converted
36141
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   676
     * @param homeGlobal global to which this object belongs.
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   677
     * @return wrapped/converted object
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   678
     */
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   679
    public static Object wrapAsJSONCompatible(final Object obj, final Object homeGlobal) {
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   680
        return wrap(obj, homeGlobal, true);
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   681
    }
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   682
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   683
    /**
36141
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   684
     * Make a script object mirror on given object if needed.
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   685
     *
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   686
     * @param obj object to be wrapped/converted
36141
696d662bcdb7 8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents: 34903
diff changeset
   687
     * @param homeGlobal global to which this object belongs.
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   688
     * @param jsonCompatible if true, the created wrapper will implement the Java {@code List} interface if
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   689
     * {@code obj} is a JavaScript {@code Array} object. Arrays retrieved through its properties (transitively)
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   690
     * will also implement the list interface.
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   691
     * @return wrapped/converted object
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   692
     */
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   693
    private static Object wrap(final Object obj, final Object homeGlobal, final boolean jsonCompatible) {
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   694
        if(obj instanceof ScriptObject) {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   695
            if (!(homeGlobal instanceof Global)) {
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   696
                return obj;
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   697
            }
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   698
            final ScriptObject sobj = (ScriptObject)obj;
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   699
            final Global global = (Global)homeGlobal;
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   700
            final ScriptObjectMirror mirror = new ScriptObjectMirror(sobj, global, jsonCompatible);
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   701
            if (jsonCompatible && sobj.isArray()) {
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   702
                return new JSONListAdapter(mirror, global);
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   703
            }
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   704
            return mirror;
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   705
        } else if(obj instanceof ConsString) {
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   706
            return obj.toString();
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   707
        } else if (jsonCompatible && obj instanceof ScriptObjectMirror) {
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   708
            // Since choosing JSON compatible representation is an explicit decision on user's part, if we're asked to
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   709
            // wrap a mirror that was not JSON compatible, explicitly create its compatible counterpart following the
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   710
            // principle of least surprise.
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   711
            return ((ScriptObjectMirror)obj).asJSONCompatible();
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   712
        }
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   713
        return obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   714
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   715
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   716
    /**
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   717
     * Wraps the passed object with the same jsonCompatible flag as this mirror.
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   718
     * @param obj the object
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   719
     * @param homeGlobal the object's home global.
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   720
     * @return a wrapper for the object.
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   721
     */
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   722
    private Object wrapLikeMe(final Object obj, final Object homeGlobal) {
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   723
        return wrap(obj, homeGlobal, jsonCompatible);
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   724
    }
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   725
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   726
    /**
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   727
     * Wraps the passed object with the same home global and jsonCompatible flag as this mirror.
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   728
     * @param obj the object
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   729
     * @return a wrapper for the object.
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   730
     */
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   731
    private Object wrapLikeMe(final Object obj) {
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   732
        return wrapLikeMe(obj, global);
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   733
    }
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   734
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   735
    /**
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   736
     * Unwrap a script object mirror if needed.
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   737
     *
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   738
     * @param obj object to be unwrapped
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   739
     * @param homeGlobal global to which this object belongs
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   740
     * @return unwrapped object
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   741
     */
21688
90bb8dc029c7 8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents: 21686
diff changeset
   742
    public static Object unwrap(final Object obj, final Object homeGlobal) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   743
        if (obj instanceof ScriptObjectMirror) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   744
            final ScriptObjectMirror mirror = (ScriptObjectMirror)obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   745
            return (mirror.global == homeGlobal)? mirror.sobj : obj;
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   746
        } else if (obj instanceof JSONListAdapter) {
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   747
            return ((JSONListAdapter)obj).unwrap(homeGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   748
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   749
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   750
        return obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   751
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   752
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   753
    /**
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   754
     * Wrap an array of object to script object mirrors if needed.
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   755
     *
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   756
     * @param args array to be unwrapped
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   757
     * @param homeGlobal global to which this object belongs
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   758
     * @return wrapped array
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   759
     */
21688
90bb8dc029c7 8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents: 21686
diff changeset
   760
    public static Object[] wrapArray(final Object[] args, final Object homeGlobal) {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   761
        return wrapArray(args, homeGlobal, false);
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   762
    }
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   763
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   764
    private static Object[] wrapArray(final Object[] args, final Object homeGlobal, final boolean jsonCompatible) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   765
        if (args == null || args.length == 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   766
            return args;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   767
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   768
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   769
        final Object[] newArgs = new Object[args.length];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   770
        int index = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   771
        for (final Object obj : args) {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   772
            newArgs[index] = wrap(obj, homeGlobal, jsonCompatible);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   773
            index++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   774
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   775
        return newArgs;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   776
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   777
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   778
    private Object[] wrapArrayLikeMe(final Object[] args, final Object homeGlobal) {
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   779
        return wrapArray(args, homeGlobal, jsonCompatible);
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   780
    }
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   781
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   782
    /**
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   783
     * Unwrap an array of script object mirrors if needed.
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   784
     *
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   785
     * @param args array to be unwrapped
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   786
     * @param homeGlobal global to which this object belongs
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   787
     * @return unwrapped array
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   788
     */
21688
90bb8dc029c7 8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents: 21686
diff changeset
   789
    public static Object[] unwrapArray(final Object[] args, final Object homeGlobal) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   790
        if (args == null || args.length == 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   791
            return args;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   792
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   793
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   794
        final Object[] newArgs = new Object[args.length];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   795
        int index = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   796
        for (final Object obj : args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   797
            newArgs[index] = unwrap(obj, homeGlobal);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   798
            index++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   799
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   800
        return newArgs;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   801
    }
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   802
26052
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   803
    /**
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   804
     * Are the given objects mirrors to same underlying object?
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   805
     *
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   806
     * @param obj1 first object
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   807
     * @param obj2 second object
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   808
     * @return true if obj1 and obj2 are identical script objects or mirrors of it.
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   809
     */
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   810
    public static boolean identical(final Object obj1, final Object obj2) {
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   811
        final Object o1 = (obj1 instanceof ScriptObjectMirror)?
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   812
            ((ScriptObjectMirror)obj1).sobj : obj1;
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   813
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   814
        final Object o2 = (obj2 instanceof ScriptObjectMirror)?
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   815
            ((ScriptObjectMirror)obj2).sobj : obj2;
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   816
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   817
        return o1 == o2;
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   818
    }
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 24778
diff changeset
   819
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   820
    // package-privates below this.
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   821
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   822
    ScriptObjectMirror(final ScriptObject sobj, final Global global) {
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   823
        this(sobj, global, false);
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   824
    }
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   825
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   826
    private ScriptObjectMirror(final ScriptObject sobj, final Global global, final boolean jsonCompatible) {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   827
        assert sobj != null : "ScriptObjectMirror on null!";
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   828
        assert global != null : "home Global is null";
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   829
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   830
        this.sobj = sobj;
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   831
        this.global = global;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   832
        this.strict = global.isStrictContext();
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   833
        this.jsonCompatible = jsonCompatible;
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   834
    }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   835
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   836
    // accessors for script engine
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   837
    ScriptObject getScriptObject() {
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   838
        return sobj;
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   839
    }
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   840
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   841
    Global getHomeGlobal() {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   842
        return global;
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   843
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   844
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24772
diff changeset
   845
    static Object translateUndefined(final Object obj) {
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   846
        return (obj == ScriptRuntime.UNDEFINED)? null : obj;
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   847
    }
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   848
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26235
diff changeset
   849
    private int getCallSiteFlags() {
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26235
diff changeset
   850
        return strict ? NashornCallSiteDescriptor.CALLSITE_STRICT : 0;
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26235
diff changeset
   851
    }
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26235
diff changeset
   852
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   853
    // internals only below this.
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   854
    private <V> V inGlobal(final Callable<V> callable) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   855
        final Global oldGlobal = Context.getGlobal();
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   856
        final boolean globalChanged = (oldGlobal != global);
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   857
        if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   858
            Context.setGlobal(global);
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   859
        }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   860
        try {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   861
            return callable.call();
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   862
        } catch (final NashornException ne) {
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   863
            throw ne.initEcmaError(global);
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   864
        } catch (final RuntimeException e) {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   865
            throw e;
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   866
        } catch (final Exception e) {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   867
            throw new AssertionError("Cannot happen", e);
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   868
        } finally {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   869
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   870
                Context.setGlobal(oldGlobal);
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   871
            }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   872
        }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   873
    }
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   874
28577
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   875
    /**
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   876
     * Ensures the key is not null, empty string, or a non-String object. The contract of the {@link Bindings}
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   877
     * interface requires that these are not accepted as keys.
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   878
     * @param key the key to check
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   879
     * @throws NullPointerException if key is null
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   880
     * @throws ClassCastException if key is not a String
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   881
     * @throws IllegalArgumentException if key is empty string
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   882
     */
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   883
    private static void checkKey(final Object key) {
28785
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 28577
diff changeset
   884
        Objects.requireNonNull(key, "key can not be null");
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 28577
diff changeset
   885
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 28577
diff changeset
   886
        if (!(key instanceof String)) {
28577
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   887
            throw new ClassCastException("key should be a String. It is " + key.getClass().getName() + " instead.");
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   888
        } else if (((String)key).length() == 0) {
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   889
            throw new IllegalArgumentException("key can not be empty");
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   890
        }
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   891
    }
d294bba4f2a4 8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents: 28317
diff changeset
   892
29413
153d198cfdc3 8074661: Forward port AbstractJSObject.getDefaultValue(JSObject, Class)
attila
parents: 29133
diff changeset
   893
    @Override @Deprecated
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   894
    public double toNumber() {
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   895
        return inGlobal(new Callable<Double>() {
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   896
            @Override public Double call() {
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   897
                return JSType.toNumber(sobj);
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   898
            }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   899
        });
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   900
    }
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   901
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   902
    @Override
29413
153d198cfdc3 8074661: Forward port AbstractJSObject.getDefaultValue(JSObject, Class)
attila
parents: 29133
diff changeset
   903
    public Object getDefaultValue(final Class<?> hint) {
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   904
        return inGlobal(new Callable<Object>() {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   905
            @Override public Object call() {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   906
                try {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   907
                    return sobj.getDefaultValue(hint);
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   908
                } catch (final ECMAException e) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   909
                    // We're catching ECMAException (likely TypeError), and translating it to
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   910
                    // UnsupportedOperationException. This in turn will be translated into TypeError of the
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   911
                    // caller's Global by JSType#toPrimitive(JSObject,Class) therefore ensuring that it's
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   912
                    // recognized as "instanceof TypeError" in the caller.
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   913
                    throw new UnsupportedOperationException(e.getMessage(), e);
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   914
                }
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   915
            }
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   916
        });
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28878
diff changeset
   917
    }
30977
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   918
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   919
    private ScriptObjectMirror asJSONCompatible() {
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   920
        if (this.jsonCompatible) {
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   921
            return this;
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   922
        }
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   923
        return new ScriptObjectMirror(sobj, global, true);
4a7a2f339176 8066773: JSON-friendly wrapper for objects
attila
parents: 29413
diff changeset
   924
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   925
}