src/jdk/nashorn/api/scripting/ScriptObjectMirror.java

changeset 3
da1e581c933b
child 7
5a1b0714df0e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Fri Dec 21 16:36:24 2012 -0400
     1.3 @@ -0,0 +1,323 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package jdk.nashorn.api.scripting;
    1.30 +
    1.31 +import java.util.AbstractMap;
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.Collection;
    1.34 +import java.util.Collections;
    1.35 +import java.util.HashSet;
    1.36 +import java.util.Iterator;
    1.37 +import java.util.List;
    1.38 +import java.util.Map;
    1.39 +import java.util.Set;
    1.40 +import java.util.concurrent.Callable;
    1.41 +import jdk.nashorn.internal.runtime.Context;
    1.42 +import jdk.nashorn.internal.runtime.ScriptFunction;
    1.43 +import jdk.nashorn.internal.runtime.ScriptObject;
    1.44 +import netscape.javascript.JSObject;
    1.45 +
    1.46 +/**
    1.47 + * Mirror object that wraps a given ScriptObject instance. User can
    1.48 + * access ScriptObject via the java.util.Map interface.
    1.49 + */
    1.50 +final class ScriptObjectMirror extends JSObject implements Map<Object, Object> {
    1.51 +    private final ScriptObject sobj;
    1.52 +    private final ScriptObject global;
    1.53 +
    1.54 +    ScriptObjectMirror(final ScriptObject sobj, final ScriptObject global) {
    1.55 +        this.sobj = sobj;
    1.56 +        this.global = global;
    1.57 +    }
    1.58 +
    1.59 +    @Override
    1.60 +    public boolean equals(final Object other) {
    1.61 +        if (other instanceof ScriptObjectMirror) {
    1.62 +            return sobj.equals(((ScriptObjectMirror)other).sobj);
    1.63 +        }
    1.64 +
    1.65 +        return false;
    1.66 +    }
    1.67 +
    1.68 +    @Override
    1.69 +    public int hashCode() {
    1.70 +        return sobj.hashCode();
    1.71 +    }
    1.72 +
    1.73 +    private <V> V inGlobal(final Callable<V> callable) {
    1.74 +        final ScriptObject oldGlobal = Context.getGlobal();
    1.75 +        final boolean globalChanged = (oldGlobal != global);
    1.76 +        if (globalChanged) {
    1.77 +            NashornScriptEngine.setNashornGlobal(global);
    1.78 +        }
    1.79 +        try {
    1.80 +            return callable.call();
    1.81 +        } catch (final RuntimeException e) {
    1.82 +            throw e;
    1.83 +        } catch (final Exception e) {
    1.84 +            throw new AssertionError("Cannot happen", e);
    1.85 +        } finally {
    1.86 +            if (globalChanged) {
    1.87 +                NashornScriptEngine.setNashornGlobal(oldGlobal);
    1.88 +            }
    1.89 +        }
    1.90 +    }
    1.91 +
    1.92 +    // JSObject methods
    1.93 +    @Override
    1.94 +    public Object call(final String methodName, final Object args[]) {
    1.95 +        final Object val = sobj.get(methodName);
    1.96 +        final ScriptObject oldGlobal = Context.getGlobal();
    1.97 +        final boolean globalChanged = (oldGlobal != global);
    1.98 +
    1.99 +        if (val instanceof ScriptFunction) {
   1.100 +            final Object[] modifiedArgs = unwrapArray(args, global);
   1.101 +            if (modifiedArgs != null) {
   1.102 +                for (int i = 0; i < modifiedArgs.length; i++) {
   1.103 +                    final Object arg = modifiedArgs[i];
   1.104 +                    if (arg instanceof ScriptObject) {
   1.105 +                        modifiedArgs[i] = wrap(arg, oldGlobal);
   1.106 +                    }
   1.107 +                }
   1.108 +            }
   1.109 +
   1.110 +            try {
   1.111 +                if (globalChanged) {
   1.112 +                    NashornScriptEngine.setNashornGlobal(global);
   1.113 +                }
   1.114 +                return wrap(((ScriptFunction)val).invoke(sobj, modifiedArgs), global);
   1.115 +            } catch (final RuntimeException | Error e) {
   1.116 +                throw e;
   1.117 +            } catch (final Throwable t) {
   1.118 +                throw new RuntimeException(t);
   1.119 +            } finally {
   1.120 +                if (globalChanged) {
   1.121 +                    NashornScriptEngine.setNashornGlobal(oldGlobal);
   1.122 +                }
   1.123 +            }
   1.124 +       }
   1.125 +
   1.126 +       throw new RuntimeException("No such method: " + methodName);
   1.127 +    }
   1.128 +
   1.129 +    @Override
   1.130 +    public Object eval(final String s) {
   1.131 +        return inGlobal(new Callable<Object>() {
   1.132 +            @Override
   1.133 +            public Object call() {
   1.134 +                return wrap(global.getContext().eval(global, s, null, null, false), global);
   1.135 +            }
   1.136 +        });
   1.137 +    }
   1.138 +
   1.139 +    @Override
   1.140 +    public Object getMember(final String name) {
   1.141 +        return get(name);
   1.142 +    }
   1.143 +
   1.144 +    @Override
   1.145 +    public Object getSlot(final int index) {
   1.146 +        return get(Integer.valueOf(index));
   1.147 +    }
   1.148 +
   1.149 +    @Override
   1.150 +    public void removeMember(final String name) {
   1.151 +        remove(name);
   1.152 +    }
   1.153 +
   1.154 +    @Override
   1.155 +    public void setMember(final String name, final Object value) {
   1.156 +        put(name, wrap(value, Context.getGlobal()));
   1.157 +    }
   1.158 +
   1.159 +    @Override
   1.160 +    public void setSlot(final int index, final Object value) {
   1.161 +        put(Integer.valueOf(index), wrap(value, Context.getGlobal()));
   1.162 +    }
   1.163 +
   1.164 +    @Override
   1.165 +    public void clear() {
   1.166 +        inGlobal(new Callable<Object>() {
   1.167 +            @Override public Object call() {
   1.168 +                sobj.clear();
   1.169 +                return null;
   1.170 +            }});
   1.171 +    }
   1.172 +
   1.173 +    @Override
   1.174 +    public boolean containsKey(final Object key) {
   1.175 +        return inGlobal(new Callable<Boolean>() {
   1.176 +            @Override public Boolean call() {
   1.177 +                return sobj.containsKey(unwrap(key, global));
   1.178 +            }});
   1.179 +    }
   1.180 +
   1.181 +    @Override
   1.182 +    public boolean containsValue(final Object value) {
   1.183 +        return inGlobal(new Callable<Boolean>() {
   1.184 +            @Override public Boolean call() {
   1.185 +                return sobj.containsValue(unwrap(value, global));
   1.186 +            }});
   1.187 +    }
   1.188 +
   1.189 +    @Override
   1.190 +    public Set<Map.Entry<Object, Object>> entrySet() {
   1.191 +        return inGlobal(new Callable<Set<Map.Entry<Object, Object>>>() {
   1.192 +            @Override public Set<Map.Entry<Object, Object>> call() {
   1.193 +                final Iterator<String>               iter    = sobj.propertyIterator();
   1.194 +                final Set<Map.Entry<Object, Object>> entries = new HashSet<>();
   1.195 +
   1.196 +                while (iter.hasNext()) {
   1.197 +                    final Object key   = wrap(iter.next(), global);
   1.198 +                    final Object value = wrap(sobj.get(key), global);
   1.199 +                    entries.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
   1.200 +                }
   1.201 +
   1.202 +                return Collections.unmodifiableSet(entries);
   1.203 +            }
   1.204 +        });
   1.205 +    }
   1.206 +
   1.207 +    @Override
   1.208 +    public Object get(final Object key) {
   1.209 +        return inGlobal(new Callable<Object>() { @Override public Object call() {
   1.210 +            return wrap(sobj.get(key), global);
   1.211 +        }});
   1.212 +    }
   1.213 +
   1.214 +    @Override
   1.215 +    public boolean isEmpty() {
   1.216 +        return inGlobal(new Callable<Boolean>() { @Override public Boolean call() {
   1.217 +            return sobj.isEmpty();
   1.218 +        }});
   1.219 +    }
   1.220 +
   1.221 +    @Override
   1.222 +    public Set<Object> keySet() {
   1.223 +        return inGlobal(new Callable<Set<Object>>() { @Override public Set<Object> call() {
   1.224 +            final Iterator<String> iter   = sobj.propertyIterator();
   1.225 +            final Set<Object>      keySet = new HashSet<>();
   1.226 +
   1.227 +            while (iter.hasNext()) {
   1.228 +                keySet.add(wrap(iter.next(), global));
   1.229 +            }
   1.230 +
   1.231 +            return Collections.unmodifiableSet(keySet);
   1.232 +        }});
   1.233 +    }
   1.234 +
   1.235 +    @Override
   1.236 +    public Object put(final Object key, final Object value) {
   1.237 +        return inGlobal(new Callable<Object>() {
   1.238 +            @Override public Object call() {
   1.239 +                return sobj.put(unwrap(key, global), unwrap(value, global));
   1.240 +        }});
   1.241 +    }
   1.242 +
   1.243 +    @Override
   1.244 +    public void putAll(final Map<?, ?> map) {
   1.245 +        final boolean strict = sobj.getContext()._strict;
   1.246 +        inGlobal(new Callable<Object>() { @Override public Object call() {
   1.247 +            for (final Map.Entry<?, ?> entry : map.entrySet()) {
   1.248 +                sobj.set(unwrap(entry.getKey(), global), unwrap(entry.getValue(), global), strict);
   1.249 +            }
   1.250 +            return null;
   1.251 +        }});
   1.252 +    }
   1.253 +
   1.254 +    @Override
   1.255 +    public Object remove(final Object key) {
   1.256 +        return inGlobal(new Callable<Object>() {
   1.257 +            @Override public Object call() {
   1.258 +                return wrap(sobj.remove(unwrap(key, global)), global);
   1.259 +            }
   1.260 +        });
   1.261 +    }
   1.262 +
   1.263 +    @Override
   1.264 +    public int size() {
   1.265 +        return inGlobal(new Callable<Integer>() {
   1.266 +            @Override public Integer call() {
   1.267 +                return sobj.size();
   1.268 +            }
   1.269 +        });
   1.270 +    }
   1.271 +
   1.272 +    @Override
   1.273 +    public Collection<Object> values() {
   1.274 +        return inGlobal(new Callable<Collection<Object>>() { @Override public Collection<Object> call() {
   1.275 +            final List<Object>     values = new ArrayList<>(size());
   1.276 +            final Iterator<Object> iter   = sobj.valueIterator();
   1.277 +
   1.278 +            while (iter.hasNext()) {
   1.279 +                values.add(wrap(iter.next(), global));
   1.280 +            }
   1.281 +
   1.282 +            return Collections.unmodifiableList(values);
   1.283 +        }});
   1.284 +    }
   1.285 +
   1.286 +    static Object wrap(final Object obj, final ScriptObject homeGlobal) {
   1.287 +        return (obj instanceof ScriptObject) ? new ScriptObjectMirror((ScriptObject)obj, homeGlobal) : obj;
   1.288 +    }
   1.289 +
   1.290 +    static Object unwrap(final Object obj, final ScriptObject homeGlobal) {
   1.291 +        if (obj instanceof ScriptObjectMirror) {
   1.292 +            final ScriptObjectMirror mirror = (ScriptObjectMirror)obj;
   1.293 +            return (mirror.global == homeGlobal)? mirror.sobj : obj;
   1.294 +        }
   1.295 +
   1.296 +        return obj;
   1.297 +    }
   1.298 +
   1.299 +    static Object[] wrapArray(final Object[] args, final ScriptObject homeGlobal) {
   1.300 +        if (args == null || args.length == 0) {
   1.301 +            return args;
   1.302 +        }
   1.303 +
   1.304 +        final Object[] newArgs = new Object[args.length];
   1.305 +        int index = 0;
   1.306 +        for (final Object obj : args) {
   1.307 +            newArgs[index] = wrap(obj, homeGlobal);
   1.308 +            index++;
   1.309 +        }
   1.310 +        return newArgs;
   1.311 +    }
   1.312 +
   1.313 +    static Object[] unwrapArray(final Object[] args, final ScriptObject homeGlobal) {
   1.314 +        if (args == null || args.length == 0) {
   1.315 +            return args;
   1.316 +        }
   1.317 +
   1.318 +        final Object[] newArgs = new Object[args.length];
   1.319 +        int index = 0;
   1.320 +        for (final Object obj : args) {
   1.321 +            newArgs[index] = unwrap(obj, homeGlobal);
   1.322 +            index++;
   1.323 +        }
   1.324 +        return newArgs;
   1.325 +    }
   1.326 +}

mercurial