JSBool wimg_apply_image(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { DWORD applyFlags = 0; LPWSTR path = NULL; JS_BeginRequest(cx); if(!JS_ConvertArguments(cx, argc, argv, "W /u", &path, &applyFlags)) { JS_ReportError(cx, "Error during argument parsing in WIMApplyImage"); JS_EndRequest(cx); return JS_FALSE; } HANDLE imageHandle = JS_GetPrivate(cx, obj); jsrefcount rCount = JS_SuspendRequest(cx); *rval = WIMApplyImage(imageHandle, path, applyFlags) ? JSVAL_TRUE : JSVAL_FALSE; JS_ResumeRequest(cx, rCount); if(*rval == JSVAL_FALSE) { JS_EndRequest(cx); return JS_TRUE; } TCHAR volumeName[MAX_PATH + 1]; memset(volumeName, 0, sizeof(TCHAR) * (MAX_PATH + 1)); _tcscat_s(volumeName, MAX_PATH + 1, TEXT("\\\\.\\")); _tcscat_s(volumeName, (MAX_PATH + 1) - 4, path); if(!GetVolumePathName(volumeName, volumeName, MAX_PATH + 1)) { JS_EndRequest(cx); *rval = JSVAL_FALSE; return JS_TRUE; } LPTSTR lastSlash = _tcsrchr(volumeName, _T('\\')); *lastSlash = '\0'; HANDLE volumeHandle = CreateFile(volumeName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if(volumeHandle == INVALID_HANDLE_VALUE) { JS_EndRequest(cx); *rval = JSVAL_FALSE; return JS_TRUE; } rCount = JS_SuspendRequest(cx); if(!FlushFileBuffers(volumeHandle)) { CloseHandle(volumeHandle); JS_ResumeRequest(cx, rCount); JS_EndRequest(cx); *rval = JSVAL_FALSE; return JS_TRUE; } CloseHandle(volumeHandle); JS_ResumeRequest(cx, rCount); *rval = JSVAL_TRUE; JS_EndRequest(cx); return JS_TRUE; }
static JSBool socket_read_bytes(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { js_socket_obj_t *socket = JS_GetPrivate(cx, obj); if (socket == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to find js object.\n"); return JS_FALSE; } if (argc == 1) { int32 bytes_to_read; switch_status_t ret; switch_size_t len; JS_ValueToInt32(cx, argv[0], &bytes_to_read); len = (switch_size_t) bytes_to_read; if (socket->buffer_size < len) { socket->read_buffer = switch_core_alloc(socket->pool, len + 1); socket->buffer_size = bytes_to_read + 1; } socket->saveDepth = JS_SuspendRequest(cx); ret = switch_socket_recv(socket->socket, socket->read_buffer, &len); JS_ResumeRequest(cx, socket->saveDepth); if (ret != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "switch_socket_send failed: %d.\n", ret); *rval = BOOLEAN_TO_JSVAL(JS_FALSE); } else { socket->read_buffer[len] = 0; *rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, socket->read_buffer)); } } return JS_TRUE; }
static size_t file_callback(void *ptr, size_t size, size_t nmemb, void *data) { register unsigned int realsize = (unsigned int) (size * nmemb); struct curl_obj *co = data; uintN argc = 0; jsval argv[4]; if (!co) { return 0; } if (co->function) { char *ret; argv[argc++] = STRING_TO_JSVAL(JS_NewStringCopyZ(co->cx, (char *) ptr)); if (co->user_data) { argv[argc++] = OBJECT_TO_JSVAL(co->user_data); } JS_ResumeRequest(co->cx, co->saveDepth); JS_CallFunction(co->cx, co->obj, co->function, argc, argv, &co->ret); co->saveDepth = JS_SuspendRequest(co->cx); if ((ret = JS_GetStringBytes(JS_ValueToString(co->cx, co->ret)))) { if (!strcmp(ret, "true") || !strcmp(ret, "undefined")) { return realsize; } else { return 0; } } } return realsize; }
JSBool wimg_capture_image(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { LPWSTR path; DWORD captureFlags = 0; JS_BeginRequest(cx); if(!JS_ConvertArguments(cx, argc, argv, "W /u", &path, &captureFlags)) { JS_ReportError(cx, "Error during argument parsing in WIMCaptureImage"); JS_EndRequest(cx); return JS_FALSE; } if(wcslen(path) > MAX_PATH) { *rval = JSVAL_FALSE; JS_EndRequest(cx); return JS_TRUE; } jsrefcount rCount = JS_SuspendRequest(cx); HANDLE wimFile = JS_GetPrivate(cx, obj); HANDLE newImage = WIMCaptureImage(wimFile, path, captureFlags); JS_ResumeRequest(cx, rCount); if(newImage == NULL) *rval = JSVAL_FALSE; else { JSObject * newImageObj = JS_NewObject(cx, &wimImageClass, wimImageProto, obj); *rval = OBJECT_TO_JSVAL(newImageObj); JS_SetPrivate(cx, newImageObj, newImage); } JS_EndRequest(cx); return JS_TRUE; }
JSBool ADM_JSAvidemux::rebuildIndex(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {// begin GoToTime ADM_JSAvidemux *p = (ADM_JSAvidemux *)JS_GetPrivate(cx, obj); // default return value *rval = BOOLEAN_TO_JSVAL(false); if(argc != 0) return JS_FALSE; jsrefcount nRefCount = JS_SuspendRequest(cx); video_body->rebuildFrameType(); JS_ResumeRequest(cx,nRefCount); return JS_TRUE; }// end GoToTime
JSBool ADM_JSAvidemux::forceUnpack(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {// begin GoToTime ADM_JSAvidemux *p = (ADM_JSAvidemux *)JS_GetPrivate(cx, obj); // default return value *rval = BOOLEAN_TO_JSVAL(false); if(argc != 0) return JS_FALSE; jsrefcount nRefCount = JS_SuspendRequest(cx); video_body->setEnv(ENV_EDITOR_PVOP); JS_ResumeRequest(cx,nRefCount); *rval = INT_TO_JSVAL(1); return JS_TRUE; }// end GoToTime
JSBool ADM_JSAvidemux::JSGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { if (JSVAL_IS_INT(id)) { ADM_JSAvidemux *priv = (ADM_JSAvidemux *) JS_GetPrivate(cx, obj); switch(JSVAL_TO_INT(id)) { case markerA_prop: *vp = INT_TO_JSVAL(frameStart); break; case markerB_prop: *vp = INT_TO_JSVAL(frameEnd); break; case audio_prop: *vp = OBJECT_TO_JSVAL(priv->getObject()->m_pAudio); break; case video_prop: *vp = OBJECT_TO_JSVAL(priv->getObject()->m_pVideo); break; case container_prop: *vp = STRING_TO_JSVAL(priv->getObject()->m_pContainer); break; case currentframe_prop: *vp = INT_TO_JSVAL(priv->getObject()->m_nCurrentFrame); break; case fps_prop: { aviInfo info; if (avifileinfo) { jsrefcount nRefCount = JS_SuspendRequest(cx); video_body->getVideoInfo(&info); priv->getObject()->m_dFPS = info.fps1000/1000.0; (uint32_t)floor(priv->getObject()->m_dFPS*1000.f); video_body->updateVideoInfo (&info); video_body->getVideoInfo (avifileinfo); JS_ResumeRequest(cx,nRefCount); } else { return JS_FALSE; } *vp = DOUBLE_TO_JSVAL(priv->getObject()->m_dFPS); } break; } } return JS_TRUE; }
static JSBool vm_dumpGCStats(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { const char *filename = JS_GetStringBytes(JS_ValueToString(cx, argv[0])); FILE *file; depth = JS_SuspendRequest(cx); file = fopen(filename, "w"); flock(file, LOCK_EX); JS_ResumeRequest(cx); js_DumpGCStats(); fclose(file); }
static void socket_destroy(JSContext * cx, JSObject * obj) { js_socket_obj_t *socket = JS_GetPrivate(cx, obj); if (socket == NULL) return; if (socket->socket != 0) { socket->saveDepth = JS_SuspendRequest(cx); switch_socket_shutdown(socket->socket, SWITCH_SHUTDOWN_READWRITE); switch_socket_close(socket->socket); switch_core_destroy_memory_pool(&socket->pool); JS_ResumeRequest(cx, socket->saveDepth); } }
JSBool ADM_JSAvidemux::ClearSegments(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {// begin ClearSegments ADM_JSAvidemux *p = (ADM_JSAvidemux *)JS_GetPrivate(cx, obj); // default return value *rval = BOOLEAN_TO_JSVAL(false); if(argc != 0) return JS_FALSE; printf("clearing segments \n"); jsrefcount nRefCount = JS_SuspendRequest(cx); *rval = BOOLEAN_TO_JSVAL(video_body->deleteAllSegments()); JS_ResumeRequest(cx,nRefCount); updateAll(); return JS_TRUE; }// end ClearSegments
static JSBool socket_close(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { js_socket_obj_t *socket = JS_GetPrivate(cx, obj); if (socket == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to find js object.\n"); return JS_FALSE; } socket->saveDepth = JS_SuspendRequest(cx); switch_socket_shutdown(socket->socket, SWITCH_SHUTDOWN_READWRITE); switch_socket_close(socket->socket); socket->socket = NULL; JS_ResumeRequest(cx, socket->saveDepth); return JS_TRUE; }
JSBool ADM_JSAvidemux::GoToTime(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {// begin GoToTime ADM_JSAvidemux *p = (ADM_JSAvidemux *)JS_GetPrivate(cx, obj); // default return value *rval = BOOLEAN_TO_JSVAL(false); if(argc != 3) return JS_FALSE; if(JSVAL_IS_INT(argv[0]) == false || JSVAL_IS_INT(argv[1]) == false || JSVAL_IS_INT(argv[2]) == false) return JS_FALSE; jsrefcount nRefCount = JS_SuspendRequest(cx); *rval = INT_TO_JSVAL(A_jumpToTime(JSVAL_TO_INT(argv[0]),JSVAL_TO_INT(argv[1]),JSVAL_TO_INT(argv[2]))); JS_ResumeRequest(cx,nRefCount); return JS_TRUE; }// end GoToTime
JSBool win32_sleep(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { if(argc < 1 || ! JSVAL_IS_NUMBER(*argv)) { JS_ReportError(cx, "Must pass a numeric value to Sleep."); return JS_FALSE; } DWORD nTimeout = 0; JS_BeginRequest(cx); JS_ValueToECMAUint32(cx, *argv, &nTimeout); jsrefcount rCount = JS_SuspendRequest(cx); Sleep(nTimeout); JS_ResumeRequest(cx, rCount); JS_EndRequest(cx); return JS_TRUE; }
static JSBool js_sleep(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { int msec; if (!JS_ConvertArguments(cx, argc, argv, "u", &msec)) return JS_FALSE; jsrefcount s = JS_SuspendRequest(cx); usleep(msec * 1000); JS_ResumeRequest(cx, s); *rval = JSVAL_VOID; return JS_TRUE; }
jsrefcount js_debug_suspendrequest(JSContext *cx, const char *file, unsigned long line) { struct request_log *req; jsrefcount ret; if(!initialized) initialize_request(); pthread_mutex_lock(&req_mutex); req=match_request(cx); if(req==NULL) { strcpy(str,"Missing req in Suspend\n"); logstr(); return -1; } if(!JS_IsInRequest(cx)) { sprintf(str,"Suspend WITHOUT REQUEST after %s from %s:%lu at %s:%lu (%p)\n",type_names[req->type],req->file,req->line,file,line,req->cx); } switch(req->type) { case LAST_REQUEST_TYPE_BEGIN: case LAST_REQUEST_TYPE_RESUME: break; case LAST_REQUEST_TYPE_NONE: if(req->file==NULL) /* Assumed to be a provided request */ break; case LAST_REQUEST_TYPE_END: case LAST_REQUEST_TYPE_SUSPEND: sprintf(str,"Suspend after %s from %s:%lu at %s:%lu (%p)\n",type_names[req->type],req->file,req->line,file,line,req->cx); logstr(); break; } switch(JS_RequestDepth(cx)) { case 0: default: sprintf(str,"depth=%u at Suspend after %s from %s:%lu at %s:%lu (%p)\n",JS_RequestDepth(cx),type_names[req->type],req->file,req->line,file,line,req->cx); logstr(); break; case 1: break; } req->type=LAST_REQUEST_TYPE_SUSPEND; req->file=file; req->line=line; ret=JS_SuspendRequest(cx); pthread_mutex_unlock(&req_mutex); return(ret); }
JSBool ADM_JSAvidemux::SaveOGM(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {// begin SaveOGM ADM_JSAvidemux *p = (ADM_JSAvidemux *)JS_GetPrivate(cx, obj); // default return value *rval = BOOLEAN_TO_JSVAL(false); if(argc != 1) return JS_FALSE; if(JSVAL_IS_STRING(argv[0]) == false) return JS_FALSE; char *pTempStr = JS_GetStringBytes(JSVAL_TO_STRING(argv[0])); printf("Saving as DVD \"%s\"\n",pTempStr); jsrefcount nRefCount = JS_SuspendRequest(cx); *rval = BOOLEAN_TO_JSVAL(A_saveDVDPS(pTempStr)); JS_ResumeRequest(cx,nRefCount); return JS_TRUE; }// end SaveOGM
JSBool ADM_JSAvidemux::setContainer(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { ADM_JSAvidemux *p = (ADM_JSAvidemux *)JS_GetPrivate(cx, obj); // default return value *rval = BOOLEAN_TO_JSVAL(false); if(argc != 1) return JS_FALSE; if(JSVAL_IS_STRING(argv[0]) == false) return JS_FALSE; char *str = JS_GetStringBytes(JSVAL_TO_STRING(argv[0])); jsrefcount nRefCount = JS_SuspendRequest(cx); if(A_setContainer(str)) *rval = BOOLEAN_TO_JSVAL( true); JS_ResumeRequest(cx,nRefCount); return JS_TRUE; }
JSBool ADM_JSAvidemux::Delete(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {// begin Delete ADM_JSAvidemux *p = (ADM_JSAvidemux *)JS_GetPrivate(cx, obj); // default return value *rval = BOOLEAN_TO_JSVAL(false); if(argc != 2) return JS_FALSE; if(JSVAL_IS_INT(argv[0]) == false || JSVAL_IS_INT(argv[1]) == false) return JS_FALSE; int a = JSVAL_TO_INT(argv[0]); int b = JSVAL_TO_INT(argv[1]); aprintf("Deleting %d-%d\n",a,b); jsrefcount nRefCount = JS_SuspendRequest(cx); *rval = BOOLEAN_TO_JSVAL(A_delete(a,b)); JS_ResumeRequest(cx,nRefCount); return JS_TRUE; }// end Delete
JSBool Core_exec (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { FILE* pipe; char* output = NULL; size_t length = 0; size_t read = 0; const char* command; JS_BeginRequest(cx); JS_EnterLocalRootScope(cx); if (argc != 1 || !JS_ConvertArguments(cx, argc, argv, "s", &command)) { JS_ReportError(cx, "Not enough parameters."); return JS_FALSE; } jsrefcount req = JS_SuspendRequest(cx); if ((pipe = popen(command, "r")) == NULL) { JS_ReportError(cx, "Command not found"); return JS_FALSE; } // Read untill the pipe is empty. while (1) { output = (char*) JS_realloc(cx, output, length+=512); read = fread(output+(length-512), sizeof(char), 512, pipe); if (read < 512) { output = (char*) JS_realloc(cx, output, length-=(512-read)); break; } } output[length-1] = '\0'; pclose(pipe); JS_ResumeRequest(cx, req); *rval = STRING_TO_JSVAL(JS_NewString(cx, output, length)); JS_LeaveLocalRootScope(cx); JS_EndRequest(cx); return JS_TRUE; }
/* void push (in JSContext cx); */ NS_IMETHODIMP XPCJSContextStack::Push(JSContext * cx) { JS_ASSERT_IF(cx, JS_GetContextThread(cx)); if(!mStack.AppendElement(cx)) return NS_ERROR_OUT_OF_MEMORY; if(mStack.Length() > 1) { XPCJSContextInfo & e = mStack[mStack.Length() - 2]; if(e.cx) { if(e.cx == cx) { nsIScriptSecurityManager* ssm = XPCWrapper::GetSecurityManager(); if(ssm) { nsIPrincipal* globalObjectPrincipal = GetPrincipalFromCx(cx); if(globalObjectPrincipal) { nsIPrincipal* subjectPrincipal = ssm->GetCxSubjectPrincipal(cx); PRBool equals = PR_FALSE; globalObjectPrincipal->Equals(subjectPrincipal, &equals); if(equals) { return NS_OK; } } } } { // Push() can be called outside any request for e.cx. JSAutoRequest ar(e.cx); e.frame = JS_SaveFrameChain(e.cx); } if(!cx) e.suspendDepth = JS_SuspendRequest(e.cx); } } return NS_OK; }
JSBool ADM_JSAvidemux::Load(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {// begin Load ADM_JSAvidemux *p = (ADM_JSAvidemux *)JS_GetPrivate(cx, obj); // default return value *rval = BOOLEAN_TO_JSVAL(false); if(argc != 1) return JS_FALSE; if(JSVAL_IS_STRING(argv[0]) == false) return JS_FALSE; char *pTempStr = JS_GetStringBytes(JSVAL_TO_STRING(argv[0])); printf("Loading \"%s\"\n",pTempStr); // Do a failure instead of returing ko *rval = BOOLEAN_TO_JSVAL(JS_TRUE); jsrefcount nRefCount = JS_SuspendRequest(cx); if(!A_openAvi(pTempStr)) return JS_FALSE; JS_ResumeRequest(cx,nRefCount); return JS_TRUE; }// end Load
/* add a segment. addsegment(source video,startframe, nbframes)", */ JSBool ADM_JSAvidemux::AddSegment(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {// begin AddSegment ADM_JSAvidemux *p = (ADM_JSAvidemux *)JS_GetPrivate(cx, obj); // default return value *rval = BOOLEAN_TO_JSVAL(false); if(argc != 3) return JS_FALSE; if(JSVAL_IS_INT(argv[0]) == false || JSVAL_IS_INT(argv[1]) == false || JSVAL_IS_INT(argv[2]) == false) return JS_FALSE; int a = JSVAL_TO_INT(argv[0]); int b = JSVAL_TO_INT(argv[1]); int c = JSVAL_TO_INT(argv[2]); aprintf("adding segment :%d %d %d\n",a,b,c); jsrefcount nRefCount = JS_SuspendRequest(cx); *rval = BOOLEAN_TO_JSVAL( video_body->addSegment(a,b,c)); JS_ResumeRequest(cx,nRefCount); updateAll(); return JS_TRUE; }// end AddSegment
/** API-Compatible with Print() in js.c. Uses more RAM, but much less likely * to intermingle output newlines when called from two different threads. */ JSBool gpsee_global_print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSString *str; jsrefcount depth; uintN i; /* Iterate over all arguments, converting them to strings as necessary, replacing * the contents of argv with the results. */ for (i = 0; i < argc; i++) { /* Not already a string? */ if (!JSVAL_IS_STRING(argv[i])) { /* Convert to string ; may call toString() and the like */ str = JS_ValueToString(cx, argv[i]); if (!str) return JS_FALSE; /* threw */ /* Root the string in our argument vector, we don't need the old argument now, anyway */ argv[i] = STRING_TO_JSVAL(str); } else str = JSVAL_TO_STRING(argv[i]); } /* Now we'll print each member of argv, which at this point contains the results of the previous * loop we iterated over argv in, so they are all guaranteed to be JSVAL_STRING type. */ for (i = 0; i < argc; i++) { str = JS_ValueToString(cx, argv[i]); GPSEE_ASSERT(str); /* Suspend request, in case this write operation blocks TODO make optional? */ depth = JS_SuspendRequest(cx); /* Print the argument, taking care for putting a space between arguments, and a newline at the end */ gpsee_printf(cx, "%s%s%s", i ? " " : "", JS_GetStringBytes(str), i+1==argc?"\n":""); JS_ResumeRequest(cx, depth); } *rval = JSVAL_VOID; return JS_TRUE; }
JSBool js_wait_for_value(JSContext *cx, prop_t *root, const char *subname, jsval value, jsval *rval) { prop_courier_t *pc = prop_courier_create_waitable(); prop_sub_t *s; wfv_t wfv; wfv.value = value; wfv.done = 0; s = prop_subscribe(0, PROP_TAG_ROOT, root, PROP_TAG_COURIER, pc, PROP_TAG_CALLBACK, vfw_setval, &wfv, PROP_TAG_NAMESTR, subname, NULL); if(s == NULL) { JS_ReportError(cx, "Unable to subscribe to %s", subname); return JS_FALSE; } *rval = JSVAL_TRUE; while(!wfv.done) { struct prop_notify_queue exp, nor; jsrefcount s = JS_SuspendRequest(cx); prop_courier_wait(pc, &nor, &exp); JS_ResumeRequest(cx, s); prop_notify_dispatch(&exp); prop_notify_dispatch(&nor); } prop_unsubscribe(s); prop_courier_destroy(pc); return JS_TRUE; }
static JSBool socket_send(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { js_socket_obj_t *socket = JS_GetPrivate(cx, obj); if (socket == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to find js object.\n"); return JS_FALSE; } if (argc == 1) { switch_status_t ret; char *buffer = JS_GetStringBytes(JS_ValueToString(cx, argv[0])); switch_size_t len = strlen(buffer); socket->saveDepth = JS_SuspendRequest(cx); ret = switch_socket_send(socket->socket, buffer, &len); JS_ResumeRequest(cx, socket->saveDepth); if (ret != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "switch_socket_send failed: %d.\n", ret); *rval = BOOLEAN_TO_JSVAL(JS_FALSE); } else *rval = BOOLEAN_TO_JSVAL(JS_TRUE); } return JS_TRUE; }
static JSBool socket_connect(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { js_socket_obj_t *socket = JS_GetPrivate(cx, obj); if (socket == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to find js object.\n"); return JS_FALSE; } if (argc == 2) { char *host = JS_GetStringBytes(JS_ValueToString(cx, argv[0])); int32 port; switch_sockaddr_t *addr; switch_status_t ret; JS_ValueToInt32(cx, argv[1], &port); ret = switch_sockaddr_info_get(&addr, host, AF_INET, (switch_port_t) port, 0, socket->pool); if (ret != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "switch_sockaddr_info_get failed: %d.\n", ret); return JS_FALSE; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Connecting to: %s:%d.\n", host, port); socket->saveDepth = JS_SuspendRequest(cx); ret = switch_socket_connect(socket->socket, addr); JS_ResumeRequest(cx, socket->saveDepth); if (ret != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "switch_socket_connect failed: %d.\n", ret); *rval = BOOLEAN_TO_JSVAL(JS_FALSE); } else *rval = BOOLEAN_TO_JSVAL(JS_TRUE); } 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 curl_run(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { struct curl_obj *co = JS_GetPrivate(cx, obj); char *method = NULL, *url, *cred = NULL; char *url_p = NULL, *data = NULL, *durl = NULL; long httpRes = 0; struct curl_slist *headers = NULL; int32 timeout = 0; char ct[80] = "Content-Type: application/x-www-form-urlencoded"; if (argc < 2 || !co) { return JS_FALSE; } method = JS_GetStringBytes(JS_ValueToString(cx, argv[0])); url = JS_GetStringBytes(JS_ValueToString(cx, argv[1])); co->curl_handle = switch_curl_easy_init(); if (!strncasecmp(url, "https", 5)) { switch_curl_easy_setopt(co->curl_handle, CURLOPT_SSL_VERIFYPEER, 0); switch_curl_easy_setopt(co->curl_handle, CURLOPT_SSL_VERIFYHOST, 0); } if (argc > 2) { data = JS_GetStringBytes(JS_ValueToString(cx, argv[2])); } if (argc > 3) { co->function = JS_ValueToFunction(cx, argv[3]); } if (argc > 4) { JS_ValueToObject(cx, argv[4], &co->user_data); } if (argc > 5) { cred = JS_GetStringBytes(JS_ValueToString(cx, argv[5])); if (!zstr(cred)) { switch_curl_easy_setopt(co->curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY); switch_curl_easy_setopt(co->curl_handle, CURLOPT_USERPWD, cred); } } if (argc > 6) { JS_ValueToInt32(cx, argv[6], &timeout); if (timeout > 0) { switch_curl_easy_setopt(co->curl_handle, CURLOPT_TIMEOUT, timeout); } } if (argc > 7) { char *content_type = JS_GetStringBytes(JS_ValueToString(cx, argv[7])); switch_snprintf(ct, sizeof(ct), "Content-Type: %s", content_type); } headers = curl_slist_append(headers, ct); switch_curl_easy_setopt(co->curl_handle, CURLOPT_HTTPHEADER, headers); url_p = url; if (!strcasecmp(method, "post")) { switch_curl_easy_setopt(co->curl_handle, CURLOPT_POST, 1); if (!data) { data = ""; } switch_curl_easy_setopt(co->curl_handle, CURLOPT_POSTFIELDS, data); } else if (!zstr(data)) { durl = switch_mprintf("%s?%s", url, data); url_p = durl; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Running: method: [%s] url: [%s] data: [%s] cred=[%s] cb: [%s]\n", method, url_p, data, switch_str_nil(cred), co->function ? "yes" : "no"); switch_curl_easy_setopt(co->curl_handle, CURLOPT_URL, url_p); switch_curl_easy_setopt(co->curl_handle, CURLOPT_NOSIGNAL, 1); switch_curl_easy_setopt(co->curl_handle, CURLOPT_WRITEFUNCTION, file_callback); switch_curl_easy_setopt(co->curl_handle, CURLOPT_WRITEDATA, (void *) co); switch_curl_easy_setopt(co->curl_handle, CURLOPT_USERAGENT, "freeswitch-spidermonkey-curl/1.0"); co->saveDepth = JS_SuspendRequest(cx); switch_curl_easy_perform(co->curl_handle); switch_curl_easy_getinfo(co->curl_handle, CURLINFO_RESPONSE_CODE, &httpRes); switch_curl_easy_cleanup(co->curl_handle); curl_slist_free_all(headers); co->curl_handle = NULL; co->function = NULL; JS_ResumeRequest(cx, co->saveDepth); switch_safe_free(durl); 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 JSBool socket_read(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { js_socket_obj_t *socket = JS_GetPrivate(cx, obj); char *delimiter = "\n"; switch_status_t ret = SWITCH_STATUS_FALSE; switch_size_t len = 1; switch_size_t total_length = 0; int can_run = TRUE; char tempbuf[2]; if (socket == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to find js object.\n"); return JS_FALSE; } if (argc == 1) { delimiter = JS_GetStringBytes(JS_ValueToString(cx, argv[0])); } if (socket->read_buffer == 0) { socket->read_buffer = switch_core_alloc(socket->pool, socket->buffer_size); } socket->saveDepth = JS_SuspendRequest(cx); while (can_run == TRUE) { ret = switch_socket_recv(socket->socket, tempbuf, &len); if (ret != SWITCH_STATUS_SUCCESS) break; tempbuf[1] = 0; if (tempbuf[0] == delimiter[0]) break; else if (tempbuf[0] == '\r' && delimiter[0] == '\n') continue; else { // Buffer is full, let's increase it. if (total_length == socket->buffer_size - 1) { switch_size_t new_size = socket->buffer_size + 4196; char *new_buffer = switch_core_alloc(socket->pool, socket->buffer_size); memcpy(new_buffer, socket->read_buffer, total_length); socket->buffer_size = new_size; socket->read_buffer = new_buffer; } socket->read_buffer[total_length] = tempbuf[0]; ++total_length; } } JS_ResumeRequest(cx, socket->saveDepth); if (ret != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "socket receive failed: %d.\n", ret); *rval = BOOLEAN_TO_JSVAL(JS_FALSE); } else { socket->read_buffer[total_length] = 0; *rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, socket->read_buffer)); } return JS_TRUE; }