开发 js 运行时,要在 c/c++ 代码中,通过调用 quickjs 的 api 来实现 native 层的异常抛出。本文讲解一下如何在quickjs中实现这个操作。
要讲清楚这个操作,需要知道 quickjs 中表示异常有两种值。
- 一种表示发生了异常,叫他 exception,表示为
JS_EXCEPTION
- 另一种表示异常具体是什么,叫他 Error,表示为 各种类型的错误。相关 API 如下
JSValue JS_Throw(JSContext *ctx, JSValue obj);
JSValue JS_GetException(JSContext *ctx);
JS_BOOL JS_IsError(JSContext *ctx, JSValueConst val);
JS_BOOL JS_IsException(JSValueConst v)
void JS_ResetUncatchableError(JSContext *ctx);
JSValue JS_NewError(JSContext *ctx);JSValue __js_printf_like(2, 3) JS_ThrowSyntaxError(JSContext *ctx, const char *fmt, ...);
JSValue __js_printf_like(2, 3) JS_ThrowTypeError(JSContext *ctx, const char *fmt, ...);
JSValue __js_printf_like(2, 3) JS_ThrowReferenceError(JSContext *ctx, const char *fmt, ...);
JSValue __js_printf_like(2, 3) JS_ThrowRangeError(JSContext *ctx, const char *fmt, ...);
JSValue __js_printf_like(2, 3) JS_ThrowInternalError(JSContext *ctx, const char *fmt, ...);
JSValue JS_ThrowOutOfMemory(JSContext *ctx);
抛出异常
- 错误发生时,我们通常返回 Execption,并将 Error 放置在 context 上。具体做法如下
// 抛出异常
void js_cfunc(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {JSValue ret = JS_UNDEFINED;if (/*error occur*/) {JSValue err = JS_NewError(ctx);/*set err info, ref: JS_AddIntrinsicBasicObjects JS_ThrowSyntaxError*/JS_Throw(ctx, err);return JS_EXCEPTION;}return ret;
}
- 便利函数,语法糖
一些常见的错误,创建异常并抛出的动作可以合并成JS_ThrowSyntaxError
等便利函数,见上。
void js_cfunc(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {JSValue ret = JS_UNDEFINED;if (/*error occur*/) {return JS_ThrowSyntaxError(ctx, "error info", ...);}return ret;
}
处理异常
异常发生时,我们通常获取 Error,并处理错误
// 处理异常
void js_cfunc(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {JSValue ret = JS_Call(ctx, ....);if (JS_IsException(ret)) {JSValue err = JS_GetException(ctx);/**处理异常,常见手段是1. 打印异常栈和异常信息:ref:函数 js_std_dump_error / eval_buf2. 也可以直接不处理,异常会自动传播到上层函数。其中原理,看这块代码是可以完全搞清楚的。*/}return ret;
}