diff --git a/mozilla/js/ref/macbuild/JSRef.mcp b/mozilla/js/ref/macbuild/JSRef.mcp new file mode 100644 index 00000000000..4d3d064fda4 Binary files /dev/null and b/mozilla/js/ref/macbuild/JSRef.mcp differ diff --git a/mozilla/js/ref/macbuild/JavaSession/JavaSession.cpp b/mozilla/js/ref/macbuild/JavaSession/JavaSession.cpp new file mode 100644 index 00000000000..08bec3e5b8a --- /dev/null +++ b/mozilla/js/ref/macbuild/JavaSession/JavaSession.cpp @@ -0,0 +1,154 @@ +/* -*- 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. + */ + +/* + JavaSession.cpp + + Uses MRJ to open a Java VM. + + by Patrick C. Beard. + */ + +#include "JavaSession.h" + +#include +#include +#include + +#include + +extern "C" { + +static void java_stdout(JMSessionRef session, const void *message, UInt32 messageLengthInBytes); +static void java_stderr(JMSessionRef session, const void *message, UInt32 messageLengthInBytes); +static SInt32 java_stdin(JMSessionRef session, void *buffer, SInt32 maxBufferLength); +static Boolean java_exit(JMSessionRef session, int value); +static Boolean java_authenticate(JMSessionRef session, const char *url, const char *realm, + char userName[255], char password[255]); +static void java_lowmem(JMSessionRef session); + +} + +static JMSessionCallbacks callbacks = { + kJMVersion, /* should be set to kJMVersion */ + &java_stdout, /* JM will route "stdout" to this function. */ + &java_stderr, /* JM will route "stderr" to this function. */ + &java_stdin, /* read from console - can be nil for default behavior (no console IO) */ + &java_exit, /* handle System.exit(int) requests */ + &java_authenticate, /* present basic authentication dialog */ + &java_lowmem /* Low Memory notification Proc */ +}; + +JavaSession::JavaSession() : mSession(NULL) +{ + OSStatus status = ::JMOpenSession(&mSession, /* eDisableJITC */ eJManager2Defaults, eCheckRemoteCode, + &callbacks, kTextEncodingMacRoman, NULL); + checkStatus(status); +} + +JavaSession::~JavaSession() +{ + if (mSession != NULL) { + OSStatus status = ::JMCloseSession(mSession); + checkStatus(status); + } +} + +JNIEnv* JavaSession::getEnv() +{ + return ::JMGetCurrentEnv(mSession); +} + +static StringPtr c2p(const char* str, StringPtr pstr) +{ + unsigned char len = strlen(str); + memcpy(pstr + 1, str, len); + *pstr = len; + return pstr; +} + +jclass JavaSession::getClass(const char* className) +{ + JNIEnv* env = getEnv(); + jclass result = env->FindClass(className); + if (result == NULL) throw OSStatusException(fnfErr); +/* if (result == NULL) { + Str255 pClassName; + c2p(className, pClassName); + Handle classHandle = ::GetNamedResource(ClassResType, pClassName); + if (classHandle != NULL) { + ::HLock(classHandle); + result = env->DefineClass(className, NULL, (signed char*)*classHandle, ::GetHandleSize(classHandle)); + ::ReleaseResource(classHandle); + } + } */ + return result; +} + +/** + * Adds a Mac-style path name to the MRJ class path. + */ +void JavaSession::addClassPath(const char* jarFilePath) +{ + Str255 pJarFilePath; + FSSpec jarFileSpec; + OSStatus status = FSMakeFSSpec( 0, 0, // use "current working directory" + c2p(jarFilePath, pJarFilePath), + &jarFileSpec); + checkStatus(status); + status = JMAddToClassPath(mSession, &jarFileSpec); + checkStatus(status); +} + +// OBLIGATORY JMSession callbacks. + +static void java_stdout(JMSessionRef session, const void *message, UInt32 messageLengthInBytes) +{ + char* msg = (char*)message; + while (messageLengthInBytes--) { + char c = *msg++; + if (c == '\r') + c = '\n'; + fputc(c, stdout); + } +} + +static void java_stderr(JMSessionRef session, const void *message, UInt32 messageLengthInBytes) +{ + char* msg = (char*)message; + while (messageLengthInBytes--) { + char c = *msg++; + if (c == '\r') + c = '\n'; + fputc(c, stderr); + } +} + +static SInt32 java_stdin(JMSessionRef session, void *buffer, SInt32 maxBufferLength) { return -1; } + +static Boolean java_exit(JMSessionRef session, int value) { return false; } + +static Boolean java_authenticate(JMSessionRef session, const char *url, const char *realm, + char userName[255], char password[255]) +{ + return true; +} + +static void java_lowmem(JMSessionRef session) +{ +} diff --git a/mozilla/js/ref/macbuild/JavaSession/JavaSession.h b/mozilla/js/ref/macbuild/JavaSession/JavaSession.h new file mode 100644 index 00000000000..4ad066d8683 --- /dev/null +++ b/mozilla/js/ref/macbuild/JavaSession/JavaSession.h @@ -0,0 +1,46 @@ +/* -*- 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. + */ + +/* + JavaSession.h + + Uses MRJ to open a Java VM. + + by Patrick C. Beard + */ + +#pragma once + +#include +#include + +#include "OSStatusException.h" + +class JavaSession { +public: + JavaSession(); + ~JavaSession(); + + JNIEnv* getEnv(); + jclass getClass(const char* className); + + void addClassPath(const char* jarFilePath); + +private: + JMSessionRef mSession; +}; diff --git a/mozilla/js/ref/macbuild/JavaSession/OSStatusException.h b/mozilla/js/ref/macbuild/JavaSession/OSStatusException.h new file mode 100644 index 00000000000..3a7c37abf27 --- /dev/null +++ b/mozilla/js/ref/macbuild/JavaSession/OSStatusException.h @@ -0,0 +1,46 @@ +/* -*- 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. + */ + +/* + OSStatusException.h + + MacOS OSStatus exception. + + by Patrick C. Beard. + */ + +#pragma once + +#ifndef __TYPES__ +#include +#endif + +class OSStatusException { +public: + OSStatusException(OSStatus status) : mStatus(status) {} + OSStatus getStatus() { return mStatus; } + +private: + OSStatus mStatus; +}; + +inline void checkStatus(OSStatus status) +{ + if (status != noErr) + throw OSStatusException(status); +} diff --git a/mozilla/js/ref/prmacos.cpp b/mozilla/js/ref/prmacos.cpp new file mode 100644 index 00000000000..6f641100197 --- /dev/null +++ b/mozilla/js/ref/prmacos.cpp @@ -0,0 +1,101 @@ +/* -*- 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. + */ + +/* + prmacos.cpp + + Mac OS Support functions for LiveConnect. Provides standard JNI JavaVM creation + functions, and strdup. + + by Patrick C. Beard. + */ + +#include +#include + +#include "JavaSession.h" + +#include "prtypes.h" +#include "prmacos.h" + +char* strdup(const char *str) +{ + char* dup = (char*) malloc(1 + strlen(str)); + if (dup != NULL) + strcpy(dup, str); + return dup; +} + +// Fake VM initialization + +jint JNICALL JNI_GetDefaultJavaVMInitArgs(void* args) +{ + if (args != NULL) { + JDK1_1InitArgs* initArgs = (JDK1_1InitArgs*)args; + memset(initArgs, 0, sizeof(JDK1_1InitArgs)); + return 0; + } + return -1; +} + +static JavaSession* theSession = NULL; +static const JNIInvokeInterface_* theActualFunctions; +static JNIInvokeInterface_ thePatchedFunctions; + +static jint MRJ_DestroyJavaVM(JavaVM* javaVM) +{ + // Restore the original functions. + javaVM->functions = theActualFunctions; + + // Toss the Java session instead. + if (theSession != NULL) { + delete theSession; + theSession = NULL; + return 0; + } + return -1; +} + +jint JNICALL JNI_CreateJavaVM(JavaVM** outVM, JNIEnv ** outEnv, void* args) +{ + int result = -1; + *outVM = NULL; + *outEnv = NULL; + + try { + if (theSession == NULL) + theSession = new JavaSession(); + JNIEnv* env = theSession->getEnv(); + JavaVM* javaVM = NULL; + result = env->GetJavaVM(&javaVM); + if (result == 0) { + *outEnv = env; + *outVM = javaVM; + + // patch the JavaVM so it won't actually destroy itself. This crashes MRJ right now. + theActualFunctions = javaVM->functions; + thePatchedFunctions = *theActualFunctions; + thePatchedFunctions.DestroyJavaVM = MRJ_DestroyJavaVM; + javaVM->functions = &thePatchedFunctions; + } + } catch (OSStatusException status) { + *outVM = NULL; + *outEnv = NULL; + } + return result; +}