diff --git a/mozilla/js/ref/liveconnect/LiveConnect.dsp b/mozilla/js/ref/liveconnect/LiveConnect.dsp index ea2492b6a33..15191f36a37 100644 --- a/mozilla/js/ref/liveconnect/LiveConnect.dsp +++ b/mozilla/js/ref/liveconnect/LiveConnect.dsp @@ -122,6 +122,10 @@ SOURCE=.\jsj_JavaClass.c # End Source File # Begin Source File +SOURCE=.\jsj_JavaMember.c +# End Source File +# Begin Source File + SOURCE=.\jsj_JavaObject.c # End Source File # Begin Source File @@ -131,6 +135,15 @@ SOURCE=.\jsj_JavaPackage.c # Begin Source File SOURCE=.\jsj_JSObject.c + +!IF "$(CFG)" == "LiveConnect - Win32 Release" + +!ELSEIF "$(CFG)" == "LiveConnect - Win32 Debug" + +# ADD CPP /I ".\_jni" + +!ENDIF + # End Source File # Begin Source File diff --git a/mozilla/js/ref/liveconnect/README.html b/mozilla/js/ref/liveconnect/README.html index 727381c072c..6777ae66aa3 100644 --- a/mozilla/js/ref/liveconnect/README.html +++ b/mozilla/js/ref/liveconnect/README.html @@ -9,9 +9,9 @@

Introduction

-This is the README file for the JavaScript LiveConnect implementation.  -It consists of build conventions and instructions, source code conventions, -and a brief file-by-file description of the source. +This is the README file for the JavaScript LiveConnect Version 2 ("LC2") +implementation.  It consists of build conventions and instructions, +source code conventions, and a brief file-by-file description of the source.

