void invokeDelegate(const char* fName, const char* name, int intVal, int argc) { if (!s_cx) { return; } //cocos2d::CCDirector::getInstance()->getScheduler()->performFunctionInCocosThread([=](){ JSContext* cx = s_cx; const char* func_name = fName; JS::RootedObject obj(cx, mJsDelegate); JSAutoCompartment ac(cx, obj); #if defined(MOZJS_MAJOR_VERSION) #if MOZJS_MAJOR_VERSION >= 33 bool hasAction; JS::RootedValue retval(cx); JS::RootedValue func_handle(cx); #else bool hasAction; jsval retval; JS::RootedValue func_handle(cx); #endif #elif defined(JS_VERSION) JSBool hasAction; jsval retval; jsval func_handle; #endif jsval dataVal[2]; if (2 == argc) { dataVal[0] = c_string_to_jsval(cx, name); dataVal[1] = INT_TO_JSVAL(intVal); } else if (1 == argc) { dataVal[0] = c_string_to_jsval(cx, name); } if (JS_HasProperty(cx, obj, func_name, &hasAction) && hasAction) { if(!JS_GetProperty(cx, obj, func_name, &func_handle)) { return; } if(func_handle == JSVAL_VOID) { return; } #if MOZJS_MAJOR_VERSION >= 31 if (0 == argc) { JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::empty(), &retval); } else { JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::fromMarkedLocation(argc, dataVal), &retval); } #else if (0 == argc) { JS_CallFunctionName(cx, obj, func_name, 0, nullptr, &retval); } else { JS_CallFunctionName(cx, obj, func_name, argc, dataVal, &retval); } #endif } //}); }
void invokeJS(const char* func, IMCallbackJS* cb) { if (!s_cx) { return; } JSContext* cx = s_cx; const char* func_name = func; JS::RootedObject obj(cx, _JSDelegate); JSAutoCompartment ac(cx, obj); #if defined(MOZJS_MAJOR_VERSION) #if MOZJS_MAJOR_VERSION >= 33 bool hasAction; JS::RootedValue retval(cx); JS::RootedValue func_handle(cx); #else bool hasAction; jsval retval; JS::RootedValue func_handle(cx); #endif #elif defined(JS_VERSION) JSBool hasAction; jsval retval; jsval func_handle; #endif int valueSize = 0; jsval* pVals = nullptr; valueSize = cb->transParams(&pVals); if (JS_HasProperty(cx, obj, func_name, &hasAction) && hasAction) { if(!JS_GetProperty(cx, obj, func_name, &func_handle)) { return; } if(func_handle == JSVAL_VOID) { return; } #if MOZJS_MAJOR_VERSION >= 31 if (0 == valueSize) { JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::empty(), &retval); } else { JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::fromMarkedLocation(valueSize, pVals), &retval); } #else if (0 == valueSize) { JS_CallFunctionName(cx, obj, func_name, 0, nullptr, &retval); } else { JS_CallFunctionName(cx, obj, func_name, valueSize, pVals, &retval); } #endif } }
nsresult abpListener::HandleEvent(nsIDOMEvent* event) { nsresult rv; abpJSContextHolder holder; JSObject* overlay = UnwrapJSObject(fakeBrowserWindow); JSContext* cx = holder.get(); if (cx == nsnull || overlay == nsnull) return NS_ERROR_FAILURE; nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID()); if (xpc == nsnull) return NS_ERROR_FAILURE; nsCOMPtr<nsIXPConnectJSObjectHolder> wrapperHolder; rv = xpc->WrapNative(cx, JS_GetParent(cx, overlay), event, NS_GET_IID(nsIDOMEvent), getter_AddRefs(wrapperHolder)); if (NS_FAILED(rv)) return rv; JSObject* jsObj; rv = wrapperHolder->GetJSObject(&jsObj); if (NS_FAILED(rv)) return rv; jsval arg = OBJECT_TO_JSVAL(jsObj); jsval retval; JS_CallFunctionName(cx, overlay, "onEvent", 1, &arg, &retval); return NS_OK; }
static JSBool test_fn2(JSContext *cx, unsigned argc, jsval *vp) { jsval r; JS::RootedObject global(cx, JS_GetGlobalObject(cx)); return JS_CallFunctionName(cx, global, "d", 0, NULL, &r); }
static JSBool DriverManager_getDriver(JSContext *cx, unsigned argc, jsval *vp) { jsval drivers; JS_LookupProperty(cx, JS_THIS_OBJECT(cx, vp), "drivers", &drivers); // FIXME check return code; check that drivers is an array JSObject *obj = JSVAL_TO_OBJECT(drivers); uint32_t len, i; JS_GetArrayLength(cx, obj, &len); for (i = 0; i < len; i++) { jsval driver, rval; jsval acceptsURL_argv[] = {JS_ARGV(cx, vp)[0]}; JS_GetElement(cx, obj, i, &driver); JS_CallFunctionName(cx, JSVAL_TO_OBJECT(driver), "acceptsURL", 1, &acceptsURL_argv[0], &rval); // FIXME check return value JSBool acceptsURL; JS_ValueToBoolean(cx, rval, &acceptsURL); // FIXME check ret val if (acceptsURL) { JS_SET_RVAL(cx, vp, driver); return JS_TRUE; } } JS_ReportError(cx, "No suitable driver"); JS_SET_RVAL(cx, vp, JSVAL_NULL); return JS_FALSE; }
Result dispatch(JSContext *cx, JSObject *thisobj, const char *dataPropName, const char *methodName, Result noHandler) { if (!data) return fail; JSBool found; if (!JS_HasProperty(cx, thisobj, methodName, &found)) return fail; if (!found) return noHandler; // Create event object. jsval v; if (!deserializeData(cx, &v)) return fail; JSObject *obj = JS_NewObject(cx, NULL, NULL, NULL); if (!obj || !JS_DefineProperty(cx, obj, dataPropName, v, NULL, NULL, 0)) return fail; // Call event handler. jsval argv[1] = { OBJECT_TO_JSVAL(obj) }; jsval rval = JSVAL_VOID; return Result(JS_CallFunctionName(cx, thisobj, methodName, 1, argv, &rval)); }
static bool test_fn2(JSContext* cx, unsigned argc, JS::Value* vp) { JS::RootedValue r(cx); JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx)); return JS_CallFunctionName(cx, global, "d", JS::HandleValueArray::empty(), &r); }
jsval new_smtp_path_instance(char *arg) { jsval path, session, smtpPathClass, smtpServer; JSObject *global; global = JS_GetGlobalForScopeChain(js_context); // Get smtpServer if (!JS_GetProperty(js_context, global, "smtpServer", &smtpServer)) { return JSVAL_NULL; } // Get session if (!JS_GetProperty(js_context, JSVAL_TO_OBJECT(smtpServer), "session", &session)) { return JSVAL_NULL; } // Get smtpPathClass if (!JS_GetProperty(js_context, global, "SmtpPath", &smtpPathClass)) { return JSVAL_NULL; } jsval argv = STRING_TO_JSVAL(JS_InternString(js_context, arg)); JS_CallFunctionName(js_context, global, "SmtpPath", 1, &argv, &path); return path; }
int Stage::DrawScreen(int h) { jsval rval; int x, y, ytimesw; if (SDL_MUSTLOCK(screen)) { if (SDL_LockSurface(screen) < 0) { return 0; } } jsval argv[5]; argv[0] = OBJECT_TO_JSVAL(screenArrayBuffer); argv[1] = INT_TO_JSVAL(WIDTH); argv[2] = INT_TO_JSVAL(HEIGHT); argv[3] = INT_TO_JSVAL(screen->pitch); argv[4] = INT_TO_JSVAL(BPP); JSBool ok = JS_CallFunctionName(cx, global, "paint", 5, argv, &rval); Blit((char *)screen->pixels, screenBuffer, screenBufferSize); if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); SDL_Flip(screen); }
static JSBool delegateToResolver(JSContext *cx, JSObject *obj, const char *name, uintN argc, jsval *argv, jsval *rval) { jsval resolver; if (!JS_GetReservedSlot(cx, obj, SLOT_RESOLVER, &resolver)) return JS_FALSE; JSObject *resolverObj = JSVAL_TO_OBJECT(resolver); jsval *allArgv; uintN allArgc = argc + 2; allArgv = (jsval *)PR_Malloc(allArgc * sizeof(jsval)); if (!JS_GetReservedSlot(cx, obj, SLOT_WRAPPEE, allArgv)) { PR_Free(allArgv); return JS_FALSE; } allArgv[1] = OBJECT_TO_JSVAL(obj); for (unsigned int i = 0; i < argc; i++) allArgv[i + 2] = argv[i]; if (!JS_CallFunctionName(cx, resolverObj, name, allArgc, allArgv, rval)) { PR_Free(allArgv); return JS_FALSE; } PR_Free(allArgv); return JS_TRUE; }
bool callJSDelegate(TableView* table, ssize_t idx, std::string jsFunctionName, jsval& retVal) { js_proxy_t * p = jsb_get_native_proxy(table); if (!p) return false; JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); bool hasAction; JS::RootedValue temp_retval(cx); jsval dataVal[2]; dataVal[0] = OBJECT_TO_JSVAL(p->obj); dataVal[1] = ssize_to_jsval(cx,idx); JSObject* obj = _JSTableViewDataSource; JSAutoCompartment ac(cx, obj); if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction) { if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval)) { return false; } if(temp_retval == JSVAL_VOID) { return false; } bool ret = JS_CallFunctionName(cx, obj, jsFunctionName.c_str(), 2, dataVal, &retVal); return ret == true ? true : false; } return false; }
static bool test_fn2(JSContext *cx, unsigned argc, jsval *vp) { jsval r; JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx)); return JS_CallFunctionName(cx, global, "d", 0, nullptr, &r); }
bool callJSDelegate(TableView* table, std::string jsFunctionName, jsval& retVal) { js_proxy_t * p = jsb_get_native_proxy(table); if (!p) return false; JSBool hasAction; jsval temp_retval; jsval dataVal = OBJECT_TO_JSVAL(p->obj); JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); JSObject* obj = _JSTableViewDataSource; if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction) { if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval)) { return false; } if(temp_retval == JSVAL_VOID) { return false; } JSAutoCompartment ac(cx, obj); JS_CallFunctionName(cx, obj, jsFunctionName.c_str(), 1, &dataVal, &retVal); return true; } return false; }
bool callJSDelegate(TableView* table, std::string jsFunctionName, JS::MutableHandleValue retVal) { js_proxy_t * p = jsb_get_native_proxy(table); if (!p) return false; JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); bool hasAction; JS::RootedValue temp_retval(cx); jsval dataVal = OBJECT_TO_JSVAL(p->obj); JS::RootedObject obj(cx, _JSTableViewDataSource); JSAutoCompartment ac(cx, obj); if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction) { if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval)) { return false; } if(temp_retval == JSVAL_VOID) { return false; } JS_CallFunctionName(cx, obj, jsFunctionName.c_str(), JS::HandleValueArray::fromMarkedLocation(1, &dataVal), retVal); return true; } return false; }
ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, const shared_ptr<ScriptRuntime>& runtime) : m_runtime(runtime), m_glob(runtime->m_rt), m_nativeScope(runtime->m_rt) { bool ok; m_cx = JS_NewContext(m_runtime->m_rt, STACK_CHUNK_SIZE); ENSURE(m_cx); JS_SetParallelIonCompilationEnabled(m_runtime->m_rt, true); // For GC debugging: // JS_SetGCZeal(m_cx, 2); JS_SetContextPrivate(m_cx, NULL); JS_SetErrorReporter(m_cx, ErrorReporter); JS_SetGlobalJitCompilerOption(m_runtime->m_rt, JSJITCOMPILER_ION_ENABLE, 1); JS_SetGlobalJitCompilerOption(m_runtime->m_rt, JSJITCOMPILER_BASELINE_ENABLE, 1); JS::ContextOptionsRef(m_cx).setExtraWarnings(1) .setWerror(0) .setVarObjFix(1) .setStrictMode(1); JS::CompartmentOptions opt; opt.setVersion(JSVERSION_LATEST); JSAutoRequest rq(m_cx); JS::RootedObject globalRootedVal(m_cx, JS_NewGlobalObject(m_cx, &global_class, NULL, JS::OnNewGlobalHookOption::FireOnNewGlobalHook, opt)); m_comp = JS_EnterCompartment(m_cx, globalRootedVal); ok = JS_InitStandardClasses(m_cx, globalRootedVal); ENSURE(ok); m_glob = globalRootedVal.get(); // Use the testing functions to globally enable gcPreserveCode. This brings quite a // big performance improvement. In future SpiderMonkey versions, we should probably // use the functions implemented here: https://bugzilla.mozilla.org/show_bug.cgi?id=1068697 JS::RootedObject testingFunctionsObj(m_cx, js::GetTestingFunctions(m_cx)); ENSURE(testingFunctionsObj); JS::RootedValue ret(m_cx); JS_CallFunctionName(m_cx, testingFunctionsObj, "gcPreserveCode", JS::HandleValueArray::empty(), &ret); JS_DefineProperty(m_cx, m_glob, "global", globalRootedVal, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); m_nativeScope = JS_DefineObject(m_cx, m_glob, nativeScopeName, NULL, NULL, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(m_cx, globalRootedVal, "print", ::print, 0, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(m_cx, globalRootedVal, "log", ::logmsg, 1, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(m_cx, globalRootedVal, "warn", ::warn, 1, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(m_cx, globalRootedVal, "error", ::error, 1, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(m_cx, globalRootedVal, "deepcopy", ::deepcopy, 1, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); Register("ProfileStart", ::ProfileStart, 1); Register("ProfileStop", ::ProfileStop, 0); runtime->RegisterContext(m_cx); }
bool ScriptInterface::ParseJSON(const std::string& string_utf8, JS::MutableHandleValue out) { JSAutoRequest rq(m->m_cx); std::wstring attrsW = wstring_from_utf8(string_utf8); utf16string string(attrsW.begin(), attrsW.end()); if (JS_ParseJSON(m->m_cx, reinterpret_cast<const jschar*>(string.c_str()), (u32)string.size(), out)) return true; LOGERROR("JS_ParseJSON failed!"); if (!JS_IsExceptionPending(m->m_cx)) return false; JS::RootedValue exc(m->m_cx); if (!JS_GetPendingException(m->m_cx, &exc)) return false; JS_ClearPendingException(m->m_cx); // We expect an object of type SyntaxError if (!exc.isObject()) return false; JS::RootedValue rval(m->m_cx); JS::RootedObject excObj(m->m_cx, &exc.toObject()); if (!JS_CallFunctionName(m->m_cx, excObj, "toString", JS::HandleValueArray::empty(), &rval)) return false; std::wstring error; ScriptInterface::FromJSVal(m->m_cx, rval, error); LOGERROR("%s", utf8_from_wstring(error)); return false; }
bool Context::realCall(const char* fn, int n, JS::Value* args, JS::Value* val) const { JSAutoCompartment ac(_jsctx, _global); bool ok = JS_CallFunctionName(_jsctx, _global, fn, n, args, val); return ok; }
void onFailure(const sdkbox::Product& info, const std::string& msg) { if (!s_cx) { return; } JSContext* cx = s_cx; const char* func_name = "onFailure"; JS::RootedObject obj(cx, _JSDelegate); JSAutoCompartment ac(cx, obj); #if MOZJS_MAJOR_VERSION >= 31 bool hasAction; JS::RootedValue retval(cx); JS::RootedValue func_handle(cx); #else JSBool hasAction; jsval retval; jsval func_handle; #endif jsval dataVal[2]; jsval value = OBJECT_TO_JSVAL(product_to_obj(s_cx, info)); dataVal[0] = value; dataVal[1] = std_string_to_jsval(cx, msg); if (JS_HasProperty(cx, obj, func_name, &hasAction) && hasAction) { if(!JS_GetProperty(cx, obj, func_name, &func_handle)) { return; } if(func_handle == JSVAL_VOID) { return; } #if MOZJS_MAJOR_VERSION >= 31 JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::fromMarkedLocation(sizeof(dataVal)/sizeof(*dataVal), dataVal), &retval); #else JS_CallFunctionName(cx, obj, func_name, sizeof(dataVal)/sizeof(*dataVal), dataVal, &retval); #endif } }
static JSBool enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op, jsval *statep, jsid *idp) { JSObject *iterator; switch (enum_op) { case JSENUMERATE_INIT: if (resolverHasMethod(cx, obj, "enumerate")) { if (!delegateToResolver(cx, obj, "enumerate", 0, NULL, statep)) return JS_FALSE; if (!JSVAL_IS_OBJECT(*statep)) { JS_ReportError(cx, "Expected enumerate() to return an iterator."); return JS_FALSE; } *idp = JSVAL_ZERO; JS_AddRoot(cx, statep); return JS_TRUE; } // TODO: Default behavior? JS_ReportError(cx, "Enumeration is not implemented on this object."); return JS_FALSE; case JSENUMERATE_NEXT: jsval rval; iterator = JSVAL_TO_OBJECT(*statep); if (!JS_CallFunctionName(cx, iterator, "next", 0, NULL, &rval)) { if (JS_IsExceptionPending(cx)) { jsval exception; if (!JS_GetPendingException(cx, &exception)) return JS_FALSE; if (!JSVAL_IS_OBJECT(exception)) return JS_FALSE; JSClass *clasp = JS_GET_CLASS(cx, JSVAL_TO_OBJECT(exception)); if (clasp && JSCLASS_CACHED_PROTO_KEY(clasp) == JSProto_StopIteration) { JS_ClearPendingException(cx); *statep = JSVAL_NULL; JS_RemoveRoot(cx, statep); return JS_TRUE; } } return JS_FALSE; } if (!JS_ValueToId(cx, rval, idp)) return JS_FALSE; return JS_TRUE; case JSENUMERATE_DESTROY: JS_RemoveRoot(cx, statep); return JS_TRUE; default: JS_ReportError(cx, "Unknown enum_op"); return JS_FALSE; } }
JSObject* pullJSObjectFrom(JSContext* cx, JSObject* topLevel, const char* nsPlusPropName) { jsval argv[2]; argv[0] = OBJECT_TO_JSVAL(topLevel); JSString* nsString = JS_NewStringCopyZ(cx, nsPlusPropName); argv[1] = STRING_TO_JSVAL(nsString); jsval rVal; JS_CallFunctionName(cx, topLevel, "pullObjectFrom", 2, argv, &rVal); return JSVAL_TO_OBJECT(rVal); }
static JSBool DriverManager_getConnection(JSContext *cx, unsigned argc, jsval *vp) { jsval drivers; JS_LookupProperty(cx, JS_THIS_OBJECT(cx, vp), "drivers", &drivers); // FIXME check return code; check that drivers is an array JSObject *obj = JSVAL_TO_OBJECT(drivers); uint32_t len, i; JS_GetArrayLength(cx, obj, &len); jsval connect_argv[2] = {JS_ARGV(cx, vp)[0]}; if (argc == 2) { /* Caller passed "info" object, so we forward it as-is */ connect_argv[1] = JS_ARGV(cx, vp)[1]; } else { JSObject *info = JS_NewObject(cx, NULL, NULL, NULL); // FIXME root it to avoid GC if (argc > 1) JS_DefineProperty(cx, info, "user", JS_ARGV(cx, vp)[1], NULL, NULL, JSPROP_ENUMERATE); if (argc > 2) JS_DefineProperty(cx, info, "password", JS_ARGV(cx, vp)[2], NULL, NULL, JSPROP_ENUMERATE); connect_argv[1] = OBJECT_TO_JSVAL(info); }; jsval reason = JSVAL_NULL; for (i = 0; i < len; i++) { jsval driver, rval; JS_GetElement(cx, obj, i, &driver); if (!JS_CallFunctionName(cx, JSVAL_TO_OBJECT(driver), "connect", 2, &connect_argv[0], &rval)) { if (JSVAL_IS_NULL(reason)) JS_GetPendingException(cx, &reason); continue; } if (JSVAL_IS_NULL(rval)) continue; JS_SET_RVAL(cx, vp, rval); return JS_TRUE; } if (JSVAL_IS_NULL(reason)) { JSString *url_str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]); // FIXME check return value // FIXME root url_str (protect from GC) -> https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_Reference/JS_ValueToString char *url = JS_EncodeString(cx, url_str); JS_ReportError(cx, "No suitable driver found for %s", url); JS_free(cx, url); } else JS_SetPendingException(cx, reason); JS_SET_RVAL(cx, vp, JSVAL_NULL); return JS_FALSE; }
jsval js_create_response(jsval *argv) { jsval response; JSObject *global; global = JS_GetGlobalForScopeChain(js_context); JS_CallFunctionName(js_context, global, "SmtpResponse", 3, argv, &response); return response; }
void onProductRequestSuccess(const std::vector<sdkbox::Product>& products) { if (!s_cx) { return; } JSContext* cx = s_cx; const char* func_name = "onProductRequestSuccess"; JS::RootedObject obj(cx, _JSDelegate); JSAutoCompartment ac(cx, obj); #if MOZJS_MAJOR_VERSION >= 31 bool hasAction; JS::RootedValue retval(cx); JS::RootedValue func_handle(cx); #else JSBool hasAction; jsval retval; jsval func_handle; #endif jsval dataVal[1]; jsval value = std_vector_product_to_jsval(s_cx, products); dataVal[0] = value; if (JS_HasProperty(cx, obj, func_name, &hasAction) && hasAction) { if(!JS_GetProperty(cx, obj, func_name, &func_handle)) { return; } if(func_handle == JSVAL_VOID) { return; } #if MOZJS_MAJOR_VERSION >= 31 JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::fromMarkedLocation(sizeof(dataVal)/sizeof(*dataVal), dataVal), &retval); #else JS_CallFunctionName(cx, obj, func_name, sizeof(dataVal)/sizeof(*dataVal), dataVal, &retval); #endif } }
static JSBool xmljs_parseFile(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSString *str, *filename; struct stat sbuf; char * buf; int fd, len; *rval = JSVAL_TRUE; if (argc < 1) return JS_TRUE; filename = JS_ValueToString(cx, argv[0]); if (!filename) return JS_TRUE; argv[0] = STRING_TO_JSVAL(filename); fd = open(JS_GetStringBytes(filename), O_RDONLY); if (fd < 0) { JS_ReportError(cx, "XMLParser.prototype.parseFile: open of %s failed!\n", JS_GetStringBytes(filename)); return JS_FALSE; } if (fstat(fd, &sbuf) < 0) { JS_ReportError(cx, "XMLParser.prototype.parseFile: stat of %s failed!\n", JS_GetStringBytes(filename)); close(fd); return JS_FALSE; } len = sbuf.st_size; buf = (char *)JS_malloc(cx, len); if (read(fd, (void *)buf, (size_t)len) < len) { JS_ReportError(cx, "XMLParser.prototype.parseFile: read of %s failed!\n", JS_GetStringBytes(filename)); JS_free(cx, buf); close(fd); return JS_FALSE; } close(fd); str = JS_NewString(cx, buf, len); /* hand buf off to engine; don't free! */ if (!str) { JS_free(cx, buf); return JS_FALSE; } argv[0] = STRING_TO_JSVAL(str); return JS_CallFunctionName(cx, obj, "parse", argc, argv, rval); }
jsint js_parseInt (JSContext* cx, jsval number, int base) { jsval ret; if (base >= 2 && base <= 36) { jsval argv[] = {number, INT_TO_JSVAL(base)}; JS_CallFunctionName(cx, JS_GetGlobalObject(cx), "parseInt", 2, argv, &ret); } else { jsval argv[] = {number}; JS_CallFunctionName(cx, JS_GetGlobalObject(cx), "parseInt", 1, argv, &ret); } if (strcmp(JS_GetStringBytes(JS_ValueToString(cx, ret)), "NaN") == 0) { return 0; } jsint nret; JS_ValueToInt32(cx, ret, &nret); return nret; }
JSBool JS_SqlRegisterDriver(JSContext *cx, JSObject *global, JSObject *driver) { jsval DriverManager, rval; jsval argv[] = { OBJECT_TO_JSVAL(driver) }; JS_GetProperty(cx, global, "DriverManager", &DriverManager); // FIXME check return value // FIXME check that DriverManager is an object JS_CallFunctionName(cx, JSVAL_TO_OBJECT(DriverManager), "registerDriver", 1, &argv[0], &rval); // FIXME check return value return JS_TRUE; }
NS_IMETHODIMP DOMEventListener::HandleEvent(nsIDOMEvent *aEvent) { if(!active) return NS_OK; SetEvent(myHandle); if(callBack != NULL) { JS_BeginRequest(aOwner->mContext); JS_CallFunctionName(aOwner->mContext, JS_GetGlobalObject(aOwner->mContext), callBack, 0, NULL, NULL); JS_EndRequest(aOwner->mContext); } return NS_OK; }
JSBool dbquery_resolve( JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp ){ if ( flags & JSRESOLVE_ASSIGNING ) return JS_TRUE; if ( ! JSVAL_IS_NUMBER( id ) ) return JS_TRUE; jsval val = JSVAL_VOID; assert( JS_CallFunctionName( cx , obj , "arrayAccess" , 1 , &id , &val ) ); Convertor c(cx); c.setProperty( obj , c.toString( id ).c_str() , val ); *objp = obj; return JS_TRUE; }
jsval js_call(const char *obj, const char *func, jsval arg, ...) { JSObject *global, *curr_obj; /* Array which stores every "arg" parameter passed to the function */ int argc = 0; jsval argv[16], curr_arg, rval; va_list ap; /* Used when fetching the given object from the global object */ jsval objval; /* Build args array with arguments given to this function */ va_start(ap, arg); curr_arg = arg; while (!JSVAL_IS_NULL(curr_arg)) { argv[argc++] = curr_arg; curr_arg = va_arg(ap, jsval); } va_end(ap); global = JS_GetGlobalForScopeChain(js_context); if (!JS_GetProperty(js_context, global, obj, &objval) || JSVAL_IS_VOID(objval)) { fprintf(stderr, "%s: ERROR: object '%s' does not exist\n", __func__, obj); return JSVAL_NULL; } curr_obj = JSVAL_TO_OBJECT(objval); /* Get the property from object just to see if it exists */ if (!JS_GetProperty(js_context, curr_obj, func, &objval) || JSVAL_IS_VOID(objval)) { fprintf(stderr, "%s: ERROR: method '%s' not defined in '%s'\n", __func__, func, obj); return JSVAL_NULL; } /* Call the given function */ if (!JS_CallFunctionName(js_context, curr_obj, func, argc, argv, &rval)) { fprintf(stderr, "%s: ERROR: failed calling '%s.%s()'\n", __func__, obj, func); return JSVAL_NULL; } return rval; }
JSBool js_ObjectIs (JSContext* cx, jsval check, const char* name) { jsval jsObj; JS_GetProperty(cx, JS_GetGlobalObject(cx), "Object", &jsObj); JSObject* Obj = JSVAL_TO_OBJECT(jsObj); jsval newArgv[] = { check, STRING_TO_JSVAL(JS_NewString(cx, JS_strdup(cx, name), strlen(name))) }; jsval ret; JS_CallFunctionName(cx, Obj, "is", 2, newArgv, &ret); return JSVAL_TO_BOOLEAN(ret); }