nashorn/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
author sundar
Wed, 21 Aug 2013 17:28:53 +0530
changeset 19619 4085b74056ee
parent 19459 79e75274df99
child 19620 3f0c79b63846
permissions -rw-r--r--
8023368: Instance __proto__ property should exist and be writable. Reviewed-by: attila, 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
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    28
import java.security.AccessControlContext;
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
    29
import java.security.AccessController;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    30
import java.security.Permissions;
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
    31
import java.security.PrivilegedAction;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    32
import java.security.ProtectionDomain;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import java.util.AbstractMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.util.ArrayList;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import java.util.Collection;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import java.util.Collections;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    37
import java.util.Iterator;
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
    38
import java.util.LinkedHashSet;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
import java.util.List;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
import java.util.Map;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
import java.util.Set;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
import java.util.concurrent.Callable;
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
    43
import javax.script.Bindings;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
import jdk.nashorn.internal.runtime.Context;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
import jdk.nashorn.internal.runtime.ScriptFunction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
import jdk.nashorn.internal.runtime.ScriptObject;
16177
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    47
import jdk.nashorn.internal.runtime.ScriptRuntime;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
 * Mirror object that wraps a given ScriptObject instance. User can
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
    51
 * access ScriptObject via the javax.script.Bindings interface or
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
    52
 * netscape.javascript.JSObject interface.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
 */
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
    54
public final class ScriptObjectMirror extends JSObject implements Bindings {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    55
    private static AccessControlContext getContextAccCtxt() {
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    56
        final Permissions perms = new Permissions();
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    57
        perms.add(new RuntimePermission(Context.NASHORN_GET_CONTEXT));
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    58
        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    59
    }
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    60
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    61
    private static final AccessControlContext GET_CONTEXT_ACC_CTXT = getContextAccCtxt();
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    62
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
    private final ScriptObject sobj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
    private final ScriptObject global;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
    public boolean equals(final Object other) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
        if (other instanceof ScriptObjectMirror) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
            return sobj.equals(((ScriptObjectMirror)other).sobj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
    public int hashCode() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
        return sobj.hashCode();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
16177
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    80
    @Override
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    81
    public String toString() {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    82
        return inGlobal(new Callable<String>() {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    83
            @Override
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    84
            public String call() {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    85
                return ScriptRuntime.safeToString(sobj);
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    86
            }
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    87
        });
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    88
    }
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    89
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
    // JSObject methods
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
    @Override
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
    92
    public Object call(final String functionName, final Object... args) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
    93
        final ScriptObject oldGlobal = Context.getGlobal();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
        final boolean globalChanged = (oldGlobal != global);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
    96
        try {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
    97
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
    98
                Context.setGlobal(global);
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
    99
            }
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   100
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   101
            final Object val = functionName == null? sobj : sobj.get(functionName);
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   102
            if (! (val instanceof ScriptFunction)) {
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   103
                throw new NoSuchMethodException("No such function " + ((functionName != null)? functionName : ""));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   106
            final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args;
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   107
            return wrap(ScriptRuntime.checkAndApply((ScriptFunction)val, sobj, unwrapArray(modArgs, global)), global);
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   108
        } catch (final RuntimeException | Error e) {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   109
            throw e;
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   110
        } catch (final Throwable t) {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   111
            throw new RuntimeException(t);
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   112
        } finally {
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(oldGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
            }
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   116
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
    @Override
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   120
    public Object newObject(final String functionName, final Object... args) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   121
        final ScriptObject oldGlobal = Context.getGlobal();
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   122
        final boolean globalChanged = (oldGlobal != global);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   123
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   124
        try {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   125
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   126
                Context.setGlobal(global);
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   127
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   128
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   129
            final Object val = functionName == null? sobj : sobj.get(functionName);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   130
            if (! (val instanceof ScriptFunction)) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   131
                throw new RuntimeException("not a constructor " + ((functionName != null)? functionName : ""));
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   132
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   133
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   134
            final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args;
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   135
            return wrap(ScriptRuntime.checkAndConstruct((ScriptFunction)val, unwrapArray(modArgs, global)), global);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   136
        } catch (final RuntimeException | Error e) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   137
            throw e;
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   138
        } catch (final Throwable t) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   139
            throw new RuntimeException(t);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   140
        } finally {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   141
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   142
                Context.setGlobal(oldGlobal);
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   143
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   144
        }
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
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   147
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
    public Object eval(final String s) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
        return inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
            public Object call() {
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   152
                final Context context = AccessController.doPrivileged(
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   153
                        new PrivilegedAction<Context>() {
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   154
                            @Override
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   155
                            public Context run() {
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   156
                                return Context.getContext();
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   157
                            }
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
   158
                        }, GET_CONTEXT_ACC_CTXT);
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   159
                return wrap(context.eval(global, s, null, null, false), global);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
    public Object getMember(final String name) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   166
        return inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   167
            @Override public Object call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   168
                return wrap(sobj.get(name), global);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   169
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   170
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
    public Object getSlot(final int index) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   175
        return inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   176
            @Override public Object call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   177
                return wrap(sobj.get(index), global);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   178
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   179
        });
