home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / jsj / classes / netscape / javascript / JSObject.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  5.1 KB  |  151 lines

  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* ** */
  20.  
  21. /* more doc todo:
  22.  *  threads
  23.  *  gc
  24.  *  
  25.  *
  26.  */
  27.  
  28. package netscape.javascript;
  29.  
  30. import java.applet.Applet;
  31.  
  32. /**
  33.  * JSObject allows Java to manipulate objects that are
  34.  * defined in JavaScript.
  35.  * Values passed from Java to JavaScript are converted as
  36.  * follows:<ul>
  37.  * <li>JSObject is converted to the original JavaScript object
  38.  * <li>Any other Java object is converted to a JavaScript wrapper,
  39.  *   which can be used to access methods and fields of the java object.
  40.  *   Converting this wrapper to a string will call the toString method
  41.  *   on the original object, converting to a number will call the
  42.  *   floatValue method if possible and fail otherwise.  Converting
  43.  *   to a boolean will try to call the booleanValue method in the
  44.  *   same way.
  45.  * <li>Java arrays are wrapped with a JavaScript object that understands
  46.  *   array.length and array[index]
  47.  * <li>A Java boolean is converted to a JavaScript boolean
  48.  * <li>Java byte, char, short, int, long, float, and double are converted
  49.  *   to JavaScript numbers
  50.  * </ul>
  51.  * Values passed from JavaScript to Java are converted as follows:<ul>
  52.  * <li>objects which are wrappers around java objects are unwrapped
  53.  * <li>other objects are wrapped with a JSObject
  54.  * <li>strings, numbers and booleans are converted to String, Float,
  55.  *   and Boolean objects respectively
  56.  * </ul>
  57.  * This means that all JavaScript values show up as some kind
  58.  * of java.lang.Object in Java.  In order to make much use of them,
  59.  * you will have to cast them to the appropriate subclass of Object,
  60.  * e.g. <code>(String) window.getMember("name");</code> or
  61.  * <code>(JSObject) window.getMember("document");</code>.
  62.  */
  63. public final class JSObject {
  64.     /* the internal object data */
  65.     private int                               internal;
  66.  
  67.     /**
  68.      * initialize
  69.      */
  70.     private static native void initClass();
  71.     static {
  72.         SecurityManager.enablePrivilege("UniversalLinkAccess");
  73.         SecurityManager.enablePrivilege("UniversalPropertyRead");
  74.         System.loadLibrary(System.getProperty("netscape.jsj.dll", "jsj"));
  75.         SecurityManager.revertPrivilege();
  76.         initClass();
  77.     }
  78.  
  79.     /**
  80.      * it is illegal to construct a JSObject manually
  81.      */
  82.     private                          JSObject() {}
  83.  
  84.     /**
  85.      * Retrieves a named member of a JavaScript object. 
  86.      * Equivalent to "this.<i>name</i>" in JavaScript.
  87.      */
  88.     public native Object    getMember(String name);
  89.  
  90.     /**
  91.      * Retrieves an indexed member of a JavaScript object.
  92.      * Equivalent to "this[<i>index</i>]" in JavaScript.
  93.      */
  94. //    public Object        getMember(int index) { return getSlot(index); }
  95.     public native Object    getSlot(int index);
  96.  
  97.     /**
  98.      * Sets a named member of a JavaScript object. 
  99.      * Equivalent to "this.<i>name</i> = <i>value</i>" in JavaScript.
  100.      */
  101.     public native void         setMember(String name, Object value);
  102.  
  103.     /**
  104.      * Sets an indexed member of a JavaScript object. 
  105.      * Equivalent to "this[<i>index</i>] = <i>value</i>" in JavaScript.
  106.      */
  107. //    public void         setMember(int index, Object value) {
  108. //        setSlot(index, value);
  109. //    }
  110.     public native void         setSlot(int index, Object value);
  111.  
  112.     /**
  113.      * Removes a named member of a JavaScript object.
  114.      */
  115.     public native void         removeMember(String name);
  116.  
  117.     /**
  118.      * Calls a JavaScript method.
  119.      * Equivalent to "this.<i>methodName</i>(<i>args</i>[0], <i>args</i>[1], ...)" in JavaScript.
  120.      */
  121.     public native Object    call(String methodName, Object args[]);
  122.  
  123.     /**
  124.      * Evaluates a JavaScript expression. The expression is a string 
  125.      * of JavaScript source code which will be evaluated in the context
  126.      * given by "this".
  127.      */
  128.     public native Object    eval(String s);
  129.  
  130.     /**
  131.      * Converts a JSObject to a String.
  132.      */
  133.     public native String        toString();
  134.  
  135.     // should use some sort of identifier rather than String
  136.     // is "property" the right word?
  137.   //    native String[]                         listProperties();
  138.  
  139.  
  140.     /**
  141.      * get a JSObject for the window containing the given applet
  142.      */
  143.     public static native JSObject    getWindow(Applet applet);
  144.  
  145.     /**
  146.      * Finalization decrements the reference count on the corresponding
  147.      * JavaScript object.
  148.      */
  149.     protected native void    finalize();
  150. }
  151.