1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.webkit.dom;
  27 
  28 import java.security.AccessControlContext;
  29 import java.security.AccessController;
  30 import netscape.javascript.JSException;
  31 
  32 class JSObject extends netscape.javascript.JSObject {
  33     private static final String UNDEFINED = new String("undefined");
  34     static final int JS_CONTEXT_OBJECT  = 0;
  35     static final int JS_DOM_NODE_OBJECT  = 1;
  36     static final int JS_DOM_WINDOW_OBJECT  = 2;
  37 
  38     private final long peer;     // C++ peer - now it is the DOMObject instance
  39     private final int peer_type; // JS_XXXX const
  40 
  41     JSObject(long peer, int peer_type) {
  42         this.peer = peer;
  43         this.peer_type = peer_type;
  44     }
  45 
  46     long getPeer() {
  47         return peer;
  48     }
  49 
  50     @Override
  51     public Object eval(String s) throws JSException {
  52         return evalImpl(peer, peer_type, s);
  53     }
  54     private static native Object evalImpl(long peer, int peer_type,
  55                                           String name);
  56 
  57     @Override
  58     public Object getMember(String name) {
  59         return getMemberImpl(peer, peer_type, name);
  60     }
  61     private static native Object getMemberImpl(long peer, int peer_type,
  62                                                String name);
  63 
  64     @Override
  65     public void setMember(String name, Object value) throws JSException {
  66         setMemberImpl(peer, peer_type, name, value,
  67                       AccessController.getContext());
  68     }
  69     private static native void setMemberImpl(long peer, int peer_type,
  70                                              String name, Object value,
  71                                              AccessControlContext acc);
  72 
  73     @Override
  74     public void removeMember(String name) throws JSException {
  75         removeMemberImpl(peer, peer_type, name);
  76     }
  77     private static native void removeMemberImpl(long peer, int peer_type,
  78                                                 String name);
  79 
  80     @Override
  81     public Object getSlot(int index) throws JSException {
  82         return getSlotImpl(peer, peer_type, index);
  83     }
  84     private static native Object getSlotImpl(long peer, int peer_type,
  85                                              int index);
  86 
  87     @Override
  88     public void setSlot(int index, Object value) throws JSException {
  89         setSlotImpl(peer, peer_type, index, value,
  90                     AccessController.getContext());
  91     }
  92     private static native void setSlotImpl(long peer, int peer_type,
  93                                            int index, Object value,
  94                                            AccessControlContext acc);
  95 
  96     @Override
  97     public Object call(String methodName, Object... args) throws JSException {
  98         return callImpl(peer, peer_type, methodName, args,
  99                         AccessController.getContext());
 100     }
 101     private static native Object callImpl(long peer, int peer_type,
 102                                           String methodName, Object[] args,
 103                                           AccessControlContext acc);
 104 
 105     @Override
 106     public String toString() {
 107         return toStringImpl(peer, peer_type);
 108     }
 109     private static native String toStringImpl(long peer, int peer_type);
 110 
 111     @Override
 112     public boolean equals(Object other) {
 113         return other == this
 114           || (other != null && other.getClass() == JSObject.class
 115               && peer == ((JSObject) other).peer);
 116     }
 117 
 118     @Override
 119     public int hashCode() {
 120         return (int) (peer ^ (peer >> 17));
 121     }
 122 
 123     private static JSException fwkMakeException(Object value) {
 124         String msg = value == null ? null : value.toString();
 125         // Would like to set wrappedException, but can't do that while
 126         // also setting the message.  Perhaps we should create a subclass.
 127         JSException ex
 128             = new JSException(value == null ? null : value.toString());
 129         if (value instanceof Throwable)
 130             ex.initCause((Throwable) value);
 131         return ex;
 132     }
 133 }