Skip to content

Commit

Permalink
add poly_exception_get_guest_stack_trace and poly_exception_get_message
Browse files Browse the repository at this point in the history
  • Loading branch information
teshull committed Aug 30, 2022
1 parent 8f9c6f0 commit 8e03781
Showing 1 changed file with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1775,11 +1775,10 @@ public static PolyglotStatus poly_exception_get_object(PolyglotIsolateThread thr
}

@CEntryPoint(name = "poly_exception_get_stack_trace", exceptionHandler = ExceptionHandler.class, documentation = {
"Gets the guest stack traces as a string.",
"The returned string is valid until the next call to this function",
"Gets the full stack trace as a UTF-8 encoded string.",
"",
" @param exception Handle to the exception object.",
" @param buffer UTF-8 string representing the stack trace. Can be NULL.",
" @param buffer Where to write the UTF-8 string representing the stack trace. Can be NULL.",
" @param buffer_size Size of the user-supplied buffer.",
" @param result If buffer is NULL, this will contain the buffer size required to put the trace string in, otherwise, it will contain the number of bytes written",
" @return poly_ok if everything went ok, otherwise an error occurred.",
Expand All @@ -1790,17 +1789,59 @@ public static PolyglotStatus poly_exception_get_stack_trace(PolyglotIsolateThrea
PolyglotException e = fetchHandle(exception);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Iterable<PolyglotException.StackFrame> traceElements = e.getPolyglotStackTrace();

e.getPolyglotStackTrace().forEach(trace -> pw.println(trace));

writeString(sw.toString(), buffer, buffer_size, result, UTF8_CHARSET);
return poly_ok;
}

@CEntryPoint(name = "poly_exception_get_guest_stack_trace", exceptionHandler = ExceptionHandler.class, documentation = {
"Gets the guest stack trace as a UTF-8 encoded string.",
"",
" @param exception Handle to the exception object.",
" @param buffer Where to write the UTF-8 string representing the stack trace. Can be NULL.",
" @param buffer_size Size of the user-supplied buffer.",
" @param result If buffer is NULL, this will contain the buffer size required to put the trace string in, otherwise, it will contain the number of bytes written",
" @return poly_ok if everything went ok, otherwise an error occurred.",
" @since 22.3",
})
public static PolyglotStatus poly_exception_get_guest_stack_trace(PolyglotIsolateThread thread, PolyglotExceptionHandle exception, CCharPointer buffer, UnsignedWord buffer_size,
SizeTPointer result) {
resetErrorState();
PolyglotException e = fetchHandle(exception);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

Iterable<PolyglotException.StackFrame> traceElements = e.getPolyglotStackTrace();
for (PolyglotException.StackFrame trace : traceElements) {
if (trace.isGuestFrame()) {
pw.println(trace.toString());
pw.println(trace);
}
}
writeString(sw.toString(), buffer, buffer_size, result, UTF8_CHARSET);
return poly_ok;
}

@CEntryPoint(name = "poly_exception_get_message", exceptionHandler = ExceptionHandler.class, documentation = {
"Gets the error message as a UTF-8 encoded string.",
"",
" @param exception Handle to the exception object.",
" @param buffer Where to write the UTF-8 string representing the error message. Can be NULL.",
" @param buffer_size Size of the user-supplied buffer.",
" @param result If buffer is NULL, this will contain the buffer size required to put the error message string in, otherwise, it will contain the number of bytes written",
" @return poly_ok if everything went ok, otherwise an error occurred.",
" @since 22.3",
})
public static PolyglotStatus poly_exception_get_message(PolyglotIsolateThread thread, PolyglotExceptionHandle exception, CCharPointer buffer, UnsignedWord buffer_size,
SizeTPointer result) {
resetErrorState();
PolyglotException e = fetchHandle(exception);

writeString(e.getMessage(), buffer, buffer_size, result, UTF8_CHARSET);
return poly_ok;
}

@CEntryPoint(name = "poly_register_recurring_callback", exceptionHandler = ExceptionHandler.class, documentation = {
"Registers (or unregisters) a recurring callback in the current thread to be",
"called approximately at the specified interval. The callback's result value is",
Expand Down Expand Up @@ -1883,9 +1924,9 @@ public static PolyglotStatus poly_perf_data_get_address_of_int64_t(PolyglotIsola
}

private static void writeString(String valueString, CCharPointer buffer, UnsignedWord length, SizeTPointer result, Charset charset) {
UnsignedWord stringLength = WordFactory.unsigned(valueString.getBytes(charset).length);
if (buffer.isNull()) {
result.write(stringLength);
int stringLength = valueString.getBytes(charset).length;
result.write(WordFactory.unsigned(stringLength));
} else {
result.write(CTypeConversion.toCString(valueString, charset, buffer, length));
}
Expand Down

0 comments on commit 8e03781

Please sign in to comment.