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 // -------------------------------------------------------------------------*/
13 #include "sjme/debug.h"
14 #include "sjme/alloc.h"
15 #include "sjme/nvm/boot.h"
16 #include "sjme/nvm/nvm.h"
17 #include "sjme/nvm/nvmFunc.h"
20 * Main program entry point.
22 * @param argc Argument count.
23 * @param argv Arguments passed.
24 * @return Returns @c 0 on success, otherwise another exit code.
26 int main(int argc
, sjme_lpcstr
* argv
)
29 sjme_alloc_pool
* pool
;
31 sjme_nvm_bootParam bootParam
;
33 sjme_jboolean terminated
;
36 /* Use default NAL. */
37 nal
= &sjme_nal_default
;
39 /* Allocate main pool. */
41 if (sjme_error_is(error
= sjme_alloc_poolInitMalloc(&pool
,
42 16777216)) || pool
== NULL
)
45 /* Setup boot parameters. */
46 memset(&bootParam
, 0, sizeof(bootParam
));
49 /* Parse main arguments. */
50 if (sjme_error_is(error
= sjme_nvm_parseCommandLine(pool
,
51 nal
, &bootParam
, argc
, argv
)))
53 /* Exit instead of continuing? */
54 if (error
== SJME_ERROR_EXIT
)
63 /* If there was no boot suite specified, load a default one. */
64 if (bootParam
.bootSuite
== NULL
)
65 if (sjme_error_is(error
= sjme_nvm_defaultBootSuite(pool
,
66 nal
, &bootParam
.bootSuite
)) ||
67 bootParam
.bootSuite
== NULL
)
68 goto fail_bootSuiteBackup
;
70 /* If no classpath was specified, load the launcher instead. */
71 if (bootParam
.mainClassPathById
== NULL
&&
72 bootParam
.mainClassPathByName
== NULL
)
74 /* Try to find default launcher. */
75 if (sjme_error_is(error
= sjme_rom_suiteDefaultLaunch(pool
,
77 (sjme_lpstr
*)&bootParam
.mainClass
,
78 (sjme_list_sjme_lpstr
**)&bootParam
.mainArgs
,
79 (sjme_list_sjme_jint
**)&bootParam
.mainClassPathById
,
80 (sjme_list_sjme_lpstr
**)&bootParam
.mainClassPathByName
)))
81 goto fail_defaultLaunch
;
84 if (bootParam
.mainClassPathById
== NULL
&&
85 bootParam
.mainClassPathByName
== NULL
)
87 error
= SJME_ERROR_NO_CLASS
;
88 goto fail_defaultLaunch
;
92 /* Boot the virtual machine. */
94 if (sjme_error_is(error
= sjme_nvm_boot(pool
,
95 NULL
, &bootParam
, &inState
)))
98 /* Iterate the virtual machine loop. */
99 for (terminated
= SJME_JNI_FALSE
; !terminated
;)
101 /* Let other threads run. */
104 /* Tick the virtual machine. */
105 if (sjme_error_is(error
= sjme_nvm_tick(inState
, -1,
108 /* Fail unless this was interrupted. */
109 if (error
== SJME_ERROR_INTERRUPTED
)
116 /* Destroy the VM before exit. */
118 if (sjme_error_is(error
= sjme_nvm_destroy(inState
, &exitCode
)))
122 /* Destroy the main memory pool. */
123 if (sjme_error_is(error
= sjme_alloc_poolDestroy(pool
)))
124 goto fail_destroyPool
;
126 /* Exit with the given code. */
134 sjme_nvm_destroy(inState
, NULL
);
137 fail_bootSuiteBackup
:
141 sjme_alloc_poolDestroy(pool
);
143 if (error
== SJME_ERROR_NONE
|| error
== SJME_ERROR_UNKNOWN
)