src/jdk/nashorn/internal/runtime/linker/BrowserJSObjectLinker.java

changeset 966
620bf937f377
child 967
934689dc9f10
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/jdk/nashorn/internal/runtime/linker/BrowserJSObjectLinker.java	Thu Aug 21 20:06:48 2014 +0530
     1.3 @@ -0,0 +1,199 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, 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.internal.runtime.linker;
    1.30 +
    1.31 +import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.*;
    1.32 +
    1.33 +import java.lang.invoke.MethodHandle;
    1.34 +import java.lang.invoke.MethodHandles;
    1.35 +import jdk.internal.dynalink.CallSiteDescriptor;
    1.36 +import jdk.internal.dynalink.linker.GuardedInvocation;
    1.37 +import jdk.internal.dynalink.linker.LinkRequest;
    1.38 +import jdk.internal.dynalink.linker.LinkerServices;
    1.39 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
    1.40 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
    1.41 +import jdk.nashorn.internal.lookup.MethodHandleFactory;
    1.42 +import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
    1.43 +import jdk.nashorn.internal.runtime.JSType;
    1.44 +
    1.45 +/**
    1.46 + * A Dynalink linker to handle web browser built-in JS (DOM etc.) objects.
    1.47 + */
    1.48 +final class BrowserJSObjectLinker implements TypeBasedGuardingDynamicLinker {
    1.49 +    private static final ClassLoader myLoader = BrowserJSObjectLinker.class.getClassLoader();
    1.50 +    private static final String JSOBJECT_CLASS = "netscape.javascript.JSObject";
    1.51 +    // not final because this is lazily initialized
    1.52 +    // when we hit a subclass for the first time.
    1.53 +    private static volatile Class<?> jsObjectClass;
    1.54 +
    1.55 +    @Override
    1.56 +    public boolean canLinkType(final Class<?> type) {
    1.57 +        return canLinkTypeStatic(type);
    1.58 +    }
    1.59 +
    1.60 +    static boolean canLinkTypeStatic(final Class<?> type) {
    1.61 +        if (jsObjectClass != null && jsObjectClass.isAssignableFrom(type)) {
    1.62 +            return true;
    1.63 +        }
    1.64 +
    1.65 +        // check if this class is a subclass of JSObject
    1.66 +        Class<?> clazz = type;
    1.67 +        while (clazz != null) {
    1.68 +            if (clazz.getClassLoader() == myLoader &&
    1.69 +                clazz.getName().equals(JSOBJECT_CLASS)) {
    1.70 +                jsObjectClass = clazz;
    1.71 +                return true;
    1.72 +            }
    1.73 +            clazz = clazz.getSuperclass();
    1.74 +        }
    1.75 +
    1.76 +        return false;
    1.77 +    }
    1.78 +
    1.79 +    private static void checkJSObjectClass() {
    1.80 +        assert jsObjectClass != null : JSOBJECT_CLASS + " not found!";
    1.81 +    }
    1.82 +
    1.83 +    @Override
    1.84 +    public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    1.85 +        final LinkRequest requestWithoutContext = request.withoutRuntimeContext(); // Nashorn has no runtime context
    1.86 +        final Object self = requestWithoutContext.getReceiver();
    1.87 +        final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
    1.88 +        checkJSObjectClass();
    1.89 +
    1.90 +        if (desc.getNameTokenCount() < 2 || !"dyn".equals(desc.getNameToken(CallSiteDescriptor.SCHEME))) {
    1.91 +            // We only support standard "dyn:*[:*]" operations
    1.92 +            return null;
    1.93 +        }
    1.94 +
    1.95 +        final GuardedInvocation inv;
    1.96 +        if (jsObjectClass.isInstance(self)) {
    1.97 +            inv = lookup(desc);
    1.98 +        } else {
    1.99 +            throw new AssertionError(); // Should never reach here.
   1.100 +        }
   1.101 +
   1.102 +        return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc);
   1.103 +    }
   1.104 +
   1.105 +    private static GuardedInvocation lookup(final CallSiteDescriptor desc) {
   1.106 +        final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
   1.107 +        final int c = desc.getNameTokenCount();
   1.108 +
   1.109 +        switch (operator) {
   1.110 +            case "getProp":
   1.111 +            case "getElem":
   1.112 +            case "getMethod":
   1.113 +                return c > 2 ? findGetMethod(desc) : findGetIndexMethod();
   1.114 +            case "setProp":
   1.115 +            case "setElem":
   1.116 +                return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
   1.117 +            default:
   1.118 +                return null;
   1.119 +        }
   1.120 +    }
   1.121 +
   1.122 +    private static GuardedInvocation findGetMethod(final CallSiteDescriptor desc) {
   1.123 +        final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
   1.124 +        final MethodHandle getter = MH.insertArguments(JSOBJECT_GETMEMBER, 1, name);
   1.125 +        return new GuardedInvocation(getter, IS_JSOBJECT_GUARD);
   1.126 +    }
   1.127 +
   1.128 +    private static GuardedInvocation findGetIndexMethod() {
   1.129 +        return new GuardedInvocation(JSOBJECTLINKER_GET, IS_JSOBJECT_GUARD);
   1.130 +    }
   1.131 +
   1.132 +    private static GuardedInvocation findSetMethod(final CallSiteDescriptor desc) {
   1.133 +        final MethodHandle getter = MH.insertArguments(JSOBJECT_SETMEMBER, 1, desc.getNameToken(2));
   1.134 +        return new GuardedInvocation(getter, IS_JSOBJECT_GUARD);
   1.135 +    }
   1.136 +
   1.137 +    private static GuardedInvocation findSetIndexMethod() {
   1.138 +        return new GuardedInvocation(JSOBJECTLINKER_PUT, IS_JSOBJECT_GUARD);
   1.139 +    }
   1.140 +
   1.141 +    @SuppressWarnings("unused")
   1.142 +    private static boolean isJSObject(final Object self) {
   1.143 +        return jsObjectClass.isInstance(self);
   1.144 +    }
   1.145 +
   1.146 +    @SuppressWarnings("unused")
   1.147 +    private static Object get(final Object jsobj, final Object key) throws Throwable {
   1.148 +        if (key instanceof Integer) {
   1.149 +            return JSOBJECT_GETSLOT.invokeExact(jsobj, (int)key);
   1.150 +        } else if (key instanceof Number) {
   1.151 +            final int index = getIndex((Number)key);
   1.152 +            if (index > -1) {
   1.153 +                return JSOBJECT_GETSLOT.invokeExact(jsobj, index);
   1.154 +            }
   1.155 +        } else if (key instanceof String) {
   1.156 +            return JSOBJECT_GETMEMBER.invokeExact(jsobj, (String)key);
   1.157 +        }
   1.158 +        return null;
   1.159 +    }
   1.160 +
   1.161 +    @SuppressWarnings("unused")
   1.162 +    private static void put(final Object jsobj, final Object key, final Object value) throws Throwable {
   1.163 +        if (key instanceof Integer) {
   1.164 +            JSOBJECT_SETSLOT.invokeExact(jsobj, (int)key, value);
   1.165 +        } else if (key instanceof Number) {
   1.166 +            JSOBJECT_SETSLOT.invokeExact(jsobj, getIndex((Number)key), value);
   1.167 +        } else if (key instanceof String) {
   1.168 +            JSOBJECT_SETMEMBER.invokeExact(jsobj, (String)key, value);
   1.169 +        }
   1.170 +    }
   1.171 +
   1.172 +    private static int getIndex(final Number n) {
   1.173 +        final double value = n.doubleValue();
   1.174 +        return JSType.isRepresentableAsInt(value) ? (int)value : -1;
   1.175 +    }
   1.176 +
   1.177 +    private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality();
   1.178 +    // method handles of the current class
   1.179 +    private static final MethodHandle IS_JSOBJECT_GUARD  = findOwnMH_S("isJSObject", boolean.class, Object.class);
   1.180 +    private static final MethodHandle JSOBJECTLINKER_GET = findOwnMH_S("get", Object.class, Object.class, Object.class);
   1.181 +    private static final MethodHandle JSOBJECTLINKER_PUT = findOwnMH_S("put", Void.TYPE, Object.class, Object.class, Object.class);
   1.182 +
   1.183 +    private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) {
   1.184 +            return MH.findStatic(MethodHandles.lookup(), BrowserJSObjectLinker.class, name, MH.type(rtype, types));
   1.185 +    }
   1.186 +
   1.187 +    // method handles of netscape.javascript.JSObject class
   1.188 +    // These are in separate class as we lazily initialize these
   1.189 +    // method handles when we hit a subclass of JSObject first time.
   1.190 +    static class JSObjectHandles {
   1.191 +        // method handles of JSObject class
   1.192 +        static final MethodHandle JSOBJECT_GETMEMBER     = findJSObjectMH_V("getMember", Object.class, String.class).asType(MH.type(Object.class, Object.class, String.class));
   1.193 +        static final MethodHandle JSOBJECT_GETSLOT       = findJSObjectMH_V("getSlot", Object.class, int.class).asType(MH.type(Object.class, Object.class, int.class));
   1.194 +        static final MethodHandle JSOBJECT_SETMEMBER     = findJSObjectMH_V("setMember", Void.TYPE, String.class, Object.class).asType(MH.type(Void.TYPE, Object.class, String.class, Object.class));
   1.195 +        static final MethodHandle JSOBJECT_SETSLOT       = findJSObjectMH_V("setSlot", Void.TYPE, int.class, Object.class).asType(MH.type(Void.TYPE, Object.class, int.class, Object.class));
   1.196 +
   1.197 +        private static MethodHandle findJSObjectMH_V(final String name, final Class<?> rtype, final Class<?>... types) {
   1.198 +            checkJSObjectClass();
   1.199 +            return MH.findVirtual(MethodHandles.publicLookup(), jsObjectClass, name, MH.type(rtype, types));
   1.200 +        }
   1.201 +    }
   1.202 +}

mercurial