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.dev.shell;
017:
018: /**
019: * This class contains a set of static methods that can be used to interact with
020: * the browser in hosted mode.
021: */
022: public class JavaScriptHost {
023:
024: private static ShellJavaScriptHost sHost;
025:
026: /**
027: * Defines a new native JavaScript function.
028: *
029: * @param file source file of the function
030: * @param line starting line number of the function
031: * @param jsniSignature the function's jsni signature
032: * @param paramNames the parameter types
033: * @param js the script body
034: */
035: public static void createNative(String file, int line,
036: String jsniSignature, String[] paramNames, String js) {
037: sHost.createNative(file, line, jsniSignature, paramNames, js);
038: }
039:
040: public static void exceptionCaught(int number, String name,
041: String description) {
042: sHost.exceptionCaught(number, name, description);
043: }
044:
045: /**
046: * Invoke a native JavaScript function that returns a boolean value.
047: */
048: public static boolean invokeNativeBoolean(String name,
049: Object jthis , Class<?>[] types, Object[] args)
050: throws Throwable {
051: return sHost.invokeNativeBoolean(name, jthis , types, args);
052: }
053:
054: /**
055: * Invoke a native JavaScript function that returns a byte value.
056: */
057: public static byte invokeNativeByte(String name, Object jthis ,
058: Class<?>[] types, Object[] args) throws Throwable {
059: return sHost.invokeNativeByte(name, jthis , types, args);
060: }
061:
062: /**
063: * Invoke a native JavaScript function that returns a character value.
064: */
065: public static char invokeNativeChar(String name, Object jthis ,
066: Class<?>[] types, Object[] args) throws Throwable {
067: return sHost.invokeNativeChar(name, jthis , types, args);
068: }
069:
070: /**
071: * Invoke a native JavaScript function that returns a double value.
072: */
073: public static double invokeNativeDouble(String name, Object jthis ,
074: Class<?>[] types, Object[] args) throws Throwable {
075: return sHost.invokeNativeDouble(name, jthis , types, args);
076: }
077:
078: /**
079: * Invoke a native JavaScript function that returns a float value.
080: */
081: public static float invokeNativeFloat(String name, Object jthis ,
082: Class<?>[] types, Object[] args) throws Throwable {
083: return sHost.invokeNativeFloat(name, jthis , types, args);
084: }
085:
086: /**
087: * Invoke a native JavaScript function that returns a handle value.
088: */
089: public static Object invokeNativeHandle(String name, Object jthis ,
090: Class<?> returnType, Class<?>[] types, Object[] args)
091: throws Throwable {
092: return sHost.invokeNativeHandle(name, jthis , returnType, types,
093: args);
094: }
095:
096: /**
097: * Invoke a native JavaScript function that returns an integer value.
098: */
099: public static int invokeNativeInt(String name, Object jthis ,
100: Class<?>[] types, Object[] args) throws Throwable {
101: return sHost.invokeNativeInt(name, jthis , types, args);
102: }
103:
104: /**
105: * Invoke a native JavaScript function that returns a long value.
106: */
107: public static long invokeNativeLong(String name, Object jthis ,
108: Class<?>[] types, Object[] args) throws Throwable {
109: return sHost.invokeNativeLong(name, jthis , types, args);
110: }
111:
112: /**
113: * Invoke a native JavaScript function that returns an object value.
114: */
115: public static Object invokeNativeObject(String name, Object jthis ,
116: Class<?>[] types, Object[] args) throws Throwable {
117: return sHost.invokeNativeObject(name, jthis , types, args);
118: }
119:
120: /**
121: * Invoke a native JavaScript function that returns a short value.
122: */
123: public static short invokeNativeShort(String name, Object jthis ,
124: Class<?>[] types, Object[] args) throws Throwable {
125: return sHost.invokeNativeShort(name, jthis , types, args);
126: }
127:
128: /**
129: * Invoke a native JavaScript function that returns a string value.
130: */
131: public static String invokeNativeString(String name, Object jthis ,
132: Class<?>[] types, Object[] args) throws Throwable {
133: return sHost.invokeNativeString(name, jthis , types, args);
134: }
135:
136: /**
137: * Invoke a native JavaScript function that returns no value.
138: */
139: public static void invokeNativeVoid(String name, Object jthis ,
140: Class<?>[] types, Object[] args) throws Throwable {
141: sHost.invokeNativeVoid(name, jthis , types, args);
142: }
143:
144: /**
145: * Logs in dev shell.
146: */
147: public static void log(String message, Throwable e) {
148: sHost.log(message, e);
149: }
150:
151: /**
152: * Resolves a deferred binding request and create the requested object.
153: */
154: public static <T> T rebindAndCreate(Class<?> requestedClass) {
155: String className = requestedClass.getName();
156: try {
157: return sHost.<T> rebindAndCreate(className);
158: } catch (Throwable e) {
159: String msg = "Deferred binding failed for '"
160: + className
161: + "' (did you forget to inherit a required module?)";
162: throw new RuntimeException(msg, e);
163: }
164: }
165:
166: /**
167: * This method is called via reflection from the shell, providing the hosted
168: * mode application with all of the methods it needs to interface with the
169: * browser and the server (for deferred binding).
170: */
171: public static void setHost(ShellJavaScriptHost host) {
172: sHost = host;
173: }
174: }
|