1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
12 #include "sjme/alloc.h"
13 #include "sjme/nvm/boot.h"
14 #include "sjme/debug.h"
16 /** Default amount of memory. */
17 #define SJME_JVM_INIT_MEMORY 67108864
20 * Creates a new Java Virtual Machine.
22 * @param outVm The resultant virtual machine.
23 * @param outEnv The output environment.
24 * @param vmArgs The arguments to the virtual machine creation.
27 sjme_attrUnused jint JNICALL
JNI_CreateJavaVM(
28 sjme_attrOutNotNull JavaVM
** outVm
,
29 sjme_attrOutNotNull
void** outEnv
,
30 sjme_attrInNotNull
void* vmArgs
)
32 struct JNIInvokeInterface_
* resultVm
;
33 struct JNINativeInterface_
* resultEnv
;
34 sjme_alloc_pool
* pool
;
39 if (outVm
== NULL
|| outEnv
== NULL
|| vmArgs
== NULL
)
45 /* Negative number of options?. */
46 if (args
->nOptions
< 0)
49 /* Either too old or too new. */
50 if (args
->version
< JNI_VERSION_1_1
|| args
->version
> JNI_VERSION_1_8
)
53 #if defined(SJME_CONFIG_DEBUG)
55 /* OpenJDK sends these: */
56 /* -Djava.class.path=. */
57 /* -Dsun.java.launcher=SUN_STANDARD */
58 /* -Dsun.java.launcher.pid=30954 */
59 for (i
= 0; i
< args
->nOptions
; i
++)
60 sjme_message("Arg %d: %s", i
, args
->options
[i
].optionString
);
63 /* Allocate the memory needed for SquirrelJME. */
65 if (sjme_error_is(sjme_alloc_poolInitMalloc(&pool
,
66 SJME_JVM_INIT_MEMORY
)) || pool
== NULL
)
69 /* Allocate resultant function structure. */
71 if (sjme_error_is(sjme_alloc(pool
, sizeof(*resultVm
),
72 (void**)&resultVm
)) ||
74 goto fail_allocResultVm
;
76 /* Allocate environment based functions. */
78 if (sjme_error_is(sjme_alloc(pool
, sizeof(*resultEnv
),
79 (void**)&resultEnv
)) ||
81 goto fail_allocResultEnv
;
83 /* Boot the virtual machine. */
85 if (sjme_error_is(sjme_nvm_boot(pool
,
86 NULL
, NULL
, &nvmState
)) || nvmState
== NULL
)
89 /* Store the environment and VM state into both structures the same. */
90 resultVm
->reserved0
= resultVm
;
91 resultVm
->reserved1
= resultEnv
;
92 resultVm
->reserved2
= nvmState
;
93 resultEnv
->reserved0
= resultVm
;
94 resultEnv
->reserved1
= resultEnv
;
95 resultEnv
->reserved2
= nvmState
;
97 /* Then link back to both. */
98 nvmState
->common
.frontEnd
.wrapper
= resultVm
;
99 nvmState
->common
.frontEnd
.data
= resultEnv
;
108 if (resultEnv
!= NULL
)
109 sjme_alloc_free(resultEnv
);
111 if (resultVm
!= NULL
)
112 sjme_alloc_free(resultVm
);
118 * Obtains the default virtual machine configuration.
120 * @param vmArgs A @c JavaVMInitArgs , the @c version field must be set before
122 * @return Either @c JNI_OK or an error such as if the Java version is not
126 sjme_attrUnused jint JNICALL
JNI_GetDefaultJavaVMInitArgs(
127 sjme_attrInOutNotNull
void* vmArgs
)
129 JavaVMInitArgs
* args
;
134 /* This is aliased under void. */
137 /* Either too old or too new. */
138 if (args
->version
< JNI_VERSION_1_1
|| args
->version
> JNI_VERSION_1_8
)
141 /* Indicate that we support this version. */
142 args
->version
= JNI_VERSION_1_8
;
146 args
->options
= NULL
;