2008-02-19 18:06:52 +00:00
|
|
|
/* Copyright (c) 2008, Avian Contributors
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2007-10-25 15:04:13 +00:00
|
|
|
#include "stdlib.h"
|
2007-10-25 22:06:05 +00:00
|
|
|
#include "stdio.h"
|
2007-10-25 15:04:13 +00:00
|
|
|
#include "string.h"
|
2008-01-19 00:54:36 +00:00
|
|
|
#include "stdint.h"
|
2007-10-25 15:04:13 +00:00
|
|
|
#include "jni.h"
|
2007-06-20 19:20:25 +00:00
|
|
|
|
2008-01-12 00:34:57 +00:00
|
|
|
// since we don't link against libstdc++, we must implement some dummy
|
|
|
|
// functions:
|
2007-10-22 20:56:27 +00:00
|
|
|
extern "C" void __cxa_pure_virtual(void) { abort(); }
|
2007-10-25 15:04:13 +00:00
|
|
|
void operator delete(void*) { abort(); }
|
2007-07-16 01:03:02 +00:00
|
|
|
|
2008-01-19 00:54:36 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
# define PATH_SEPARATOR ';'
|
|
|
|
# define EXPORT __declspec(dllexport)
|
|
|
|
# define SYMBOL(x) binary_classpath_jar_##x
|
|
|
|
#else
|
|
|
|
# define PATH_SEPARATOR ':'
|
|
|
|
# define EXPORT __attribute__ ((visibility("default")))
|
|
|
|
# define SYMBOL(x) _binary_classpath_jar_##x
|
|
|
|
#endif
|
|
|
|
|
2008-01-23 17:12:56 +00:00
|
|
|
#define BOOT_CLASSPATH "[classpathJar]"
|
2008-01-19 00:54:36 +00:00
|
|
|
|
2008-01-23 17:12:56 +00:00
|
|
|
extern "C" {
|
2008-01-19 00:54:36 +00:00
|
|
|
|
|
|
|
extern const uint8_t SYMBOL(start)[];
|
|
|
|
extern const uint8_t SYMBOL(size)[];
|
|
|
|
|
|
|
|
EXPORT const uint8_t*
|
|
|
|
classpathJar(unsigned* size)
|
|
|
|
{
|
|
|
|
*size = reinterpret_cast<uintptr_t>(SYMBOL(size));
|
|
|
|
return SYMBOL(start);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-10-28 01:54:30 +00:00
|
|
|
#ifdef JNI_VERSION_1_6
|
|
|
|
// todo: use JavaVMInitArgs instead
|
|
|
|
typedef struct JDK1_1InitArgs {
|
|
|
|
jint version;
|
|
|
|
|
|
|
|
char **properties;
|
|
|
|
jint checkSource;
|
|
|
|
jint nativeStackSize;
|
|
|
|
jint javaStackSize;
|
|
|
|
jint minHeapSize;
|
|
|
|
jint maxHeapSize;
|
|
|
|
jint verifyMode;
|
|
|
|
char *classpath;
|
|
|
|
|
|
|
|
jint (JNICALL *vfprintf)(FILE *fp, const char *format, va_list args);
|
|
|
|
void (JNICALL *exit)(jint code);
|
|
|
|
void (JNICALL *abort)(void);
|
|
|
|
|
|
|
|
jint enableClassGC;
|
|
|
|
jint enableVerboseGC;
|
|
|
|
jint disableAsyncGC;
|
|
|
|
jint verbose;
|
|
|
|
jboolean debugging;
|
|
|
|
jint debugPort;
|
|
|
|
} JDK1_1InitArgs;
|
|
|
|
#endif
|
|
|
|
|
2007-10-25 15:04:13 +00:00
|
|
|
namespace {
|
2007-06-20 19:20:25 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
usageAndExit(const char* name)
|
|
|
|
{
|
2007-10-25 15:04:13 +00:00
|
|
|
fprintf(stderr, "usage: %s [-cp <classpath>] [-Xmx<maximum heap size>] "
|
2007-06-20 21:27:22 +00:00
|
|
|
"<class name> [<argument> ...]\n", name);
|
2007-06-20 19:20:25 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int ac, const char** av)
|
|
|
|
{
|
2007-10-25 15:04:13 +00:00
|
|
|
JDK1_1InitArgs vmArgs;
|
|
|
|
vmArgs.version = 0x00010001;
|
|
|
|
JNI_GetDefaultJavaVMInitArgs(&vmArgs);
|
|
|
|
|
2007-06-20 19:20:25 +00:00
|
|
|
const char* class_ = 0;
|
|
|
|
int argc = 0;
|
|
|
|
const char** argv = 0;
|
2007-10-25 18:33:43 +00:00
|
|
|
int propertyCount = 0;
|
2007-06-20 19:20:25 +00:00
|
|
|
|
|
|
|
for (int i = 1; i < ac; ++i) {
|
|
|
|
if (strcmp(av[i], "-cp") == 0) {
|
2007-10-25 15:04:13 +00:00
|
|
|
vmArgs.classpath = const_cast<char*>(av[++i]);
|
|
|
|
} else if (strncmp(av[i], "-Xmx", 4) == 0) {
|
|
|
|
vmArgs.maxHeapSize = atoi(av[i] + 4);
|
2007-10-25 18:33:43 +00:00
|
|
|
} else if (strncmp(av[i], "-D", 2) == 0) {
|
|
|
|
++ propertyCount;
|
2007-06-20 19:20:25 +00:00
|
|
|
} else {
|
|
|
|
class_ = av[i++];
|
|
|
|
if (i < ac) {
|
|
|
|
argc = ac - i;
|
|
|
|
argv = av + i;
|
|
|
|
i = ac;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-19 00:54:36 +00:00
|
|
|
#ifdef BOOT_CLASSPATH
|
|
|
|
unsigned size = sizeof(BOOT_CLASSPATH) + 1 + strlen(vmArgs.classpath);
|
|
|
|
char classpath[size];
|
|
|
|
snprintf(classpath, size, "%s%c%s",
|
|
|
|
BOOT_CLASSPATH, PATH_SEPARATOR, vmArgs.classpath);
|
|
|
|
vmArgs.classpath = classpath;
|
|
|
|
#endif
|
|
|
|
|
2007-10-25 18:33:43 +00:00
|
|
|
const char* properties[propertyCount + 1];
|
|
|
|
properties[propertyCount] = 0;
|
|
|
|
for (int i = 1; i < ac; ++i) {
|
|
|
|
if (strncmp(av[i], "-D", 2) == 0) {
|
|
|
|
properties[--propertyCount] = av[i] + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vmArgs.properties = const_cast<char**>(properties);
|
|
|
|
|
2007-06-20 19:20:25 +00:00
|
|
|
if (class_ == 0) {
|
|
|
|
usageAndExit(av[0]);
|
|
|
|
}
|
|
|
|
|
2007-10-25 15:04:13 +00:00
|
|
|
JavaVM* vm;
|
2007-10-25 22:34:40 +00:00
|
|
|
void* env;
|
|
|
|
JNI_CreateJavaVM(&vm, &env, &vmArgs);
|
|
|
|
JNIEnv* e = static_cast<JNIEnv*>(env);
|
2007-10-25 15:04:13 +00:00
|
|
|
|
|
|
|
jclass c = e->FindClass(class_);
|
|
|
|
if (not e->ExceptionOccurred()) {
|
|
|
|
jmethodID m = e->GetStaticMethodID(c, "main", "([Ljava/lang/String;)V");
|
|
|
|
if (not e->ExceptionOccurred()) {
|
|
|
|
jclass stringClass = e->FindClass("java/lang/String");
|
|
|
|
if (not e->ExceptionOccurred()) {
|
|
|
|
jobjectArray a = e->NewObjectArray(argc, stringClass, 0);
|
|
|
|
if (not e->ExceptionOccurred()) {
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
e->SetObjectArrayElement(a, i, e->NewStringUTF(argv[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
e->CallStaticVoidMethod(c, m, a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int exitCode = 0;
|
|
|
|
if (e->ExceptionOccurred()) {
|
|
|
|
exitCode = -1;
|
|
|
|
e->ExceptionDescribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
vm->DestroyJavaVM();
|
|
|
|
|
|
|
|
return exitCode;
|
2007-06-20 19:20:25 +00:00
|
|
|
}
|