forked from polyglot-compiler/JLang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (58 loc) · 2.18 KB
/
Copy pathmain.cpp
File metadata and controls
66 lines (58 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//Copyright (C) 2018 Cornell University
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "factory.h"
#include "gc.h"
#include "rep.h"
#include "stack_trace.h"
extern "C" {
void Polyglot_jlang_runtime_MainWrapper_runMain___3Ljava_lang_String_2(jarray args);
jarray Polyglot_jlang_runtime_Factory_ObjectArray__I(jint len);
void Polyglot_java_lang_System_initializeSystemClass__();
} // extern "C"
static void sigaction(int sig, siginfo_t* info, void* ucontext) {
const char* cause = "";
if (sig == SIGSEGV)
cause = "This likely indicates a null pointer exception.\n";
if (sig == SIGFPE)
cause = "This indicates an arithmetic exception "
"(e.g., divide by zero).\n";
fprintf(stderr,
"- - - - - - - - - - - - - - - - - - - - - - - - - - -\n"
"Aborting due to signal: %s\n%s"
"- - - - - - - - - - - - - - - - - - - - - - - - - - -\n"
, strsignal(sig), cause);
DumpStackTrace();
fflush(stderr);
abort();
}
int main(int argc, char** argv) {
// Initialize the garbage collector.
GC_INIT();
// Set up signal handling to report (for example) null pointer exceptions.
struct sigaction sa;
sa.sa_sigaction = sigaction;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV, &sa, 0);
sigaction(SIGBUS, &sa, 0);
sigaction(SIGFPE, &sa, 0);
// Ignore the 0th argument, which is the name of the program.
--argc, ++argv;
jarray args = Polyglot_jlang_runtime_Factory_ObjectArray__I(argc);
jstring* args_data = reinterpret_cast<jstring*>(Unwrap(args)->Data());
for (int i = 0; i < argc; ++i) {
size_t len = strlen(argv[i]);
jcharArray argChars = CreateJavaCharArray(len);
jchar* data = reinterpret_cast<jchar*>(Unwrap(argChars)->Data());
// TODO: Not a proper character encoding conversion.
for (size_t j = 0; j < len; ++j)
data[j] = static_cast<jchar>(argv[i][j]);
jstring argStr = CreateJavaString(argChars);
args_data[i] = argStr;
}
Polyglot_java_lang_System_initializeSystemClass__();
Polyglot_jlang_runtime_MainWrapper_runMain___3Ljava_lang_String_2(args);
}