JSBool netjoindomain(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { JSString * serverStr = NULL, * domainStr = NULL, *accountOUStr = NULL, *accountStr = NULL, *passwordStr = NULL; DWORD joinOptions = 0; JS_BeginRequest(cx); if(!JS_ConvertArguments(cx, argc, argv, "S S S S u", &domainStr, &accountOUStr, &accountStr, &passwordStr, &joinOptions)) { JS_ReportError(cx, "Unable to parse arguments in netjoindomain"); JS_EndRequest(cx); return JS_FALSE; } JS_YieldRequest(cx); DWORD result = NetJoinDomain(NULL, (LPWSTR)JS_GetStringChars(domainStr), (JSVAL_IS_NULL(argv[1]) ? NULL : (LPWSTR)JS_GetStringChars(accountOUStr)), (JSVAL_IS_NULL(argv[2]) ? NULL : (LPWSTR)JS_GetStringChars(accountStr)), (JSVAL_IS_NULL(argv[3]) ? NULL : (LPWSTR)JS_GetStringChars(passwordStr)), joinOptions); if(result == NERR_Success) { *rval = JSVAL_TRUE; return JS_TRUE; } JSBool retBool = JS_NewNumberValue(cx, result, rval); JS_EndRequest(cx); return retBool; }
JSBool win32_getenv(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { JS_BeginRequest(cx); if(argc < 1) { JS_ReportError(cx, "getenv requires the name of the variable to be passed"); JS_EndRequest(cx); return JS_FALSE; } JSString * name = JS_ValueToString(cx, argv[0]); DWORD size = GetEnvironmentVariable((LPWSTR)JS_GetStringChars(name), NULL, 0); if(size == 0) { *rval = JSVAL_FALSE; JS_EndRequest(cx); return JS_TRUE; } LPWSTR valueBuffer = (LPWSTR)JS_malloc(cx, (size + 2) * sizeof(WCHAR)); size = GetEnvironmentVariable((LPWSTR)JS_GetStringChars(name), valueBuffer, size + 1); if(size == 0) { JS_free(cx, valueBuffer); *rval = JSVAL_FALSE; JS_EndRequest(cx); return JS_TRUE; } JSString * value = JS_NewUCString(cx, valueBuffer, size + 1); *rval = STRING_TO_JSVAL(value); JS_EndRequest(cx); return JS_TRUE; }
/* PRBool newResolve (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in PRUint32 flags, out JSObjectPtr objp); */ NS_IMETHODIMP calDateTime::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext * cx, JSObject * obj, jsval id, PRUint32 flags, JSObject * *objp, PRBool *_retval) { NS_ENSURE_ARG_POINTER(objp); NS_ENSURE_ARG_POINTER(_retval); if (JSVAL_IS_STRING(id)) { JSString *str = JSVAL_TO_STRING(id); nsDependentString const name( reinterpret_cast<PRUnichar const*>(JS_GetStringChars(str)), JS_GetStringLength(str)); if (name.EqualsLiteral("jsDate")) { *_retval = JS_DefineUCProperty(cx, obj, JS_GetStringChars(str), JS_GetStringLength(str), JSVAL_VOID, nsnull, nsnull, 0); *objp = obj; return *_retval ? NS_OK : NS_ERROR_FAILURE; } } *_retval = PR_TRUE; return NS_OK; }
static JSBool vm_jschars(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSString *str = NULL; jschar *ptr; if (argc != 1) return gpsee_throw(cx, MODULE_ID ".jschars.arguments.count"); if (!JSVAL_IS_STRING(argv[0])) { str = JS_ValueToString(cx, argv[0]); if (!str) return JS_FALSE; /* OOM */ argv[0] = STRING_TO_JSVAL(str); /* Provide a temporary GC root */ ptr = JS_GetStringChars(str); } else { ptr = JS_GetStringChars(JSVAL_TO_STRING(argv[0])); } obj = gpsee_newByteThing(cx, ptr, 0, JS_FALSE); if (!obj) return JS_FALSE; *rval = OBJECT_TO_JSVAL(obj); if (str) JS_SetReservedSlot(cx, obj, 0, STRING_TO_JSVAL(str)); /* Provide a proper lifetimed GC root */ return JS_TRUE; }
JSDOMNodeList* JSDOMDocument::getElementsByTagNameNS(JSString *namespaceURI, JSString *localName) { if (!_doc) return new JSDOMNodeList(NULL, this); GET_VXICHAR_FROM_JSCHAR(nsval, JS_GetStringChars(namespaceURI)); VXIcharToXMLCh xmlns(nsval); GET_VXICHAR_FROM_JSCHAR(nameval, JS_GetStringChars(localName)); VXIcharToXMLCh xmlname(nameval); DOMNodeList *list = _doc->getElementsByTagNameNS(xmlns.c_str(), xmlname.c_str()); return new JSDOMNodeList(list, this); }
JSBool wimg_image_info_setter(JSContext * cx, JSObject * obj, jsval idval, jsval * vp) { JS_BeginRequest(cx); HANDLE hImage = JS_GetPrivate(cx, obj); JSString * newInfo = JS_ValueToString(cx, *vp); *vp = STRING_TO_JSVAL(newInfo); DWORD length = JS_GetStringLength(newInfo); LPWSTR chars = (LPWSTR)JS_GetStringChars(newInfo); if(*chars != 0xfeff) { length++; LPWSTR back = chars; chars = (LPWSTR)JS_malloc(cx, sizeof(WCHAR) * (length + 1)); memset(chars, 0, sizeof(WCHAR) * (length + 1)); *chars = 0xfeff; wcscat(chars, back); newInfo = JS_NewUCString(cx, (jschar*)chars, sizeof(WCHAR) * length); *vp = STRING_TO_JSVAL(newInfo); } DWORD errorCode = 0; if(!WIMSetImageInformation(hImage, (LPVOID)chars, length * sizeof(WCHAR))) errorCode = GetLastError(); JS_EndRequest(cx); return JS_TRUE; }
JSBool wimg_image_info_getter(JSContext * cx, JSObject * obj, jsval idval, jsval * vp) { JS_BeginRequest(cx); HANDLE hImage = JS_GetPrivate(cx, obj); LPVOID rawImageInfo; DWORD rawImageInfoSize; if(!WIMGetImageInformation(hImage, &rawImageInfo, &rawImageInfoSize)) *vp = JSVAL_NULL; JSString * xmlStr = JS_NewUCStringCopyN(cx, (jschar*)rawImageInfo, rawImageInfoSize / sizeof(WCHAR)); if(xmlStr == NULL) { JS_ReportError(cx, "Error creating xml string from raw image information"); JS_EndRequest(cx); return JS_FALSE; } jsval xmlStrVal = STRING_TO_JSVAL(xmlStr); *vp = xmlStrVal; LocalFree(rawImageInfo); WIMSetImageInformation(hImage, JS_GetStringChars(xmlStr), JS_GetStringLength(xmlStr) * sizeof(jschar)); JS_EndRequest(cx); return JS_TRUE; }
SV * PJS_JSString2SV( pTHX_ JSString *jstr ) { SV *ret; #if PJS_UTF8_NATIVE char *str = JS_GetStringBytes(jstr); ret = newSVpv(str, 0); SvUTF8_on(ret); #else dSP; jschar *chars = JS_GetStringChars(jstr); SV *esv = newSVpv((char *)chars, JS_GetStringLength(jstr) * sizeof(jschar)); ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(sv_2mortal(newSVpv(PJS_STR_ENCODING, 0))); XPUSHs(sv_2mortal(esv)); PUTBACK; call_pv("Encode::decode", G_SCALAR); SPAGAIN; ret = newSVsv(POPs); PUTBACK; FREETMPS; LEAVE; // sv_utf8_downgrade(ret, 1); Its safe, but pays the cost? #endif return ret; }
static JSBool IsValFrame(JSObject *obj, jsval v, XPCWrappedNative *wn) { // Fast path for the common case. if (STOBJ_GET_CLASS(obj)->name[0] != 'W') { return JS_FALSE; } nsCOMPtr<nsIDOMWindow> domwin(do_QueryWrappedNative(wn)); if (!domwin) { return JS_FALSE; } nsCOMPtr<nsIDOMWindowCollection> col; domwin->GetFrames(getter_AddRefs(col)); if (!col) { return JS_FALSE; } if (JSVAL_IS_INT(v)) { col->Item(JSVAL_TO_INT(v), getter_AddRefs(domwin)); } else { nsAutoString str(reinterpret_cast<PRUnichar *> (JS_GetStringChars(JSVAL_TO_STRING(v)))); col->NamedItem(str, getter_AddRefs(domwin)); } return domwin != nsnull; }
int to_erl_object(emonk_buf_t* buf, JSContext* cx, JSObject* obj) { JSObject* iter; JSString* key; jsid idp; jsval val; jschar* keyname; size_t keylen; int count = 0; int lengthpos; REQUEST(7); BUFPTR[0] = SMALL_TUPLE; BUFPTR[1] = (char) 1; BUFPTR[2] = LIST; // Remember the byte offset where length goes so we can write it // after enumerating the properties. lengthpos = buf->used + 3; buf->used += 7; iter = JS_NewPropertyIterator(cx, obj); if(iter == NULL) return ERROR; while(JS_NextProperty(cx, iter, &idp)) { // Done iterating, write length and bail. if(idp == JSVAL_VOID) { count = htonl(count); memcpy(buf->buf+lengthpos, &count, 4); REQUEST(1); BUFPTR[0] = NIL; buf->used += 1; return OK; } REQUEST(2); BUFPTR[0] = SMALL_TUPLE; BUFPTR[1] = 2; buf->used += 2; if(!JS_IdToValue(cx, idp, &val)) return ERROR; if(!to_erl_string(buf, cx, val)) return ERROR; key = JS_ValueToString(cx, val); keyname = JS_GetStringChars(key); keylen = JS_GetStringLength(key); if(!JS_GetUCProperty(cx, obj, keyname, keylen, &val)) return ERROR; if(!to_erl_intern(buf, cx, val)) return ERROR; count += 1; } return ERROR; }
JSBool xpc_qsJsvalToWcharStr(JSContext *cx, jsval *pval, PRUnichar **pstr) { jsval v = *pval; JSString *str; if(JSVAL_IS_STRING(v)) { str = JSVAL_TO_STRING(v); } else if(JSVAL_IS_VOID(v) || JSVAL_IS_NULL(v)) { *pstr = NULL; return JS_TRUE; } else { if(!(str = JS_ValueToString(cx, v))) return JS_FALSE; *pval = STRING_TO_JSVAL(str); // Root the new string. } *pstr = (PRUnichar*)JS_GetStringChars(str); return JS_TRUE; }
nsresult nsDOMWorkerTimeout::ExpressionCallback::Run(nsDOMWorkerTimeout* aTimeout, JSContext* aCx) { JSObject* global = JS_GetGlobalObject(aCx); NS_ENSURE_TRUE(global, NS_ERROR_FAILURE); JSPrincipals* principal = nsDOMWorkerSecurityManager::WorkerPrincipal(); NS_ENSURE_TRUE(principal, NS_ERROR_FAILURE); JSString* expression = JS_ValueToString(aCx, mExpression); NS_ENSURE_TRUE(expression, NS_ERROR_FAILURE); jschar* string = JS_GetStringChars(expression); NS_ENSURE_TRUE(string, NS_ERROR_FAILURE); size_t stringLength = JS_GetStringLength(expression); jsval rval; PRBool success = JS_EvaluateUCScriptForPrincipals(aCx, global, principal, string, stringLength, mFileName.get(), mLineNumber, &rval); if (!success) { return NS_ERROR_FAILURE; } return NS_OK; }
char* enc_string(JSContext* cx, jsval arg, size_t* buflen) { JSString* str = NULL; jschar* src = NULL; char* bytes = NULL; size_t srclen = 0; size_t byteslen = 0; str = JS_ValueToString(cx, arg); if(!str) goto error; src = JS_GetStringChars(str); srclen = JS_GetStringLength(str); if(!enc_charbuf(src, srclen, NULL, &byteslen)) goto error; bytes = JS_malloc(cx, (byteslen) + 1); bytes[byteslen] = 0; if(!enc_charbuf(src, srclen, bytes, &byteslen)) goto error; if(buflen) *buflen = byteslen; goto success; error: if(bytes != NULL) JS_free(cx, bytes); bytes = NULL; success: return bytes; }
PyObject* Context_execute(Context* self, PyObject* args, PyObject* kwargs) { PyObject* obj = NULL; PyObject* ret = NULL; JSContext* cx = NULL; JSObject* root = NULL; JSString* script = NULL; jschar* schars = NULL; JSBool started_counter = JS_FALSE; size_t slen; jsval rval; JS_BeginRequest(self->cx); if(!PyArg_ParseTuple(args, "O", &obj)) goto error; script = py2js_string_obj(self, obj); if(script == NULL) goto error; schars = JS_GetStringChars(script); slen = JS_GetStringLength(script); cx = self->cx; root = self->root; // Mark us for time consumption if(self->start_time == 0) { started_counter = JS_TRUE; self->start_time = time(NULL); } if(!JS_EvaluateUCScript(cx, root, schars, slen, "<JavaScript>", 1, &rval)) { if(!PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "Failed to execute script."); } goto error; } if(PyErr_Occurred()) goto error; ret = js2py(self, rval); JS_EndRequest(self->cx); JS_MaybeGC(self->cx); goto success; error: JS_EndRequest(self->cx); success: if(started_counter) { self->start_time = 0; } return ret; }
/* PRBool getProperty (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in JSValPtr vp); */ NS_IMETHODIMP calDateTime::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext * cx, JSObject * obj_, jsval id, jsval * vp, PRBool *_retval) { NS_ENSURE_ARG_POINTER(vp); NS_ENSURE_ARG_POINTER(_retval); if (JSVAL_IS_STRING(id)) { nsDependentString const jsid( reinterpret_cast<PRUnichar const*>( JS_GetStringChars(JSVAL_TO_STRING(id))), JS_GetStringLength(JSVAL_TO_STRING(id))); if (jsid.EqualsLiteral("jsDate")) { PRTime tmp, thousand; jsdouble msec; LL_I2L(thousand, 1000); LL_DIV(tmp, mNativeTime, thousand); LL_L2D(msec, tmp); JSObject *obj; PRBool b; if (NS_SUCCEEDED(mTimezone->GetIsFloating(&b)) && b) obj = js_NewDateObject(cx, mYear, mMonth, mDay, mHour, mMinute, mSecond); else obj = js_NewDateObjectMsec(cx, msec); *vp = OBJECT_TO_JSVAL(obj); *_retval = PR_TRUE; return NS_SUCCESS_I_DID_SOMETHING; } } *_retval = PR_TRUE; return NS_OK; }
/* * Convert any jsval v to an integer jsval if ToString(v) * contains a base-10 integer that fits into 31 bits. * Otherwise return v. */ static jsval try_convert_to_jsint(JSContext *cx, jsval idval) { const jschar *cp; JSString *jsstr; jsstr = JS_ValueToString(cx, idval); if (!jsstr) return idval; cp = JS_GetStringChars(jsstr); if (JS7_ISDEC(*cp)) { jsuint index = JS7_UNDEC(*cp++); jsuint oldIndex = 0; jsuint c = 0; if (index != 0) { while (JS7_ISDEC(*cp)) { oldIndex = index; c = JS7_UNDEC(*cp); index = 10*index + c; cp++; } } if (*cp == 0 && (oldIndex < (JSVAL_INT_MAX / 10) || (oldIndex == (JSVAL_INT_MAX / 10) && c < (JSVAL_INT_MAX % 10)))) { return INT_TO_JSVAL(index); } } return idval; }
JSBool stringPropSetter(JSContext * cx, JSObject * obj, jsval idval, jsval * vp) { JS_BeginRequest(cx); nsCOMPtr<nsIDOMNode> mNode = (nsIDOMNode*)JS_GetPrivate(cx, obj); JSString * valStr = JS_ValueToString(cx, *vp); *vp = STRING_TO_JSVAL(valStr); jschar * valChars = JS_GetStringChars(valStr); nsString value(valChars); nsresult result; EnterCriticalSection(&domStateLock); domState = 1; switch(JSVAL_TO_INT(idval)) { case VALUE_PROP: result = mNode->SetNodeValue(value); break; case TEXT_PROP: { nsCOMPtr<nsIDOM3Node> l3Node = do_QueryInterface(mNode, &result); if(NS_SUCCEEDED(result)) l3Node->SetTextContent(value); } break; } LeaveCriticalSection(&domStateLock); JS_EndRequest(cx); return JS_TRUE; }
static JSBool js_json_parse(JSContext *cx, JSObject* pThis, uintN argc, jsval *vp, jsval* rval) { JSString *s = NULL; jsval *argv = vp; // Must throw an Error if there isn't a first arg if (!JS_ConvertArguments(cx, argc, argv, "S", &s)) return JS_FALSE; JSBool ok = JS_TRUE; JSONParser *jp = js_BeginJSONParse(cx, rval); if (!jp) ok = JS_FALSE; 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; }
JSDOMElement* JSDOMDocument::getElementById(JSString *elementId) { if (!_doc) return NULL; GET_VXICHAR_FROM_JSCHAR(idval, JS_GetStringChars(elementId)); VXIcharToXMLCh xmlid(idval); DOMElement *elem = _doc->getElementById(xmlid.c_str()); return elem ? new JSDOMElement(elem) : NULL; }
JSDOMNodeList* JSDOMDocument::getElementsByTagName(JSString* tagname) { if (!_doc) return new JSDOMNodeList(NULL, this); GET_VXICHAR_FROM_JSCHAR(tmpval, JS_GetStringChars(tagname)); VXIcharToXMLCh tag(tmpval); DOMNodeList *list = _doc->getElementsByTagName(tag.c_str()); return new JSDOMNodeList(list, this); }
jstring jsj_ConvertJSStringToJavaString(JSContext *cx, JNIEnv *jEnv, JSString *js_str) { jstring result; result = (*jEnv)->NewString(jEnv, JS_GetStringChars(js_str), JS_GetStringLength(js_str)); if (!result) { jsj_UnexpectedJavaError(cx, jEnv, "Couldn't construct instance " "of java.lang.String"); } return result; }
JSBool win32_impersonateuser(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { JSString * username, * password, *domain = NULL; DWORD logonType = LOGON32_LOGON_INTERACTIVE; JS_BeginRequest(cx); if(!JS_ConvertArguments(cx, argc, argv, "S S /S u", &username, &password, &domain, &logonType)) { JS_ReportError(cx, "Unable to parse arguments in ImpersonateUser"); JS_EndRequest(cx); return JS_FALSE; } if(logonType == LOGON32_LOGON_NETWORK) { *rval = JSVAL_FALSE; JS_EndRequest(cx); return JS_TRUE; } LPWSTR domainName = NULL; if(domain != NULL) domainName = (LPWSTR)JS_GetStringChars(domain); HANDLE newToken = NULL; JS_YieldRequest(cx); if(!LogonUser((LPWSTR)JS_GetStringChars(username), domainName, (LPWSTR)JS_GetStringChars(password), logonType, LOGON32_PROVIDER_DEFAULT, &newToken)) { *rval = JSVAL_FALSE; JS_EndRequest(cx); return JS_TRUE; } if(!ImpersonateLoggedOnUser(newToken)) *rval = JSVAL_FALSE; else *rval = JSVAL_TRUE; CloseHandle(newToken); JS_EndRequest(cx); return JS_TRUE; }
static JSBool evalcx(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSString *str; JSObject *sandbox; JSContext *subcx; const jschar *src; size_t srclen; JSBool ret = JS_FALSE; char *name = NULL; sandbox = NULL; if(!JS_ConvertArguments(cx, argc, argv, "S / o", &str, &sandbox)) { return JS_FALSE; } subcx = JS_NewContext(JS_GetRuntime(cx), 8L * 1024L); if(!subcx) { JS_ReportOutOfMemory(cx); return JS_FALSE; } SETUP_REQUEST(subcx); src = JS_GetStringChars(str); srclen = JS_GetStringLength(str); if(!sandbox) { sandbox = JS_NewObject(subcx, NULL, NULL, NULL); if(!sandbox || !JS_InitStandardClasses(subcx, sandbox)) { goto done; } } if(argc > 2) { name = enc_string(cx, argv[2], NULL); } if(srclen == 0) { *rval = OBJECT_TO_JSVAL(sandbox); } else { JS_EvaluateUCScript(subcx, sandbox, src, srclen, name, 1, rval); } ret = JS_TRUE; done: if(name) JS_free(cx, name); FINISH_REQUEST(subcx); JS_DestroyContext(subcx); return ret; }
JSBool win32_setdlldirectory(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { JS_BeginRequest(cx); if(argc < 1) { JS_ReportError(cx, "Must pass a path to SetDllDirectory."); JS_EndRequest(cx); return JS_FALSE; } JSString * str = JS_ValueToString(cx, *argv); *rval = SetDllDirectory((LPWSTR)JS_GetStringChars(str)) ? JSVAL_TRUE : JSVAL_FALSE; JS_EndRequest(cx); return JS_TRUE; }
PSID convert_jsstring_to_sid(JSContext * cx, JSString * curMemberString, DWORD * errorCode) { PSID curMember; if(!ConvertStringSidToSid((LPWSTR)JS_GetStringChars(curMemberString), &curMember)) { DWORD sidSize = 0, cbDomain; SID_NAME_USE peUse; *errorCode = GetLastError(); JS_YieldRequest(cx); if(!LookupAccountName(NULL, (LPWSTR)JS_GetStringChars(curMemberString), NULL, &sidSize, NULL, &cbDomain, &peUse) && GetLastError() != ERROR_INSUFFICIENT_BUFFER) { *errorCode = GetLastError(); return NULL; } curMember = (PSID)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sidSize); JS_YieldRequest(cx); LPTSTR domainName = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, cbDomain * sizeof(TCHAR)); if(!LookupAccountName(NULL, (LPWSTR)JS_GetStringChars(curMemberString), curMember, &sidSize, domainName, &cbDomain, &peUse)) { *errorCode = GetLastError(); HeapFree(GetProcessHeap(), 0, curMember); HeapFree(GetProcessHeap(), 0, domainName); return NULL; } HeapFree(GetProcessHeap(), 0, domainName); *errorCode = ERROR_SUCCESS; } else { DWORD sidSize = GetLengthSid(curMember); PSID retMember = (PSID)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sidSize); CopySid(sidSize, retMember, curMember); LocalFree(curMember); curMember = retMember; } return curMember; }
static JSBool evalcx(JSContext *cx, uintN argc, jsval* vp) { jsval* argv = JS_ARGV(cx, vp); JSString *str; JSObject *sandbox; JSContext *subcx; const jschar *src; size_t srclen; jsval rval; JSBool ret = JS_FALSE; sandbox = NULL; if(!JS_ConvertArguments(cx, argc, argv, "S / o", &str, &sandbox)) { return JS_FALSE; } subcx = JS_NewContext(JS_GetRuntime(cx), 8L * 1024L); if(!subcx) { JS_ReportOutOfMemory(cx); return JS_FALSE; } SETUP_REQUEST(subcx); src = JS_GetStringChars(str); srclen = JS_GetStringLength(str); if(!sandbox) { sandbox = JS_NewObject(subcx, NULL, NULL, NULL); if(!sandbox || !JS_InitStandardClasses(subcx, sandbox)) { goto done; } } if(srclen == 0) { JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(sandbox)); } else { JS_EvaluateUCScript(subcx, sandbox, src, srclen, NULL, 0, &rval); JS_SET_RVAL(cx, vp, rval); } ret = JS_TRUE; done: FINISH_REQUEST(subcx); JS_DestroyContext(subcx); return ret; }
JSBool win32_setcurrentdirectory(JSContext * cx, JSObject * obj, uintN argc, jsval *argv, jsval * rval) { JS_BeginRequest(cx); if(argc < 1 || !JSVAL_IS_STRING(*argv)) { JS_ReportError(cx, "Must provide directory name to setcurrentdirectory."); JS_EndRequest(cx); return JS_FALSE; } JSString * pathName = JS_ValueToString(cx, *argv); *rval = SetCurrentDirectory((LPWSTR)JS_GetStringChars(pathName)) ? JSVAL_TRUE : JSVAL_FALSE; JS_EndRequest(cx); return JS_TRUE; }
NS_IMETHODIMP nsJSSh::ExecuteBuffer() { #ifdef DEBUG // nsCOMPtr<nsIThread> thread; // nsIThread::GetCurrent(getter_AddRefs(thread)); // printf("executing on thread %p\n", thread.get()); #endif JS_BeginRequest(mJSContext); JS_ClearPendingException(mJSContext); JSPrincipals *jsprincipals; mPrincipal->GetJSPrincipals(mJSContext, &jsprincipals); if(NS_FAILED(mContextStack->Push(mJSContext))) { NS_ERROR("failed to push the current JSContext on the nsThreadJSContextStack"); return NS_ERROR_FAILURE; } JSScript *script = JS_CompileScriptForPrincipals(mJSContext, mContextObj, jsprincipals, mBuffer, mBufferPtr, "interactive", 0); if (script) { jsval result; if (JS_ExecuteScript(mJSContext, mContextObj, script, &result) && result!=JSVAL_VOID && mOutput) { // XXX for some wrapped native objects the following code will crash; probably because the // native object is getting released before we reach this: JSString *str = JS_ValueToString(mJSContext, result); if (str) { nsDependentString chars(reinterpret_cast<const PRUnichar*> (JS_GetStringChars(str)), JS_GetStringLength(str)); NS_ConvertUTF16toUTF8 cstr(chars); PRUint32 bytesWritten; mOutput->Write(cstr.get(), cstr.Length(), &bytesWritten); } } JS_DestroyScript(mJSContext, script); } JSContext *oldcx; mContextStack->Pop(&oldcx); NS_ASSERTION(oldcx == mJSContext, "JS thread context push/pop mismatch"); JSPRINCIPALS_DROP(mJSContext, jsprincipals); JS_EndRequest(mJSContext); return NS_OK; }
void ConvertJSValToStr(nsString& aString, JSContext* aContext, jsval aValue) { JSString *jsstring; if ( !JSVAL_IS_NULL(aValue) && (jsstring = JS_ValueToString(aContext, aValue)) != nsnull) { aString.Assign(reinterpret_cast<const PRUnichar*>(JS_GetStringChars(jsstring))); } else { aString.Truncate(); } }
JSBool childGetter(JSContext * cx, JSObject * obj, jsval idval, jsval * vp) { JS_BeginRequest(cx); nsCOMPtr<nsIDOMNode> mNode = (nsIDOMNode*)JS_GetPrivate(cx, obj); JSString * idvalStr = JS_ValueToString(cx, idval); jschar * idvalChars = JS_GetStringChars(idvalStr); if(_wcsicmp(idvalChars, TEXT("childNodes")) == 0) { nsIDOMNodeList * nodeList; mNode->GetChildNodes(&nodeList); buildNodeList(nodeList, cx, obj, vp); nodeList->Release(); } else *vp = JSVAL_NULL; JS_EndRequest(cx); return JS_TRUE; }