C: fix MSVC support

This commit is contained in:
Alexey Andreev 2019-05-28 20:13:14 +03:00
parent 1899641f63
commit d17b459ecc
5 changed files with 88 additions and 35 deletions

View File

@ -478,7 +478,7 @@ public class ClassGenerator {
headerWriter.print("extern ").print(structName).print(" ").print(name).println(";"); headerWriter.print("extern ").print(structName).print(" ").print(name).println(";");
if (classLayout != null) { if (classLayout != null) {
codeWriter.println("static int16_t teavm_classLayouts_" + name + "[];"); codeWriter.println("static int16_t teavm_classLayouts_" + name + "[" + (classLayout.length + 1) + "];");
} }
codeWriter.print("alignas(8) ").print(structName).print(" ").print(name).println(" = {").indent(); codeWriter.print("alignas(8) ").print(structName).print(" ").print(name).println(" = {").indent();
@ -741,7 +741,8 @@ public class ClassGenerator {
} }
String name = context.getNames().forClassInstance(ValueType.object(className)); String name = context.getNames().forClassInstance(ValueType.object(className));
codeWriter.print("static int16_t teavm_classLayouts_" + name + "[" + classLayout.length + " + 1] = {").indent(); codeWriter.print("static int16_t teavm_classLayouts_" + name + "[" + (classLayout.length + 1) + "] = {")
.indent();
codeWriter.println().print("INT16_C(" + classLayout.length + ")"); codeWriter.println().print("INT16_C(" + classLayout.length + ")");
for (FieldReference field : classLayout) { for (FieldReference field : classLayout) {

View File

@ -12,6 +12,11 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#endif #endif
#ifdef _MSC_VER
#define timegm _mkgmtime
#define localtime_r(a, b) localtime_s(b, a)
#endif
#include <time.h> #include <time.h>
static time_t teavm_epochStart; static time_t teavm_epochStart;
@ -73,10 +78,14 @@ int64_t teavm_date_createUtc(int32_t year, int32_t month, int32_t day, int32_t h
} }
int64_t teavm_date_parse(char* s) { int64_t teavm_date_parse(char* s) {
#ifdef __GNUC__
struct tm t; struct tm t;
strptime(s, teavm_date_defaultFormat, &t); strptime(s, teavm_date_defaultFormat, &t);
time_t result = mktime(&t); time_t result = mktime(&t);
return (int64_t) (1000 * difftime(result, teavm_epochStart)); return (int64_t) (1000 * difftime(result, teavm_epochStart));
#else
return 0;
#endif
} }
int32_t teavm_date_getYear(int64_t time) { int32_t teavm_date_getYear(int64_t time) {

View File

@ -18,6 +18,7 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#include <Windows.h> #include <Windows.h>
#include <synchapi.h>
#endif #endif
void* teavm_gc_heapAddress = NULL; void* teavm_gc_heapAddress = NULL;
@ -55,6 +56,10 @@ TeaVM_ResourceMapEntry* teavm_lookupResource(TeaVM_ResourceMap *map, TeaVM_Strin
static timer_t teavm_queueTimer; static timer_t teavm_queueTimer;
#endif #endif
#ifdef _MSC_VER
static HANDLE teavm_queueTimer;
#endif
void teavm_beforeInit() { void teavm_beforeInit() {
srand(time(NULL)); srand(time(NULL));
@ -73,7 +78,10 @@ void teavm_beforeInit() {
sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN; sev.sigev_signo = SIGRTMIN;
timer_create(CLOCK_REALTIME, &sev, &teavm_queueTimer); timer_create(CLOCK_REALTIME, &sev, &teavm_queueTimer);
#endif
#ifdef _MSC_VER
teavm_queueTimer = CreateEvent(NULL, TRUE, FALSE, TEXT("TeaVM_eventQueue"));
#endif #endif
} }
@ -218,6 +226,19 @@ void teavm_interrupt() {
#endif #endif
#ifdef _MSC_VER
void teavm_waitFor(int64_t timeout) {
WaitForSingleObject(teavm_queueTimer, timeout);
ResetEvent(teavm_queueTimer);
}
void teavm_interrupt() {
SetEvent(teavm_queueTimer);
}
#endif
void teavm_outOfMemory() { void teavm_outOfMemory() {
fprintf(stderr, "Application crashed due to lack of free memory\n"); fprintf(stderr, "Application crashed due to lack of free memory\n");
exit(1); exit(1);
@ -279,7 +300,7 @@ TeaVM_Array* teavm_resourceMapKeys(TeaVM_ResourceMap *map) {
size_t teavm_mbSize(char16_t* javaChars, int32_t javaCharsCount) { size_t teavm_mbSize(char16_t* javaChars, int32_t javaCharsCount) {
size_t sz = 0; size_t sz = 0;
char buffer[__STDC_UTF_16__]; char buffer[8];
mbstate_t state = {0}; mbstate_t state = {0};
for (int32_t i = 0; i < javaCharsCount; ++i) { for (int32_t i = 0; i < javaCharsCount; ++i) {
size_t result = c16rtomb(buffer, javaChars[i], &state); size_t result = c16rtomb(buffer, javaChars[i], &state);

View File

@ -10,7 +10,8 @@
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
#define alignas(x) #define alignas(n) __declspec(align(n))
#pragma comment (lib,"uuid.lib")
#endif #endif
typedef struct TeaVM_Object { typedef struct TeaVM_Object {

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
class CRunStrategy implements TestRunStrategy { class CRunStrategy implements TestRunStrategy {
private String compilerCommand; private String compilerCommand;
@ -87,25 +88,45 @@ class CRunStrategy implements TestRunStrategy {
return runProcess(new ProcessBuilder(command).directory(inputDir).start(), output); return runProcess(new ProcessBuilder(command).directory(inputDir).start(), output);
} }
private boolean runProcess(Process process, List<String> output) throws IOException, InterruptedException { private boolean runProcess(Process process, List<String> output) throws InterruptedException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader stdin = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stderr = new BufferedReader(new InputStreamReader(process.getErrorStream())); BufferedReader stderr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
ConcurrentLinkedQueue<String> lines = new ConcurrentLinkedQueue<>();
Thread thread = new Thread(() -> {
try {
while (true) { while (true) {
String line = stderr.readLine(); String line = stderr.readLine();
if (line == null) { if (line == null) {
break; break;
} }
output.add(line); lines.add(line);
} }
} catch (IOException e) {
// do nothing
}
});
thread.setDaemon(true);
thread.start();
thread = new Thread(() -> {
try {
while (true) { while (true) {
String line = stdin.readLine(); String line = stdin.readLine();
if (line == null) { if (line == null) {
break; break;
} }
output.add(line); lines.add(line);
} }
} catch (IOException e) {
// do nothing
}
});
thread.setDaemon(true);
thread.start();
return process.waitFor() == 0; boolean result = process.waitFor() == 0;
output.addAll(lines);
return result;
} }
} }