home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / jsj / jsjsa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.9 KB  |  221 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* ** */
  19. /*
  20.  *  stand-alone glue for js<->java
  21.  *  this is used as a default if the host does not override
  22.  *   the glue.
  23.  */
  24.  
  25. #if defined (JAVA)
  26.  
  27. #include "jsapi.h"
  28. #include "jri.h"
  29. #include "jriext.h"
  30. #include "jsjava.h"
  31. #ifndef NSPR20
  32. #include "prglobal.h"
  33. #endif
  34. #include "prthread.h"
  35. #include "prmon.h"
  36. #include "prlog.h"
  37. #include "prcmon.h"
  38.  
  39. #ifndef NSPR20
  40. #define WAIT_FOREVER LL_MAXINT
  41. #else
  42. #define WAIT_FOREVER PR_INTERVAL_NO_TIMEOUT
  43. #endif
  44.  
  45. static JSTaskState *jsjiTask = 0;
  46. static PRMonitor *jsmon = NULL;
  47.  
  48.  
  49. /*
  50.  * callbacks to make js<->java glue work
  51.  */
  52.  
  53. /**
  54.   * Liveconnect callback. 
  55.   * Called to determine if Java is enabled
  56.   *
  57.   */
  58. PR_STATIC_CALLBACK(int)
  59. sajsj_IsEnabled(void)
  60. {
  61.   return (int)PR_TRUE; 
  62. }
  63.  
  64. static JSClass sajsj_global_class = {
  65.     "global", 0,
  66.     JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,
  67.     JS_EnumerateStub, JS_ResolveStub,   JS_ConvertStub,   JS_FinalizeStub
  68. };
  69.  
  70. static JSObject *globalObject = NULL;
  71. static JSContext *globalContext = NULL;
  72.  
  73. /*
  74.  *  callback to find the JSContext for a particular JRIEnv
  75.  *  here, this just returns the only JSContext we have.
  76.  *  in the client, it looks up the java call stack for one?
  77.  */
  78. PR_STATIC_CALLBACK(JSContext *)
  79. sajsj_CurrentContext(JRIEnv *env, char **errp)
  80. {
  81.   *errp = 0;
  82.  
  83.   return globalContext;
  84.  
  85. }
  86.  
  87. PR_STATIC_CALLBACK(JRIEnv *)
  88. sajsj_CurrentEnv(JSContext *cx)
  89. {
  90.     /* FIXME insurance? */
  91.     JRIEnv *env = JRI_GetCurrentEnv();
  92.     PR_ASSERT(env);
  93.     return env;
  94. }
  95.  
  96. /*
  97.  * callbacks to maintain run-to-completion in the client
  98.  * and thread-safety in the standalone java.
  99.  */
  100.  
  101. PR_STATIC_CALLBACK(void)
  102. sajsj_EnterJS(void)
  103. {
  104.     /* should ensure that only one thread can use the same
  105.      * context / object space at once */
  106.     PR_EnterMonitor(jsmon);
  107. }
  108.  
  109. PR_STATIC_CALLBACK(void)
  110. sajsj_ExitJS(void)
  111. {
  112.     PR_ExitMonitor(jsmon);
  113. }
  114.  
  115.  
  116. PR_STATIC_CALLBACK(JSPrincipals *)
  117. sajsj_getJSPrincipalsFromJavaCaller(JSContext *cx, int callerDepth)
  118. {
  119.     return NULL;
  120. }
  121.  
  122. PR_IMPLEMENT(JSObject *)
  123. sajsj_GetDefaultObject(JRIEnv *env, jobject hint)
  124. {
  125.     return globalObject;
  126. }
  127.  
  128. static JSJCallbacks sajsjCallbacks = {
  129.     sajsj_IsEnabled,
  130.     sajsj_CurrentEnv,
  131.     sajsj_CurrentContext,
  132.     sajsj_EnterJS,
  133.     sajsj_ExitJS,
  134.     NULL,
  135.     sajsj_getJSPrincipalsFromJavaCaller,
  136.     sajsj_GetDefaultObject,
  137. };
  138.  
  139.  
  140. static void
  141. sajsj_InitLocked()
  142. {
  143.     JRIEnv *env;
  144.  
  145.     /* if jsj has already been initialized, we don't do this
  146.      * standalone jsj setup, and none of the stuff in this
  147.      * file gets used */
  148.     if (!JSJ_Init(&sajsjCallbacks))
  149.         return;
  150.  
  151.     jsjiTask = JS_Init(8L * 1024L * 1024L);
  152.     jsmon = PR_NewMonitor();
  153.     globalContext = JS_NewContext(jsjiTask, 8192);
  154.     PR_ASSERT(globalContext);
  155.  
  156.     globalObject = NULL;
  157.     JS_AddRoot(globalContext, &globalObject);
  158.     globalObject = JS_NewObject(globalContext, &sajsj_global_class,
  159.                                 NULL, NULL);
  160.     if (!globalObject) {
  161.         PR_ASSERT(globalObject);
  162.         goto trash_cx;
  163.     }
  164.     if (!JS_InitStandardClasses(globalContext, globalObject))
  165.         goto trash_cx;
  166.     if (!JSJ_InitContext(globalContext, globalObject))
  167.         goto trash_cx;
  168.  
  169.  
  170.     env = JRI_GetCurrentEnv();
  171.     PR_ASSERT(env);
  172.  
  173.  
  174.     return;
  175. trash_cx:
  176.     JS_DestroyContext(globalContext);   
  177.     globalContext = NULL;
  178.  
  179. /* FIXME error codes? */
  180.     return;
  181. }
  182.  
  183. PR_IMPLEMENT(void)
  184. JSJ_InitStandalone()
  185. {
  186.     static int initialized = -1;
  187.  
  188.     if (initialized == 1)
  189.         return;
  190.  
  191.     PR_CEnterMonitor(&initialized);
  192.     switch (initialized) {
  193.       case -1:
  194.     /* we're first */
  195.     initialized = 0; /* in progress */
  196.     PR_CExitMonitor(&initialized);
  197.  
  198.     /* do it */
  199.         sajsj_InitLocked();
  200.  
  201.     PR_CEnterMonitor(&initialized);
  202.     initialized = 1;
  203.     PR_CNotifyAll(&initialized);
  204.     break;
  205.       case 0:
  206.     /* in progress */
  207.     PR_CWait(&initialized, WAIT_FOREVER);
  208.     break;
  209.       case 1:
  210.     /* happened since the last check */
  211.     break;
  212.     }
  213.     PR_CExitMonitor(&initialized);
  214.  
  215.     if (!jsjiTask)
  216.     return;  /* FIXME fail loudly! */
  217.  
  218.     return;
  219. }
  220.  
  221. #endif /* defined (JAVA) */