nashorn/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
author sundar
Fri, 30 May 2014 17:22:38 +0530
changeset 24639 cde22f00c4e6
parent 23375 a1110f2cbe75
child 24770 17f8e3b82ad3
permissions -rw-r--r--
8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages Reviewed-by: jlaskey
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.api.scripting;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
23078
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
    28
import java.nio.ByteBuffer;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    29
import java.security.AccessControlContext;
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
    30
import java.security.AccessController;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    31
import java.security.Permissions;
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
    32
import java.security.PrivilegedAction;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    33
import java.security.ProtectionDomain;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.util.AbstractMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import java.util.ArrayList;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import java.util.Collection;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import java.util.Collections;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    38
import java.util.Iterator;
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
    39
import java.util.LinkedHashSet;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
import java.util.List;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
import java.util.Map;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
import java.util.Set;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
import java.util.concurrent.Callable;
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
    44
import javax.script.Bindings;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
    45
import jdk.nashorn.internal.objects.Global;
23078
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
    46
import jdk.nashorn.internal.runtime.arrays.ArrayData;
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
    47
import jdk.nashorn.internal.runtime.ConsString;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
import jdk.nashorn.internal.runtime.Context;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
    49
import jdk.nashorn.internal.runtime.JSType;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
import jdk.nashorn.internal.runtime.ScriptFunction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
import jdk.nashorn.internal.runtime.ScriptObject;
16177
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    52
import jdk.nashorn.internal.runtime.ScriptRuntime;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
/**
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
    55
 * Mirror object that wraps a given Nashorn Script object.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
 */
21453
b48953eede53 8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents: 21450
diff changeset
    57
