jni-libjvm.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // jni-libjvm.cc - an implementation of the JNI invocation API.
  2. /* Copyright (C) 2006 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. #include <gcj/cni.h>
  8. #include <gcj/javaprims.h>
  9. #include <java-assert.h>
  10. #include <jvm.h>
  11. #include <jni.h>
  12. using namespace gcj;
  13. // Forward declarations.
  14. extern struct JNIInvokeInterface_ _Jv_JNI_InvokeFunctions;
  15. extern jint JNICALL _Jv_JNI_AttachCurrentThread (JavaVM *vm,
  16. void **penv, void *args);
  17. extern JavaVM *_Jv_the_vm;
  18. jint JNICALL
  19. JNI_GetDefaultJavaVMInitArgs (void *args)
  20. {
  21. jint version = * (jint *) args;
  22. // Here we only support 1.2 and 1.4.
  23. if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
  24. return JNI_EVERSION;
  25. JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
  26. ia->version = JNI_VERSION_1_4;
  27. ia->nOptions = 0;
  28. ia->options = NULL;
  29. ia->ignoreUnrecognized = true;
  30. return 0;
  31. }
  32. jint JNICALL
  33. JNI_CreateJavaVM (JavaVM **vm, void **penv, void *args)
  34. {
  35. JvAssert (! _Jv_the_vm);
  36. jint version = * (jint *) args;
  37. // We only support 1.2 and 1.4.
  38. if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
  39. return JNI_EVERSION;
  40. JvVMInitArgs* vm_args = reinterpret_cast<JvVMInitArgs *> (args);
  41. jint result = _Jv_CreateJavaVM (vm_args);
  42. if (result)
  43. return result;
  44. // FIXME: synchronize
  45. JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
  46. if (nvm == NULL)
  47. return JNI_ERR;
  48. nvm->functions = &_Jv_JNI_InvokeFunctions;
  49. jint r =_Jv_JNI_AttachCurrentThread (nvm, penv, NULL);
  50. if (r < 0)
  51. return r;
  52. _Jv_the_vm = nvm;
  53. *vm = _Jv_the_vm;
  54. return 0;
  55. }
  56. jint JNICALL
  57. JNI_GetCreatedJavaVMs (JavaVM **vm_buffer, jsize buf_len, jsize *n_vms)
  58. {
  59. if (buf_len <= 0)
  60. return JNI_ERR;
  61. // We only support a single VM.
  62. if (_Jv_the_vm != NULL)
  63. {
  64. vm_buffer[0] = _Jv_the_vm;
  65. *n_vms = 1;
  66. }
  67. else
  68. *n_vms = 0;
  69. return 0;
  70. }