1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 /* The convenience functions below present a complete, but simplified
41 LiveConnect API which is designed to handle the special case of a single
42 Java-VM, single-threaded operation, and use of only one JSContext. */
50 /* We can get away with global variables in our single-threaded,
51 single-JSContext case. */
52 static JSJavaVM
* the_jsj_vm
= NULL
;
53 static JSContext
* the_cx
= NULL
;
54 static JSJavaThreadState
* the_jsj_thread
= NULL
;
55 static JSObject
* the_global_js_obj
= NULL
;
57 /* Trivial implementation of callback function */
58 static JSJavaThreadState
*
59 default_map_js_context_to_jsj_thread(JSContext
*cx
, char **errp
)
61 return the_jsj_thread
;
64 /* Trivial implementation of callback function */
66 default_map_jsj_thread_to_js_context(JSJavaThreadState
*jsj_env
,
68 void *java_applet_obj
,
76 /* Trivial implementation of callback function */
78 default_map_java_object_to_js_object(JNIEnv
*jEnv
, void *hint
, char **errp
)
80 return the_global_js_obj
;
84 default_create_java_vm(SystemJavaVM
* *jvm
, JNIEnv
* *initialEnv
, void* initargs
)
87 const char* user_classpath
= (const char*)initargs
;
88 char* full_classpath
= NULL
;
90 /* No Java VM supplied, so create our own */
91 JDK1_1InitArgs vm_args
;
92 memset(&vm_args
, 0, sizeof(vm_args
));
94 /* Magic constant indicates JRE version 1.1 */
95 vm_args
.version
= 0x00010001;
96 JNI_GetDefaultJavaVMInitArgs(&vm_args
);
98 /* Prepend the classpath argument to the default JVM classpath */
100 #if defined(XP_UNIX) || defined(XP_BEOS)
101 full_classpath
= JS_smprintf("%s:%s", user_classpath
, vm_args
.classpath
);
103 full_classpath
= JS_smprintf("%s;%s", user_classpath
, vm_args
.classpath
);
105 if (!full_classpath
) {
108 vm_args
.classpath
= (char*)full_classpath
;
111 err
= JNI_CreateJavaVM((JavaVM
**)jvm
, initialEnv
, &vm_args
);
114 JS_smprintf_free(full_classpath
);
120 default_destroy_java_vm(SystemJavaVM
* jvm
, JNIEnv
* initialEnv
)
122 JavaVM
* java_vm
= (JavaVM
*)jvm
;
123 jint err
= (*java_vm
)->DestroyJavaVM(java_vm
);
128 default_attach_current_thread(SystemJavaVM
* jvm
)
130 JavaVM
* java_vm
= (JavaVM
*)jvm
;
132 (*java_vm
)->AttachCurrentThread(java_vm
, &env
, NULL
);
137 default_detach_current_thread(SystemJavaVM
* jvm
, JNIEnv
* env
)
139 JavaVM
* java_vm
= (JavaVM
*)jvm
;
140 /* assert that env is the JNIEnv of the current thread */
141 jint err
= (*java_vm
)->DetachCurrentThread(java_vm
);
146 default_get_java_vm(JNIEnv
* env
)
148 JavaVM
* java_vm
= NULL
;
149 (*env
)->GetJavaVM(env
, &java_vm
);
150 return (SystemJavaVM
*)java_vm
;
153 /* Trivial implementations of callback functions */
154 JSJCallbacks jsj_default_callbacks
= {
155 default_map_jsj_thread_to_js_context
,
156 default_map_js_context_to_jsj_thread
,
157 default_map_java_object_to_js_object
,
164 default_create_java_vm
,
165 default_destroy_java_vm
,
166 default_attach_current_thread
,
167 default_detach_current_thread
,
172 * Initialize the provided JSContext by setting up the JS classes necessary for
173 * reflection and by defining JavaPackage objects for the default Java packages
174 * as properties of global_obj. If java_vm is NULL, a new Java VM is
175 * created, using the provided classpath in addition to any default classpath.
176 * The classpath argument is ignored, however, if java_vm is non-NULL.
179 JSJ_SimpleInit(JSContext
*cx
, JSObject
*global_obj
, SystemJavaVM
*java_vm
, const char *classpath
)
183 JSJ_Init(&jsj_default_callbacks
);
185 JS_ASSERT(!the_jsj_vm
);
186 the_jsj_vm
= JSJ_ConnectToJavaVM(java_vm
, (void*)classpath
);
190 if (!JSJ_InitJSContext(cx
, global_obj
, NULL
))
193 the_global_js_obj
= global_obj
;
195 the_jsj_thread
= JSJ_AttachCurrentThreadToJava(the_jsj_vm
, "main thread", &jEnv
);
198 JSJ_SetDefaultJSContextForJavaThread(cx
, the_jsj_thread
);
202 JSJ_SimpleShutdown();
207 * Free up all LiveConnect resources. Destroy the Java VM if it was
208 * created by LiveConnect.
213 JS_ASSERT(the_jsj_vm
);
214 JSJ_DisconnectFromJavaVM(the_jsj_vm
);
217 the_global_js_obj
= NULL
;
218 the_jsj_thread
= NULL
;