static JSBool js_db_step(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { js_db_t *jd = JS_GetPrivate(cx, obj); if(js_db_check(cx, jd)) return JS_FALSE; if(jd->jd_stmt == NULL) { *rval = JSVAL_NULL; } else if(jd->jd_step_rc == SQLITE_ROW) { int cols = sqlite3_data_count(jd->jd_stmt); int i; JSObject *r = JS_NewObjectWithGivenProto(cx, NULL, NULL, obj); *rval = OBJECT_TO_JSVAL(r); if(!JS_EnterLocalRootScope(cx)) return JS_FALSE; for(i = 0; i < cols; i++) { const char *cn = sqlite3_column_name(jd->jd_stmt, i); switch(sqlite3_column_type(jd->jd_stmt, i)) { case SQLITE_INTEGER: js_set_prop_int(cx, r, cn, sqlite3_column_int(jd->jd_stmt, i)); break; case SQLITE_TEXT: js_set_prop_str(cx, r, cn, (const char *)sqlite3_column_text(jd->jd_stmt, i)); break; case SQLITE_FLOAT: js_set_prop_dbl(cx, r, cn, sqlite3_column_double(jd->jd_stmt, i)); break; } } JS_LeaveLocalRootScope(cx); return js_stmt_step(cx, jd, rval); } *rval = JSVAL_FALSE; return JS_TRUE; }
static void js_sub_query(subtitle_provider_t *SP, sub_scanner_t *ss, int score, int autosel) { js_subprovider_t *sp = (js_subprovider_t *)SP; JSContext *cx = js_newctx(NULL); JS_BeginRequest(cx); if(ss != NULL) { JSObject *obj = JS_NewObject(cx, &subreq_class, NULL, NULL); JS_AddNamedRoot(cx, &obj, "subscanner"); if(sp->sp_jsp != NULL) usage_inc_plugin_counter(sp->sp_jsp->jsp_id, "subsearch", 1); js_sub_job_t *jsj = malloc(sizeof(js_sub_job_t)); jsj->jsj_ss = ss; jsj->jsj_score = score; jsj->jsj_autosel = autosel; sub_scanner_retain(ss); JS_SetPrivate(cx, obj, jsj); JS_DefineFunctions(cx, obj, sub_functions); js_set_prop_rstr(cx, obj, "title", ss->ss_title); js_set_prop_rstr(cx, obj, "imdb", ss->ss_imdbid); if(ss->ss_season > 0) js_set_prop_int(cx, obj, "season", ss->ss_season); if(ss->ss_year > 0) js_set_prop_int(cx, obj, "year", ss->ss_year); if(ss->ss_episode > 0) js_set_prop_int(cx, obj, "episode", ss->ss_episode); if(ss->ss_fsize > 0) js_set_prop_dbl(cx, obj, "filesize", ss->ss_fsize); if(ss->ss_hash_valid) { char str[64]; snprintf(str, sizeof(str), "%016" PRIx64, ss->ss_opensub_hash); js_set_prop_str(cx, obj, "opensubhash", str); bin2hex(str, sizeof(str), ss->ss_subdbhash, 16); js_set_prop_str(cx, obj, "subdbhash", str); } if(ss->ss_duration > 0) js_set_prop_int(cx, obj, "duration", ss->ss_duration); jsval result; jsval arg = OBJECT_TO_JSVAL(obj); JS_CallFunctionValue(cx, NULL, sp->sp_func, 1, &arg, &result); JS_RemoveRoot(cx, &obj); } js_subprovider_release(cx, sp); JS_DestroyContext(cx); }