bool JSGlobal::JS_setInterval(JSContext *cx, JS::CallArgs &args) { struct nidium_sm_timer *params; int ms = 0, i; int argc = args.length(); params = new nidium_sm_timer(cx); if (!params) { JS_ReportOutOfMemory(cx); return false; } params->cx = cx; params->global = m_Instance; params->argc = nidium_max(0, argc - 2); params->argv = new JS::PersistentRootedValue *[params->argc]; for (i = 0; i < argc - 2; i++) { params->argv[i] = new JS::PersistentRootedValue(cx); } if (!JSUtils::ReportIfNotFunction(cx, args[0])) { delete[] params->argv; delete params; return false; } params->func = args[0]; if (argc > 1 && !JS::ToInt32(cx, args[1], &ms)) { ms = 0; } params->ms = nidium_max(8, ms); for (i = 0; i < static_cast<int>(argc) - 2; i++) { *params->argv[i] = args.array()[i + 2]; } ape_timer_t *timer = APE_timer_create( static_cast<ape_global *>(JS_GetContextPrivate(cx)), params->ms, nidium_timerng_wrapper, static_cast<void *>(params)); APE_timer_unprotect(timer); APE_timer_setclearfunc(timer, nidium_timer_deleted); args.rval().setNumber(static_cast<double>(APE_timer_getid(timer))); return true; }
JSBool gjs_js_define_system_stuff(JSContext *context, JSObject **module_out) { GjsContext *gjs_context; char *program_name; jsval value; JSBool retval; JSObject *module; module = JS_NewObject (context, NULL, NULL, NULL); if (!JS_DefineFunctions(context, module, &module_funcs[0])) return JS_FALSE; retval = JS_FALSE; gjs_context = (GjsContext*) JS_GetContextPrivate(context); g_object_get(gjs_context, "program-name", &program_name, NULL); if (!gjs_string_from_utf8(context, program_name, -1, &value)) goto out; /* The name is modeled after program_invocation_name, part of the glibc */ if (!JS_DefineProperty(context, module, "programInvocationName", value, JS_PropertyStub, JS_StrictPropertyStub, GJS_MODULE_PROP_FLAGS | JSPROP_READONLY)) goto out; if (!JS_DefineProperty(context, module, "version", INT_TO_JSVAL(GJS_VERSION), JS_PropertyStub, JS_StrictPropertyStub, GJS_MODULE_PROP_FLAGS | JSPROP_READONLY)) goto out; retval = JS_TRUE; out: g_free(program_name); *module_out = module; return retval; }
bool JSGlobal::JS_clearTimeout(JSContext *cx, JS::CallArgs &args) { double identifier; if (!JS_ConvertArguments(cx, args, "d", &identifier)) { return false; } APE_timer_clearbyid(static_cast<ape_global *>(JS_GetContextPrivate(cx)), static_cast<uint64_t>(identifier), 0); return true; }
JSBool JsHttpServerResponse::setContentType(JSContext *cx,JSObject *obj,uintN argc,jsval *argv,jsval *rval) { char *contentType = {0}; if ( !JS_ConvertArguments(cx,argc,argv,"s",&contentType) ) { return Engine::throwUsageError(cx,argv); } ContextPrivate *cxPrivate = (ContextPrivate*)JS_GetContextPrivate(cx); cxPrivate->getHttpResponse().setContentType(contentType); return JS_TRUE; }
JSBool JsHttpServerResponse::write(JSContext *cx,JSObject *obj,uintN argc,jsval *argv,jsval *rval) { char *output = {0}; if ( !JS_ConvertArguments(cx,argc,argv,"s",&output) ) { return Engine::throwUsageError(cx,argv); } ContextPrivate *cxPrivate = (ContextPrivate*)JS_GetContextPrivate(cx); cxPrivate->getHttpResponse().write(output,strlen(output)); return JS_TRUE; }
static JSBool gjs_on_context_gc (JSContext *cx, JSGCStatus status) { GjsContext *gjs_context = JS_GetContextPrivate(cx); if (status == JSGC_END) { if (gjs_context->idle_emit_gc_id == 0) gjs_context->idle_emit_gc_id = g_idle_add (gjs_context_idle_emit_gc, gjs_context); } return TRUE; }
static void error_reporter(JSContext *ctx, const char *message, JSErrorReport *report) { struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); struct session *ses = interpreter->vs->doc_view->session; struct terminal *term; unsigned char *strict, *exception, *warning, *error; struct string msg; assert(interpreter && interpreter->vs && interpreter->vs->doc_view && ses && ses->tab); if_assert_failed goto reported; term = ses->tab->term; #ifdef CONFIG_LEDS set_led_value(ses->status.ecmascript_led, 'J'); #endif if (!get_opt_bool("ecmascript.error_reporting", ses) || !init_string(&msg)) goto reported; strict = JSREPORT_IS_STRICT(report->flags) ? " strict" : ""; exception = JSREPORT_IS_EXCEPTION(report->flags) ? " exception" : ""; warning = JSREPORT_IS_WARNING(report->flags) ? " warning" : ""; error = !report->flags ? " error" : ""; add_format_to_string(&msg, _("A script embedded in the current " "document raised the following%s%s%s%s", term), strict, exception, warning, error); add_to_string(&msg, ":\n\n"); add_to_string(&msg, message); if (report->linebuf && report->tokenptr) { int pos = report->tokenptr - report->linebuf; add_format_to_string(&msg, "\n\n%s\n.%*s^%*s.", report->linebuf, pos - 2, " ", strlen(report->linebuf) - pos - 1, " "); } info_box(term, MSGBOX_FREE_TEXT, N_("JavaScript Error"), ALIGN_CENTER, msg.source); reported: /* Im clu'les. --pasky */ JS_ClearPendingException(ctx); }
static JSBool js_saveline(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { sbbs_t* sbbs; if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL) return(JS_FALSE); sbbs->slatr[sbbs->slcnt]=sbbs->latr; sprintf(sbbs->slbuf[sbbs->slcnt<SAVE_LINES ? sbbs->slcnt++ : sbbs->slcnt] ,"%.*s",sbbs->lbuflen,sbbs->lbuf); sbbs->lbuflen=0; return(JS_TRUE); }
static JSBool js_restoreline(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { sbbs_t* sbbs; if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL) return(JS_FALSE); sbbs->lbuflen=0; sbbs->attr(sbbs->slatr[--sbbs->slcnt]); sbbs->rputs(sbbs->slbuf[sbbs->slcnt]); sbbs->curatr=LIGHTGRAY; return(JS_TRUE); }
static JSBool js_cursor_left(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { int32 val=1; sbbs_t* sbbs; if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL) return(JS_FALSE); if(argc) JS_ValueToInt32(cx,argv[0],&val); sbbs->cursor_left(val); return(JS_TRUE); }
/* The function callback. */ JSBool go_func_callback(JSContext *cx, uintN argc, jsval *vp) { JSObject *callee = JSVAL_TO_OBJECT(JS_CALLEE(cx, vp)); jsval name; JS_GetProperty(cx, callee, "name", &name); char* cname = JS_EncodeString(cx, JS_ValueToString(cx, name)); JSBool result = call_go_func(JS_GetContextPrivate(cx), cname, argc, vp); JS_free(cx, (void*)cname); return result; }
/** set numeric property (xn). */ JSBool xgg_pbar_propop_xn_set(JSContext *pcxa, JSObject *pobja, jsval id, jsval *vp) { XGGPRIVHDR* phdr = JS_GetPrivate(pcxa, pobja); XGG_PBAR_PRIV* pv0 = JS_GetPrivate(pcxa, pobja); XJSEXADGELEM* pelem0 = phdr->pxadgelem; XJSECTX* pjsectx = (XJSECTX*)JS_GetContextPrivate(pcxa); XJSE_ASSERT(pelem0 != 0); // basicelement inheritances must have this xjse_result_t xr1; xjse_int_t nid = 0;//, nvalue = 0;//, n1; char* psz0; JS_ValueToInt32(pcxa, id, &nid); //JS_ValueToInt32(pcxa, vp[0], &nvalue); //XJSE_TRACE("xgg_pbar_propop_xn_set. [%d](%d)", nid, nvalue); switch(nid) { case PBAR_TID_EMPTYIMAGE: psz0 = JS_GetStringBytes(JS_ValueToString(pcxa, vp[0])); xr1 = xgg_xadget_reload_img(pjsectx, &(pv0->ppbempty), psz0, 0, 0); if(xr1 != XJSE_SUCCESS) goto failed; if(pelem0->fcx == 0.0f) pelem0->fcx = (xjse_float_t)(pv0->ppbempty->nimgwidth); if(pelem0->fcy == 0.0f) pelem0->fcy = (xjse_float_t)(pv0->ppbempty->nimgheight); break; case PBAR_TID_FULLIMAGE: psz0 = JS_GetStringBytes(JS_ValueToString(pcxa, vp[0])); xr1 = xgg_xadget_reload_img(pjsectx, &(pv0->ppbfull), psz0, 0, 0); if(xr1 != XJSE_SUCCESS) goto failed; break; case PBAR_TID_MAX: JS_ValueToInt32(pcxa, vp[0], &(pv0->nmax)); break; case PBAR_TID_MIN: JS_ValueToInt32(pcxa, vp[0], &(pv0->nmin)); break; case PBAR_TID_VALUE: JS_ValueToInt32(pcxa, vp[0], &(pv0->nvalue)); break; default: XJSE_TRACE("(E) unknown xn prop [property id: %d]", nid); } return JS_TRUE; failed: return JS_FALSE; }
static JSBool js_cleartoeol(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { sbbs_t* sbbs; if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL) return(JS_FALSE); if(argc) js_set_attr(cx, sbbs, argv[0]); sbbs->cleartoeol(); return(JS_TRUE); }
JSBool branch_cb(JSContext* jscx, JSScript* script) { Context* pycx = (Context*) JS_GetContextPrivate(jscx); time_t now = time(NULL); if(pycx == NULL) { JS_ReportError(jscx, "Failed to find Python context."); return JS_FALSE; } // Get out quick if we don't have any quotas. if(pycx->max_time == 0 && pycx->max_heap == 0) { return JS_TRUE; } // Only check occasionally for resource usage. pycx->branch_count++; if((pycx->branch_count > 0x3FFF) != 1) { return JS_TRUE; } pycx->branch_count = 0; if(pycx->max_heap > 0 && jscx->runtime->gcBytes > pycx->max_heap) { // First see if garbage collection gets under the threshold. JS_GC(jscx); if(jscx->runtime->gcBytes > pycx->max_heap) { PyErr_NoMemory(); return JS_FALSE; } } if( pycx->max_time > 0 && pycx->start_time > 0 && pycx->max_time < now - pycx->start_time ) { PyErr_SetNone(PyExc_SystemError); return JS_FALSE; } return JS_TRUE; }
~WorkletJSContext() override { MOZ_ASSERT(!NS_IsMainThread()); JSContext* cx = MaybeContext(); if (!cx) { return; // Initialize() must have failed } delete static_cast<WorkletThreadContextPrivate*>(JS_GetContextPrivate(cx)); JS_SetContextPrivate(cx, nullptr); nsCycleCollector_shutdown(); }
JSBool resolve(JSContext* jscx, JSObject* jsobj, jsval key) { Context* pycx = NULL; PyObject* pykey = NULL; jsid pid; JSBool ret = JS_FALSE; pycx = (Context*) JS_GetContextPrivate(jscx); if(pycx == NULL) { JS_ReportError(jscx, "Failed to get Python context."); goto done; } // Bail if there's no registered global handler. if(pycx->global == NULL) { ret = JS_TRUE; goto done; } pykey = js2py(pycx, key); if(pykey == NULL) goto done; if(!PyMapping_HasKey(pycx->global, pykey)) { ret = JS_TRUE; goto done; } if(!JS_ValueToId(jscx, key, &pid)) { JS_ReportError(jscx, "Failed to convert property id."); goto done; } if(!js_DefineProperty(jscx, pycx->root, pid, JSVAL_VOID, NULL, NULL, JSPROP_SHARED, NULL)) { JS_ReportError(jscx, "Failed to define property."); goto done; } ret = JS_TRUE; done: Py_XDECREF(pykey); return ret; }
/* static */ WorkletThread* WorkletThread::Get() { AssertIsOnWorkletThread(); CycleCollectedJSContext* ccjscx = CycleCollectedJSContext::Get(); MOZ_ASSERT(ccjscx); void* cxPrivate = JS_GetContextPrivate(ccjscx->Context()); MOZ_ASSERT(cxPrivate); return static_cast<WorkletThreadContextPrivate*>(cxPrivate)->GetWorkletThread(); }
static JSBool js_mnemonics(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { sbbs_t* sbbs; JSString* js_str; if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL) return(JS_FALSE); if((js_str=JS_ValueToString(cx, argv[0]))==NULL) return(JS_FALSE); sbbs->mnemonics(JS_GetStringBytes(js_str)); return(JS_TRUE); }
static JSBool js_getnum(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { ulong maxnum=~0; sbbs_t* sbbs; if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL) return(JS_FALSE); if(argc && JSVAL_IS_NUMBER(argv[0])) JS_ValueToInt32(cx,argv[0],(int32*)&maxnum); *rval = INT_TO_JSVAL(sbbs->getnum(maxnum)); return(JS_TRUE); }
static JSBool xgg_domdocument_ctor(JSContext *pcxa, JSObject *pobja, uintN argc, jsval *pva1, jsval *rval) { XJSE_XDOMPRIV *p0; p0 = (XJSE_XDOMPRIV*)xjse_malloc(0, sizeof(XJSE_XDOMPRIV)); xjse_memset(0, p0, 0, sizeof(XJSE_XDOMPRIV)); p0->pjsectx = (XJSECTX*)JS_GetContextPrivate(pcxa); p0->pothis = pobja; JSBool b0 = JS_SetPrivate(pcxa, pobja, p0); if(b0 != JS_TRUE) { XJSE_TRACE("JS_SetPrivate failed!"); return JS_FALSE; } return JS_TRUE; }
static JSBool js_editfile(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSString* str; sbbs_t* sbbs; if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL) return(JS_FALSE); if((str=JS_ValueToString(cx, argv[0]))==NULL) return(JS_FALSE); sbbs->editfile(JS_GetStringBytes(str)); return(JS_TRUE); }
static JSBool js_getstr(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { char *p; long mode=0; uintN i; size_t maxlen=0; sbbs_t* sbbs; JSString* js_str=NULL; if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL) return(JS_FALSE); for(i=0;i<argc;i++) { if(JSVAL_IS_NUMBER(argv[i])) { if(!maxlen) JS_ValueToInt32(cx,argv[i],(int32*)&maxlen); else JS_ValueToInt32(cx,argv[i],(int32*)&mode); continue; } if(JSVAL_IS_STRING(argv[i])) { js_str = JS_ValueToString(cx, argv[i]); if (!js_str) return(JS_FALSE); } } if(!maxlen) maxlen=128; if((p=(char *)calloc(1,maxlen+1))==NULL) return(JS_FALSE); if(js_str!=NULL) sprintf(p,"%.*s",(int)maxlen,JS_GetStringBytes(js_str)); sbbs->getstr(p,maxlen,mode); js_str = JS_NewStringCopyZ(cx, p); free(p); if(js_str==NULL) return(JS_FALSE); *rval = STRING_TO_JSVAL(js_str); return(JS_TRUE); }
static JSBool safeguard(JSContext *ctx, JSScript *script) { struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); struct session *ses = interpreter->vs->doc_view->session; int max_exec_time = get_opt_int("ecmascript.max_exec_time", ses); if (time(NULL) - interpreter->exec_start > max_exec_time) { struct terminal *term = ses->tab->term; /* A killer script! Alert! */ ecmascript_timeout_dialog(term, max_exec_time); return JS_FALSE; } return JS_TRUE; }
/* @history_funcs{"back"} */ static JSBool history_back(JSContext *ctx, unsigned int argc, jsval *rval) { struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); struct document_view *doc_view = interpreter->vs->doc_view; struct session *ses = doc_view->session; go_back(ses); /* history_back() must return 0 for onClick to cause displaying previous page * and return non zero for <a href="javascript:history.back()"> to prevent * "calculating" new link. Returned value 2 is changed to 0 in function * spidermonkey_eval_boolback */ JS_SET_RVAL(ctx, rval, JSVAL_NULL); return 2; }
JSBool get_prop(JSContext* jscx, JSObject* jsobj, jsval key, jsval* rval) { Context* pycx = NULL; PyObject* pykey = NULL; PyObject* pyval = NULL; JSBool ret = JS_FALSE; pycx = (Context*) JS_GetContextPrivate(jscx); if(pycx == NULL) { JS_ReportError(jscx, "Failed to get Python context."); goto done; } // Bail if there's no registered global handler. if(pycx->global == NULL) { ret = JS_TRUE; goto done; } pykey = js2py(pycx, key); if(pykey == NULL) goto done; if(Context_has_access(pycx, jscx, pycx->global, pykey) <= 0) goto done; pyval = PyObject_GetItem(pycx->global, pykey); if(pyval == NULL) { if(PyErr_GivenExceptionMatches(PyErr_Occurred(), PyExc_KeyError)) { PyErr_Clear(); ret = JS_TRUE; } goto done; } *rval = py2js(pycx, pyval); if(*rval == JSVAL_VOID) goto done; ret = JS_TRUE; done: Py_XDECREF(pykey); Py_XDECREF(pyval); return ret; }
/* define properties that JS Error() expose, such as fileName, lineNumber and stack */ static void define_error_properties(JSContext *context, JSObject *obj) { JSStackFrame *frame; JSScript *script; jsbytecode *pc; jsval v; GString *stack; const char *filename; GjsContext *gjs_context; /* find the JS frame that triggered the error */ frame = NULL; while (JS_FrameIterator(context, &frame)) { if (JS_IsScriptFrame(context, frame)) break; } /* someone called gjs_throw at top of the stack? well, no stack in that case */ if (!frame) return; script = JS_GetFrameScript(context, frame); pc = JS_GetFramePC(context, frame); stack = g_string_new(NULL); gjs_context = JS_GetContextPrivate(context); gjs_context_print_stack_to_buffer(gjs_context, frame, stack); if (gjs_string_from_utf8(context, stack->str, stack->len, &v)) JS_DefineProperty(context, obj, "stack", v, NULL, NULL, JSPROP_ENUMERATE); filename = JS_GetScriptFilename(context, script); if (gjs_string_from_filename(context, filename, -1, &v)) JS_DefineProperty(context, obj, "fileName", v, NULL, NULL, JSPROP_ENUMERATE); v = INT_TO_JSVAL(JS_PCToLineNumber(context, script, pc)); JS_DefineProperty(context, obj, "lineNumber", v, NULL, NULL, JSPROP_ENUMERATE); g_string_free(stack, TRUE); }
static JSBool js_telnet_cmd(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { sbbs_t* sbbs; int32 cmd,opt=0; if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL) return(JS_FALSE); JS_ValueToInt32(cx,argv[0],&cmd); if(argc>1) JS_ValueToInt32(cx,argv[1],&opt); sbbs->send_telnet_cmd((uchar)cmd,(uchar)opt); return(JS_TRUE); }
// callback for JS_SetErrorReporter static void report_js_error(JSContext* js, const char* message, JSErrorReport* UNUSED(report)) { // first we find ourselves VALUE self = (VALUE)JS_GetContextPrivate(js); // then we find our bridge JohnsonContext* context; Data_Get_Struct(self, JohnsonContext, context); // NOTE: SpiderMonkey REALLY doesn't like being interrupted. If we // jump over to Ruby and raise here, segfaults and such ensue. // Instead, we store the exception (if any) and the error message // on the context. They're dealt with in the if (!ok) block of evaluate(). strncpy(context->msg, message, MAX_EXCEPTION_MESSAGE_SIZE); JS_GetPendingException(context->js, &context->ex); }
bool JSGlobal::JS_setImmediate(JSContext *cx, JS::CallArgs &args) { struct nidium_sm_timer *params; int i; int argc = args.length(); params = new nidium_sm_timer(cx); if (params == NULL || argc < 1) { if (params) delete params; return true; } params->cx = cx; params->global = m_Instance; params->argc = argc - 1; params->ms = 0; params->argv = new JS::PersistentRootedValue *[argc - 1]; for (i = 0; i < argc - 1; i++) { params->argv[i] = new JS::PersistentRootedValue(cx); } if (!JSUtils::ReportIfNotFunction(cx, args[0])) { delete[] params->argv; delete params; return false; } params->func = args[0]; for (i = 0; i < static_cast<int>(argc) - 1; i++) { *params->argv[i] = args[i + 1]; } ape_timer_async_t *async = APE_async(static_cast<ape_global *>(JS_GetContextPrivate(cx)), nidium_timerng_wrapper, static_cast<void *>(params)); APE_async_setclearfunc(async, nidium_timer_deleted); args.rval().setNull(); return true; }
static JSBool js_beep(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { sbbs_t* sbbs; int32 i; int32 count=1; if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL) return(JS_FALSE); if(argc) JS_ValueToInt32(cx, argv[0], &count); for(i=0;i<count;i++) sbbs->outchar('\a'); return(JS_TRUE); }