16147
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
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
    public void removeMember(final String name) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
        remove(name);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
    public void setMember(final String name, final Object value) {
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   189
        put(name, value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
    public void setSlot(final int index, final Object value) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   194
        inGlobal(new Callable<Void>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   195
            @Override public Void call() {
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   196
                sobj.set(index, unwrap(value, global), global.isStrictContext());
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   197
                return null;
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   198
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   199
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   202
    // javax.script.Bindings methods
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   203
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
    public void clear() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
        inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
            @Override public Object call() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
                sobj.clear();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
                return null;
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   210
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   211
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
    public boolean containsKey(final Object key) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
        return inGlobal(new Callable<Boolean>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
            @Override public Boolean call() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
                return sobj.containsKey(unwrap(key, global));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   219
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   220
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
    public boolean containsValue(final Object value) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
        return inGlobal(new Callable<Boolean>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
            @Override public Boolean call() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
                return sobj.containsValue(unwrap(value, global));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   228
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   229
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   233
    public Set<Map.Entry<String, Object>> entrySet() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   234
        return inGlobal(new Callable<Set<Map.Entry<String, Object>>>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   235
            @Override public Set<Map.Entry<String, Object>> call() {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
                final Iterator<String>               iter    = sobj.propertyIterator();
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   237
                final Set<Map.Entry<String, Object>> entries = new LinkedHashSet<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
                while (iter.hasNext()) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   240
                    final String key   = iter.next();
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   241
                    final Object value = translateUndefined(wrap(sobj.get(key), global));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
                    entries.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
                return Collections.unmodifiableSet(entries);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
    public Object get(final Object key) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   252
        return inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   253
            @Override public Object call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   254
                return translateUndefined(wrap(sobj.get(key), global));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   255
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   256
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
    public boolean isEmpty() {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   261
        return inGlobal(new Callable<Boolean>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   262
            @Override public Boolean call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   263
                return sobj.isEmpty();
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   264
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   265
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   266
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   269
    public Set<String> keySet() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   270
        return inGlobal(new Callable<Set<String>>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   271
            @Override public Set<String> call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   272
                final Iterator<String> iter   = sobj.propertyIterator();
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   273
                final Set<String>      keySet = new LinkedHashSet<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   275
                while (iter.hasNext()) {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   276
                    keySet.add(iter.next());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   277
                }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   278
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   279
                return Collections.unmodifiableSet(keySet);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
            }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   281
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   285
    public Object put(final String key, final Object value) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   286
        final ScriptObject oldGlobal = Context.getGlobal();
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   287
        final boolean globalChanged = (oldGlobal != global);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
        return inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
            @Override public Object call() {
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   290
                final Object modValue = globalChanged? wrap(value, oldGlobal) : value;
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   291
                return translateUndefined(wrap(sobj.put(key, unwrap(modValue, global)), global));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   292
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   293
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   297
    public void putAll(final Map<? extends String, ? extends Object> map) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   298
        final ScriptObject oldGlobal = Context.getGlobal();
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   299
        final boolean globalChanged = (oldGlobal != global);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   300
        inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   301
            @Override public Object call() {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18614
diff changeset
   302
                final boolean strict = global.isStrictContext();
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   303
                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
   304
                    final Object value = entry.getValue();
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   305
                    final Object modValue = globalChanged? wrap(value, oldGlobal) : value;
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   306
                    sobj.set(entry.getKey(), unwrap(modValue, global), strict);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   307
                }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   308
                return null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
            }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   310
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   314
    public Object remove(final Object key) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   315
        return inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   316
            @Override public Object call() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
                return wrap(sobj.remove(unwrap(key, global)), global);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   320
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   322
    /**
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   323
     * Delete a property from this object.
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   324
     *
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   325
     * @param key the property to be deleted
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   326
     *
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   327
     * @return if the delete was successful or not
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   328
     */
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   329
    public boolean delete(final Object key) {
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   330
        return inGlobal(new Callable<Boolean>() {
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   331
            @Override public Boolean call() {
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   332
                return sobj.delete(unwrap(key, global));
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   333
            }
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   334
        });
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   335
    }
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   336
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   337
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
    public int size() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
        return inGlobal(new Callable<Integer>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   340
            @Override public Integer call() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   341
                return sobj.size();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   342
            }
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
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
    public Collection<Object> values() {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   348
        return inGlobal(new Callable<Collection<Object>>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   349
            @Override public Collection<Object> call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   350
                final List<Object>     values = new ArrayList<>(size());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   351
                final Iterator<Object> iter   = sobj.valueIterator();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   352
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   353
                while (iter.hasNext()) {
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   354
                    values.add(translateUndefined(wrap(iter.next(), global)));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   355
                }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   356
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   357
                return Collections.unmodifiableList(values);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
            }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   359
        });
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   360
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   361
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   362
    // Support for ECMAScript Object API on mirrors
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   363
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   364
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   365
     * Return the __proto__ of this object.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   366
     * @return __proto__ object.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   367
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   368
    public Object getProto() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   369
        return inGlobal(new Callable<Object>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   370
            @Override public Object call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   371
                return wrap(sobj.getProto(), global);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   372
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   373
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   374
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   375
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   376
    /**
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   377
     * Set the __proto__ of this object.
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   378
     * @param proto new proto for this object
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   379
     */
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   380
    public void setProto(final Object proto) {
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   381
        inGlobal(new Callable<Void>() {
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   382
            @Override public Void call() {
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   383
                sobj.setProtoCheck(unwrap(proto, global));
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   384
                return null;
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   385
            }
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   386
        });
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   387
    }
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   388
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   389
    /**
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   390
     * ECMA 8.12.1 [[GetOwnProperty]] (P)
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   391
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   392
     * @param key property key
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   393
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   394
     * @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
   395
     * object, or undefined if absent.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   396
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   397
    public Object getOwnPropertyDescriptor(final String key) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   398
        return inGlobal(new Callable<Object>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   399
            @Override public Object call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   400
                return wrap(sobj.getOwnPropertyDescriptor(key), global);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   401
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   402
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   403
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   404
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   405
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   406
     * 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
   407
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   408
     * @param all True if to include non-enumerable keys.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   409
     * @return Array of keys.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   410
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   411
    public String[] getOwnKeys(final boolean all) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   412
        return inGlobal(new Callable<String[]>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   413
            @Override public String[] call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   414
                return sobj.getOwnKeys(all);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   415
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   416
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   417
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   418
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   419
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   420
     * Flag this script object as non extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   421
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   422
     * @return the object after being made non extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   423
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   424
    public ScriptObjectMirror preventExtensions() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   425
        return inGlobal(new Callable<ScriptObjectMirror>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   426
            @Override public ScriptObjectMirror call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   427
                sobj.preventExtensions();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   428
                return ScriptObjectMirror.this;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   429
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   430
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   431
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   432
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   433
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   434
     * Check if this script object is extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   435
     * @return true if extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   436
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   437
    public boolean isExtensible() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   438
        return inGlobal(new Callable<Boolean>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   439
            @Override public Boolean call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   440
                return sobj.isExtensible();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   441
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   442
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   443
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   444
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   445
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   446
     * ECMAScript 15.2.3.8 - seal implementation
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   447
     * @return the sealed script object
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   448
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   449
    public ScriptObjectMirror seal() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   450
        return inGlobal(new Callable<ScriptObjectMirror>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   451
            @Override public ScriptObjectMirror call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   452
                sobj.seal();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   453
                return ScriptObjectMirror.this;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   454
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   455
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   456
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   457
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   458
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   459
     * Check whether this script object is sealed
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   460
     * @return true if sealed
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   461
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   462
    public boolean isSealed() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   463
        return inGlobal(new Callable<Boolean>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   464
            @Override public Boolean call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   465
                return sobj.isSealed();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   466
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   467
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   468
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   469
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   470
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   471
     * ECMA 15.2.39 - freeze implementation. Freeze this script object
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   472
     * @return the frozen script object
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   473
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   474
    public ScriptObjectMirror freeze() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   475
        return inGlobal(new Callable<ScriptObjectMirror>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   476
            @Override public ScriptObjectMirror call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   477
                sobj.freeze();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   478
                return ScriptObjectMirror.this;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   479
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   480
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   481
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   482
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   483
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   484
     * Check whether this script object is frozen
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   485
     * @return true if frozen
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   486
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   487
    public boolean isFrozen() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   488
        return inGlobal(new Callable<Boolean>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   489
            @Override public Boolean call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   490
                return sobj.isFrozen();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   491
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   492
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   493
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   494
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   495
    // ECMAScript instanceof check
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   496
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   497
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   498
     * Checking whether a script object is an instance of another by
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   499
     * walking the proto chain
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
     * @param instance instace to check
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   502
     * @return true if 'instance' is an instance of this object
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   503
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   504
    public boolean isInstance(final ScriptObjectMirror instance) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   505
        // if not belongs to my global scope, return false
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   506
        if (instance == null || global != instance.global) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   507
            return false;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   508
        }
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
        return inGlobal(new Callable<Boolean>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   511
            @Override public Boolean call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   512
                return sobj.isInstance(instance.sobj);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   513
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   514
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   515
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   516
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   517
    /**
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   518
     * is this a function object?
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   519
     *
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   520
     * @return if this mirror wraps a ECMAScript function instance
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   521
     */
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   522
    public boolean isFunction() {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   523
        return sobj instanceof ScriptFunction;
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   524
    }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   525
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   526
    /**
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   527
     * is this a 'use strict' function object?
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   528
     *
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   529
     * @return true if this mirror represents a ECMAScript 'use strict' function
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   530
     */
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   531
    public boolean isStrictFunction() {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   532
        return isFunction() && ((ScriptFunction)sobj).isStrict();
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   533
    }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   534
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   535
    /**
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   536
     * is this an array object?
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   537
     *
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   538
     * @return if this mirror wraps a ECMAScript array object
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   539
     */
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   540
    public boolean isArray() {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   541
        return sobj.isArray();
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   542
    }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   543
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   544
    /**
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   545
     * Utility to check if given object is ECMAScript undefined value
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   546
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   547
     * @param obj object to check
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   548
     * @return true if 'obj' is ECMAScript undefined value
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   549
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   550
    public static boolean isUndefined(final Object obj) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   551
        return obj == ScriptRuntime.UNDEFINED;
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
    /**
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   555
     * Make a script object mirror on given object if needed.
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   556
     *
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   557
     * @param obj object to be wrapped
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   558
     * @param homeGlobal global to which this object belongs
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   559
     * @return wrapped object
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   560
     */
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   561
    public static Object wrap(final Object obj, final ScriptObject homeGlobal) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   562
        return (obj instanceof ScriptObject && homeGlobal != null) ? new ScriptObjectMirror((ScriptObject)obj, homeGlobal) : obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   563
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   564
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   565
    /**
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   566
     * Unwrap a script object mirror if needed.
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   567
     *
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   568
     * @param obj object to be unwrapped
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   569
     * @param homeGlobal global to which this object belongs
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   570
     * @return unwrapped object
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   571
     */
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   572
    public static Object unwrap(final Object obj, final ScriptObject homeGlobal) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   573
        if (obj instanceof ScriptObjectMirror) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   574
            final ScriptObjectMirror mirror = (ScriptObjectMirror)obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   575
            return (mirror.global == homeGlobal)? mirror.sobj : obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   576
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   577
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   578
        return obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   579
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   580
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   581
    /**
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   582
     * 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
   583
     *
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   584
     * @param args array to be unwrapped
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   585
     * @param homeGlobal global to which this object belongs
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   586
     * @return wrapped array
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   587
     */
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   588
    public static Object[] wrapArray(final Object[] args, final ScriptObject homeGlobal) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   589
        if (args == null || args.length == 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   590
            return args;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   591
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   592
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   593
        final Object[] newArgs = new Object[args.length];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   594
        int index = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   595
        for (final Object obj : args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   596
            newArgs[index] = wrap(obj, homeGlobal);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   597
            index++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   598
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   599
        return newArgs;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   600
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   601
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   602
    /**
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   603
     * Unwrap an array of script object mirrors if needed.
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   604
     *
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   605
     * @param args array to be unwrapped
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   606
     * @param homeGlobal global to which this object belongs
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   607
     * @return unwrapped array
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   608
     */
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   609
    public static Object[] unwrapArray(final Object[] args, final ScriptObject homeGlobal) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   610
        if (args == null || args.length == 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   611
            return args;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   612
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   613
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   614
        final Object[] newArgs = new Object[args.length];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   615
        int index = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   616
        for (final Object obj : args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   617
            newArgs[index] = unwrap(obj, homeGlobal);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   618
            index++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   619
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   620
        return newArgs;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   621
    }
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   622
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   623
    // package-privates below this.
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   624
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   625
    ScriptObjectMirror(final ScriptObject sobj, final ScriptObject global) {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   626
        assert sobj != null : "ScriptObjectMirror on null!";
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   627
        assert global != null : "null global for ScriptObjectMirror!";
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   628
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   629
        this.sobj = sobj;
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   630
        this.global = global;
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   631
    }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   632
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   633
    // accessors for script engine
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   634
    ScriptObject getScriptObject() {
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   635
        return sobj;
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   636
    }
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   637
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   638
    ScriptObject getHomeGlobal() {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   639
        return global;
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   640
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   641
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   642
    static Object translateUndefined(Object obj) {
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   643
        return (obj == ScriptRuntime.UNDEFINED)? null : obj;
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   644
    }
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   645
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   646
    // internals only below this.
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   647
    private <V> V inGlobal(final Callable<V> callable) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   648
        final ScriptObject oldGlobal = Context.getGlobal();
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   649
        final boolean globalChanged = (oldGlobal != global);
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   650
        if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   651
            Context.setGlobal(global);
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   652
        }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   653
        try {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   654
            return callable.call();
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   655
        } catch (final RuntimeException e) {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   656
            throw e;
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   657
        } catch (final Exception e) {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   658
            throw new AssertionError("Cannot happen", e);
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   659
        } finally {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   660
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   661
                Context.setGlobal(oldGlobal);
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   662
            }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   663
        }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   664
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   665
}