Setup and clear of string pool.
[SquirrelJME.git] / nanocoat / frontend / libjvm / vmInit.c
blobf11881c9f2c7a32baa499aa59cf92d499ecbec47
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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 // -------------------------------------------------------------------------*/
10 #include <jni.h>
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
19 /**
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.
25 * @return
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;
35 sjme_nvm nvmState;
36 JavaVMInitArgs* args;
37 jint i;
39 if (outVm == NULL || outEnv == NULL || vmArgs == NULL)
40 return JNI_EINVAL;
42 /* Aliased. */
43 args = vmArgs;
45 /* Negative number of options?. */
46 if (args->nOptions < 0)
47 return JNI_EINVAL;
49 /* Either too old or too new. */
50 if (args->version < JNI_VERSION_1_1 || args->version > JNI_VERSION_1_8)
51 return JNI_EVERSION;
53 #if defined(SJME_CONFIG_DEBUG)
54 /* 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);
61 #endif
63 /* Allocate the memory needed for SquirrelJME. */
64 pool = NULL;
65 if (sjme_error_is(sjme_alloc_poolInitMalloc(&pool,
66 SJME_JVM_INIT_MEMORY)) || pool == NULL)
67 return JNI_ENOMEM;
69 /* Allocate resultant function structure. */
70 resultVm = NULL;
71 if (sjme_error_is(sjme_alloc(pool, sizeof(*resultVm),
72 (void**)&resultVm)) ||
73 resultVm == NULL)
74 goto fail_allocResultVm;
76 /* Allocate environment based functions. */
77 resultEnv = NULL;
78 if (sjme_error_is(sjme_alloc(pool, sizeof(*resultEnv),
79 (void**)&resultEnv)) ||
80 resultEnv == NULL)
81 goto fail_allocResultEnv;
83 /* Boot the virtual machine. */
84 nvmState = NULL;
85 if (sjme_error_is(sjme_nvm_boot(pool,
86 NULL, NULL, &nvmState)) || nvmState == NULL)
87 goto fail_nvmBoot;
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;
101 /* Success! */
102 **outVm = resultVm;
103 *outEnv = resultEnv;
104 return JNI_OK;
106 fail_nvmBoot:
107 fail_allocResultEnv:
108 if (resultEnv != NULL)
109 sjme_alloc_free(resultEnv);
110 fail_allocResultVm:
111 if (resultVm != NULL)
112 sjme_alloc_free(resultVm);
114 return JNI_ERR;
118 * Obtains the default virtual machine configuration.
120 * @param vmArgs A @c JavaVMInitArgs , the @c version field must be set before
121 * this is called.
122 * @return Either @c JNI_OK or an error such as if the Java version is not
123 * supported.
124 * @since 2024/03/18
126 sjme_attrUnused jint JNICALL JNI_GetDefaultJavaVMInitArgs(
127 sjme_attrInOutNotNull void* vmArgs)
129 JavaVMInitArgs* args;
131 if (vmArgs == NULL)
132 return JNI_EINVAL;
134 /* This is aliased under void. */
135 args = vmArgs;
137 /* Either too old or too new. */
138 if (args->version < JNI_VERSION_1_1 || args->version > JNI_VERSION_1_8)
139 return JNI_EVERSION;
141 /* Indicate that we support this version. */
142 args->version = JNI_VERSION_1_8;
144 /* Clear these. */
145 args->nOptions = 0;
146 args->options = NULL;
148 /* Success! */
149 return JNI_OK;