public final class ScriptObjectMirror extends AbstractJSObject implements Bindings {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    58
    private static AccessControlContext getContextAccCtxt() {
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    59
        final Permissions perms = new Permissions();
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    60
        perms.add(new RuntimePermission(Context.NASHORN_GET_CONTEXT));
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    61
        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    62
    }
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    63
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    64
    private static final AccessControlContext GET_CONTEXT_ACC_CTXT = getContextAccCtxt();
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    65
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
    private final ScriptObject sobj;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
    67
    private final Global  global;
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
    68
    private final boolean strict;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
    public boolean equals(final Object other) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
        if (other instanceof ScriptObjectMirror) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
            return sobj.equals(((ScriptObjectMirror)other).sobj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
    public int hashCode() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
        return sobj.hashCode();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
16177
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    84
    @Override
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    85
    public String toString() {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    86
        return inGlobal(new Callable<String>() {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    87
            @Override
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    88
            public String call() {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    89
                return ScriptRuntime.safeToString(sobj);
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    90
            }
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    91
        });
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    92
    }
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
    93
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
    // JSObject methods
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
    95
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
    @Override
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
    97
    public Object call(final Object thiz, final Object... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
    98
        final Global oldGlobal = Context.getGlobal();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
        final boolean globalChanged = (oldGlobal != global);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   101
        try {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   102
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   103
                Context.setGlobal(global);
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   104
            }
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   105
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   106
            if (sobj instanceof ScriptFunction) {
19620
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19619
diff changeset
   107
                final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args;
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   108
                final Object self = globalChanged? wrap(thiz, oldGlobal) : thiz;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   109
                return wrap(ScriptRuntime.apply((ScriptFunction)sobj, unwrap(self, global), unwrapArray(modArgs, global)), global);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   112
            throw new RuntimeException("not a function: " + toString());
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   113
        } catch (final NashornException ne) {
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   114
            throw ne.initEcmaError(global);
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   115
        } catch (final RuntimeException | Error e) {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   116
            throw e;
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   117
        } catch (final Throwable t) {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   118
            throw new RuntimeException(t);
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   119
        } finally {
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   120
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   121
                Context.setGlobal(oldGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
            }
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   123
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
    @Override
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   127
    public Object newObject(final Object... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   128
        final Global oldGlobal = Context.getGlobal();
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   129
        final boolean globalChanged = (oldGlobal != global);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   130
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   131
        try {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   132
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   133
                Context.setGlobal(global);
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   134
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   135
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   136
            if (sobj instanceof ScriptFunction) {
19620
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19619
diff changeset
   137
                final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args;
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   138
                return wrap(ScriptRuntime.construct((ScriptFunction)sobj, unwrapArray(modArgs, global)), global);
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   139
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   140
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   141
            throw new RuntimeException("not a constructor: " + toString());
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   142
        } catch (final NashornException ne) {
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   143
            throw ne.initEcmaError(global);
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   144
        } catch (final RuntimeException | Error e) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   145
            throw e;
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   146
        } catch (final Throwable t) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   147
            throw new RuntimeException(t);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   148
        } finally {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   149
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   150
                Context.setGlobal(oldGlobal);
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   151
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   152
        }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   153
    }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   154
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   155
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
    public Object eval(final String s) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
        return inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
            public Object call() {
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   160
                final Context context = AccessController.doPrivileged(
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   161
                        new PrivilegedAction<Context>() {
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   162
                            @Override
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   163
                            public Context run() {
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   164
                                return Context.getContext();
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   165
                            }
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
   166
                        }, GET_CONTEXT_ACC_CTXT);
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   167
                return wrap(context.eval(global, s, null, null, false), global);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   172
    public Object callMember(final String functionName, final Object... args) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   173
        functionName.getClass(); // null check
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   174
        final Global oldGlobal = Context.getGlobal();
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   175
        final boolean globalChanged = (oldGlobal != global);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   176
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   177
        try {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   178
            if (globalChanged) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   179
                Context.setGlobal(global);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   180
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   181
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   182
            final Object val = sobj.get(functionName);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   183
            if (val instanceof ScriptFunction) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   184
                final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   185
                return wrap(ScriptRuntime.apply((ScriptFunction)val, sobj, unwrapArray(modArgs, global)), global);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   186
            } else if (val instanceof JSObject && ((JSObject)val).isFunction()) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   187
                return ((JSObject)val).call(sobj, args);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   188
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   189
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   190
            throw new NoSuchMethodException("No such function " + functionName);
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   191
        } catch (final NashornException ne) {
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   192
            throw ne.initEcmaError(global);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   193
        } catch (final RuntimeException | Error e) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   194
            throw e;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   195
        } catch (final Throwable t) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   196
            throw new RuntimeException(t);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   197
        } finally {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   198
            if (globalChanged) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   199
                Context.setGlobal(oldGlobal);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   200
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   201
        }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   202
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   203
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   204
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
    public Object getMember(final String name) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   206
        name.getClass();
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   207
        return inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   208
            @Override public Object call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   209
                return wrap(sobj.get(name), global);
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 Object getSlot(final int index) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   216
        return inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   217
            @Override public Object call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   218
                return wrap(sobj.get(index), global);
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
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   224
    public boolean hasMember(final String name) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   225
        name.getClass();
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   226
        return inGlobal(new Callable<Boolean>() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   227
            @Override public Boolean call() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   228
                return sobj.has(name);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   229
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   230
        });
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   231
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   232
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   233
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   234
    public boolean hasSlot(final int slot) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   235
        return inGlobal(new Callable<Boolean>() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   236
            @Override public Boolean call() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   237
                return sobj.has(slot);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   238
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   239
        });
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   240
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   241
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   242
    @Override
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
    public void removeMember(final String name) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   244
        name.getClass();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
        remove(name);
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
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
    public void setMember(final String name, final Object value) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   250
        name.getClass();
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   251
        put(name, value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
    public void setSlot(final int index, final Object value) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   256
        inGlobal(new Callable<Void>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   257
            @Override public Void call() {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
   258
                sobj.set(index, unwrap(value, global), strict);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   259
                return null;
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   260
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   261
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
23078
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   264
    /**
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   265
     * Nashorn extension: setIndexedPropertiesToExternalArrayData.
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   266
     * set indexed properties be exposed from a given nio ByteBuffer.
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   267
     *
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   268
     * @param buf external buffer - should be a nio ByteBuffer
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   269
     */
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   270
    public void setIndexedPropertiesToExternalArrayData(final ByteBuffer buf) {
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   271
        inGlobal(new Callable<Void>() {
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   272
            @Override public Void call() {
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   273
                sobj.setArray(ArrayData.allocate(buf));
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   274
                return null;
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   275
            }
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   276
        });
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   277
    }
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   278
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22370
diff changeset
   279
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   280
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   281
    public boolean isInstance(final Object obj) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   282
        if (! (obj instanceof ScriptObjectMirror)) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   283
            return false;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   284
        }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   285
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   286
        final ScriptObjectMirror instance = (ScriptObjectMirror)obj;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   287
        // if not belongs to my global scope, return false
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   288
        if (global != instance.global) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   289
            return false;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   290
        }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   291
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   292
        return inGlobal(new Callable<Boolean>() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   293
            @Override public Boolean call() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   294
                return sobj.isInstance(instance.sobj);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   295
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   296
        });
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   297
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   298
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   299
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   300
    public String getClassName() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   301
        return sobj.getClassName();
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   302
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   303
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   304
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   305
    public boolean isFunction() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   306
        return sobj instanceof ScriptFunction;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   307
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   308
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   309
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   310
    public boolean isStrictFunction() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   311
        return isFunction() && ((ScriptFunction)sobj).isStrict();
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   312
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   313
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   314
    @Override
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   315
    public boolean isArray() {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   316
        return sobj.isArray();
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   317
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   318
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   319
    // javax.script.Bindings methods
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   320
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   322
    public void clear() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   323
        inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
            @Override public Object call() {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
   325
                sobj.clear(strict);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   326
                return null;
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   327
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   328
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   329
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   330
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   331
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   332
    public boolean containsKey(final Object key) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   333
        return inGlobal(new Callable<Boolean>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
            @Override public Boolean call() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   335
                return sobj.containsKey(unwrap(key, global));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   336
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   337
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   340
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   341
    public boolean containsValue(final Object value) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   342
        return inGlobal(new Callable<Boolean>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   343
            @Override public Boolean call() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   344
                return sobj.containsValue(unwrap(value, global));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   345
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   346
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   348
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   349
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   350
    public Set<Map.Entry<String, Object>> entrySet() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   351
        return inGlobal(new Callable<Set<Map.Entry<String, Object>>>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   352
            @Override public Set<Map.Entry<String, Object>> call() {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   353
                final Iterator<String>               iter    = sobj.propertyIterator();
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   354
                final Set<Map.Entry<String, Object>> entries = new LinkedHashSet<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   356
                while (iter.hasNext()) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   357
                    final String key   = iter.next();
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   358
                    final Object value = translateUndefined(wrap(sobj.get(key), global));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
                    entries.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   361
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   362
                return Collections.unmodifiableSet(entries);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
    public Object get(final Object key) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   369
        return inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   370
            @Override public Object call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   371
                return translateUndefined(wrap(sobj.get(key), global));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   372
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   373
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
    public boolean isEmpty() {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   378
        return inGlobal(new Callable<Boolean>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   379
            @Override public Boolean call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   380
                return sobj.isEmpty();
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   381
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   382
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   386
    public Set<String> keySet() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   387
        return inGlobal(new Callable<Set<String>>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   388
            @Override public Set<String> call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   389
                final Iterator<String> iter   = sobj.propertyIterator();
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   390
                final Set<String>      keySet = new LinkedHashSet<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   392
                while (iter.hasNext()) {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   393
                    keySet.add(iter.next());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   394
                }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   395
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   396
                return Collections.unmodifiableSet(keySet);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   397
            }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   398
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   402
    public Object put(final String key, final Object value) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   403
        final ScriptObject oldGlobal = Context.getGlobal();
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   404
        final boolean globalChanged = (oldGlobal != global);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   405
        return inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   406
            @Override public Object call() {
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   407
                final Object modValue = globalChanged? wrap(value, oldGlobal) : value;
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
   408
                return translateUndefined(wrap(sobj.put(key, unwrap(modValue, global), strict), global));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   409
            }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   410
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   411
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   412
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
    @Override
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   414
    public void putAll(final Map<? extends String, ? extends Object> map) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   415
        final ScriptObject oldGlobal = Context.getGlobal();
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   416
        final boolean globalChanged = (oldGlobal != global);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   417
        inGlobal(new Callable<Object>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   418
            @Override public Object call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   419
                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
   420
                    final Object value = entry.getValue();
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   421
                    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
   422
                    sobj.set(entry.getKey(), unwrap(modValue, global), strict);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   423
                }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   424
                return null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   425
            }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   426
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   427
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   429
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   430
    public Object remove(final Object key) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   431
        return inGlobal(new Callable<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   432
            @Override public Object call() {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
   433
                return wrap(sobj.remove(unwrap(key, global), strict), global);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   438
    /**
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   439
     * Delete a property from this object.
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   440
     *
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   441
     * @param key the property to be deleted
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   442
     *
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   443
     * @return if the delete was successful or not
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   444
     */
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   445
    public boolean delete(final Object key) {
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   446
        return inGlobal(new Callable<Boolean>() {
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   447
            @Override public Boolean call() {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
   448
                return sobj.delete(unwrap(key, global), strict);
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   449
            }
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   450
        });
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   451
    }
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17751
diff changeset
   452
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
    public int size() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
        return inGlobal(new Callable<Integer>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
            @Override public Integer call() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
                return sobj.size();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   462
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   463
    public Collection<Object> values() {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   464
        return inGlobal(new Callable<Collection<Object>>() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   465
            @Override public Collection<Object> call() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   466
                final List<Object>     values = new ArrayList<>(size());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   467
                final Iterator<Object> iter   = sobj.valueIterator();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   468
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   469
                while (iter.hasNext()) {
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16189
diff changeset
   470
                    values.add(translateUndefined(wrap(iter.next(), global)));
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   471
                }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   472
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   473
                return Collections.unmodifiableList(values);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   474
            }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   475
        });
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   476
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   478
    // Support for ECMAScript Object API on mirrors
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
     * Return the __proto__ of this object.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   482
     * @return __proto__ object.
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
    public Object getProto() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   485
        return inGlobal(new Callable<Object>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   486
            @Override public Object call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   487
                return wrap(sobj.getProto(), global);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   488
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   489
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   490
    }
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
    /**
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   493
     * Set the __proto__ of this object.
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   494
     * @param proto new proto for this object
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   495
     */
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   496
    public void setProto(final Object proto) {
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   497
        inGlobal(new Callable<Void>() {
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   498
            @Override public Void call() {
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   499
                sobj.setProtoCheck(unwrap(proto, global));
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   500
                return null;
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   501
            }
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   502
        });
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   503
    }
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   504
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19459
diff changeset
   505
    /**
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   506
     * ECMA 8.12.1 [[GetOwnProperty]] (P)
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   507
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   508
     * @param key property key
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 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
   511
     * object, or undefined if absent.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   512
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   513
    public Object getOwnPropertyDescriptor(final String key) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   514
        return inGlobal(new Callable<Object>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   515
            @Override public Object call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   516
                return wrap(sobj.getOwnPropertyDescriptor(key), global);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   517
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   518
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   519
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   520
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   521
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   522
     * 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
   523
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   524
     * @param all True if to include non-enumerable keys.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   525
     * @return Array of keys.
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   526
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   527
    public String[] getOwnKeys(final boolean all) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   528
        return inGlobal(new Callable<String[]>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   529
            @Override public String[] call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   530
                return sobj.getOwnKeys(all);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   531
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   532
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   533
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   534
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   535
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   536
     * Flag this script object as non extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   537
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   538
     * @return the object after being made non extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   539
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   540
    public ScriptObjectMirror preventExtensions() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   541
        return inGlobal(new Callable<ScriptObjectMirror>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   542
            @Override public ScriptObjectMirror call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   543
                sobj.preventExtensions();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   544
                return ScriptObjectMirror.this;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   545
            }
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
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   548
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
     * Check if this script object is extensible
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   551
     * @return true if extensible
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
    public boolean isExtensible() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   554
        return inGlobal(new Callable<Boolean>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   555
            @Override public Boolean call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   556
                return sobj.isExtensible();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   557
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   558
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   559
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   560
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   561
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   562
     * ECMAScript 15.2.3.8 - seal implementation
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   563
     * @return the sealed script object
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   564
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   565
    public ScriptObjectMirror seal() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   566
        return inGlobal(new Callable<ScriptObjectMirror>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   567
            @Override public ScriptObjectMirror call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   568
                sobj.seal();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   569
                return ScriptObjectMirror.this;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   570
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   571
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   572
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   573
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   574
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   575
     * Check whether this script object is sealed
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   576
     * @return true if sealed
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   577
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   578
    public boolean isSealed() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   579
        return inGlobal(new Callable<Boolean>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   580
            @Override public Boolean call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   581
                return sobj.isSealed();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   582
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   583
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   584
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   585
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   586
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   587
     * ECMA 15.2.39 - freeze implementation. Freeze this script object
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   588
     * @return the frozen script object
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   589
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   590
    public ScriptObjectMirror freeze() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   591
        return inGlobal(new Callable<ScriptObjectMirror>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   592
            @Override public ScriptObjectMirror call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   593
                sobj.freeze();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   594
                return ScriptObjectMirror.this;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   595
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   596
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   597
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   598
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   599
    /**
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   600
     * Check whether this script object is frozen
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   601
     * @return true if frozen
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   602
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   603
    public boolean isFrozen() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   604
        return inGlobal(new Callable<Boolean>() {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   605
            @Override public Boolean call() {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   606
                return sobj.isFrozen();
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   607
            }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   608
        });
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   609
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   610
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   611
    /**
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   612
     * Utility to check if given object is ECMAScript undefined value
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   613
     *
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   614
     * @param obj object to check
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   615
     * @return true if 'obj' is ECMAScript undefined value
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   616
     */
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   617
    public static boolean isUndefined(final Object obj) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   618
        return obj == ScriptRuntime.UNDEFINED;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   619
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   620
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17979
diff changeset
   621
    /**
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   622
     * Utilitity to convert this script object to the given type.
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   623
     *
24639
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents: 23375
diff changeset
   624
     * @param <T> destination type to convert to
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   625
     * @param type destination type to convert to
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   626
     * @return converted object
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   627
     */
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   628
    public <T> T to(final Class<T> type) {
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   629
        return inGlobal(new Callable<T>() {
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   630
            @Override
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   631
            public T call() {
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   632
                return type.cast(ScriptUtils.convert(sobj, type));
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   633
            }
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   634
        });
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   635
    }
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   636
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 21688
diff changeset
   637
    /**
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   638
     * Make a script object mirror on given object if needed. Also converts ConsString instances to Strings.
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   639
     *
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   640
     * @param obj object to be wrapped/converted
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   641
     * @param homeGlobal global to which this object belongs. Not used for ConsStrings.
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   642
     * @return wrapped/converted object
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   643
     */
21688
90bb8dc029c7 8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents: 21686
diff changeset
   644
    public static Object wrap(final Object obj, final Object homeGlobal) {
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   645
        if(obj instanceof ScriptObject) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   646
            return homeGlobal instanceof Global ? new ScriptObjectMirror((ScriptObject)obj, (Global)homeGlobal) : obj;
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   647
        }
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   648
        if(obj instanceof ConsString) {
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   649
            return obj.toString();
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   650
        }
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
   651
        return obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   652
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   653
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   654
    /**
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   655
     * Unwrap a script object mirror if needed.
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   656
     *
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   657
     * @param obj object to be unwrapped
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   658
     * @param homeGlobal global to which this object belongs
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   659
     * @return unwrapped object
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   660
     */
21688
90bb8dc029c7 8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents: 21686
diff changeset
   661
    public static Object unwrap(final Object obj, final Object homeGlobal) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   662
        if (obj instanceof ScriptObjectMirror) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   663
            final ScriptObjectMirror mirror = (ScriptObjectMirror)obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   664
            return (mirror.global == homeGlobal)? mirror.sobj : obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   665
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   666
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   667
        return obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   668
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   669
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   670
    /**
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   671
     * 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
   672
     *
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   673
     * @param args array to be unwrapped
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   674
     * @param homeGlobal global to which this object belongs
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   675
     * @return wrapped array
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   676
     */
21688
90bb8dc029c7 8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents: 21686
diff changeset
   677
    public static Object[] wrapArray(final Object[] args, final Object homeGlobal) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   678
        if (args == null || args.length == 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   679
            return args;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   680
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   681
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   682
        final Object[] newArgs = new Object[args.length];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   683
        int index = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   684
        for (final Object obj : args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   685
            newArgs[index] = wrap(obj, homeGlobal);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   686
            index++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   687
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   688
        return newArgs;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   689
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   690
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   691
    /**
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   692
     * Unwrap an array of script object mirrors if needed.
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   693
     *
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   694
     * @param args array to be unwrapped
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   695
     * @param homeGlobal global to which this object belongs
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   696
     * @return unwrapped array
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   697
     */
21688
90bb8dc029c7 8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents: 21686
diff changeset
   698
    public static Object[] unwrapArray(final Object[] args, final Object homeGlobal) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   699
        if (args == null || args.length == 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   700
            return args;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   701
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   702
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   703
        final Object[] newArgs = new Object[args.length];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   704
        int index = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   705
        for (final Object obj : args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   706
            newArgs[index] = unwrap(obj, homeGlobal);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   707
            index++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   708
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   709
        return newArgs;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   710
    }
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   711
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   712
    // package-privates below this.
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   713
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   714
    ScriptObjectMirror(final ScriptObject sobj, final Global global) {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   715
        assert sobj != null : "ScriptObjectMirror on null!";
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   716
        assert global != null : "home Global is null";
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   717
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   718
        this.sobj = sobj;
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   719
        this.global = global;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   720
        this.strict = global.isStrictContext();
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   721
    }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   722
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   723
    // accessors for script engine
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   724
    ScriptObject getScriptObject() {
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   725
        return sobj;
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   726
    }
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   727
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   728
    Global getHomeGlobal() {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   729
        return global;
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   730
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   731
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   732
    static Object translateUndefined(Object obj) {
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   733
        return (obj == ScriptRuntime.UNDEFINED)? null : obj;
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17978
diff changeset
   734
    }
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   735
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   736
    // internals only below this.
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   737
    private <V> V inGlobal(final Callable<V> callable) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23078
diff changeset
   738
        final Global oldGlobal = Context.getGlobal();
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   739
        final boolean globalChanged = (oldGlobal != global);
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   740
        if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   741
            Context.setGlobal(global);
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   742
        }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   743
        try {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   744
            return callable.call();
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   745
        } catch (final NashornException ne) {
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 21691
diff changeset
   746
            throw ne.initEcmaError(global);
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   747
        } catch (final RuntimeException e) {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   748
            throw e;
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   749
        } catch (final Exception e) {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   750
            throw new AssertionError("Cannot happen", e);
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   751
        } finally {
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   752
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   753
                Context.setGlobal(oldGlobal);
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   754
            }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   755
        }
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18851
diff changeset
   756
    }
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   757
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   758
    @Override
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   759
    public double toNumber() {
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   760
        return inGlobal(new Callable<Double>() {
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   761
            @Override public Double call() {
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   762
                return JSType.toNumber(sobj);
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   763
            }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   764
        });
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 19889
diff changeset
   765
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   766
}