JSBool js_cache_put(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { char stash[256]; const char *key,*lstash; uint32_t len, maxage; JSObject *o; htsbuf_queue_t out; js_plugin_t *jsp = JS_GetPrivate(cx, obj); if (!JS_ConvertArguments(cx, argc, argv, "ssou", &lstash, &key, &o, &maxage)) return JS_FALSE; if (o == NULL) { JS_ReportError(cx, "Not an object"); return JS_FALSE; } // json encode object htsbuf_queue_init(&out, 0); if (js_json_encode_from_object(cx, o, &out) != 0) { JS_ReportError(cx, "Not an JSON object"); return JS_FALSE; } len = out.hq_size; buf_t *b = buf_create(len + 1); htsbuf_read(&out, b->b_ptr, len); buf_str(b)[len] = '\0'; // put json encoded object onto cache snprintf(stash, sizeof(stash), "plugin/%s/%s", jsp->jsp_id, lstash); blobcache_put(key, stash, b, maxage, NULL, 0, 0); buf_release(b); return JS_TRUE; }
static JSBool js_json_stringify(JSContext *cx, JSObject* pThis, uintN argc, jsval *vp, jsval* rval) { JSObject *obj; jsval *argv = vp; // Must throw an Error if there isn't a first arg if (!JS_ConvertArguments(cx, argc, argv, "o", &obj)) return JS_FALSE; // Only use objects and arrays as the root for now jsval v = OBJECT_TO_JSVAL(obj); JSBool ok = js_TryJSON(cx, &v); JSType type; if (!(ok && !JSVAL_IS_PRIMITIVE(v) && (type = JS_TypeOfValue(cx, v)) != JSTYPE_FUNCTION && type != JSTYPE_XML)) { JS_ReportError(cx, "Invalid argument."); return JS_FALSE; } JSString *s = JS_NewStringCopyN(cx, "", 0); if (!s) ok = JS_FALSE; if (ok) { jsval sv = STRING_TO_JSVAL(s); StringifyClosure sc; sc.cx = cx; sc.s = &sv; JSAutoTempValueRooter tvr(cx, 1, sc.s); ok = js_Stringify(cx, &v, NULL, &WriteCallback, &sc, 0); *rval = *sc.s; } return ok; }
JSBool js_createSettings(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { const char *title; const char *icon = NULL; const char *desc = NULL; char spath[URL_MAX]; if(!JS_ConvertArguments(cx, argc, argv, "s/ss", &title, &icon, &desc)) return JS_FALSE; js_plugin_t *jsp = JS_GetPrivate(cx, obj); snprintf(spath, sizeof(spath), "plugins/%s", jsp->jsp_id); js_setting_group_t *jsg = calloc(1, sizeof(js_setting_group_t)); JSObject *robj; jsg->jsg_refcount = 2; LIST_INSERT_HEAD(&jsp->jsp_setting_groups, jsg, jsg_link); jsg->jsg_frozen = 1; jsg->jsg_spath = strdup(spath); jsg->jsg_store = htsmsg_store_load(spath) ?: htsmsg_create_map(); jsg->jsg_root_owner = 1; jsg->jsg_root = prop_ref_inc(settings_add_dir_cstr(settings_apps, title, NULL, icon, desc, NULL)); robj = JS_NewObjectWithGivenProto(cx, &setting_group_class, NULL, obj); jsg->jsg_val = *rval = OBJECT_TO_JSVAL(robj); JS_AddNamedRoot(cx, &jsg->jsg_val, "jsg"); JS_SetPrivate(cx, robj, jsg); JS_DefineFunctions(cx, robj, setting_functions); jsg->jsg_frozen = 0; return JS_TRUE; }
JSBool js_createService(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { const char *title; const char *url; const char *type; const char *icon = NULL; JSObject *robj; JSBool enabled; char svcid[256]; if (!JS_ConvertArguments(cx, argc, argv, "sssb/s", &title, &url, &type, &enabled, &icon)) return JS_FALSE; js_plugin_t *jsp = JS_GetPrivate(cx, obj); js_service_t *jss = malloc(sizeof(js_service_t)); jss->jss_ref = 2; snprintf(svcid, sizeof(svcid), "plugin:%s", jsp->jsp_id); jss->jss_s = service_create(svcid, title, url, type, icon, 0, enabled, SVC_ORIGIN_APP); LIST_INSERT_HEAD(&jsp->jsp_services, jss, jss_link); robj = JS_NewObjectWithGivenProto(cx, &service_class, NULL, NULL); *rval = OBJECT_TO_JSVAL(robj); JS_SetPrivate(cx, robj, jss); JS_DefineProperty(cx, robj, "enabled", BOOLEAN_TO_JSVAL(enabled), NULL, setEnabled, JSPROP_PERMANENT); JS_DefineFunction(cx, robj, "destroy", destroy, 0, 0); return JS_TRUE; }
static JSBool js_createString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { js_setting_group_t *jsg = JS_GetPrivate(cx, obj); const char *id; const char *title; const char *def; JSObject *func; JSBool persistent = JS_FALSE; if(!JS_ConvertArguments(cx, argc, argv, "ssso/b", &id, &title, &def, &func, &persistent)) return JS_FALSE; js_setting_t *jss = jss_create(cx, obj, id, rval, func, jsg, persistent); if(jss == NULL) return JS_FALSE; rstr_t *r = NULL; if(persistent && jsg->jsg_kv_url) { r = kv_url_opt_get_rstr(jsg->jsg_kv_url, KVSTORE_DOMAIN_PLUGIN, id); if(r != NULL) def = rstr_get(r); } jss->jss_s = settings_create_string(jsg->jsg_root, id, _p(title), def, jsg->jsg_store, js_store_update_string, jss, SETTINGS_INITIAL_UPDATE | jsg->jsg_settings_flags, js_global_pc, js_setting_group_save, jsg); jss->jss_cx = NULL; rstr_release(r); return JS_TRUE; }
JSBool js_json_stringify(JSContext *cx, uintN argc, jsval *vp) { JSObject *obj; jsval *argv = vp + 2; // Must throw an Error if there isn't a first arg if (!JS_ConvertArguments(cx, argc, argv, "o", &obj)) return JS_FALSE; // Only use objects and arrays as the root for now *vp = OBJECT_TO_JSVAL(obj); JSBool ok = js_TryJSON(cx, vp); JSType type; if (!ok || JSVAL_IS_PRIMITIVE(*vp) || ((type = JS_TypeOfValue(cx, *vp)) == JSTYPE_FUNCTION || type == JSTYPE_XML)) { JS_ReportError(cx, "Invalid argument"); return JS_FALSE; } JSString *s = JS_NewStringCopyN(cx, "", 0); if (!s) ok = JS_FALSE; if (ok) { jsval vec[2] = {STRING_TO_JSVAL(s), JSVAL_VOID}; StringifyClosure sc(cx, 2, vec); JSAutoTempValueRooter resultTvr(cx, 1, sc.s); ok = js_Stringify(cx, vp, NULL, &WriteCallback, &sc, 0); *vp = *sc.s; } return ok; }
static JSBool rpmseq_call(JSContext *cx, uintN argc, jsval *vp) { jsval *argv = JS_ARGV(cx , vp); JSObject *obj = JS_NewObjectForConstructor(cx , vp); if(!obj) { JS_ReportError(cx , "Failed to create 'this' object"); return JS_FALSE; } /* XXX obj is the global object so lookup "this" object. */ JSObject * o = JSVAL_TO_OBJECT(argv[-2]); void * ptr = JS_GetInstancePrivate(cx, o, &rpmseqClass, NULL); #ifdef NOTYET DB_SEQUENCE * seq = ptr; const char *_fn = NULL; const char * _con = NULL; #endif JSBool ok = JS_FALSE; #ifdef NOTYET if (!(ok = JS_ConvertArguments(cx, argc, argv, "s", &_fn))) goto exit; *rval = (seq && _fn && (_con = rpmseqLgetfilecon(seq, _fn)) != NULL) ? STRING_TO_JSVAL(JS_NewStringCopyZ(cx, _con)) : JSVAL_VOID; _con = _free(_con); ok = JS_TRUE; exit: #endif if (_debug) fprintf(stderr, "<== %s(%p,%p,%p[%u],%p) o %p ptr %p\n", __FUNCTION__, cx, obj, argv, (unsigned)argc, &JS_RVAL(cx, vp), o, ptr); return ok; }
static JSBool vector4_constructor(JSContext *cx, unsigned argc, jsval *vp) { JSObject *returnObject; jsval returnValue; double w, x, y, z; jsval r, g, b, a; if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "dddd", &w, &x, &y, &z)) return JS_FALSE; returnObject = JS_NewObject(cx, &vector4_class, NULL, NULL); returnValue = OBJECT_TO_JSVAL(returnObject); JS_NewNumberValue(cx, w, &r); JS_NewNumberValue(cx, x, &g); JS_NewNumberValue(cx, y, &b); JS_NewNumberValue(cx, z, &a); JS_SetProperty(cx, returnObject, "R", &r); JS_SetProperty(cx, returnObject, "G", &g); JS_SetProperty(cx, returnObject, "G", &b); JS_SetProperty(cx, returnObject, "A", &b); JS_SET_RVAL(cx, vp, returnValue); return JS_TRUE; }
JSBool wimg_load_image(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { DWORD imageIndex; JS_BeginRequest(cx); if(!JS_ConvertArguments(cx, argc, argv, "u", &imageIndex)) { JS_ReportError(cx, "Error during argument parsing in WIMLoadImage"); JS_EndRequest(cx); return JS_FALSE; } JS_YieldRequest(cx); HANDLE hImage = WIMLoadImage(JS_GetPrivate(cx, obj), imageIndex); if(hImage == NULL) { *rval = JSVAL_FALSE; JS_EndRequest(cx); return JS_TRUE; } JSObject * retObj = JS_NewObject(cx, &wimImageClass, wimImageProto, obj); *rval = OBJECT_TO_JSVAL(retObj); JS_SetPrivate(cx, retObj, hImage); JS_EndRequest(cx); return JS_TRUE; }
JSBool js_json_parse(JSContext *cx, uintN argc, jsval *vp) { JSString *s = NULL; jsval *argv = vp + 2; // Must throw an Error if there isn't a first arg if (!JS_ConvertArguments(cx, argc, argv, "S", &s)) return JS_FALSE; JSONParser *jp = js_BeginJSONParse(cx, vp); JSBool ok = jp != NULL; if (ok) { ok = js_ConsumeJSONText(cx, jp, JS_GetStringChars(s), JS_GetStringLength(s)); ok &= js_FinishJSONParse(cx, jp); } if (!ok) JS_ReportError(cx, "Error parsing JSON"); return ok; }
static JSBool rpmseq_Stat(JSContext *cx, uintN argc, jsval *vp) { jsval *argv = JS_ARGV(cx , vp); JSObject *obj = JS_NewObjectForConstructor(cx , vp); if(!obj) { JS_ReportError(cx , "Failed to create 'this' object"); return JS_FALSE; } void * ptr = JS_GetInstancePrivate(cx, obj, &rpmseqClass, NULL); DB_SEQUENCE * seq = ptr; uint32_t _flags = 0; JSBool ok = JS_FALSE; _METHOD_DEBUG_ENTRY(_debug); if (seq == NULL) goto exit; JS_SET_RVAL(cx, vp, JSVAL_FALSE); if (!(ok = JS_ConvertArguments(cx, argc, argv, "/u", &_flags))) goto exit; { DB_SEQUENCE_STAT * sp = NULL; int ret = seq->stat(seq, &sp, _flags); if (ret) fprintf(stderr, "DB_SEQUENCE->stat: %s\n", db_strerror(ret)); else JS_SET_RVAL(cx, vp, JSVAL_TRUE); sp = _free(sp); } ok = JS_TRUE; exit: return ok; }
JSBool appendChild(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { JSObject *refChildObj; nsCOMPtr<nsIDOMNode> refChild, mNode, outNode; JS_BeginRequest(cx); if(!JS_ConvertArguments(cx, argc, argv, "o", &refChildObj)) { JS_ReportError(cx, "Error converting arguments in replaceChild"); JS_EndRequest(cx); return JS_FALSE; } if(!JS_InstanceOf(cx, refChildObj, &lDOMNodeClass, NULL)) { JS_ReportWarning(cx, "Arguments to replaceChild must both be DOMNode objects."); JS_EndRequest(cx); return JS_TRUE; } refChild = (nsIDOMNode*)JS_GetPrivate(cx, refChildObj); mNode = (nsIDOMNode*)JS_GetPrivate(cx, obj); EnterCriticalSection(&domStateLock); domState = 1; if(mNode->AppendChild(refChild, getter_AddRefs(outNode)) == NS_OK) { JSObject * retObj = JS_NewObject(cx, &lDOMNodeClass, lDOMNodeProto, obj); *rval = OBJECT_TO_JSVAL(retObj); JS_SetPrivate(cx, retObj, outNode); } else *rval = JSVAL_FALSE; LeaveCriticalSection(&domStateLock); JS_EndRequest(cx); return JS_TRUE; }
JSBool win32_messagebox(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval *rval) { LPWSTR caption = NULL, title = NULL; UINT type = MB_OK; JS_BeginRequest(cx); if(!JS_ConvertArguments(cx, argc, argv, "W /W c", &caption, &title, &type)) { JS_ReportError(cx, "Unable to parse arguments for win32_messagebox"); JS_EndRequest(cx); return JS_FALSE; } type |= MB_TOPMOST | MB_SETFOREGROUND; if(title == NULL) title = TEXT("nJord"); jsrefcount rCount = JS_SuspendRequest(cx); DWORD errorCode = MessageBoxW(NULL, caption, title, type); JS_ResumeRequest(cx, rCount); JSBool retVal = JS_NewNumberValue(cx, errorCode, rval); JS_EndRequest(cx); return retVal; }
static JSBool rpmdc_ctor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSBool ok = JS_FALSE; unsigned int _dalgo = PGPHASHALGO_NONE; unsigned int _flags = RPMDIGEST_NONE; _CTOR_DEBUG_ENTRY(_debug); if (!(ok = JS_ConvertArguments(cx, argc, argv, "/uu", &_dalgo, &_flags))) goto exit; if (JS_IsConstructing(cx)) { (void) rpmdc_init(cx, obj, _dalgo, _flags); } else { if ((obj = JS_NewObject(cx, &rpmdcClass, NULL, NULL)) == NULL) goto exit; *rval = OBJECT_TO_JSVAL(obj); } ok = JS_TRUE; exit: return ok; }
static JSBool rpmbf_union(JSContext *cx, uintN argc, jsval *vp) { jsval *argv = JS_ARGV(cx, vp); JSObject *obj = JS_THIS_OBJECT(cx, vp); void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, NULL); rpmbf _a = ptr; JSObject * o = NULL; rpmbf _b = NULL; JSBool ok = JS_FALSE; _METHOD_DEBUG_ENTRY(_debug); *vp = JSVAL_FALSE; if (!(ok = JS_ConvertArguments(cx, argc, argv, "o", &o)) || (_b = JS_GetInstancePrivate(cx, o, &rpmbfClass, NULL)) == NULL) goto exit; if (!rpmbfUnion(_a, _b)) *vp = JSVAL_TRUE; ok = JS_TRUE; exit: return ok; }
JSBool S_CCParticleSystem::jsparticleWithFile(JSContext *cx, uint32_t argc, jsval *vp) { if (argc == 1) { JSString *arg0; JS_ConvertArguments(cx, 1, JS_ARGV(cx, vp), "S", &arg0); char *narg0 = JS_EncodeString(cx, arg0); CCParticleSystem* ret = CCParticleSystemQuad::particleWithFile(narg0); if (ret == NULL) { JS_SET_RVAL(cx, vp, JSVAL_NULL); return JS_TRUE; } do { JSObject *tmp = JS_NewObject(cx, S_CCParticleSystem::jsClass, S_CCParticleSystem::jsObject, NULL); pointerShell_t *pt = (pointerShell_t *)JS_malloc(cx, sizeof(pointerShell_t)); pt->flags = kPointerTemporary; pt->data = (void *)ret; JS_SetPrivate(tmp, pt); JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(tmp)); } while (0); return JS_TRUE; } JS_SET_RVAL(cx, vp, JSVAL_TRUE); return JS_TRUE; }
static JSBool rpmmpf_Put(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { void * ptr = JS_GetInstancePrivate(cx, obj, &rpmmpfClass, NULL); DB_MPOOLFILE * mpf = ptr; DB_CACHE_PRIORITY _priority = DB_PRIORITY_UNCHANGED; JSBool ok = JS_FALSE; _METHOD_DEBUG_ENTRY(_debug); if (mpf == NULL) goto exit; *rval = JSVAL_FALSE; if (!(ok = JS_ConvertArguments(cx, argc, argv, "/u", &_priority))) goto exit; { void * _pgaddr = NULL; uint32_t _flags = 0; int ret = mpf->put(mpf, _pgaddr, _priority, _flags); switch (ret) { default: fprintf(stderr, "DB_MPOOLFILE->put: %s\n", db_strerror(ret)); *rval = JSVAL_FALSE; goto exit; break; case 0: *rval = JSVAL_TRUE; break; } } ok = JS_TRUE; exit: return ok; }
static JSBool rpmxar_ctor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { const char * _fn = "popt-1.14.xar"; const char * _fmode = "r"; JSBool ok = JS_FALSE; _CTOR_DEBUG_ENTRY(_debug); if (!(ok = JS_ConvertArguments(cx, argc, argv, "/ss", &_fn, &_fmode))) goto exit; if (JS_IsConstructing(cx)) { (void) rpmxar_init(cx, obj, _fn, _fmode); } else { if ((obj = JS_NewObject(cx, &rpmxarClass, NULL, NULL)) == NULL) goto exit; *rval = OBJECT_TO_JSVAL(obj); } ok = JS_TRUE; exit: return ok; }
static JSBool js_textDialog(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { const char *message; char *input; JSBool ok, cancel; int r; jsval val; if(!JS_ConvertArguments(cx, argc, argv, "sbb", &message, &ok, &cancel)) return JS_FALSE; r = text_dialog(message, &input, (ok ? MESSAGE_POPUP_OK : 0) | (cancel ? MESSAGE_POPUP_CANCEL : 0) | MESSAGE_POPUP_RICH_TEXT); if(r == 1) { *rval = BOOLEAN_TO_JSVAL(0); return JS_TRUE; } obj = JS_NewObject(cx, NULL, NULL, NULL); *rval = OBJECT_TO_JSVAL(obj); if(r == -1) { val = BOOLEAN_TO_JSVAL(1); JS_SetProperty(cx, obj, "rejected", &val); } else { val = STRING_TO_JSVAL(JS_NewString(cx, input, strlen(input))); JS_SetProperty(cx, obj, "input", &val); } return JS_TRUE; }
JSBool openfiledlg(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { JSObject * filterArray = NULL; LPWSTR initialDirectory = NULL; LPWSTR dlgTitle = NULL; LPWSTR defaultExtension = NULL; JSBool save = JS_FALSE; JS_BeginRequest(cx); if(!JS_ConvertArguments(cx, argc, argv, "W W W o/ b", &initialDirectory, &defaultExtension, &dlgTitle, &filterArray, &save)) { JS_ReportError(cx, "Error parsing arguments in OpenFileDialog"); JS_EndRequest(cx); return JS_FALSE; } jsuint nFilters; JS_GetArrayLength(cx, filterArray, &nFilters); LPWSTR filterBuffer = (LPWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (50 * nFilters) * sizeof(WCHAR)); long filterBufferUsed = 0, filterBufferSize = (50 * nFilters); for(jsuint i = 0; i < nFilters; i++) { jsval curFilter; JS_GetElement(cx, filterArray, i, &curFilter); JSString * curFilterString = JS_ValueToString(cx, curFilter); LPWSTR curFilterRaw = (LPWSTR)JS_GetStringChars(curFilterString); int delta = wcslen(curFilterRaw); if(filterBufferSize - ( 2 + JS_GetStringLength(curFilterString) + filterBufferUsed) <= 0) { filterBufferSize += 50; HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, filterBuffer, filterBufferSize * sizeof(WCHAR)); } wcscpy_s(filterBuffer + filterBufferUsed, filterBufferSize - filterBufferUsed, (LPWSTR)JS_GetStringChars(curFilterString)); filterBufferUsed += JS_GetStringLength(curFilterString) + 1; } filterBuffer[filterBufferUsed] = TEXT('\0'); OPENFILENAME ofn; memset(&ofn, 0, sizeof(OPENFILENAME)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.lpstrFilter = filterBuffer; ofn.nFilterIndex = 0; ofn.lpstrFile = (LPWSTR)JS_malloc(cx, 260 * sizeof(WCHAR)); memset(ofn.lpstrFile, 0, sizeof(WCHAR) * 260); ofn.nMaxFile = 260; ofn.lpstrFileTitle = (LPWSTR)JS_malloc(cx, 260 * sizeof(WCHAR)); memset(ofn.lpstrFileTitle, 0, sizeof(WCHAR) * 260); ofn.nMaxFileTitle = 260; ofn.lpstrInitialDir = initialDirectory; ofn.lpstrTitle = dlgTitle; ofn.lpstrDefExt = defaultExtension; ofn.lpfnHook = OFN_Hook; ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER; jsrefcount rCount = JS_SuspendRequest(cx); BOOL ok; if(save) ok = GetSaveFileName(&ofn); else ok = GetOpenFileName(&ofn); DWORD errorCode = CommDlgExtendedError(); HeapFree(GetProcessHeap(), 0, filterBuffer); JS_ResumeRequest(cx, rCount); if(!ok) { JS_free(cx, ofn.lpstrFile); JS_free(cx, ofn.lpstrFileTitle); *rval = JSVAL_FALSE; return JS_TRUE; } JSObject * retObj = JS_NewObject(cx, NULL, NULL, obj); *rval = OBJECT_TO_JSVAL(retObj); jsval filePathVal = STRING_TO_JSVAL(JS_NewUCString(cx, (jschar*)ofn.lpstrFile, wcslen(ofn.lpstrFile))); JS_DefineProperty(cx, retObj, "filePath", filePathVal, NULL, NULL, JSPROP_PERMANENT | JSPROP_ENUMERATE); jsval fileTitleVal = STRING_TO_JSVAL(JS_NewUCString(cx, (jschar*)ofn.lpstrFileTitle, wcslen(ofn.lpstrFileTitle))); JS_DefineProperty(cx, retObj, "fileTitle", fileTitleVal, NULL, NULL, JSPROP_PERMANENT | JSPROP_ENUMERATE); JS_EndRequest(cx); return JS_TRUE; }
static void TestArgFormatter(JSContext* jscontext, JSObject* glob, nsIXPConnect* xpc) { JSBool ok = JS_TRUE; const char* a_in = "some string"; nsCOMPtr<nsITestXPCFoo> b_in = new nsTestXPCFoo(); nsCOMPtr<nsIWritableVariant> c_in = do_CreateInstance("@mozilla.org/variant;1"); static NS_NAMED_LITERAL_STRING(d_in, "foo bar"); const char* e_in = "another meaningless chunck of text"; JSBool a_match; nsCOMPtr<nsISupports> b_out; nsCOMPtr<nsIVariant> c_out; nsAutoString d_out; JSBool e_match; nsCOMPtr<nsITestXPCFoo> specified; PRInt32 val; printf("ArgumentFormatter test: "); if(!b_in || !c_in || NS_FAILED(c_in->SetAsInt32(5))) { printf(" failed to construct test objects -- FAILED!\n"); return; } do { JSAutoRequest ar(jscontext); // Prepare an array of arguments for JS_ConvertArguments jsval argv[5]; js::AutoArrayRooter tvr(jscontext, JS_ARRAY_LENGTH(argv), argv); if (!PushArguments(jscontext, 5, argv, "s %ip %iv %is s", a_in, &NS_GET_IID(nsITestXPCFoo2), b_in.get(), c_in.get(), static_cast<const nsAString*>(&d_in), e_in)) { printf(" could not convert from native to JS -- FAILED!\n"); return; } JSString *a_out, *e_out; ok = JS_ConvertArguments(jscontext, 5, argv, "S %ip %iv %is S", &a_out, static_cast<nsISupports**>(getter_AddRefs(b_out)), static_cast<nsIVariant**>(getter_AddRefs(c_out)), static_cast<nsAString*>(&d_out), &e_out); TAF_CHECK(ok, " could not convert from JS to native -- FAILED!\n"); TAF_CHECK(b_out, " JS to native for %%ip returned NULL -- FAILED!\n"); specified = do_QueryInterface(b_out); TAF_CHECK(specified, " could not QI value JS to native returned -- FAILED!\n"); ok = specified.get() == b_in.get(); TAF_CHECK(ok, " JS to native returned wrong value -- FAILED!\n"); TAF_CHECK(c_out, " JS to native for %%iv returned NULL -- FAILED!\n"); TAF_CHECK(NS_SUCCEEDED(c_out->GetAsInt32(&val)) && val == 5, " JS to native for %%iv holds wrong value -- FAILED!\n"); TAF_CHECK(d_in.Equals(d_out), " JS to native for %%is returned the wrong value -- FAILED!\n"); TAF_CHECK(JS_StringEqualsAscii(jscontext, a_out, a_in, &a_match), " oom -- FAILED!\n"); TAF_CHECK(JS_StringEqualsAscii(jscontext, e_out, e_in, &e_match), " oom -- FAILED!\n"); } while (0); if (!ok) return; if(a_match && e_match) printf("passed\n"); else printf(" conversion OK, but surrounding was mangled -- FAILED!\n"); }
static JSBool js_createMultiOpt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { js_setting_group_t *jsg = JS_GetPrivate(cx, obj); const char *id; const char *title; JSObject *func; JSObject *optlist; JSBool persistent = JS_FALSE; if(!JS_ConvertArguments(cx, argc, argv, "ssoo/b", &id, &title, &optlist, &func, &persistent)) return JS_FALSE; js_setting_t *jss = jss_create(cx, obj, id, rval, func, jsg, persistent); if(jss == NULL) return JS_FALSE; char **options = NULL; JSIdArray *opts, *opt; int i; char *defvalue = NULL; if((opts = JS_Enumerate(cx, optlist)) != NULL) { for(i = 0; i < opts->length; i++) { jsval name, value, id, title, def; if(!JS_IdToValue(cx, opts->vector[i], &name) || !JSVAL_IS_INT(name) || !JS_GetElement(cx, optlist, JSVAL_TO_INT(name), &value) || !JSVAL_IS_OBJECT(value) || (opt = JS_Enumerate(cx, JSVAL_TO_OBJECT(value))) == NULL) continue; if(opt->length >= 2 && JS_GetElement(cx, JSVAL_TO_OBJECT(value), 0, &id) && JS_GetElement(cx, JSVAL_TO_OBJECT(value), 1, &title)) { if(opt->length < 3 || !JS_GetElement(cx, JSVAL_TO_OBJECT(value), 2, &def)) def = JSVAL_FALSE; const char *k = JS_GetStringBytes(JS_ValueToString(cx, id)); if(def == JSVAL_TRUE) mystrset(&defvalue, k); strvec_addp(&options, k); strvec_addp(&options, JS_GetStringBytes(JS_ValueToString(cx, title))); } JS_DestroyIdArray(cx, opt); } JS_DestroyIdArray(cx, opts); } rstr_t *r = NULL; if(persistent && jsg->jsg_kv_url) r = kv_url_opt_get_rstr(jsg->jsg_kv_url, KVSTORE_DOMAIN_PLUGIN, id); jss->jss_s = setting_create(SETTING_MULTIOPT, jsg->jsg_root, SETTINGS_INITIAL_UPDATE | jsg->jsg_settings_flags, SETTING_TITLE_CSTR(title), SETTING_COURIER(js_global_pc), SETTING_CALLBACK(js_store_update_string, jss), SETTING_VALUE(r ? rstr_get(r) : defvalue), SETTING_OPTION_LIST(options), SETTING_HTSMSG_CUSTOM_SAVER(id, jsg->jsg_store, js_setting_group_save, jsg), NULL); strvec_free(options); rstr_release(r); jss->jss_cx = NULL; return JS_TRUE; }
static void TestArgFormatter(JSContext* jscontext, JSObject* glob, nsIXPConnect* xpc) { jsval* argv; void* mark; const char* a_in = "some string"; nsCOMPtr<nsITestXPCFoo> b_in = new nsTestXPCFoo(); nsCOMPtr<nsIWritableVariant> c_in = do_CreateInstance("@mozilla.org/variant;1"); static NS_NAMED_LITERAL_STRING(d_in, "foo bar"); const char* e_in = "another meaningless chunck of text"; char* a_out; nsCOMPtr<nsISupports> b_out; nsCOMPtr<nsIVariant> c_out; nsAutoString d_out; char* e_out; nsCOMPtr<nsITestXPCFoo> specified; PRInt32 val; printf("ArgumentFormatter test: "); if(!b_in || !c_in || NS_FAILED(c_in->SetAsInt32(5))) { printf(" failed to construct test objects -- FAILED!\n"); return; } argv = JS_PushArguments(jscontext, &mark, "s %ip %iv %is s", a_in, &NS_GET_IID(nsITestXPCFoo2), b_in.get(), c_in.get(), static_cast<const nsAString*>(&d_in), e_in); if(!argv) { printf(" could not convert from native to JS -- FAILED!\n"); return; } if(!JS_ConvertArguments(jscontext, 5, argv, "s %ip %iv %is s", &a_out, static_cast<nsISupports**>(getter_AddRefs(b_out)), static_cast<nsIVariant**>(getter_AddRefs(c_out)), static_cast<nsAString*>(&d_out), &e_out)) { printf(" could not convert from JS to native -- FAILED!\n"); goto out; } if(!b_out) { printf(" JS to native for %%ip returned NULL -- FAILED!\n"); goto out; } specified = do_QueryInterface(b_out); if(!specified) { printf(" could not QI value JS to native returned -- FAILED!\n"); goto out; } if(specified.get() != b_in.get()) { printf(" JS to native returned wrong value -- FAILED!\n"); goto out; } if(!c_out) { printf(" JS to native for %%iv returned NULL -- FAILED!\n"); goto out; } if(NS_FAILED(c_out->GetAsInt32(&val)) || val != 5) { printf(" JS to native for %%iv holds wrong value -- FAILED!\n"); goto out; } if(!d_in.Equals(d_out)) { printf(" JS to native for %%is returned the wrong value -- FAILED!\n"); goto out; } if(!strcmp(a_in, a_out) && !strcmp(e_in, e_out)) printf("passed\n"); else printf(" conversion OK, but surrounding was mangled -- FAILED!\n"); out: JS_PopArguments(jscontext, mark); }
static JSBool rpmts_dbkeys(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { void * ptr = JS_GetInstancePrivate(cx, obj, &rpmtsClass, NULL); rpmts ts = ptr; jsval tagid = JSVAL_VOID; jsval v = JSVAL_VOID; rpmTag tag = RPMTAG_NVRA; rpmMireMode _mode = RPMMIRE_PCRE; const char * _pat = "^a.*$"; ARGV_t _av = NULL; JSBool ok = JS_FALSE; int xx; _METHOD_DEBUG_ENTRY(_debug); if (!(ok = JS_ConvertArguments(cx, argc, argv, "/vvu", &v, &tagid, &v, &_mode))) goto exit; if (!JSVAL_IS_VOID(tagid)) { /* XXX TODO: make sure both tag and key were specified. */ tag = JSVAL_IS_INT(tagid) ? (rpmTag) JSVAL_TO_INT(tagid) : tagValue(JS_GetStringBytes(JS_ValueToString(cx, tagid))); } if (JSVAL_IS_VOID(v)) _pat = "^.*$"; else if (JSVAL_IS_NULL(v)) _pat = NULL; else if (JSVAL_IS_STRING(v)) _pat = JS_GetStringBytes(JS_ValueToString(cx, v)); #ifdef NOTYET else if (JSVAL_IS_NUMBER(v)) { uint32_t _u = 0; if (!JS_ValueToECMAUint32(cx, v, &_u)) { *rval = JSVAL_VOID; goto exit; } } else ; #endif switch (_mode) { default: *rval = JSVAL_VOID; goto exit; break; case RPMMIRE_DEFAULT: case RPMMIRE_STRCMP: case RPMMIRE_REGEX: case RPMMIRE_GLOB: case RPMMIRE_PCRE: break; } if (rpmtsGetRdb(ts) == NULL) xx = rpmtsOpenDB(ts, O_RDONLY); if (rpmdbMireApply(rpmtsGetRdb(ts), tag, _mode, _pat, &_av)) *rval = JSVAL_VOID; else if (_av == NULL || _av[0] == NULL) *rval = JSVAL_NULL; else { int _ac = argvCount(_av); int i; JSObject * arr = JS_NewArrayObject(cx, 0, NULL); *rval = OBJECT_TO_JSVAL(arr); for (i = 0; i < _ac; i++) { v = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, _av[i])); ok = JS_SetElement(cx, arr, i, &v); } } exit: ok = JS_TRUE; _av = argvFree(_av); return ok; }