001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.core.client;
017:
018: /**
019: * An opaque handle to a native JavaScript object. A
020: * <code>JavaScriptObject</code> cannot be created directly.
021: * <code>JavaScriptObject</code> should be declared as the return type of a
022: * JSNI method that returns native (non-Java) objects. A
023: * <code>JavaScriptObject</code> passed back into JSNI from Java becomes the
024: * original object, and can be accessed in JavaScript as expected.
025: *
026: * <p>
027: * <b>SUBCLASSING IS NOT SUPPORTED EXCEPT FOR THE EXISTING SUBCLASSES.</b>
028: * </p>
029: */
030: public class JavaScriptObject {
031:
032: /**
033: * Returns a new array.
034: */
035: public static native JavaScriptObject createArray() /*-{
036: return [];
037: }-*/;
038:
039: /**
040: * Returns an empty function.
041: */
042: public static native JavaScriptObject createFunction() /*-{
043: return function() {
044: };
045: }-*/;
046:
047: /**
048: * Returns a new object.
049: */
050: public static native JavaScriptObject createObject() /*-{
051: return {};
052: }-*/;
053:
054: private static native boolean equalsImpl(JavaScriptObject o,
055: JavaScriptObject other) /*-{
056: return o === other;
057: }-*/;
058:
059: private static native String toStringImpl(JavaScriptObject o) /*-{
060: if (o.toString)
061: return o.toString();
062: return "[object]";
063: }-*/;
064:
065: /**
066: * The underlying JavaScript object. This is used internally and should never
067: * be accessed by client code.
068: */
069: protected Object hostedModeReference;
070:
071: /**
072: * Not directly instantiable. Subclasses should also define a protected
073: * no-arg constructor to prevent client code from directly instantiating
074: * the class.
075: */
076: protected JavaScriptObject() {
077: }
078:
079: @Override
080: public boolean equals(Object other) {
081: if (!(other instanceof JavaScriptObject)) {
082: return false;
083: }
084: return equalsImpl(this , (JavaScriptObject) other);
085: }
086:
087: @Override
088: public int hashCode() {
089: return Impl.getHashCode(this );
090: }
091:
092: @Override
093: public String toString() {
094: /*
095: * Hosted mode will marshal an explicit argument from a JavaScriptObject
096: * back to its underlying object, but it won't currently do that for the
097: * implicit "this" arg. For now, can't implement instance methods on JSO
098: * directly as natives, so use a delegator.
099: */
100: return toStringImpl(this);
101: }
102: }
|