diff --git a/src/ssb.js.c b/src/ssb.js.c index 9f47b204..75dace21 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -382,6 +382,7 @@ static void _tf_ssb_sqlAsync_work(uv_work_t* work) memcpy(&length, p, sizeof(length)); p += sizeof(length); sqlite3_bind_text(statement, column + 1, (const char*)p, length, NULL); + p += length; } break; case SQLITE_NULL: @@ -525,22 +526,22 @@ static void _tf_ssb_sqlAsync_after_work(uv_work_t* work, int status) static JSValue _tf_ssb_sqlAsync(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); - JSValue result = JS_UNDEFINED; + const char* query = JS_ToCString(context, argv[0]); + sql_work_t* work = tf_malloc(sizeof(sql_work_t)); + *work = (sql_work_t) + { + .request = + { + .data = work, + }, + .ssb = ssb, + .callback = JS_DupValue(context, argv[2]), + .query = query, + }; + JSValue result = JS_NewPromiseCapability(context, work->promise); + JSValue error_value = JS_UNDEFINED; if (ssb) { - const char* query = JS_ToCString(context, argv[0]); - sql_work_t* work = tf_malloc(sizeof(sql_work_t)); - *work = (sql_work_t) - { - .request = - { - .data = work, - }, - .ssb = ssb, - .callback = JS_DupValue(context, argv[2]), - .query = query, - }; - result = JS_NewPromiseCapability(context, work->promise); int32_t length = tf_util_get_length(context, argv[1]); for (int i = 0; i < length; i++) { @@ -560,7 +561,7 @@ static JSValue _tf_ssb_sqlAsync(JSContext* context, JSValueConst this_val, int a _tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type)); _tf_ssb_sql_append(&work->binds, &work->binds_count, &number, sizeof(number)); } - else if (JS_IsNumber(value)) + else if (JS_IsNull(value)) { uint8_t type = SQLITE_NULL; _tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type)); @@ -575,6 +576,7 @@ static JSValue _tf_ssb_sqlAsync(JSContext* context, JSValueConst this_val, int a string = ""; } _tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type)); + _tf_ssb_sql_append(&work->binds, &work->binds_count, &length, sizeof(length)); _tf_ssb_sql_append(&work->binds, &work->binds_count, string, length); JS_FreeCString(context, string); } @@ -582,18 +584,21 @@ static JSValue _tf_ssb_sqlAsync(JSContext* context, JSValueConst this_val, int a int r = uv_queue_work(tf_ssb_get_loop(ssb), &work->request, _tf_ssb_sqlAsync_work, _tf_ssb_sqlAsync_after_work); if (r) { - JSValue error = JS_ThrowInternalError(context, "uv_queue_work failed: %s", uv_strerror(r)); - JSValue result = JS_Call(context, work->promise[1], JS_UNDEFINED, 1, &error); - tf_util_report_error(context, result); - JS_FreeValue(context, work->promise[0]); - JS_FreeValue(context, work->promise[1]); - JS_FreeValue(context, work->callback); - JS_FreeValue(context, error); - JS_FreeCString(context, query); - tf_free(work->binds); - tf_free(work); + error_value = JS_ThrowInternalError(context, "uv_queue_work failed: %s", uv_strerror(r)); } } + if (!JS_IsUndefined(error_value)) + { + JSValue call_result = JS_Call(context, work->promise[1], JS_UNDEFINED, 1, &error_value); + tf_util_report_error(context, call_result); + JS_FreeValue(context, work->promise[0]); + JS_FreeValue(context, work->promise[1]); + JS_FreeValue(context, work->callback); + JS_FreeValue(context, error_value); + JS_FreeCString(context, query); + tf_free(work->binds); + tf_free(work); + } return result; }