This document assumes basic familiarity with JSRef, the reference implementation of JavaScript, and with the LiveConnect technology  (LiveConnect allows @@ -21,13 +21,13 @@ for Java to access JavaScript object properties and evaluate JavaScript.  More information on LiveConnect can be found by searching the index on Netscape's DevEdge site.) -

JSRef builds a library or DLL containing the JavaScript runtime (compiler, -interpreter, decompiler, garbage collector, atom manager, standard classes).  -The LiveConnect project/makefiles build a library that links both with -JSRef and with a Java Virtual Machine (JVM) that implements the Java Native -Interface (JNI), as specified by JavaSoft.  It then compiles a small -"shell" program and links that with the library to make an interpreter -that can be used interactively and with test scripts. +

JSRef project/makefiles build a library or DLL containing the JavaScript +runtime (compiler, interpreter, decompiler, garbage collector, atom manager, +standard classes).  The LiveConnect project/makefiles build a library +that links both with JSRef and with a Java Virtual Machine (JVM) that implements +the Java Native Interface (JNI), as specified by JavaSoft.  It then +compiles a small "shell" program and links that with the library to make +an interpreter that can be used interactively and with test scripts.

Scott Furman, 4/8/98

@@ -46,37 +46,35 @@ if they refer to the same instance, by using the `==' operator.  Instead, developers must now use the equals()method of netscape.javascript.JSObject for comparison, a method that overrides java.lang.Object.equals().  Using equals() instead of `==' should work the same on both LC1 -and LC2. 
-
-It is not possible to replicate the identity behavior of the `==' operator +and LC2. + + +

It is not possible to replicate the identity behavior of the `==' operator that LC1 provides without the use of "weak" references, i.e. references that do not contribute to making a Java object reachable for purposes of garbage collection, but which nonetheless allow reference to an object as long as it is reachable by other means.  The use of weak references is not portable, however.  It is not part of the JNI or the JDK and it is not provided on all JVMs.  The JDK1.2 release will include standard -support for weak references.
-
- +support for weak references.

  • It's possible that, in a set of overloaded Java methods, more than one -is compatible with the types of the actual arguments in a call from JavaScript -via LiveConnect.  LC1 resolved these ambiguities in a simplistic manner, -by simply invoking whatever method was enumerated first by the JVM.  -The enumeration order of reflected methods using java.lang.reflect  +method is compatible with the types of the actual arguments in a call from +JavaScript via LiveConnect.  LC1 resolved these ambiguities in a simplistic +manner, by simply invoking whatever method was enumerated first by the +JVM.  The enumeration order of reflected methods using java.lang.reflect  is not specified by Sun and may differ among vendor's JVMs, i.e. enumeration could be in order of classfile appearance, hashtable order, etc.    With the Netscape and Sun JVMs, it is possible to change the behavior of an LC1 program by changing the order that Java methods appear in a source file, thus changing the method enumeration order.  Hence, the Java method chosen when there is more than one compatible method may vary depending -on the JVM.
    -
    -A future release of LiveConnect will provide a new algorithm for overloaded -Java method resolution that is both independent of the JVM used and more -likely than LC1 to invoke the method that the developer expects.
    -
  • +on the JVM. + +

    A future release of LiveConnect will provide a new algorithm for overloaded +Java method resolution that is both independent of the JVM used and more +likely than LC1 to invoke the method that the developer expects.

  • There are several minor changes in error handling to make LiveConnect more conformant to ECMAScript.  These include, for example, making any @@ -92,7 +90,6 @@ New Features
  • Several minor features have been added to this release of LiveConnect.  These features were not available in the versions of LiveConnect that were integrated with Netscape Naviagtor versions 4.x and earlier. -
     
  • The Java methods of java.lang.Object are now invokeable methods @@ -194,8 +191,9 @@ in the JDK's bin directory.
  • Use any Java compiler to compile the java source files in the js/ref/liveconnect/classes/netscape/javascript directory. -
    -Mac OS + + +

    Mac OS

    Unix
    • -No Makefiles have been completed yet.  These -notes are a work in progress.
    • - -
    • -Use vendor cc or gcc (ftp://prep.ai.mit.edu/pub/gnu) for compiling,  +Use vendor cc or gcc for compiling,  and use gmake for building.
    • -To compile optimized code, set BUILD_OPT=1 on the nmake/gmake command line -or preset it in the environment or makefile.  The C preprocessor macro -DEBUG  will be undefined, and NDEBUG (archaic Unix-ism for "No Debugging") -will be defined.  Without BUILD_OPT, DEBUG is predefined and NDEBUG -is undefined.
    • - -
    • -Your own debug flag, DEBUG_$USER, will be defined or undefined as BUILD_OPT -is unset or set.
    • - -
    • -To add C compiler options from the make command line, set XCFLAGS=-Dfoo.
    • - -
    • -To predefine -D or -U options in the makefile, set DEFINES.
    • - -
    • -To predefine -I options in the makefile, set INCLUDES.
    • +Makefile has only been tested on Solaris 5.5.  To build on other platforms +you must add a .mk file to the ref/liveconnect/config subdir.
    @@ -394,6 +371,15 @@ of JavaObject are used to reflect Java objects, except for Java arrays, although some of the code in this file is used by the JavaArray code. + +jsj_JavaMember.c + +Implementation of the JavaScript JavaMember class.  JavaMember's +are a strange beast required only to handle the special case of a public +field and a public method that appear in the same Java class and which +have the same name. + + jsj_JavaPackage.c diff --git a/mozilla/js/ref/liveconnect/_jni/netscape_javascript_JSObject.h b/mozilla/js/ref/liveconnect/_jni/netscape_javascript_JSObject.h new file mode 100644 index 00000000000..11b97d4ca2f --- /dev/null +++ b/mozilla/js/ref/liveconnect/_jni/netscape_javascript_JSObject.h @@ -0,0 +1,111 @@ +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. + */ +#include +/* Header for class netscape_javascript_JSObject */ + +#ifndef _Included_netscape_javascript_JSObject +#define _Included_netscape_javascript_JSObject +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: netscape_javascript_JSObject + * Method: initClass + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_netscape_javascript_JSObject_initClass + (JNIEnv *, jclass); + +/* + * Class: netscape_javascript_JSObject + * Method: getMember + * Signature: (Ljava/lang/String;)Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL Java_netscape_javascript_JSObject_getMember + (JNIEnv *, jobject, jstring); + +/* + * Class: netscape_javascript_JSObject + * Method: getSlot + * Signature: (I)Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL Java_netscape_javascript_JSObject_getSlot + (JNIEnv *, jobject, jint); + +/* + * Class: netscape_javascript_JSObject + * Method: setMember + * Signature: (Ljava/lang/String;Ljava/lang/Object;)V + */ +JNIEXPORT void JNICALL Java_netscape_javascript_JSObject_setMember + (JNIEnv *, jobject, jstring, jobject); + +/* + * Class: netscape_javascript_JSObject + * Method: setSlot + * Signature: (ILjava/lang/Object;)V + */ +JNIEXPORT void JNICALL Java_netscape_javascript_JSObject_setSlot + (JNIEnv *, jobject, jint, jobject); + +/* + * Class: netscape_javascript_JSObject + * Method: removeMember + * Signature: (Ljava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_netscape_javascript_JSObject_removeMember + (JNIEnv *, jobject, jstring); + +/* + * Class: netscape_javascript_JSObject + * Method: call + * Signature: (Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL Java_netscape_javascript_JSObject_call + (JNIEnv *, jobject, jstring, jobjectArray); + +/* + * Class: netscape_javascript_JSObject + * Method: eval + * Signature: (Ljava/lang/String;)Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL Java_netscape_javascript_JSObject_eval + (JNIEnv *, jobject, jstring); + +/* + * Class: netscape_javascript_JSObject + * Method: toString + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_netscape_javascript_JSObject_toString + (JNIEnv *, jobject); + +/* + * Class: netscape_javascript_JSObject + * Method: getWindow + * Signature: (Ljava/applet/Applet;)Lnetscape/javascript/JSObject; + */ +JNIEXPORT jobject JNICALL Java_netscape_javascript_JSObject_getWindow + (JNIEnv *, jclass, jobject); + +/* + * Class: netscape_javascript_JSObject + * Method: finalize + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_netscape_javascript_JSObject_finalize + (JNIEnv *, jobject); + +/* + * Class: netscape_javascript_JSObject + * Method: equals + * Signature: (Ljava/lang/Object;)Z + */ +JNIEXPORT jboolean JNICALL Java_netscape_javascript_JSObject_equals + (JNIEnv *, jobject, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSException.java b/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSException.java index 4ae02ffcb77..0b016f16d0f 100644 --- a/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSException.java +++ b/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSException.java @@ -1,4 +1,6 @@ -/* Insert copyright and license here 19** */ +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. + */ package netscape.javascript; diff --git a/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSObject.java b/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSObject.java index bada391a9e2..10d12d47382 100644 --- a/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSObject.java +++ b/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSObject.java @@ -1,4 +1,6 @@ -/* -*- Mode: C; tab-width: 4; -*- */ +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. + */ /* Insert copyright and license here 19** */ @@ -23,7 +25,7 @@ import java.applet.Applet; * which can be used to access methods and fields of the java object. * Converting this wrapper to a string will call the toString method * on the original object, converting to a number will call the - * floatValue method if possible and fail otherwise. Converting + * doubleValue method if possible and fail otherwise. Converting * to a boolean will try to call the booleanValue method in the * same way. *
  • Java arrays are wrapped with a JavaScript object that understands @@ -35,8 +37,9 @@ import java.applet.Applet; * Values passed from JavaScript to Java are converted as follows:
      *
    • objects which are wrappers around java objects are unwrapped *
    • other objects are wrapped with a JSObject - *
    • strings, numbers and booleans are converted to String, Float, + *
    • strings, numbers and booleans are converted to String, Double, * and Boolean objects respectively + *
    • undefined is converted to a string *
    * This means that all JavaScript values show up as some kind * of java.lang.Object in Java. In order to make much use of them, @@ -53,60 +56,74 @@ public final class JSObject { */ private static native void initClass(); static { - System.loadLibrary("LiveConnect"); - initClass(); + String liveConnectLibrary = null; + String OSName = System.getProperty("os.name", null); + + // On MRJ, this property won't exist, because the library is preloaded. + liveConnectLibrary = System.getProperty("netscape.jsj.dll", null); + + // On Mac, native methods are statically linked and manually registered. + if ((liveConnectLibrary == null) && (OSName.indexOf("Mac") == -1)) + liveConnectLibrary = "LiveConnect"; + + if (liveConnectLibrary != null) { + System.loadLibrary(liveConnectLibrary); + initClass(); + } } /** * it is illegal to construct a JSObject manually */ - private JSObject(int jsobj_addr) {internal = jsobj_addr;} + private JSObject(int jsobj_addr) { + internal = jsobj_addr; + } /** * Retrieves a named member of a JavaScript object. * Equivalent to "this.name" in JavaScript. */ - public native Object getMember(String name); + public native Object getMember(String name); /** * Retrieves an indexed member of a JavaScript object. * Equivalent to "this[index]" in JavaScript. */ -// public Object getMember(int index) { return getSlot(index); } - public native Object getSlot(int index); +// public Object getMember(int index) { return getSlot(index); } + public native Object getSlot(int index); /** * Sets a named member of a JavaScript object. * Equivalent to "this.name = value" in JavaScript. */ - public native void setMember(String name, Object value); + public native void setMember(String name, Object value); /** * Sets an indexed member of a JavaScript object. * Equivalent to "this[index] = value" in JavaScript. */ -// public void setMember(int index, Object value) { +// public void setMember(int index, Object value) { // setSlot(index, value); // } - public native void setSlot(int index, Object value); + public native void setSlot(int index, Object value); /** * Removes a named member of a JavaScript object. */ - public native void removeMember(String name); + public native void removeMember(String name); /** * Calls a JavaScript method. * Equivalent to "this.methodName(args[0], args[1], ...)" in JavaScript. */ - public native Object call(String methodName, Object args[]); + public native Object call(String methodName, Object args[]); /** * Evaluates a JavaScript expression. The expression is a string * of JavaScript source code which will be evaluated in the context * given by "this". */ - public native Object eval(String s); + public native Object eval(String s); /** * Converts a JSObject to a String. @@ -115,30 +132,23 @@ public final class JSObject { // should use some sort of identifier rather than String // is "property" the right word? - // native String[] listProperties(); + // native String[] listProperties(); /** * get a JSObject for the window containing the given applet */ - public static native JSObject getWindow(Applet applet); + public static native JSObject getWindow(Applet applet); /** * Finalization decrements the reference count on the corresponding * JavaScript object. */ - protected native void finalize(); + protected native void finalize(); /** * Override java.lang.Object.equals() because identity is not preserved * with instances of JSObject. */ - public boolean equals(Object obj) { - JSObject jsobj; - - if (!(obj instanceof JSObject)) - return false; - jsobj = (JSObject)obj; - return (internal == jsobj.internal); - } + public native boolean equals(Object obj); } diff --git a/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSProxy.java b/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSProxy.java index ee266c4a538..fd40bf9920b 100644 --- a/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSProxy.java +++ b/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSProxy.java @@ -1,19 +1,5 @@ -/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "NPL"); you may not use this file except in - * compliance with the NPL. You may obtain a copy of the NPL at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the NPL is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL - * for the specific language governing rights and limitations under the - * NPL. - * - * The Initial Developer of this code under the NPL is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1998 Netscape Communications Corporation. All Rights - * Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* ** */ /** diff --git a/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSRunnable.java b/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSRunnable.java index d9dae63a2e8..9788cd8d7cc 100644 --- a/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSRunnable.java +++ b/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSRunnable.java @@ -1,3 +1,7 @@ +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. + */ + package netscape.javascript; /** diff --git a/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSUtil.java b/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSUtil.java index 6d3cab84048..184f0f23ff2 100644 --- a/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSUtil.java +++ b/mozilla/js/ref/liveconnect/classes/netscape/javascript/JSUtil.java @@ -1,19 +1,5 @@ -/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "NPL"); you may not use this file except in - * compliance with the NPL. You may obtain a copy of the NPL at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the NPL is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL - * for the specific language governing rights and limitations under the - * NPL. - * - * The Initial Developer of this code under the NPL is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1998 Netscape Communications Corporation. All Rights - * Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* ** */ diff --git a/mozilla/js/ref/liveconnect/config/SunOS5.5.1.mk b/mozilla/js/ref/liveconnect/config/SunOS5.5.1.mk new file mode 100644 index 00000000000..f8368e58a37 --- /dev/null +++ b/mozilla/js/ref/liveconnect/config/SunOS5.5.1.mk @@ -0,0 +1,19 @@ +# +# The contents of this file are subject to the Netscape Public License +# Version 1.0 (the "NPL"); you may not use this file except in +# compliance with the NPL. You may obtain a copy of the NPL at +# http://www.mozilla.org/NPL/ +# +# Software distributed under the NPL is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL +# for the specific language governing rights and limitations under the +# NPL. +# +# The Initial Developer of this code under the NPL is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All Rights +# Reserved. + +INCLUDES += -I$(JDK)/include/solaris + +OTHER_LIBS = -L../$(OBJDIR) -ljs $(JDK)/lib/sparc/native_threads/libjava.so diff --git a/mozilla/js/ref/liveconnect/jsj.c b/mozilla/js/ref/liveconnect/jsj.c index 1c219f7ed84..e49b3d02cf7 100644 --- a/mozilla/js/ref/liveconnect/jsj.c +++ b/mozilla/js/ref/liveconnect/jsj.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -27,10 +12,6 @@ #include #include -#include "prtypes.h" -#include "prassert.h" -#include "prprintf.h" -#include "prosdep.h" #include "jsj_private.h" /* LiveConnect internals */ #include "jsjava.h" /* LiveConnect external API */ @@ -131,6 +112,7 @@ jfieldID njJSException_filename; /* netscape.javascript.JSException.filen { \ jclass _##class = (*jEnv)->FindClass(jEnv, #qualified_name); \ if (_##class == 0) { \ + (*jEnv)->ExceptionClear(jEnv); \ report_java_initialization_error(jEnv, \ "Can't load class " #qualified_name); \ return JS_FALSE; \ @@ -261,8 +243,43 @@ init_java_VM_reflection(JSJavaVM *jsjava_vm, JNIEnv *jEnv) LOAD_FIELD_OBJ(java.lang.Void, TYPE, "Ljava/lang/Class;", jlVoid); + return JS_TRUE; } +#if XP_MAC + +/** + * Workaround for the fact that MRJ loads a different instance of the shared library. + */ + +#include "netscape_javascript_JSObject.h" + +static JSObject_RegisterNativeMethods(JNIEnv* jEnv) +{ + // Manually load the required native methods. + static JNINativeMethod nativeMethods[] = { + "initClass", "()V", (void*)&Java_netscape_javascript_JSObject_initClass, + "getMember", "(Ljava/lang/String;)Ljava/lang/Object;", (void*)&Java_netscape_javascript_JSObject_getMember, + "getSlot", "(I)Ljava/lang/Object;", (void*)&Java_netscape_javascript_JSObject_getSlot, + "setMember", "(Ljava/lang/String;Ljava/lang/Object;)V", (void*)&Java_netscape_javascript_JSObject_setMember, + "setSlot", "(ILjava/lang/Object;)V", (void*)&Java_netscape_javascript_JSObject_setSlot, + "removeMember", "(Ljava/lang/String;)V", (void*)&Java_netscape_javascript_JSObject_removeMember, + "call", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void*)&Java_netscape_javascript_JSObject_call, + "eval", "(Ljava/lang/String;)Ljava/lang/Object;", (void*)&Java_netscape_javascript_JSObject_eval, + + "toString", "()Ljava/lang/String;", (void*)&Java_netscape_javascript_JSObject_toString, + "getWindow", "(Ljava/applet/Applet;)Lnetscape/javascript/JSObject;", (void*)&Java_netscape_javascript_JSObject_getWindow, + "finalize", "()V", (void*)&Java_netscape_javascript_JSObject_finalize, + "equals", "(Ljava/lang/Object;)Z", (void*)&Java_netscape_javascript_JSObject_equals + }; + (*jEnv)->RegisterNatives(jEnv, njJSObject, nativeMethods, sizeof(nativeMethods) / sizeof(JNINativeMethod)); + + // call the initClass method, since we nailed the static initializer for testing. + Java_netscape_javascript_JSObject_initClass(jEnv, njJSObject); +} + +#endif + /* Load Netscape-specific Java extension classes, methods, and fields */ static JSBool init_netscape_java_classes(JSJavaVM *jsjava_vm, JNIEnv *jEnv) @@ -271,6 +288,10 @@ init_netscape_java_classes(JSJavaVM *jsjava_vm, JNIEnv *jEnv) LOAD_CLASS(netscape/javascript/JSException, njJSException); LOAD_CLASS(netscape/javascript/JSUtil, njJSUtil); +#if XP_MAC + JSObject_RegisterNativeMethods(jEnv); +#endif + LOAD_CONSTRUCTOR(netscape.javascript.JSObject, JSObject, "(I)V", njJSObject); LOAD_CONSTRUCTOR(netscape.javascript.JSException, @@ -307,7 +328,6 @@ JSJ_ConnectToJavaVM(JavaVM *java_vm_arg, const char *user_classpath) { JavaVM *java_vm; JSJavaVM *jsjava_vm; - const char *full_classpath; JNIEnv *jEnv; jsjava_vm = (JSJavaVM*)malloc(sizeof(JSJavaVM)); @@ -324,7 +344,25 @@ JSJ_ConnectToJavaVM(JavaVM *java_vm_arg, const char *user_classpath) free(jsjava_vm); return NULL; } - } else { + } else if ( JSJ_callbacks && JSJ_callbacks->get_java_vm != NULL ) { + char* err_msg = NULL; + java_vm = JSJ_callbacks->get_java_vm(&err_msg); + PR_ASSERT( java_vm ); + if (!java_vm) { + free(jsjava_vm); + if (err_msg) + free(err_msg); + return NULL; + } + + if ((*java_vm)->AttachCurrentThread(java_vm, &jEnv, NULL) < 0) { + jsj_LogError("Failed to attach to Java VM thread\n"); + free(jsjava_vm); + return NULL; + } + } +#ifndef OJI + else { /* No Java VM supplied, so create our own */ JDK1_1InitArgs vm_args; @@ -334,7 +372,11 @@ JSJ_ConnectToJavaVM(JavaVM *java_vm_arg, const char *user_classpath) /* Prepend the classpath argument to the default JVM classpath */ if (user_classpath) { - full_classpath = PR_smprintf("%s;%s", user_classpath, vm_args.classpath); +#ifdef XP_UNIX + const char *full_classpath = PR_smprintf("%s:%s", user_classpath, vm_args.classpath); +#else + const char *full_classpath = PR_smprintf("%s;%s", user_classpath, vm_args.classpath); +#endif if (!full_classpath) { free(jsjava_vm); return NULL; @@ -352,6 +394,7 @@ JSJ_ConnectToJavaVM(JavaVM *java_vm_arg, const char *user_classpath) /* Remember that we created the VM so that we know to destroy it later */ jsjava_vm->jsj_created_java_vm = JS_TRUE; } +#endif /* !OJI */ jsjava_vm->java_vm = java_vm; jsjava_vm->main_thread_env = jEnv; @@ -417,6 +460,9 @@ JSJ_InitJSContext(JSContext *cx, JSObject *global_obj, if (!jsj_init_JavaArray(cx, global_obj)) return JS_FALSE; + if (!jsj_init_JavaMember(cx, global_obj)) + return JS_FALSE; + return JS_TRUE; } @@ -438,6 +484,7 @@ JSJ_DisconnectFromJavaVM(JSJavaVM *jsjava_vm) { JNIEnv *jEnv; JavaVM *java_vm; + JSJavaVM *j, **jp; java_vm = jsjava_vm->java_vm; (*java_vm)->AttachCurrentThread(java_vm, &jEnv, NULL); @@ -464,6 +511,18 @@ JSJ_DisconnectFromJavaVM(JSJavaVM *jsjava_vm) UNLOAD_CLASS(netscape/javascript/JSException, njJSException); UNLOAD_CLASS(netscape/javascript/JSUtil, njJSUtil); } + + + /* Remove this VM from the list of all JSJavaVM objects. */ + for (jp = &jsjava_vm_list; j = *jp; jp = &j->next) { + if (j == jsjava_vm) { + *jp = jsjava_vm->next; + break; + } + } + PR_ASSERT(j); + + free(jsjava_vm); } static JSJavaThreadState *thread_list = NULL; @@ -515,7 +574,7 @@ find_jsjava_thread(JNIEnv *jEnv) return jsj_env; } -PR_PUBLIC_API(JSJavaThreadState *) +PR_IMPLEMENT(JSJavaThreadState *) JSJ_AttachCurrentThreadToJava(JSJavaVM *jsjava_vm, const char *name, JNIEnv **java_envp) { JNIEnv *jEnv; @@ -604,7 +663,7 @@ jsj_MapJavaThreadToJSJavaThreadState(JNIEnv *jEnv, char **errp) * before Java is invoked on that thread.) The return value is the previous * context associated with the given Java thread. */ -PR_PUBLIC_API(JSContext *) +PR_IMPLEMENT(JSContext *) JSJ_SetDefaultJSContextForJavaThread(JSContext *cx, JSJavaThreadState *jsj_env) { JSContext *old_context; @@ -613,7 +672,7 @@ JSJ_SetDefaultJSContextForJavaThread(JSContext *cx, JSJavaThreadState *jsj_env) return old_context; } -PR_PUBLIC_API(JSBool) +PR_IMPLEMENT(JSBool) JSJ_DetachCurrentThreadFromJava(JSJavaThreadState *jsj_env) { JavaVM *java_vm; @@ -649,11 +708,13 @@ JSJ_ConvertJavaObjectToJSValue(JSContext *cx, jobject java_obj, jsval *vp) if (!jEnv) return JS_FALSE; - jsj_ConvertJavaObjectToJSValue(cx, jEnv, java_obj, vp); + return jsj_ConvertJavaObjectToJSValue(cx, jEnv, java_obj, vp); } /*===========================================================================*/ +#ifndef OJI + /* The convenience functions below present a complete, but simplified LiveConnect API which is designed to handle the special case of a single Java-VM, single-threaded operation, and use of only one JSContext. */ @@ -674,7 +735,7 @@ default_map_js_context_to_jsj_thread(JSContext *cx, char **errp) /* Trivial implementation of callback function */ static JSContext * -default_map_jsj_thread_to_js_context(JSJavaThreadState *jsj_env, char **errp) +default_map_jsj_thread_to_js_context(JSJavaThreadState *jsj_env, JNIEnv *jEnv, char **errp) { return the_cx; } @@ -720,7 +781,7 @@ JSJ_SimpleInit(JSContext *cx, JSObject *global_obj, JavaVM *java_vm, const char the_jsj_thread = JSJ_AttachCurrentThreadToJava(the_jsj_vm, "main thread", &jEnv); if (!the_jsj_thread) goto error; - + JSJ_SetDefaultJSContextForJavaThread(cx, the_jsj_thread); return JS_TRUE; error: @@ -732,7 +793,7 @@ error: * Free up all LiveConnect resources. Destroy the Java VM if it was * created by LiveConnect. */ -PR_PUBLIC_API(void) +PR_IMPLEMENT(void) JSJ_SimpleShutdown() { PR_ASSERT(the_jsj_vm); @@ -743,3 +804,4 @@ JSJ_SimpleShutdown() the_jsj_thread = NULL; } +#endif /* !OJI */ diff --git a/mozilla/js/ref/liveconnect/jsj_JSObject.c b/mozilla/js/ref/liveconnect/jsj_JSObject.c index b841fff0ecd..5305e015fcd 100644 --- a/mozilla/js/ref/liveconnect/jsj_JSObject.c +++ b/mozilla/js/ref/liveconnect/jsj_JSObject.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* This file is part of the Java-vendor-neutral implementation of LiveConnect @@ -28,9 +13,6 @@ #include #include -#include "prtypes.h" -#include "prprintf.h" -#include "prassert.h" #include "jsj_private.h" #include "jsjava.h" @@ -53,6 +35,7 @@ struct CapturedJSError { /*********************** Reflection of JSObjects ****************************/ +#ifdef PRESERVE_JSOBJECT_IDENTITY /* * This is a hash table that maps from JS objects to Java objects. * It is used to ensure that the same Java object results when a JS @@ -72,11 +55,13 @@ static PRHashTable *js_obj_reflections = NULL; * read/write or * write/write access. */ static PRMonitor *js_obj_reflections_monitor = NULL; -#endif +#endif /* JS_THREADSAFE */ +#endif /* PRESERVE_JSOBJECT_IDENTITY */ static JSBool init_js_obj_reflections_table() { +#ifdef PRESERVE_JSOBJECT_IDENTITY js_obj_reflections = PR_NewHashTable(128, NULL, PR_CompareValues, PR_CompareValues, NULL, NULL); if (!js_obj_reflections) @@ -88,7 +73,8 @@ init_js_obj_reflections_table() PR_HashTableDestroy(js_obj_reflections); return JS_FALSE; } -#endif +#endif /* JS_THREADSAFE */ +#endif /* PRESERVE_JSOBJECT_IDENTITY */ return JS_TRUE; } @@ -101,6 +87,9 @@ init_js_obj_reflections_table() * * If an error occurs, returns NULL and reports an error. */ + +#ifdef PRESERVE_JSOBJECT_IDENTITY + jobject jsj_WrapJSObject(JSContext *cx, JNIEnv *jEnv, JSObject *js_obj) { @@ -115,16 +104,14 @@ jsj_WrapJSObject(JSContext *cx, JNIEnv *jEnv, JSObject *js_obj) /* First, look in the hash table for an existing reflection of the same JavaScript object. If one is found, return it. */ - hep = PR_HashTableRawLookup(js_obj_reflections, (prhashcode)js_obj, js_obj); + hep = PR_HashTableRawLookup(js_obj_reflections, (PRHashNumber)js_obj, js_obj); -#ifdef PRESERVE_JSOBJECT_IDENTITY /* If the same JSObject is reflected into Java more than once then we should return the same Java object, both for efficiency and so that the '==' operator works as expected in Java when comparing two JSObjects. However, it is not possible to hold a reference to a Java object without - inhibiting GC of that object, at least not in a way that is portable - to all vendor's JVM, i.e. a weak reference. So, for now, JSObject identity - is broken. */ + inhibiting GC of that object, at least not in a portable way, i.e. + a weak reference. So, for now, JSObject identity is broken. */ he = *hep; if (he) { @@ -133,7 +120,6 @@ jsj_WrapJSObject(JSContext *cx, JNIEnv *jEnv, JSObject *js_obj) if (java_wrapper_obj) goto done; } -#endif /* PRESERVE_JSOBJECT_IDENTITY */ /* No existing reflection found, so create a new Java object that wraps the JavaScript object by storing its address in a private integer field. */ @@ -146,7 +132,7 @@ jsj_WrapJSObject(JSContext *cx, JNIEnv *jEnv, JSObject *js_obj) } /* Add the new reflection to the hash table. */ - he = PR_HashTableRawAdd(js_obj_reflections, hep, (prhashcode)js_obj, + he = PR_HashTableRawAdd(js_obj_reflections, hep, (PRHashNumber)js_obj, js_obj, java_wrapper_obj); if (he) { /* Tell the JavaScript GC about this object since the only reference @@ -188,7 +174,7 @@ remove_js_obj_reflection_from_hashtable(JSContext *cx, JSObject *js_obj) #endif /* Get the hash-table entry for this wrapper object */ - hep = PR_HashTableRawLookup(js_obj_reflections, (prhashcode)js_obj, js_obj); + hep = PR_HashTableRawLookup(js_obj_reflections, (PRHashNumber)js_obj, js_obj); he = *hep; PR_ASSERT(he); @@ -207,6 +193,64 @@ remove_js_obj_reflection_from_hashtable(JSContext *cx, JSObject *js_obj) return success; } +#else /* !PRESERVE_JSOBJECT_IDENTITY */ + +/* This object provides is the "anchor" by which netscape.javscript.JSObject + objects hold a reference to native JSObjects. */ +typedef struct JSObjectRoot { + JSObject *js_obj; + JSContext *cx; /* Creating context, needed for finalization */ +} JSObjectHandle; + +/* + * The caller must call DeleteLocalRef() on the returned object when no more + * references remain. + */ +jobject +jsj_WrapJSObject(JSContext *cx, JNIEnv *jEnv, JSObject *js_obj) +{ + jobject java_wrapper_obj; + JSObjectHandle *handle; + + /* Create a tiny stub object to act as the GC root that points to the + JSObject from its netscape.javascript.JSObject counterpart. */ + handle = (JSObjectHandle*)JS_malloc(cx, sizeof(JSObjectHandle)); + if (!handle) + return NULL; + handle->js_obj = js_obj; + handle->cx = cx; + + /* No existing reflection found, so create a new Java object that wraps + the JavaScript object by storing its address in a private integer field. */ + java_wrapper_obj = + (*jEnv)->NewObject(jEnv, njJSObject, njJSObject_JSObject, (jint)handle); + if (!java_wrapper_obj) { + jsj_UnexpectedJavaError(cx, jEnv, "Couldn't create new instance of " + "netscape.javascript.JSObject"); + goto done; + } + + JS_AddRoot(cx, &handle->js_obj); + +done: + + return java_wrapper_obj; +} + +JSObject * +jsj_UnwrapJSObjectWrapper(JNIEnv *jEnv, jobject java_wrapper_obj) +{ + JSObjectHandle *handle; + + handle = (JSObjectHandle*)((*jEnv)->GetIntField(jEnv, java_wrapper_obj, njJSObject_internal)); + PR_ASSERT(handle); + if (!handle) + return NULL; + return handle->js_obj; +} + +#endif /* !PRESERVE_JSOBJECT_IDENTITY */ + /*************** Handling of Java exceptions in JavaScript ******************/ /* @@ -498,7 +542,6 @@ done: } - /*************** Utilities for calling JavaScript from Java *****************/ /* @@ -523,14 +566,19 @@ enter_js(JNIEnv *jEnv, jobject java_wrapper_obj, /* Invoke callback, presumably used to implement concurrency constraints */ if (JSJ_callbacks->enter_js_from_java) { - if (!JSJ_callbacks->enter_js_from_java(&err_msg)) + if (!JSJ_callbacks->enter_js_from_java(jEnv, &err_msg)) goto entry_failure; } - /* Check the JSObject pointer in the wrapper object for null while holding - the JS lock to deal with shutdown issues. */ + /* Check the JSObject pointer in the wrapper object. */ if (js_objp) { + +#ifdef PRESERVE_JSOBJECT_IDENTITY js_obj = (JSObject *)((*jEnv)->GetIntField(jEnv, java_wrapper_obj, njJSObject_internal)); +#else /* !PRESERVE_JSOBJECT_IDENTITY */ + js_obj = jsj_UnwrapJSObjectWrapper(jEnv, java_wrapper_obj); +#endif /* PRESERVE_JSOBJECT_IDENTITY */ + PR_ASSERT(js_obj); if (!js_obj) goto error; @@ -549,7 +597,7 @@ enter_js(JNIEnv *jEnv, jobject java_wrapper_obj, Java and back into JS. Invoke a callback to obtain/create a JSContext for us to use. */ if (JSJ_callbacks->map_jsj_thread_to_js_context) { - cx = JSJ_callbacks->map_jsj_thread_to_js_context(jsj_env, &err_msg); + cx = JSJ_callbacks->map_jsj_thread_to_js_context(jsj_env, jEnv, &err_msg); if (!cx) goto error; } else { @@ -572,7 +620,7 @@ enter_js(JNIEnv *jEnv, jobject java_wrapper_obj, error: /* Invoke callback, presumably used to implement concurrency constraints */ if (JSJ_callbacks->exit_js) - JSJ_callbacks->exit_js(); + JSJ_callbacks->exit_js(jEnv); entry_failure: if (err_msg) { @@ -617,7 +665,7 @@ exit_js(JSContext *cx, JSJavaThreadState *jsj_env, JSErrorReporter original_repo /* Invoke callback, presumably used to implement concurrency constraints */ if (JSJ_callbacks->exit_js) - JSJ_callbacks->exit_js(); + JSJ_callbacks->exit_js(jEnv); return JS_TRUE; } @@ -680,7 +728,7 @@ Java_netscape_javascript_JSObject_getMember(JNIEnv *jEnv, property_name_ucs2 = NULL; if (!property_name_jstr) { - JS_ReportError(cx, "illegal null member name"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_NULL_MEMBER_NAME); member = NULL; goto done; } @@ -771,7 +819,7 @@ Java_netscape_javascript_JSObject_setMember(JNIEnv *jEnv, property_name_ucs2 = NULL; if (!property_name_jstr) { - JS_ReportError(cx, "illegal null member name"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_NULL_MEMBER_NAME); goto done; } @@ -847,7 +895,7 @@ Java_netscape_javascript_JSObject_removeMember(JNIEnv *jEnv, return; if (!property_name_jstr) { - JS_ReportError(cx, "illegal null member name"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_NULL_MEMBER_NAME); goto done; } /* Get the Unicode string for the JS property name */ @@ -897,7 +945,8 @@ Java_netscape_javascript_JSObject_call(JNIEnv *jEnv, jobject java_wrapper_obj, function_name_ucs2 = NULL; result = NULL; if (!function_name_jstr) { - JS_ReportError(cx, "illegal null JavaScript function name"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_NULL_FUNCTION_NAME); goto done; } @@ -985,7 +1034,7 @@ Java_netscape_javascript_JSObject_eval(JNIEnv *jEnv, result = NULL; eval_ucs2 = NULL; if (!eval_jstr) { - JS_ReportError(cx, "illegal null string eval argument"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_NULL_EVAL_ARG); goto done; } @@ -1108,15 +1157,49 @@ done: JNIEXPORT void JNICALL Java_netscape_javascript_JSObject_finalize(JNIEnv *jEnv, jobject java_wrapper_obj) { + JSBool success; JSContext *cx; - JSErrorReporter saved_reporter; - JSJavaThreadState *jsj_env; - JSObject *js_obj; - - jsj_env = enter_js(jEnv, java_wrapper_obj, &cx, &js_obj, &saved_reporter); - if (!jsj_env) /* Note: memory leak if we exit here */ + JSObjectHandle *handle; + + success = JS_FALSE; + + handle = (JSObjectHandle *)((*jEnv)->GetIntField(jEnv, java_wrapper_obj, njJSObject_internal)); + PR_ASSERT(handle); + if (!handle) return; - remove_js_obj_reflection_from_hashtable(cx, js_obj); - exit_js(cx, jsj_env, saved_reporter); + cx = handle->cx; + + success = JS_RemoveRoot(cx, &handle->js_obj); + JS_free(cx, handle); + + PR_ASSERT(success); } +/* + * Class: netscape_javascript_JSObject + * Method: equals + * Signature: (Ljava/lang/Object;)Z + */ + +JNIEXPORT jboolean JNICALL +Java_netscape_javascript_JSObject_equals(JNIEnv *jEnv, + jobject java_wrapper_obj, + jobject comparison_obj) +{ +#ifdef PRESERVE_JSOBJECT_IDENTITY +# error "Missing code should be added here" +#else + JSObject *js_obj1, *js_obj2; + + /* Check that we're comparing with another netscape.javascript.JSObject */ + if (!comparison_obj) + return 0; + if (!(*jEnv)->IsInstanceOf(jEnv, comparison_obj, njJSObject)) + return 0; + + js_obj1 = jsj_UnwrapJSObjectWrapper(jEnv, java_wrapper_obj); + js_obj2 = jsj_UnwrapJSObjectWrapper(jEnv, comparison_obj); + + return (js_obj1 == js_obj2); +#endif /* PRESERVE_JSOBJECT_IDENTITY */ +} diff --git a/mozilla/js/ref/liveconnect/jsj_JavaArray.c b/mozilla/js/ref/liveconnect/jsj_JavaArray.c index c4914ad835f..8b4c41c4bd7 100644 --- a/mozilla/js/ref/liveconnect/jsj_JavaArray.c +++ b/mozilla/js/ref/liveconnect/jsj_JavaArray.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -26,13 +11,9 @@ #include #include -#include "prassert.h" #include "jsj_private.h" /* LiveConnect internals */ - -/* Shorthands for ASCII (7-bit) decimal and hex conversion. */ -#define JS7_ISDEC(c) (((c) >= '0') && ((c) <= '9')) -#define JS7_UNDEC(c) ((c) - '0') +#include "jscntxt.h" /* for error reporting */ /* * Convert any jsval v to an integer jsval if ToString(v) @@ -99,7 +80,7 @@ access_java_array_element(JSContext *cx, return JS_TRUE; } } - JS_ReportError(cx, "illegal operation on JavaArray prototype object"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_BAD_OP_JARRAY); return JS_FALSE; } class_descriptor = java_wrapper->class_descriptor; @@ -128,8 +109,8 @@ access_java_array_element(JSContext *cx, if (!JSVERSION_IS_ECMA(version)) { - JS_ReportError(cx, "Attempt to write to invalid Java array " - "element \"%s\"", member_name); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_CANT_WRITE_JARRAY, member_name); return JS_FALSE; } else { *vp = JSVAL_VOID; @@ -150,7 +131,7 @@ access_java_array_element(JSContext *cx, } } - JS_ReportError(cx, "invalid Java array index expression"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_BAD_INDEX_EXPR); return JS_FALSE; } @@ -163,7 +144,10 @@ access_java_array_element(JSContext *cx, /* Just let Java throw an exception instead of checking array bounds here */ if (index < 0 || index >= array_length) { - JS_ReportError(cx, "Java array index %d out of range", index); + char numBuf[12]; + sprintf(numBuf, "%d", index); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_BAD_JARRAY_INDEX, numBuf); return JS_FALSE; } #endif @@ -223,7 +207,7 @@ JavaArray_defineProperty(JSContext *cx, JSObject *obj, jsid id, jsval value, JSPropertyOp getter, JSPropertyOp setter, uintN attrs, JSProperty **propp) { - JS_ReportError(cx, "Cannot define a new property in a JavaArray"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_JARRAY_PROP_DEFINE); return JS_FALSE; } @@ -241,7 +225,7 @@ JavaArray_setAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop, uintN *attrsp) { /* We don't maintain JS property attributes for Java class members */ - if (*attrsp != JSPROP_PERMANENT|JSPROP_ENUMERATE) { + if (*attrsp != (JSPROP_PERMANENT|JSPROP_ENUMERATE)) { PR_ASSERT(0); return JS_FALSE; } @@ -258,7 +242,7 @@ JavaArray_deleteProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp) *vp = JSVAL_FALSE; if (!JSVERSION_IS_ECMA(version)) { - JS_ReportError(cx, "Properties of JavaArray objects may not be deleted"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_JARRAY_PROP_DELETE); return JS_FALSE; } else { /* Attempts to delete permanent properties are silently ignored @@ -335,11 +319,11 @@ JavaArray_checkAccess(JSContext *cx, JSObject *obj, jsid id, { switch (mode) { case JSACC_WATCH: - JS_ReportError(cx, "Cannot place watchpoints on JavaArray object properties"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_JARRAY_PROP_WATCH); return JS_FALSE; case JSACC_IMPORT: - JS_ReportError(cx, "Cannot export a JavaArray object's properties"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_JARRAY_PROP_EXPORT); return JS_FALSE; default: @@ -384,7 +368,7 @@ JSClass JavaArray_class = { JavaArray_getObjectOps, }; -extern PR_IMPORT_DATA(JSObjectOps) js_ObjectOps; +extern JS_FRIEND_DATA(JSObjectOps) js_ObjectOps; /* Initialize the JS JavaArray class */ diff --git a/mozilla/js/ref/liveconnect/jsj_JavaClass.c b/mozilla/js/ref/liveconnect/jsj_JavaClass.c index 25d1fb6030b..80af7393382 100644 --- a/mozilla/js/ref/liveconnect/jsj_JavaClass.c +++ b/mozilla/js/ref/liveconnect/jsj_JavaClass.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* This file is part of the Java-vendor-neutral implementation of LiveConnect @@ -36,11 +21,8 @@ #include #include -#include "prtypes.h" -#include "prprintf.h" -#include "prassert.h" - #include "jsj_private.h" /* LiveConnect internals */ +#include "jscntxt.h" /* for error reporting */ static JSBool JavaClass_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp) @@ -79,8 +61,9 @@ JavaClass_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp) return JS_TRUE; default: - return JS_TRUE; + break; } + return JS_TRUE; } static JSBool @@ -107,9 +90,9 @@ lookup_static_member_by_id(JSContext *cx, JNIEnv *jEnv, JSObject *obj, if (!member_descriptor) { JS_IdToValue(cx, id, &idval); if (!JSVAL_IS_STRING(idval)) { - JS_ReportError(cx, "invalid JavaClass property expression. " - "(methods and fields of a JavaClass object can only be identified by their name)"); - return JS_FALSE; + JS_ReportErrorNumber(cx, + jsj_GetErrorMessage, JSJMSG_BAD_JCLASS_EXPR); + return JS_FALSE; } member_name = JS_GetStringBytes(JSVAL_TO_STRING(idval)); @@ -120,7 +103,7 @@ lookup_static_member_by_id(JSContext *cx, JNIEnv *jEnv, JSObject *obj, return JS_TRUE; } - JS_ReportError(cx, "Java class %s has no public static field or method named \"%s\"", + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_MISSING_NAME, class_descriptor->name, member_name); return JS_FALSE; } @@ -173,8 +156,8 @@ JavaClass_getPropertyById(JSContext *cx, JSObject *obj, jsid id, jsval *vp) return JS_FALSE; *vp = OBJECT_TO_JSVAL(JS_GetFunctionObject(function)); - return JS_TRUE; } + return JS_TRUE; } PR_STATIC_CALLBACK(JSBool) @@ -212,7 +195,7 @@ JavaClass_setPropertyById(JSContext *cx, JSObject *obj, jsid id, jsval *vp) no_such_field: JS_IdToValue(cx, id, &idval); member_name = JS_GetStringBytes(JSVAL_TO_STRING(idval)); - JS_ReportError(cx, "No static field named \"%s\" in Java class %s", + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_MISSING_STATIC, member_name, class_descriptor->name); return JS_FALSE; } @@ -234,7 +217,7 @@ JavaClass_finalize(JSContext *cx, JSObject *obj) if (!jEnv) return; - printf("Finalizing %s\n", class_descriptor->name); + /* printf("Finalizing %s\n", class_descriptor->name); */ jsj_ReleaseJavaClassDescriptor(cx, jEnv, class_descriptor); } @@ -248,18 +231,25 @@ JavaClass_lookupProperty(JSContext *cx, JSObject *obj, jsid id, ) { JNIEnv *jEnv; + JSErrorReporter old_reporter; /* printf("In JavaClass_lookupProperty()\n"); */ - + /* Get the Java per-thread environment pointer for this JSContext */ jsj_MapJSContextToJSJThread(cx, &jEnv); if (!jEnv) return JS_FALSE; - if (!lookup_static_member_by_id(cx, jEnv, obj, NULL, id, NULL)) - return JS_FALSE; - *objp = obj; - *propp = (JSProperty*)1; + old_reporter = JS_SetErrorReporter(cx, NULL); + if (lookup_static_member_by_id(cx, jEnv, obj, NULL, id, NULL)) { + *objp = obj; + *propp = (JSProperty*)1; + } else { + *objp = NULL; + *propp = NULL; + } + + JS_SetErrorReporter(cx, old_reporter); return JS_TRUE; } @@ -268,7 +258,7 @@ JavaClass_defineProperty(JSContext *cx, JSObject *obj, jsid id, jsval value, JSPropertyOp getter, JSPropertyOp setter, uintN attrs, JSProperty **propp) { - JS_ReportError(cx, "Cannot define a new property in a JavaClass"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_JCLASS_PROP_DEFINE); return JS_FALSE; } @@ -286,7 +276,7 @@ JavaClass_setAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop, uintN *attrsp) { /* We don't maintain JS property attributes for Java class members */ - if (*attrsp != JSPROP_PERMANENT|JSPROP_ENUMERATE) { + if (*attrsp != (JSPROP_PERMANENT|JSPROP_ENUMERATE)) { PR_ASSERT(0); return JS_FALSE; } @@ -303,7 +293,8 @@ JavaClass_deleteProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp) *vp = JSVAL_FALSE; if (!JSVERSION_IS_ECMA(version)) { - JS_ReportError(cx, "Properties of JavaClass objects may not be deleted"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_JCLASS_PROP_DELETE); return JS_FALSE; } else { /* Attempts to delete permanent properties are silently ignored @@ -374,11 +365,13 @@ JavaClass_checkAccess(JSContext *cx, JSObject *obj, jsid id, { switch (mode) { case JSACC_WATCH: - JS_ReportError(cx, "Cannot place watchpoints on JavaClass object properties"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_JCLASS_PROP_WATCH); return JS_FALSE; case JSACC_IMPORT: - JS_ReportError(cx, "Cannot export a JavaClass object's properties"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_JCLASS_PROP_EXPORT); return JS_FALSE; default: @@ -406,7 +399,7 @@ JavaClass_hasInstance(JSContext *cx, JSObject *obj, jsval candidate_jsval, has_instance = JS_FALSE; class_descriptor = JS_GetPrivate(cx, obj); if (!class_descriptor) { - JS_ReportError(cx, "illegal operation on JavaClass prototype object"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_BAD_OP_JCLASS); return JS_FALSE; } @@ -424,7 +417,7 @@ JavaClass_hasInstance(JSContext *cx, JSObject *obj, jsval candidate_jsval, java_class = class_descriptor->java_class; java_wrapper = JS_GetPrivate(cx, candidate_obj); if (!java_wrapper) { - JS_ReportError(cx, "illegal operation on prototype object"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_BAD_OP_PROTO); return JS_FALSE; } java_obj = java_wrapper->java_obj; @@ -538,17 +531,17 @@ getClass(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) return JS_FALSE; if (argc != 1 || - !JSVAL_IS_OBJECT(argv[0]) || - !(obj_arg = JSVAL_TO_OBJECT(argv[0])) || - (!JS_InstanceOf(cx, obj_arg, &JavaObject_class, 0) && + !JSVAL_IS_OBJECT(argv[0]) || + !(obj_arg = JSVAL_TO_OBJECT(argv[0])) || + (!JS_InstanceOf(cx, obj_arg, &JavaObject_class, 0) && !JS_InstanceOf(cx, obj_arg, &JavaArray_class, 0))) { - JS_ReportError(cx, "getClass expects a Java object argument"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_NEED_JOBJECT_ARG); return JS_FALSE; } java_wrapper = JS_GetPrivate(cx, obj_arg); if (!java_wrapper) { - JS_ReportError(cx, "getClass called on prototype object"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_PROTO_GETCLASS); return JS_FALSE; } @@ -561,7 +554,7 @@ getClass(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) return JS_TRUE; } -extern PR_IMPORT_DATA(JSObjectOps) js_ObjectOps; +extern JS_FRIEND_DATA(JSObjectOps) js_ObjectOps; JSBool jsj_init_JavaClass(JSContext *cx, JSObject *global_obj) diff --git a/mozilla/js/ref/liveconnect/jsj_JavaObject.c b/mozilla/js/ref/liveconnect/jsj_JavaObject.c index 2d6e52adaec..18db6f69559 100644 --- a/mozilla/js/ref/liveconnect/jsj_JavaObject.c +++ b/mozilla/js/ref/liveconnect/jsj_JavaObject.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -28,10 +13,10 @@ #include #include -#include "prassert.h" #include "jsj_private.h" /* LiveConnect internals */ #include "jsj_hash.h" /* Hash table with Java object as key */ +#include "jscntxt.h" /* for error reporting */ /* @@ -229,9 +214,13 @@ enumerate_remove_java_obj(JSJHashEntry *he, PRIntn i, void *arg) void jsj_DiscardJavaObjReflections(JNIEnv *jEnv) { - JSJ_HashTableEnumerateEntries(java_obj_reflections, - enumerate_remove_java_obj, - (void*)jEnv); + if (java_obj_reflections) { + JSJ_HashTableEnumerateEntries(java_obj_reflections, + enumerate_remove_java_obj, + (void*)jEnv); + JSJ_HashTableDestroy(java_obj_reflections); + java_obj_reflections = NULL; + } } PR_CALLBACK JSBool @@ -254,7 +243,7 @@ JavaObject_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp) return JS_TRUE; } - JS_ReportError(cx, "illegal operation on JavaObject prototype object"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_BAD_OP_JOBJECT); return JS_FALSE; } @@ -267,7 +256,7 @@ JavaObject_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp) return JS_TRUE; case JSTYPE_FUNCTION: - JS_ReportError(cx, "can't convert Java object to function"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_CONVERT_TO_FUNC); return JS_FALSE; case JSTYPE_VOID: @@ -278,11 +267,11 @@ JavaObject_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp) case JSTYPE_NUMBER: /* Call Java doubleValue() method, if applicable */ - return jsj_ConvertJavaObjectToJSNumber(cx, jEnv, java_obj, vp); + return jsj_ConvertJavaObjectToJSNumber(cx, jEnv, class_descriptor, java_obj, vp); case JSTYPE_BOOLEAN: /* Call booleanValue() method, if applicable */ - return jsj_ConvertJavaObjectToJSBoolean(cx, jEnv, java_obj, vp); + return jsj_ConvertJavaObjectToJSBoolean(cx, jEnv, class_descriptor, java_obj, vp); default: PR_ASSERT(0); @@ -312,7 +301,7 @@ lookup_member_by_id(JSContext *cx, JNIEnv *jEnv, JSObject *obj, return JS_TRUE; } } - JS_ReportError(cx, "illegal operation on JavaObject prototype object"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_BAD_OP_JOBJECT); return JS_FALSE; } @@ -324,16 +313,16 @@ lookup_member_by_id(JSContext *cx, JNIEnv *jEnv, JSObject *obj, if (!member_descriptor) { JS_IdToValue(cx, id, &idval); if (!JSVAL_IS_STRING(idval)) { - JS_ReportError(cx, "invalid JavaObject property expression. " - "(methods and field properties of a JavaObject object can only be strings)"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_BAD_JOBJECT_EXPR); return JS_FALSE; } member_name = JS_GetStringBytes(JSVAL_TO_STRING(idval)); - JS_ReportError(cx, "Java class %s has no public instance field or " - "method named \"%s\"", - class_descriptor->name, member_name); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_NO_INSTANCE_NAME, + class_descriptor->name, member_name); return JS_FALSE; } @@ -353,6 +342,8 @@ JavaObject_getPropertyById(JSContext *cx, JSObject *obj, jsid id, jsval *vp) JavaObjectWrapper *java_wrapper; JNIEnv *jEnv; JSObject *funobj; + jsval field_val, method_val; + JSBool success; /* printf("In JavaObject_getProperty\n"); */ @@ -372,22 +363,56 @@ JavaObject_getPropertyById(JSContext *cx, JSObject *obj, jsid id, jsval *vp) } java_obj = java_wrapper->java_obj; + field_val = method_val = JSVAL_VOID; + + /* If a field member, get the value of the field */ if (member_descriptor->field) { - if (!member_descriptor->methods) { - return jsj_GetJavaFieldValue(cx, jEnv, member_descriptor->field, java_obj, vp); - } else { - PR_ASSERT(0); - } - } else { + success = jsj_GetJavaFieldValue(cx, jEnv, member_descriptor->field, java_obj, &field_val); + if (!success) + return JS_FALSE; + } + + /* If a method member, build a wrapper around the Java method */ + if (member_descriptor->methods) { /* Create a function object with this JavaObject as its parent, so that JSFUN_BOUND_METHOD binds it as the default 'this' for the function. */ funobj = JS_CloneFunctionObject(cx, member_descriptor->invoke_func_obj, obj); - if (funobj) { - *vp = OBJECT_TO_JSVAL(funobj); - return JS_TRUE; - } - return JS_FALSE; + if (!funobj) + return JS_FALSE; + method_val = OBJECT_TO_JSVAL(funobj); } + +#if TEST_JAVAMEMBER + /* Always create a JavaMember object, even though it's inefficient */ + obj = jsj_CreateJavaMember(cx, method_val, field_val); + if (!obj) + return JS_FALSE; + *vp = OBJECT_TO_JSVAL(obj); +#else /* !TEST_JAVAMEMBER */ + + if (member_descriptor->field) { + if (!member_descriptor->methods) { + /* Return value of Java field */ + *vp = field_val; + } else { + /* Handle special case of access to a property that could refer + to either a Java field or a method that share the same name. + In Java, such ambiguity is not possible because the compiler + can statically determine which is being accessed. */ + obj = jsj_CreateJavaMember(cx, method_val, field_val); + if (!obj) + return JS_FALSE; + *vp = OBJECT_TO_JSVAL(obj); + } + + } else { + /* Return wrapper around Java method */ + *vp = method_val; + } + +#endif /* !TEST_JAVAMEMBER */ + + return JS_TRUE; } PR_STATIC_CALLBACK(JSBool) @@ -427,43 +452,11 @@ no_such_field: JS_IdToValue(cx, id, &idval); member_name = JS_GetStringBytes(JSVAL_TO_STRING(idval)); class_descriptor = java_wrapper->class_descriptor; - JS_ReportError(cx, "No instance field named \"%s\" in Java class %s", + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_NO_NAME_IN_CLASS, member_name, class_descriptor->name); return JS_FALSE; } -PR_CALLBACK JSBool -JavaObject_enumerate(JSContext *cx, JSObject *obj) -{ - JavaObjectWrapper *java_wrapper; - JavaClassDescriptor *class_descriptor; - JavaMemberDescriptor *member_descriptor; - JNIEnv *jEnv; - -/* printf("In JavaObject_enumerate\n"); */ - - java_wrapper = JS_GetPrivate(cx, obj); - - /* Check if this is the prototype object */ - if (!java_wrapper) - return JS_TRUE; - - /* Get the Java per-thread environment pointer for this JSContext */ - jsj_MapJSContextToJSJThread(cx, &jEnv); - if (!jEnv) - return JS_FALSE; - - class_descriptor = java_wrapper->class_descriptor; - member_descriptor = jsj_GetClassInstanceMembers(cx, jEnv, class_descriptor); - while (member_descriptor) { - JS_DefineProperty(cx, obj, member_descriptor->name, JSVAL_VOID, 0, 0, - JSPROP_PERMANENT|JSPROP_ENUMERATE); - member_descriptor = member_descriptor->next; - } - return JS_TRUE; -} - - static JSBool JavaObject_lookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp, JSProperty **propp @@ -473,6 +466,7 @@ JavaObject_lookupProperty(JSContext *cx, JSObject *obj, jsid id, ) { JNIEnv *jEnv; + JSErrorReporter old_reporter; /* printf("In JavaObject_lookupProperty()\n"); */ @@ -481,10 +475,16 @@ JavaObject_lookupProperty(JSContext *cx, JSObject *obj, jsid id, if (!jEnv) return JS_FALSE; - if (!lookup_member_by_id(cx, jEnv, obj, NULL, id, NULL)) - return JS_FALSE; - *objp = obj; - *propp = (JSProperty*)1; + old_reporter = JS_SetErrorReporter(cx, NULL); + if (lookup_member_by_id(cx, jEnv, obj, NULL, id, NULL)) { + *objp = obj; + *propp = (JSProperty*)1; + } else { + *objp = NULL; + *propp = NULL; + } + + JS_SetErrorReporter(cx, old_reporter); return JS_TRUE; } @@ -493,7 +493,7 @@ JavaObject_defineProperty(JSContext *cx, JSObject *obj, jsid id, jsval value, JSPropertyOp getter, JSPropertyOp setter, uintN attrs, JSProperty **propp) { - JS_ReportError(cx, "Cannot define a new property in a JavaObject"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_JOBJECT_PROP_DEFINE); return JS_FALSE; } @@ -511,7 +511,7 @@ JavaObject_setAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop, uintN *attrsp) { /* We don't maintain JS property attributes for Java class members */ - if (*attrsp != JSPROP_PERMANENT|JSPROP_ENUMERATE) { + if (*attrsp != (JSPROP_PERMANENT|JSPROP_ENUMERATE)) { PR_ASSERT(0); return JS_FALSE; } @@ -528,7 +528,8 @@ JavaObject_deleteProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp) *vp = JSVAL_FALSE; if (!JSVERSION_IS_ECMA(version)) { - JS_ReportError(cx, "Properties of JavaObject objects may not be deleted"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_JOBJECT_PROP_DELETE); return JS_FALSE; } else { /* Attempts to delete permanent properties are silently ignored @@ -541,7 +542,7 @@ static JSBool JavaObject_defaultValue(JSContext *cx, JSObject *obj, JSType type, jsval *vp) { /* printf("In JavaObject_defaultValue()\n"); */ - return JavaObject_convert(cx, obj, JSTYPE_STRING, vp); + return JavaObject_convert(cx, obj, type, vp); } static JSBool @@ -604,11 +605,13 @@ JavaObject_checkAccess(JSContext *cx, JSObject *obj, jsid id, { switch (mode) { case JSACC_WATCH: - JS_ReportError(cx, "Cannot place watchpoints on JavaObject object properties"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_JOBJECT_PROP_WATCH); return JS_FALSE; case JSACC_IMPORT: - JS_ReportError(cx, "Cannot export a JavaObject object's properties"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_JOBJECT_PROP_EXPORT); return JS_FALSE; default: @@ -653,7 +656,7 @@ JSClass JavaObject_class = { JavaObject_getObjectOps, }; -extern PR_IMPORT_DATA(JSObjectOps) js_ObjectOps; +extern JS_FRIEND_DATA(JSObjectOps) js_ObjectOps; JSBool diff --git a/mozilla/js/ref/liveconnect/jsj_JavaPackage.c b/mozilla/js/ref/liveconnect/jsj_JavaPackage.c index 7a11dcd9b6c..0e36d12dada 100644 --- a/mozilla/js/ref/liveconnect/jsj_JavaPackage.c +++ b/mozilla/js/ref/liveconnect/jsj_JavaPackage.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* This file is part of the Java-vendor-neutral implementation of LiveConnect @@ -36,12 +21,9 @@ #include #include -#include "prtypes.h" -#include "prprintf.h" -#include "prosdep.h" - #include "jsj_private.h" /* LiveConnect internals */ #include "jsjava.h" +#include "jscntxt.h" /* for error reporting */ JSClass JavaPackage_class; /* Forward declaration */ @@ -99,11 +81,11 @@ JavaPackage_setProperty(JSContext *cx, JSObject *obj, jsval slot, jsval *vp) { JavaPackage_Private *package = JS_GetPrivate(cx, obj); if (!package) { - JS_ReportError(cx, "illegal attempt to add property to " - "JavaPackage prototype object"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_BAD_ADD_TO_PACKAGE); return JS_FALSE; } - JS_ReportError(cx, "You may not add properties to a JavaPackage object"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_DONT_ADD_TO_PACKAGE); return JS_FALSE; } @@ -121,13 +103,17 @@ JavaPackage_resolve(JSContext *cx, JSObject *obj, jsval id) char *subPath, *newPath; const char *path; JNIEnv *jEnv; - + + /* Painful hack for pre_define_java_packages() */ + if (quiet_resolve_failure) + return JS_FALSE; + package = (JavaPackage_Private *)JS_GetPrivate(cx, obj); if (!package) return JS_TRUE; if (!JSVAL_IS_STRING(id)) - return JS_TRUE; + return JS_TRUE; subPath = JS_GetStringBytes(JSVAL_TO_STRING(id)); /* @@ -186,6 +172,11 @@ JavaPackage_resolve(JSContext *cx, JSObject *obj, jsval id) } } else { + /* We assume that any failed attempt to load a class is because it + doesn't exist. If we wanted to do a better job, we would check + the exception type and make sure that it's NoClassDefFoundError */ + (*jEnv)->ExceptionClear(jEnv); + /* * If there's no class of the given name, then we must be referring to * a package. However, don't allow bogus sub-packages of pre-defined @@ -197,14 +188,12 @@ JavaPackage_resolve(JSContext *cx, JSObject *obj, jsval id) package = JS_GetPrivate(cx, obj); if (package->flags & PKG_SYSTEM) { char *msg, *cp; - - /* Painful hack for pre_define_java_packages() */ - if (quiet_resolve_failure) - return JS_FALSE; - +/* msg = PR_smprintf("No Java system package with name \"%s\" was identified " "and no Java class with that name exists either", newPath); +*/ + msg = JS_strdup(cx, newPath); /* Check for OOM */ if (msg) { @@ -212,10 +201,10 @@ JavaPackage_resolve(JSContext *cx, JSObject *obj, jsval id) for (cp = msg; *cp != '\0'; cp++) if (*cp == '/') *cp = '.'; - JS_ReportError(cx, msg); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_MISSING_PACKAGE, msg); free((char*)msg); } - return JS_FALSE; } } @@ -357,7 +346,8 @@ standard_java_packages[] = { {"sun", NULL, PKG_USER}, {"Packages", "", PKG_USER}, - 0 + + {NULL, NULL, 0} }; /* @@ -394,7 +384,8 @@ pre_define_java_packages(JSContext *cx, JSObject *global_obj, jsval v; if (!simple_name) { - JS_ReportError(cx, "Package %s defined twice ?", package_name); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_DOUBLE_SHIPPING, package_name); goto error; } @@ -410,7 +401,8 @@ pre_define_java_packages(JSContext *cx, JSObject *global_obj, /* New package objects should only be created at the terminal sub-package in a fully-qualified package-name */ if (strtok(NULL, ".")) { - JS_ReportError(cx, "Illegal predefined package definition for %s", + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_BAD_PACKAGE_PREDEF, package_def->name); goto error; } @@ -458,7 +450,7 @@ error: static JSBool JavaPackage_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, - jsval *rval) + jsval *rval) { if (!JS_InstanceOf(cx, obj, &JavaPackage_class, argv)) return JS_FALSE; diff --git a/mozilla/js/ref/liveconnect/jsj_array.c b/mozilla/js/ref/liveconnect/jsj_array.c index 82af588e160..28274eadd8d 100644 --- a/mozilla/js/ref/liveconnect/jsj_array.c +++ b/mozilla/js/ref/liveconnect/jsj_array.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -23,9 +8,6 @@ * It contains the code for reading and writing elements of a Java array. */ -#include "prtypes.h" -#include "prassert.h" - #include "jsj_private.h" /* LiveConnect internals */ /* diff --git a/mozilla/js/ref/liveconnect/jsj_class.c b/mozilla/js/ref/liveconnect/jsj_class.c index c4da8678b84..94bb293b5c0 100644 --- a/mozilla/js/ref/liveconnect/jsj_class.c +++ b/mozilla/js/ref/liveconnect/jsj_class.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -30,12 +15,7 @@ #include #include -#include "prtypes.h" -#include "prprintf.h" -#include "prassert.h" - #include "jsj_private.h" /* LiveConnect internals */ - #include "jsj_hash.h" /* Hash tables */ /* A one-to-one mapping between all referenced java.lang.Class objects and @@ -301,9 +281,9 @@ destroy_class_descriptor(JSContext *cx, JNIEnv *jEnv, JavaClassDescriptor *class { JS_FREE_IF(cx, (char *)class_descriptor->name); if (class_descriptor->java_class) { - (*jEnv)->DeleteGlobalRef(jEnv, class_descriptor->java_class); JSJ_HashTableRemove(java_class_reflections, class_descriptor->java_class, (void*)jEnv); + (*jEnv)->DeleteGlobalRef(jEnv, class_descriptor->java_class); } if (class_descriptor->array_component_signature) @@ -331,7 +311,7 @@ new_class_descriptor(JSContext *cx, JNIEnv *jEnv, jclass java_class) java_class = (*jEnv)->NewGlobalRef(jEnv, java_class); if (!java_class) { - jsj_ReportJavaError(cx, jEnv, "Unable to reference Java class"); + jsj_UnexpectedJavaError(cx, jEnv, "Unable to reference Java class"); goto error; } class_descriptor->java_class = java_class; @@ -377,9 +357,13 @@ enumerate_remove_java_class(JSJHashEntry *he, PRIntn i, void *arg) void jsj_DiscardJavaClassReflections(JNIEnv *jEnv) { - JSJ_HashTableEnumerateEntries(java_class_reflections, - enumerate_remove_java_class, - (void*)jEnv); + if (java_class_reflections) { + JSJ_HashTableEnumerateEntries(java_class_reflections, + enumerate_remove_java_class, + (void*)jEnv); + JSJ_HashTableDestroy(java_class_reflections); + java_class_reflections = NULL; + } } extern JavaClassDescriptor * diff --git a/mozilla/js/ref/liveconnect/jsj_convert.c b/mozilla/js/ref/liveconnect/jsj_convert.c index 254144f6b10..bf1542b4345 100644 --- a/mozilla/js/ref/liveconnect/jsj_convert.c +++ b/mozilla/js/ref/liveconnect/jsj_convert.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* This file is part of the Java-vendor-neutral implementation of LiveConnect @@ -26,11 +11,8 @@ #include #include -#include "prtypes.h" -#include "prprintf.h" -#include "prassert.h" - #include "jsj_private.h" /* LiveConnect internals */ +#include "jscntxt.h" /* for error reporting */ /* Floating-point double utilities, stolen from jsnum.h */ #ifdef IS_LITTLE_ENDIAN @@ -59,9 +41,7 @@ convert_js_obj_to_JSObject_wrapper(JSContext *cx, JNIEnv *jEnv, JSObject *js_obj { if (!njJSObject) { if (java_value) - JS_ReportError(cx, "Couldn't convert JavaScript object to an " - "instance of netscape.javascript.JSObject " - "because that class could not be loaded."); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_CANT_LOAD_JSOBJECT); return JS_FALSE; } @@ -160,27 +140,31 @@ jsj_ConvertJSValueToJavaObject(JSContext *cx, JNIEnv *jEnv, jsval v, JavaSignatu } /* Check if target type is netscape.javascript.JSObject wrapper class */ - if (convert_js_obj_to_JSObject_wrapper(cx, jEnv, js_obj, signature, cost, java_value)) + if (convert_js_obj_to_JSObject_wrapper(cx, jEnv, js_obj, signature, cost, java_value)) { + if (*java_value) + *is_local_refp = JS_TRUE; return JS_TRUE; + } /* Fall through, to attempt conversion to a Java string */ - } else if (JS_TypeOfValue(cx, v) == JSTYPE_FUNCTION) { - /* JS functions can be wrapped as a netscape.javascript.JSObject */ - if (convert_js_obj_to_JSObject_wrapper(cx, jEnv, js_obj, signature, cost, java_value)) - return JS_TRUE; - - /* That didn't work, so fall through, to attempt conversion to - a java.lang.String ... */ - - /* Check for a Java object wrapped inside a JS object */ + } else if (JS_InstanceOf(cx, js_obj, &JavaMember_class, 0)) { + + if (!JS_ConvertValue(cx, v, JSTYPE_OBJECT, &v)) + return JS_FALSE; + return jsj_ConvertJSValueToJavaObject(cx, jEnv, v, signature, cost, + java_value, is_local_refp); + } else { /* Otherwise, see if the target type is the netscape.javascript.JSObject wrapper class or one of its subclasses, in which case a reference is passed to the original JS object by wrapping it inside an instance of netscape.javascript.JSObject */ - if (convert_js_obj_to_JSObject_wrapper(cx, jEnv, js_obj, signature, cost, java_value)) + if (convert_js_obj_to_JSObject_wrapper(cx, jEnv, js_obj, signature, cost, java_value)) { + if (*java_value) + *is_local_refp = JS_TRUE; return JS_TRUE; + } /* Fall through, to attempt conversion to a Java string */ } @@ -235,7 +219,9 @@ jsj_ConvertJSValueToJavaObject(JSContext *cx, JNIEnv *jEnv, jsval v, JavaSignatu /* If no other conversion is possible, see if the target type is java.lang.String */ if ((*jEnv)->IsAssignableFrom(jEnv, jlString, target_java_class)) { +#ifdef LIVECONNECT_IMPROVEMENTS JSBool is_string = JSVAL_IS_STRING(v); +#endif /* Convert to JS string, if necessary, and then to a Java Unicode string */ jsstr = JS_ValueToString(cx, v); @@ -260,9 +246,21 @@ conversion_error: return JS_FALSE; } +/* Valid ranges for Java numeric types */ +#define jbyte_MAX_VALUE 127.0 +#define jbyte_MIN_VALUE -128.0 +#define jchar_MAX_VALUE 65535.0 +#define jchar_MIN_VALUE 0.0 +#define jshort_MAX_VALUE 32767.0 +#define jshort_MIN_VALUE -32768.0 +#define jint_MAX_VALUE 2147483647.0 +#define jint_MIN_VALUE -2147483648.0 +#define jlong_MAX_VALUE 9223372036854775807.0 +#define jlong_MIN_VALUE -9223372036854775808.0 + /* Utility macro for jsj_ConvertJSValueToJavaValue(), below */ #define JSVAL_TO_INTEGRAL_JVALUE(type_name, member_name, member_type, jsval, java_value) \ -if (!JSVAL_IS_NUMBER(v)) { \ + if (!JSVAL_IS_NUMBER(v)) { \ if (!JS_ConvertValue(cx, v, JSTYPE_NUMBER, &v)) \ goto conversion_error; \ (*cost)++; \ @@ -277,12 +275,20 @@ if (!JSVAL_IS_NUMBER(v)) { \ /* Check to see if the jsval's magnitude is too large to be \ representable in the target java type */ \ if (member_name != ival) \ - goto conversion_error; \ + goto numeric_conversion_error; \ } else { \ jdouble dval = *JSVAL_TO_DOUBLE(v); \ + \ + /* NaN becomes zero when converted to integral value */ \ if (JSDOUBLE_IS_NaN(dval)) \ member_name = 0; \ - else \ + \ + /* Unrepresentably large numbers, including infinities, */ \ + /* cause an error. */ \ + else if ((dval > member_type ## _MAX_VALUE) || \ + (dval < member_type ## _MIN_VALUE)) { \ + goto numeric_conversion_error; \ + } else \ member_name = (member_type) dval; \ \ /* Don't allow a non-integral number to be converted \ @@ -297,7 +303,7 @@ if (!JSVAL_IS_NUMBER(v)) { \ #if XP_MAC -// on MRJ jlong is typedef'd to wide, which is a struct. +/* on MRJ jlong is typedef'd to wide, which is a struct. */ #include static jsint jlong_to_jsint(jlong lvalue) @@ -309,7 +315,8 @@ static jsint jlong_to_jsint(jlong lvalue) static jlong jsint_to_jlong(jsint ivalue) { SInt64 val = S64Set(ivalue); - return SInt64ToWide(val); + wide wval =SInt64ToWide(val); + return *(jlong*)&wval; } static jdouble jlong_to_jdouble(jlong lvalue) @@ -321,7 +328,8 @@ static jdouble jlong_to_jdouble(jlong lvalue) static jlong jdouble_to_jlong(jdouble dvalue) { SInt64 val = LongDoubleToSInt64(dvalue); - return SInt64ToWide(val); + wide wval = SInt64ToWide(val); + return *(jlong*)&wval; } /* Mac utility macro for jsj_ConvertJSValueToJavaValue(), below */ @@ -340,9 +348,17 @@ if (!JSVAL_IS_NUMBER(jsvalue)) { \ \ } else { \ jdouble dval = *JSVAL_TO_DOUBLE(jsvalue); \ + \ + /* NaN becomes zero when converted to integral value */ \ if (JSDOUBLE_IS_NaN(dval)) \ member_name = jsint_to_jlong(0); \ - else \ + \ + /* Unrepresentably large numbers, including infinities, */ \ + /* cause an error. */ \ + else if ((dval > member_type ## _MAX_VALUE) || \ + (dval < member_type ## _MIN_VALUE)) { \ + goto numeric_conversion_error; \ + } else \ member_name = jdouble_to_jlong(dval); \ \ /* Don't allow a non-integral number to be converted \ @@ -379,6 +395,7 @@ jsj_ConvertJSValueToJavaValue(JSContext *cx, JNIEnv *jEnv, jsval v, int *cost, jvalue *java_value, JSBool *is_local_refp) { JavaSignatureChar type; + JSBool success = JS_FALSE; /* Initialize to default case, in which no new Java object is synthesized to perform the conversion and, therefore, no JNI local @@ -468,6 +485,10 @@ jsj_ConvertJSValueToJavaValue(JSContext *cx, JNIEnv *jEnv, jsval v, /* Success */ return JS_TRUE; +numeric_conversion_error: + success = JS_TRUE; + /* Fall through ... */ + conversion_error: if (java_value) { @@ -481,11 +502,11 @@ conversion_error: if (!jsval_string) jsval_string = ""; - JS_ReportError(cx, "Unable to convert JavaScript value %s to " - "Java value of type %s", + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_CANT_CONVERT_JS, jsval_string, signature->name); + return JS_FALSE; } - return JS_FALSE; + return success; } /* @@ -511,7 +532,7 @@ jsj_ConvertJavaStringToJSString(JSContext *cx, JNIEnv *jEnv, jstring java_str) js_str = NULL; - /* Unlike JS_NewString(), the string data passed into JS_NewUCString() is + /* The string data passed into JS_NewUCString() is not copied, so make a copy of the Unicode character vector. */ num_bytes = ucs2_str_len * sizeof(jchar); copy_ucs2_str = (jchar*)JS_malloc(cx, num_bytes); @@ -585,27 +606,23 @@ jsj_ConvertJavaObjectToJSString(JSContext *cx, */ JSBool jsj_ConvertJavaObjectToJSNumber(JSContext *cx, JNIEnv *jEnv, + JavaClassDescriptor *class_descriptor, jobject java_obj, jsval *vp) { jdouble d; jmethodID doubleValue; + jclass java_class; -#ifndef SUN_VM_IS_NOT_GARBAGE - /* Late breaking news: calling GetMethodID() on an object that doesn't - contain the given method may cause the Sun VM to crash. So we only - call the method on instances of java.lang.Double */ - JSBool is_Double; - - /* Make sure that we have a java.lang.Double */ - is_Double = (*jEnv)->IsInstanceOf(jEnv, java_obj, jlDouble); - if (!is_Double) - return JS_FALSE; - doubleValue = jlDouble_doubleValue; -#else - doubleValue = (*jEnv)->GetMethodID(jEnv, java_obj, "doubleValue", "()D"); - if (!doubleValue) - return JS_FALSE; -#endif + java_class = class_descriptor->java_class; + doubleValue = (*jEnv)->GetMethodID(jEnv, java_class, "doubleValue", "()D"); + if (!doubleValue) { + /* There is no doubleValue() method for the object. Try toString() + instead and the JS engine will attempt to convert the result to + a number. */ + (*jEnv)->ExceptionClear(jEnv); + return jsj_ConvertJavaObjectToJSString(cx, jEnv, class_descriptor, + java_obj, vp); + } d = (*jEnv)->CallDoubleMethod(jEnv, java_obj, doubleValue); if ((*jEnv)->ExceptionOccurred(jEnv)) { @@ -626,28 +643,28 @@ jsj_ConvertJavaObjectToJSNumber(JSContext *cx, JNIEnv *jEnv, */ extern JSBool jsj_ConvertJavaObjectToJSBoolean(JSContext *cx, JNIEnv *jEnv, + JavaClassDescriptor *class_descriptor, jobject java_obj, jsval *vp) { jboolean b; jmethodID booleanValue; + jclass java_class; -#ifndef SUN_VM_IS_NOT_GARBAGE - /* Late breaking news: calling GetMethodID() on an object that doesn't - contain the given method may cause the Sun VM to crash. So we only - call the method on instances of java.lang.Boolean */ - - JSBool is_Boolean; - - /* Make sure that we have a java.lang.Boolean */ - is_Boolean = (*jEnv)->IsInstanceOf(jEnv, java_obj, jlBoolean); - if (!is_Boolean) - return JS_FALSE; - booleanValue = jlBoolean_booleanValue; -#else + /* Null converts to false. */ + if (!java_obj) { + *vp = JSVAL_FALSE; + return JS_TRUE; + } + java_class = class_descriptor->java_class; booleanValue = (*jEnv)->GetMethodID(jEnv, java_obj, "booleanValue", "()Z"); - if (!booleanValue) - return JS_FALSE; -#endif + + /* Non-null Java object does not have a booleanValue() method, so + it converts to true. */ + if (!booleanValue) { + (*jEnv)->ExceptionClear(jEnv); + *vp = JSVAL_TRUE; + return JS_TRUE; + } b = (*jEnv)->CallBooleanMethod(jEnv, java_obj, booleanValue); if ((*jEnv)->ExceptionOccurred(jEnv)) { @@ -683,7 +700,11 @@ jsj_ConvertJavaObjectToJSValue(JSContext *cx, JNIEnv *jEnv, * it to obtain the original JS object. */ if (njJSObject && (*jEnv)->IsInstanceOf(jEnv, java_obj, njJSObject)) { +#ifdef PRESERVE_JSOBJECT_IDENTITY js_obj = (JSObject *)((*jEnv)->GetIntField(jEnv, java_obj, njJSObject_internal)); +#else + js_obj = jsj_UnwrapJSObjectWrapper(jEnv, java_obj); +#endif PR_ASSERT(js_obj); if (!js_obj) return JS_FALSE; diff --git a/mozilla/js/ref/liveconnect/jsj_field.c b/mozilla/js/ref/liveconnect/jsj_field.c index ee3670c5cdd..537c05a465a 100644 --- a/mozilla/js/ref/liveconnect/jsj_field.c +++ b/mozilla/js/ref/liveconnect/jsj_field.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -26,9 +11,6 @@ */ #include -#include "prtypes.h" -#include "prprintf.h" -#include "prassert.h" #include "jsj_private.h" /* LiveConnect internals */ @@ -249,7 +231,7 @@ jsj_GetJavaFieldValue(JSContext *cx, JNIEnv *jEnv, JavaFieldSpec *field_spec, java_value.member = \ (*jEnv)->Get##Type##Field(jEnv, java_obj, fieldID); \ if ((*jEnv)->ExceptionOccurred(jEnv)) { \ - jsj_ReportJavaError(cx, jEnv, "Error reading Java field"); \ + jsj_UnexpectedJavaError(cx, jEnv, "Error reading Java field"); \ return JS_FALSE; \ } \ PR_END_MACRO @@ -325,7 +307,7 @@ jsj_SetJavaFieldValue(JSContext *cx, JNIEnv *jEnv, JavaFieldSpec *field_spec, (*jEnv)->Set##Type##Field(jEnv, java_obj, fieldID,java_value.member);\ } \ if ((*jEnv)->ExceptionOccurred(jEnv)) { \ - jsj_ReportJavaError(cx, jEnv, "Error assigning to Java field"); \ + jsj_UnexpectedJavaError(cx, jEnv, "Error assigning to Java field"); \ return JS_FALSE; \ } \ PR_END_MACRO @@ -372,7 +354,7 @@ jsj_SetJavaFieldValue(JSContext *cx, JNIEnv *jEnv, JavaFieldSpec *field_spec, case JAVA_SIGNATURE_CLASS: case JAVA_SIGNATURE_ARRAY: SET_JAVA_FIELD(Object,l); - if (is_local_ref) \ + if (is_local_ref) (*jEnv)->DeleteLocalRef(jEnv, java_value.l); break; diff --git a/mozilla/js/ref/liveconnect/jsj_hash.c b/mozilla/js/ref/liveconnect/jsj_hash.c index 67c42d8630a..2ab5ef516d3 100644 --- a/mozilla/js/ref/liveconnect/jsj_hash.c +++ b/mozilla/js/ref/liveconnect/jsj_hash.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -26,12 +11,18 @@ * been renamed from their original NSPR names to protect the innocent. */ -#include "jsj_hash.h" -#include "prtypes.h" -#include "prassert.h" #include #include +#include "jsj_hash.h" +#include "prtypes.h" +#ifdef NSPR20 +# include "prlog.h" +# include "prbit.h" +#else +# include "prassert.h" +#endif + /* Compute the number of buckets in ht */ #define NBUCKETS(ht) (1 << (JSJ_HASH_BITS - (ht)->shift)) @@ -114,7 +105,7 @@ JSJ_NewHashTable(PRUint32 n, JSJHashFunction keyHash, ht = (*allocOps->allocTable)(allocPriv, sizeof *ht); if (!ht) - return 0; + return 0; memset(ht, 0, sizeof *ht); ht->shift = JSJ_HASH_BITS - n; n = 1 << n; @@ -248,7 +239,7 @@ JSJ_HashTableRawAdd(JSJHashTable *ht, JSJHashEntry **hep, /* Make a new key value entry */ he = (*ht->allocOps->allocEntry)(ht->allocPriv, key); if (!he) - return 0; + return 0; he->keyHash = keyHash; he->key = key; he->value = value; diff --git a/mozilla/js/ref/liveconnect/jsj_hash.h b/mozilla/js/ref/liveconnect/jsj_hash.h index e6fb30aa41b..00a067cf8fc 100644 --- a/mozilla/js/ref/liveconnect/jsj_hash.h +++ b/mozilla/js/ref/liveconnect/jsj_hash.h @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* diff --git a/mozilla/js/ref/liveconnect/jsj_method.c b/mozilla/js/ref/liveconnect/jsj_method.c index 61bbb676346..9c8a8f3a22b 100644 --- a/mozilla/js/ref/liveconnect/jsj_method.c +++ b/mozilla/js/ref/liveconnect/jsj_method.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -28,13 +13,9 @@ #include #include -#include "prtypes.h" -#include "prprintf.h" -#include "prassert.h" -#include "prosdep.h" - #include "jsj_private.h" /* LiveConnect internals */ #include "jsjava.h" /* LiveConnect external API */ +#include "jscntxt.h" /* for error reporting */ /* * A helper function for jsj_ConvertJavaMethodSignatureToString(): @@ -322,8 +303,8 @@ jsj_InitJavaMethodSignature(JSContext *cx, JNIEnv *jEnv, a = arg_signatures[i] = jsj_GetJavaClassDescriptor(cx, jEnv, arg_class); if (!a) { - jsj_ReportJavaError(cx, jEnv, "Could not determine Java class " - "signature using java.lang.reflect"); + jsj_UnexpectedJavaError(cx, jEnv, "Could not determine Java class " + "signature using java.lang.reflect"); goto error; } } @@ -715,7 +696,7 @@ resolve_overloaded_method(JSContext *cx, JNIEnv *jEnv, JavaMemberDescriptor *mem static jvalue * convert_JS_method_args_to_java_argv(JSContext *cx, JNIEnv *jEnv, jsval *argv, - JavaMethodSpec *method, JSBool **localvp) + JavaMethodSpec *method, JSBool **localvp) { jvalue *jargv; JSBool ok, *localv; @@ -752,7 +733,8 @@ convert_JS_method_args_to_java_argv(JSContext *cx, JNIEnv *jEnv, jsval *argv, ok = jsj_ConvertJSValueToJavaValue(cx, jEnv, argv[i], arg_signatures[i], &dummy_cost, &jargv[i], &localv[i]); if (!ok) { - JS_ReportError(cx, "Internal error: can't convert JS value to Java value"); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_CONVERT_JS_VALUE); JS_free(cx, jargv); JS_free(cx, localv); *localvp = NULL; @@ -767,9 +749,9 @@ static JSBool invoke_java_method(JSContext *cx, JSJavaThreadState *jsj_env, jobject java_class_or_instance, JavaClassDescriptor *class_descriptor, - JavaMethodSpec *method, + JavaMethodSpec *method, JSBool is_static_method, - jsval *argv, jsval *vp) + jsval *argv, jsval *vp) { jvalue java_value; jvalue *jargv; @@ -800,10 +782,12 @@ invoke_java_method(JSContext *cx, JSJavaThreadState *jsj_env, } old_cx = JSJ_SetDefaultJSContextForJavaThread(cx, jsj_env); - if (old_cx) { - JS_ReportError(cx, "Java thread in simultaneous use by more than " - "one JSContext ?"); + +#ifdef DEBUG + if (old_cx && (old_cx != cx)) { + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_MULTIPLE_JTHREADS); } +#endif jargv = NULL; localv = NULL; @@ -1019,8 +1003,8 @@ static JSBool invoke_java_constructor(JSContext *cx, JSJavaThreadState *jsj_env, jclass java_class, - JavaMethodSpec *method, - jsval *argv, jsval *vp) + JavaMethodSpec *method, + jsval *argv, jsval *vp) { jvalue *jargv; uintN argc, i; @@ -1049,10 +1033,12 @@ invoke_java_constructor(JSContext *cx, } old_cx = JSJ_SetDefaultJSContextForJavaThread(cx, jsj_env); - if (old_cx) { - JS_ReportError(cx, "Java thread in simultaneous use by more than " - "one JSContext ?"); + +#ifdef DEBUG + if (old_cx && (old_cx != cx)) { + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, JSJMSG_MULTIPLE_JTHREADS); } +#endif /* Call the constructor */ java_object = (*jEnv)->NewObjectA(jEnv, java_class, methodID, jargv); @@ -1067,14 +1053,16 @@ invoke_java_constructor(JSContext *cx, } out: - for (i = 0; i < argc; i++) { - if (localv[i]) - (*jEnv)->DeleteLocalRef(jEnv, jargv[i].l); + if (localv) { + for (i = 0; i < argc; i++) { + if (localv[i]) + (*jEnv)->DeleteLocalRef(jEnv, jargv[i].l); + } + JS_free(cx, localv); } if (jargv) JS_free(cx, jargv); - if (localv) - JS_free(cx, localv); + if (error_occurred) return JS_FALSE; else @@ -1134,25 +1122,25 @@ jsj_JavaConstructorWrapper(JSContext *cx, JSObject *obj, /* Get class/interface flags and check them */ modifiers = class_descriptor->modifiers; if (modifiers & ACC_ABSTRACT) { - JS_ReportError(cx, "Java class %s is abstract and therefore may not " - "be instantiated", class_descriptor->name); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_ABSTRACT_JCLASS, class_descriptor->name); return JS_FALSE; } if (modifiers & ACC_INTERFACE) { - JS_ReportError(cx, "%s is a Java interface and therefore may not " - "be instantiated", class_descriptor->name); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_IS_INTERFACE, class_descriptor->name); return JS_FALSE; } if ( !(modifiers & ACC_PUBLIC) ) { - JS_ReportError(cx, "Java class %s is not public and therefore may not " - "be instantiated", class_descriptor->name); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_NOT_PUBLIC, class_descriptor->name); return JS_FALSE; } member_descriptor = jsj_LookupJavaClassConstructors(cx, jEnv, class_descriptor); if (!member_descriptor) { - JS_ReportError(cx, "No public constructors defined for Java class %s", - class_descriptor->name); + JS_ReportErrorNumber(cx, jsj_GetErrorMessage, + JSJMSG_NO_CONSTRUCTORS, class_descriptor->name); return JS_FALSE; } diff --git a/mozilla/js/ref/liveconnect/jsj_private.h b/mozilla/js/ref/liveconnect/jsj_private.h index 279a37e1834..1407eaaab74 100644 --- a/mozilla/js/ref/liveconnect/jsj_private.h +++ b/mozilla/js/ref/liveconnect/jsj_private.h @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -29,10 +14,28 @@ #ifndef _JSJAVA_PVT_H #define _JSJAVA_PVT_H +#include "prtypes.h" + +/* NSPR1 compatibility definitions */ +#ifdef NSPR20 +# include "prprf.h" +# include "prlog.h" +# include "plhash.h" /* NSPR hash-tables */ +#else +# include "prprintf.h" +# include "prassert.h" +# include "prhash.h" /* NSPR hash-tables */ +# define PRHashNumber prhashcode +#endif + +#ifdef XP_MAC +# include "prosdep.h" +#endif + #include "jsj_hash.h" /* Hash tables */ -#include "prhash.h" /* NSPR hash-tables */ #include "jni.h" /* Java Native Interface */ #include "jsapi.h" /* JavaScript engine API */ +#include "jsjava.h" /* LiveConnect public API */ /*************************** Type Declarations ******************************/ @@ -42,11 +45,7 @@ typedef struct JavaMemberDescriptor JavaMemberDescriptor; typedef struct JavaMethodSpec JavaMethodSpec; typedef struct JavaClassDescriptor JavaClassDescriptor; typedef struct JavaClassDescriptor JavaSignature; -typedef struct JSJCallbacks JSJCallbacks; typedef struct CapturedJSError CapturedJSError; -typedef struct JavaPackageDef JavaPackageDef; -typedef struct JSJavaThreadState JSJavaThreadState; -typedef struct JSJavaVM JSJavaVM; typedef struct JavaMemberVal JavaMemberVal; /* @@ -86,12 +85,12 @@ typedef struct JavaFieldSpec { /* A descriptor for the reflection of a single Java method. Each overloaded method has a separate corresponding JavaMethodSpec. */ -typedef struct JavaMethodSpec { +struct JavaMethodSpec { jmethodID methodID; /* JVM opaque access handle for method */ JavaMethodSignature signature; const char * name; /* UTF8; TODO - Should support Unicode method names */ JavaMethodSpec * next; /* next method in chain of overloaded methods */ -} JavaMethodSpec; +}; /* * A descriptor for the reflection of a single member of a Java object. @@ -100,17 +99,17 @@ typedef struct JavaMethodSpec { * they are overloaded methods sharing the same simple name.) This same * descriptor type is used for both static or instance members. */ -typedef struct JavaMemberDescriptor { +struct JavaMemberDescriptor { const char * name; /* simple name of field and/or method */ jsid id; /* hashed name for quick JS property lookup */ JavaFieldSpec * field; /* field with the given name, if any */ JavaMethodSpec * methods; /* Overloaded methods which share the same name, if any */ JavaMemberDescriptor * next; /* next descriptor in same defining class */ JSObject * invoke_func_obj; /* If non-null, JSFunction obj to invoke method */ -} JavaMemberDescriptor; +}; /* This is the native portion of a reflected Java class */ -typedef struct JavaClassDescriptor { +struct JavaClassDescriptor { const char * name; /* Name of class, e.g. "java/lang/Byte" */ JavaSignatureChar type; /* class category: primitive type, object, array */ jclass java_class; /* Opaque JVM handle to corresponding java.lang.Class */ @@ -125,18 +124,18 @@ typedef struct JavaClassDescriptor { e.g. abstract, private */ int ref_count; /* # of references to this struct */ JavaSignature * array_component_signature; /* Only non-NULL for array classes */ -} JavaClassDescriptor; +}; /* This is the native portion of a reflected Java method or field */ -typedef struct JavaMemberVal { +struct JavaMemberVal { jsval field_val; /* Captured value of Java field */ jsval invoke_method_func_val; /* JSFunction wrapper around Java method invoker */ JavaMemberDescriptor * descriptor; JavaMemberVal * next; -} JavaMemberVal; +}; /* This is the native portion of a reflected Java object */ -typedef struct { +typedef struct JavaObjectWrapper { jobject java_obj; /* Opaque JVM ref to Java object */ JavaClassDescriptor * class_descriptor; /* Java class info */ } JavaObjectWrapper; @@ -152,7 +151,7 @@ typedef struct { /* A JSJavaVM structure must be created for each Java VM that is accessed via LiveConnect */ -typedef struct JSJavaVM { +struct JSJavaVM { /* TODO - all LiveConnect global variables should be migrated into this structure in order to allow more than one LiveConnect-enabled Java VM to exist within the same process. */ @@ -161,17 +160,17 @@ typedef struct JSJavaVM { JSBool jsj_created_java_vm; int num_attached_threads; JSJavaVM * next; /* next VM among all created VMs */ -} JSJavaVM; +}; /* Per-thread state that encapsulates the connection to the Java VM */ -typedef struct JSJavaThreadState { +struct JSJavaThreadState { const char * name; /* Thread name, for debugging */ JSJavaVM * jsjava_vm; /* All per-JVM state */ JNIEnv * jEnv; /* Per-thread opaque handle to Java VM */ CapturedJSError * pending_js_errors; /* JS errors to be thrown as Java exceptions */ JSContext * cx; /* current JS context for thread */ JSJavaThreadState * next; /* next thread state among all created threads */ -} JSJavaThreadState; +}; /******************************** Globals ***********************************/ @@ -181,6 +180,7 @@ extern JSJCallbacks *JSJ_callbacks; extern JSClass JavaObject_class; extern JSClass JavaArray_class; extern JSClass JavaClass_class; +extern JSClass JavaMember_class; /* * Opaque JVM handles to Java classes, methods and objects required for @@ -291,9 +291,11 @@ jsj_ConvertJavaObjectToJSString(JSContext *cx, JNIEnv *jEnv, jobject java_obj, jsval *vp); extern JSBool jsj_ConvertJavaObjectToJSNumber(JSContext *cx, JNIEnv *jEnv, + JavaClassDescriptor *class_descriptor, jobject java_obj, jsval *vp); extern JSBool jsj_ConvertJavaObjectToJSBoolean(JSContext *cx, JNIEnv *jEnv, + JavaClassDescriptor *class_descriptor, jobject java_obj, jsval *vp); /************************ Java package reflection **************************/ @@ -402,6 +404,9 @@ extern JSBool jsj_ReflectJavaMethodsAndFields(JSContext *cx, JavaClassDescriptor *class_descriptor, JSBool reflect_only_statics); +extern JSObject * +jsj_CreateJavaMember(JSContext *cx, jsval method_val, jsval field_val); + /************************* Java object reflection **************************/ extern JSBool jsj_init_JavaObject(JSContext *, JSObject *); @@ -421,9 +426,6 @@ JavaObject_finalize(JSContext *cx, JSObject *obj); extern JSBool JavaObject_resolve(JSContext *cx, JSObject *obj, jsval id); -extern JSBool -JavaObject_enumerate(JSContext *cx, JSObject *obj); - extern JSBool JavaObject_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp); @@ -448,6 +450,9 @@ jsj_SetJavaArrayElement(JSContext *cx, JNIEnv *jEnv, jarray java_array, extern jobject jsj_WrapJSObject(JSContext *cx, JNIEnv *jEnv, JSObject *js_obj); +extern JSObject * +jsj_UnwrapJSObjectWrapper(JNIEnv *jEnv, jobject java_wrapper_obj); + extern void jsj_ClearPendingJSErrors(JSJavaThreadState *jsj_env); @@ -467,6 +472,9 @@ jsj_GetJavaErrorMessage(JNIEnv *env); extern void jsj_LogError(const char *error_msg); +extern const JSErrorFormatString * +jsj_GetErrorMessage(const uintN errorNumber); + PR_CALLBACK JSJHashNumber jsj_HashJavaObject(const void *key, void* env); @@ -504,4 +512,14 @@ jsj_MapJSContextToJSJThread(JSContext *cx, JNIEnv **envp); JS_free(cx, x); \ PR_END_MACRO + +enum JSJErrNum { +#define MSG_DEF(name, number, format, count) \ + name = number, +#include "jsj_msg.def" +#undef MSG_DEF + JSJ_Err_Limit +#undef MSGDEF +}; + #endif /* _JSJAVA_PVT_H */ diff --git a/mozilla/js/ref/liveconnect/jsj_utils.c b/mozilla/js/ref/liveconnect/jsj_utils.c index 7af99d28b70..3da781b413a 100644 --- a/mozilla/js/ref/liveconnect/jsj_utils.c +++ b/mozilla/js/ref/liveconnect/jsj_utils.c @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -27,11 +12,6 @@ #include #include -#include "prtypes.h" -#include "prprintf.h" -#include "prassert.h" -#include "prosdep.h" - #include "jsj_private.h" /* LiveConnect internals */ #include "jsjava.h" /* External LiveConnect API */ @@ -43,7 +23,7 @@ PR_CALLBACK JSJHashNumber jsj_HashJavaObject(const void *key, void* env) { - prhashcode hash_code; + PRHashNumber hash_code; jobject java_obj; JNIEnv *jEnv; @@ -161,7 +141,7 @@ jsj_GetJavaErrorMessage(JNIEnv *jEnv) jstring java_exception_jstring; exception = (*jEnv)->ExceptionOccurred(jEnv); - if (exception) { + if (exception && jlThrowable_toString) { java_exception_jstring = (*jEnv)->CallObjectMethod(jEnv, exception, jlThrowable_toString); java_error_msg = (*jEnv)->GetStringUTFChars(jEnv, java_exception_jstring, NULL); @@ -312,6 +292,31 @@ jsj_LogError(const char *error_msg) fputs(error_msg, stderr); } +/* + Error number handling. + + jsj_ErrorFormatString is an array of format strings mapped + by error number. It is initialized by the contents of jsj_msg.def + + jsj_GetErrorMessage is invoked by the engine whenever it wants + to convert an error number into an error format string. +*/ +JSErrorFormatString jsj_ErrorFormatString[JSJ_Err_Limit] = { +#define MSG_DEF(name, number, count, format) \ + { format, count } , +#include "jsj_msg.def" +#undef MSG_DEF +}; + +const JSErrorFormatString * +jsj_GetErrorMessage(const uintN errorNumber) +{ + if ((errorNumber > 0) && (errorNumber < JSJ_Err_Limit)) + return &jsj_ErrorFormatString[errorNumber]; + else + return NULL; +} + jsize jsj_GetJavaArrayLength(JSContext *cx, JNIEnv *jEnv, jarray java_array) { @@ -339,7 +344,6 @@ jsj_MapJSContextToJSJThread(JSContext *cx, JNIEnv **envp) } return NULL; } - if (envp) - *envp = jsj_env->jEnv; + *envp = jsj_env->jEnv; return jsj_env; } diff --git a/mozilla/js/ref/liveconnect/jsjava.h b/mozilla/js/ref/liveconnect/jsjava.h index 6abec4e8bb4..e4b4b709499 100644 --- a/mozilla/js/ref/liveconnect/jsjava.h +++ b/mozilla/js/ref/liveconnect/jsjava.h @@ -1,20 +1,5 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is Netscape Communications - * Corporation. Portions created by Netscape are Copyright (C) 1998 - * Netscape Communications Corporation. All Rights Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* @@ -27,6 +12,12 @@ #ifndef _JSJAVA_H #define _JSJAVA_H +#ifndef prtypes_h___ +#include "prtypes.h" +#endif + +PR_BEGIN_EXTERN_C + #include "jni.h" /* Java Native Interface */ #include "jsapi.h" /* JavaScript engine API */ @@ -60,6 +51,7 @@ typedef struct JSJCallbacks { callback can call JSJ_SetJSContextForJavaThread() to avoid any further callbacks of this type for this Java thread. */ JSContext * (*map_jsj_thread_to_js_context)(JSJavaThreadState *jsj_env, + JNIEnv *jEnv, char **errp); /* This callback is invoked whenever a call is made into Java from @@ -87,8 +79,8 @@ typedef struct JSJCallbacks { browser embedding, these are used to maintain the run-to-completion semantics of JavaScript. It is acceptable for either function pointer to be NULL. */ - JSBool (*enter_js_from_java)(char **errp); - void (*exit_js)(void); + JSBool (*enter_js_from_java)(JNIEnv *jEnv, char **errp); + void (*exit_js)(JNIEnv *jEnv); /* Most LiveConnect errors are signaled by calling JS_ReportError(), but in some circumstances, the target JSContext for such errors is not @@ -97,6 +89,8 @@ typedef struct JSJCallbacks { NULL, error messages are sent to stderr. */ void (*error_print)(const char *error_msg); + JavaVM * (*get_java_vm)(char **errp); + /* Reserved for future use */ void * reserved[10]; } JSJCallbacks; @@ -139,12 +133,12 @@ typedef struct JavaPackageDef { as properties of global_obj. If java_vm is NULL, a new Java VM is created, using the provided classpath in addition to any default classpath. The classpath argument is ignored, however, if java_vm is non-NULL. */ -PR_PUBLIC_API(JSBool) +PR_IMPLEMENT(JSBool) JSJ_SimpleInit(JSContext *cx, JSObject *global_obj, JavaVM *java_vm, const char *classpath); /* Free up all resources. Destroy the Java VM if it was created by LiveConnect */ -PR_PUBLIC_API(void) +PR_IMPLEMENT(void) JSJ_SimpleShutdown(); /*===========================================================================*/ @@ -166,14 +160,14 @@ JSJ_SimpleShutdown(); */ /* Called once for all instances of LiveConnect to set up callbacks */ -PR_PUBLIC_API(void) +PR_IMPLEMENT(void) JSJ_Init(JSJCallbacks *callbacks); /* Called once per Java VM, this function initializes the classes, fields, and methods required for Java reflection. If java_vm is NULL, a new Java VM is created, using the provided classpath in addition to any default classpath. The classpath argument is ignored, however, if java_vm is non-NULL. */ -PR_PUBLIC_API(JSJavaVM *) +PR_IMPLEMENT(JSJavaVM *) JSJ_ConnectToJavaVM(JavaVM *java_vm, const char *classpath); /* Initialize the provided JSContext by setting up the JS classes necessary for @@ -183,7 +177,7 @@ JSJ_ConnectToJavaVM(JavaVM *java_vm, const char *classpath); initialization time is not necessary, but it will make package lookup faster and, more importantly, will avoid unnecessary network accesses if classes are being loaded over the network.) */ -PR_PUBLIC_API(JSBool) +PR_IMPLEMENT(JSBool) JSJ_InitJSContext(JSContext *cx, JSObject *global_obj, JavaPackageDef *predefined_packages); @@ -194,19 +188,19 @@ JSJ_InitJSContext(JSContext *cx, JSObject *global_obj, used for debugging purposes and can be set to NULL. The Java JNI environment associated with this thread is returned through the java_envp argument if java_envp is non-NULL. */ -PR_PUBLIC_API(JSJavaThreadState *) +PR_IMPLEMENT(JSJavaThreadState *) JSJ_AttachCurrentThreadToJava(JSJavaVM *jsjava_vm, const char *thread_name, JNIEnv **java_envp); /* Destructor routine for per-thread JSJavaThreadState structure */ -PR_PUBLIC_API(JSBool) +PR_IMPLEMENT(JSBool) JSJ_DetachCurrentThreadFromJava(JSJavaThreadState *jsj_env); /* This function is used to specify a particular JSContext as *the* JavaScript execution environment to be used when LiveConnect is accessed from the given Java thread, i.e. when one of the methods of netscape.javascript.JSObject has been called. There can only be one such JS context for any given Java - thread at a time. To multiplex JSContexts among a single thread, this + thread at a time. (To multiplex JSContexts among a single thread, this function could be called before Java is invoked on that thread.) The return value is the previous JSContext associated with the given Java thread. @@ -215,20 +209,22 @@ JSJ_DetachCurrentThreadFromJava(JSJavaThreadState *jsj_env); be invoked to determine the JSContext for the thread. The purpose of the function is to improve performance by avoiding the expense of the callback. */ -PR_PUBLIC_API(JSContext *) +PR_IMPLEMENT(JSContext *) JSJ_SetDefaultJSContextForJavaThread(JSContext *cx, JSJavaThreadState *jsj_env); /* This routine severs the connection to a Java VM, freeing all related resources. It shouldn't be called until the global scope has been cleared in all related JSContexts (so that all LiveConnect objects are finalized) and a JavaScript GC is performed. Otherwise, accessed to free'ed memory could result. */ -PR_PUBLIC_API(void) +PR_IMPLEMENT(void) JSJ_DisconnectFromJavaVM(JSJavaVM *); /* * Reflect a Java object into a JS value. The source object, java_obj, must * be of type java.lang.Object or a subclass and may, therefore, be an array. */ -PR_PUBLIC_API(JSBool) +PR_IMPLEMENT(JSBool) JSJ_ConvertJavaObjectToJSValue(JSContext *cx, jobject java_obj, jsval *vp); -#endif /* _JSJAVA_H */ \ No newline at end of file + +PR_END_EXTERN_C +#endif /* _JSJAVA_H */ diff --git a/mozilla/js/ref/liveconnect/macbuild/JavaSession/JavaSession.cpp b/mozilla/js/ref/liveconnect/macbuild/JavaSession/JavaSession.cpp index a12d0270c8a..15b008ba111 100644 --- a/mozilla/js/ref/liveconnect/macbuild/JavaSession/JavaSession.cpp +++ b/mozilla/js/ref/liveconnect/macbuild/JavaSession/JavaSession.cpp @@ -1,19 +1,5 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "NPL"); you may not use this file except in - * compliance with the NPL. You may obtain a copy of the NPL at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the NPL is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL - * for the specific language governing rights and limitations under the - * NPL. - * - * The Initial Developer of this code under the NPL is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1998 Netscape Communications Corporation. All Rights - * Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* diff --git a/mozilla/js/ref/liveconnect/macbuild/JavaSession/JavaSession.h b/mozilla/js/ref/liveconnect/macbuild/JavaSession/JavaSession.h index 4ad066d8683..5f4f0145d30 100644 --- a/mozilla/js/ref/liveconnect/macbuild/JavaSession/JavaSession.h +++ b/mozilla/js/ref/liveconnect/macbuild/JavaSession/JavaSession.h @@ -1,19 +1,5 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "NPL"); you may not use this file except in - * compliance with the NPL. You may obtain a copy of the NPL at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the NPL is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL - * for the specific language governing rights and limitations under the - * NPL. - * - * The Initial Developer of this code under the NPL is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1998 Netscape Communications Corporation. All Rights - * Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* diff --git a/mozilla/js/ref/liveconnect/macbuild/JavaSession/OSStatusException.h b/mozilla/js/ref/liveconnect/macbuild/JavaSession/OSStatusException.h index 3a7c37abf27..a5bb7d233e2 100644 --- a/mozilla/js/ref/liveconnect/macbuild/JavaSession/OSStatusException.h +++ b/mozilla/js/ref/liveconnect/macbuild/JavaSession/OSStatusException.h @@ -1,19 +1,5 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- - * - * The contents of this file are subject to the Netscape Public License - * Version 1.0 (the "NPL"); you may not use this file except in - * compliance with the NPL. You may obtain a copy of the NPL at - * http://www.mozilla.org/NPL/ - * - * Software distributed under the NPL is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL - * for the specific language governing rights and limitations under the - * NPL. - * - * The Initial Developer of this code under the NPL is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1998 Netscape Communications Corporation. All Rights - * Reserved. +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. */ /* diff --git a/mozilla/js/ref/liveconnect/macbuild/LiveConnectShell.mcp b/mozilla/js/ref/liveconnect/macbuild/LiveConnectShell.mcp index ec3c94fa7c4..bde21200f65 100644 Binary files a/mozilla/js/ref/liveconnect/macbuild/LiveConnectShell.mcp and b/mozilla/js/ref/liveconnect/macbuild/LiveConnectShell.mcp differ diff --git a/mozilla/js/ref/liveconnect/netscape_javascript_JSObject.h b/mozilla/js/ref/liveconnect/netscape_javascript_JSObject.h index f710bbefcbe..11b97d4ca2f 100644 --- a/mozilla/js/ref/liveconnect/netscape_javascript_JSObject.h +++ b/mozilla/js/ref/liveconnect/netscape_javascript_JSObject.h @@ -1,4 +1,6 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ +/* -*- Mode: C; tab-width: 8 -*- + * Copyright (C) 1998 Netscape Communications Corporation, All Rights Reserved. + */ #include /* Header for class netscape_javascript_JSObject */ @@ -95,6 +97,14 @@ JNIEXPORT jobject JNICALL Java_netscape_javascript_JSObject_getWindow JNIEXPORT void JNICALL Java_netscape_javascript_JSObject_finalize (JNIEnv *, jobject); +/* + * Class: netscape_javascript_JSObject + * Method: equals + * Signature: (Ljava/lang/Object;)Z + */ +JNIEXPORT jboolean JNICALL Java_netscape_javascript_JSObject_equals + (JNIEnv *, jobject, jobject); + #ifdef __cplusplus } #endif