Consolidate error handling until util, too.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3682 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2021-11-03 22:28:25 +00:00
parent fde7fb4270
commit 2fb7fceb0c
6 changed files with 81 additions and 92 deletions

View File

@ -2,6 +2,8 @@
#include "task.h"
#include "quickjs-libc.h"
static JSValue _util_utf8_decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_NULL;
@ -42,17 +44,17 @@ JSValue tf_util_utf8_decode(JSContext* context, JSValue value)
return _util_utf8_decode(context, JS_NULL, 1, &value);
}
uint8_t* tf_util_try_get_array_buffer(JSContext *ctx, size_t *psize, JSValueConst obj)
uint8_t* tf_util_try_get_array_buffer(JSContext* context, size_t* psize, JSValueConst obj)
{
uint8_t* result = JS_GetArrayBuffer(ctx, psize, obj);
JS_FreeValue(ctx, JS_GetException(ctx));
uint8_t* result = JS_GetArrayBuffer(context, psize, obj);
JS_FreeValue(context, JS_GetException(context));
return result;
}
JSValue tf_util_try_get_typed_array_buffer(JSContext *ctx, JSValueConst obj, size_t *pbyte_offset, size_t *pbyte_length, size_t *pbytes_per_element)
JSValue tf_util_try_get_typed_array_buffer(JSContext* context, JSValueConst obj, size_t* pbyte_offset, size_t* pbyte_length, size_t* pbytes_per_element)
{
JSValue result = JS_GetTypedArrayBuffer(ctx, obj, pbyte_offset, pbyte_length, pbytes_per_element);
JS_FreeValue(ctx, JS_GetException(ctx));
JSValue result = JS_GetTypedArrayBuffer(context, obj, pbyte_offset, pbyte_length, pbytes_per_element);
JS_FreeValue(context, JS_GetException(context));
return result;
}
@ -80,6 +82,45 @@ JSValue _util_print(JSContext* context, JSValueConst this_val, int argc, JSValue
return JS_NULL;
}
void tf_util_report_error(JSContext* context, JSValue value)
{
if (JS_IsError(context, value))
{
const char* string = JS_ToCString(context, value);
printf("ERROR: %s\n", string);
JS_FreeCString(context, string);
JSValue stack = JS_GetPropertyStr(context, value, "stack");
if (!JS_IsUndefined(stack))
{
const char* stack_str = JS_ToCString(context, stack);
printf("%s\n", stack_str);
JS_FreeCString(context, stack_str);
}
JS_FreeValue(context, stack);
tf_task_t* task = tf_task_get(context);
if (task)
{
tf_task_send_error_to_parent(task, value);
}
}
else if (JS_IsException(value))
{
js_std_dump_error(context);
JSValue exception = JS_GetException(context);
const char* string = JS_ToCString(context, exception);
printf("Exception: %s\n", string);
JS_FreeCString(context, string);
tf_task_t* task = tf_task_get(context);
if (task)
{
tf_task_send_error_to_parent(task, exception);
}
JS_FreeValue(context, exception);
}
}
void tf_util_register(JSContext* context)
{
JSValue global = JS_GetGlobalObject(context);