diff --git a/core/src/main/java/org/teavm/runtime/ExceptionHandling.java b/core/src/main/java/org/teavm/runtime/ExceptionHandling.java index 9832660fe..4ce6bc8c7 100644 --- a/core/src/main/java/org/teavm/runtime/ExceptionHandling.java +++ b/core/src/main/java/org/teavm/runtime/ExceptionHandling.java @@ -99,19 +99,21 @@ public final class ExceptionHandling { int handlerId = 0; stackLoop: while (stackFrame != null) { int callSiteId = ShadowStack.getCallSiteId(stackFrame); - CallSite callSite = findCallSiteById(callSiteId, stackFrame); - ExceptionHandler handler = callSite.firstHandler; + if (callSiteId >= 0) { + CallSite callSite = findCallSiteById(callSiteId, stackFrame); + ExceptionHandler handler = callSite.firstHandler; - while (handler != null) { - if (handler.exceptionClass == null || handler.exceptionClass.isSupertypeOf.apply(exceptionClass)) { - handlerId = handler.id; - ShadowStack.setExceptionHandlerId(stackFrame, handler.id); - break stackLoop; + while (handler != null) { + if (handler.exceptionClass == null || handler.exceptionClass.isSupertypeOf.apply(exceptionClass)) { + handlerId = handler.id; + ShadowStack.setExceptionHandlerId(stackFrame, handler.id); + break stackLoop; + } + handler = handler.next; } - handler = handler.next; - } - ShadowStack.setExceptionHandlerId(stackFrame, callSiteId - 1); + ShadowStack.setExceptionHandlerId(stackFrame, callSiteId - 1); + } stackFrame = ShadowStack.getNextStackFrame(stackFrame); } @@ -151,14 +153,16 @@ public final class ExceptionHandling { int size = 0; while (stackFrame != null) { int callSiteId = ShadowStack.getCallSiteId(stackFrame); - CallSite callSite = findCallSiteById(callSiteId, stackFrame); - CallSiteLocation location = callSite.location; - if (isObfuscated() || location == null) { - size++; - } else { - while (location != null) { + if (callSiteId >= 0) { + CallSite callSite = findCallSiteById(callSiteId, stackFrame); + CallSiteLocation location = callSite.location; + if (isObfuscated() || location == null) { size++; - location = location.next; + } else { + while (location != null) { + size++; + location = location.next; + } } } @@ -179,27 +183,29 @@ public final class ExceptionHandling { int index = 0; while (stackFrame != null) { int callSiteId = ShadowStack.getCallSiteId(stackFrame); - CallSite callSite = findCallSiteById(callSiteId, stackFrame); - CallSiteLocation location = callSite.location; - if (isObfuscated()) { - target[index++] = new StackTraceElement("Obfuscated", "obfuscated", "Obfuscated.java", callSiteId); - } else if (location == null) { - target[index++] = new StackTraceElement("", "", null, -1); - } else { - while (location != null) { - MethodLocation methodLocation = location.method; - StackTraceElement element; - if (methodLocation != null) { - element = new StackTraceElement( - methodLocation.className != null ? methodLocation.className.value : "", - methodLocation.methodName != null ? methodLocation.methodName.value : "", - methodLocation.fileName != null ? methodLocation.fileName.value : null, - location.lineNumber); - } else { - element = new StackTraceElement("", "", null, location.lineNumber); + if (callSiteId >= 0) { + CallSite callSite = findCallSiteById(callSiteId, stackFrame); + CallSiteLocation location = callSite.location; + if (isObfuscated()) { + target[index++] = new StackTraceElement("Obfuscated", "obfuscated", "Obfuscated.java", callSiteId); + } else if (location == null) { + target[index++] = new StackTraceElement("", "", null, -1); + } else { + while (location != null) { + MethodLocation methodLocation = location.method; + StackTraceElement element; + if (methodLocation != null) { + element = new StackTraceElement( + methodLocation.className != null ? methodLocation.className.value : "", + methodLocation.methodName != null ? methodLocation.methodName.value : "", + methodLocation.fileName != null ? methodLocation.fileName.value : null, + location.lineNumber); + } else { + element = new StackTraceElement("", "", null, location.lineNumber); + } + target[index++] = element; + location = location.next; } - target[index++] = element; - location = location.next; } } stackFrame = ShadowStack.getNextStackFrame(stackFrame); diff --git a/core/src/main/resources/org/teavm/backend/c/string.c b/core/src/main/resources/org/teavm/backend/c/string.c index 8e41fc0f7..4ec81f6a9 100644 --- a/core/src/main/resources/org/teavm/backend/c/string.c +++ b/core/src/main/resources/org/teavm/backend/c/string.c @@ -1,6 +1,12 @@ #include "string.h" +#include "stack.h" #include #include +#include + +#if TEAVM_INCREMENTAL +#define TEAVM_ALLOC_STACK(sz) TEAVM_ALLOC_STACK_DEF(sz, NULL) +#endif int32_t teavm_hashCode(TeaVM_String* string) { int32_t hashCode = INT32_C(0); @@ -106,9 +112,15 @@ TeaVM_String* teavm_cToString(char* cstring) { return NULL; } + TEAVM_ALLOC_STACK(1); + TEAVM_CALL_SITE(-1); + TEAVM_GC_ROOT_RELEASE(0); + size_t clen = strlen(cstring); int32_t size = teavm_c16Size(cstring, clen); TeaVM_Array* charArray = teavm_allocateCharArray(size); + TEAVM_GC_ROOT(0, charArray); + char16_t* javaChars = TEAVM_ARRAY_DATA(charArray, char16_t); mbstate_t state = {0}; for (int32_t i = 0; i < size; ++i) { @@ -120,7 +132,10 @@ TeaVM_String* teavm_cToString(char* cstring) { cstring += result; } } - return teavm_createString(charArray); + + TeaVM_String* result = teavm_createString(charArray); + TEAVM_RELEASE_STACK; + return result; } TeaVM_String* teavm_c16ToString(char16_t* cstring) { @@ -128,14 +143,23 @@ TeaVM_String* teavm_c16ToString(char16_t* cstring) { return NULL; } + + TEAVM_ALLOC_STACK(1); + TEAVM_CALL_SITE(-1); + TEAVM_GC_ROOT_RELEASE(0); + int32_t size = 0; while (cstring[size] != 0) { ++size; } TeaVM_Array* charArray = teavm_allocateCharArray(size); + TEAVM_GC_ROOT(0, charArray); char16_t* javaChars = TEAVM_ARRAY_DATA(charArray, char16_t); memcpy(javaChars, cstring, size * sizeof(char16_t)); - return teavm_createString(charArray); + + TeaVM_String* result = teavm_createString(charArray); + TEAVM_RELEASE_STACK; + return result; } char16_t* teavm_mbToChar16(char* cstring, int32_t* length) {