nsresult IDBRequest::NotifyHelperCompleted(HelperBase* aHelper) { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); NS_ASSERTION(!mHaveResultOrErrorCode, "Already called!"); NS_ASSERTION(JSVAL_IS_VOID(mResultVal), "Should be undefined!"); // See if our window is still valid. If not then we're going to pretend that // we never completed. if (NS_FAILED(CheckInnerWindowCorrectness())) { return NS_OK; } mHaveResultOrErrorCode = true; nsresult rv = aHelper->GetResultCode(); // If the request failed then set the error code and return. if (NS_FAILED(rv)) { SetError(rv); return NS_OK; } // Otherwise we need to get the result from the helper. JSContext* cx = GetJSContext(); if (!cx) { NS_WARNING("Failed to get safe JSContext!"); rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; SetError(rv); return rv; } JSObject* global = GetParentObject(); NS_ASSERTION(global, "This should never be null!"); JSAutoRequest ar(cx); JSAutoEnterCompartment ac; if (ac.enter(cx, global)) { AssertIsRooted(); rv = aHelper->GetSuccessResult(cx, &mResultVal); if (NS_FAILED(rv)) { NS_WARNING("GetSuccessResult failed!"); } } else { NS_WARNING("Failed to enter correct compartment!"); rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; } if (NS_SUCCEEDED(rv)) { mError = nullptr; } else { SetError(rv); mResultVal = JSVAL_VOID; } return rv; }
nsresult xgGtkElement::DefineProperties () { nsresult rv; JSContext *jscx; rv = GetJSContext (&jscx); NS_ENSURE_SUCCESS (rv, rv); nsCOMPtr<nsIXPConnect> xpc (do_GetService ("@mozilla.org/js/xpc/XPConnect;1", &rv)); NS_ENSURE_SUCCESS (rv, rv); nsCOMPtr<nsIDOMElement> elem; rv = mWrapper->GetElementNode (getter_AddRefs (elem)); NS_ENSURE_SUCCESS (rv, rv); nsCOMPtr<nsIXPConnectJSObjectHolder> jswrapper; rv = xpc->WrapNative (jscx, JS_GetGlobalObject (jscx), elem, NS_GET_IID (nsISupports), getter_AddRefs (jswrapper)); NS_ENSURE_SUCCESS (rv, rv); JSObject *jsobj; rv = jswrapper->GetJSObject (&jsobj); NS_ENSURE_SUCCESS (rv, rv); g_message (GOM_LOC ("Got JSObject: %p"), (void *)jsobj); guint n_properties; GParamSpec **props = g_object_class_list_properties (G_OBJECT_GET_CLASS (mObject), &n_properties); g_message (GOM_LOC ("Adding %d properties from %s"), n_properties, G_OBJECT_TYPE_NAME (mObject)); JS_BeginRequest (jscx); const char *camelName; for (guint i = 0; i < n_properties; i++) { camelName = gom_camel_case (props[i]->name); if (!JS_DefineProperty (jscx, jsobj, camelName, JSVAL_VOID, xgGObject::GetProperty, xgGObject::SetProperty, JSPROP_ENUMERATE | JSPROP_PERMANENT)) { g_printerr ("Could not define a property for %s\n", camelName); } else { g_print (GOM_LOC ("Defined property: %s.%s\n"), G_OBJECT_TYPE_NAME (mObject), camelName); } GOM_CAMEL_FREE (camelName, props[i]->name); } JS_EndRequest (jscx); return NS_OK; }
JSArray::JSArray(const std::initializer_list<JSValue>& values) : ctx_(GetJSContext()) { size_t argCount = values.size(); JSValueRef* args = nullptr; if (argCount) { args = new JSValueRef[argCount]; size_t i = 0; for (auto arg : values) args[i++] = arg; } instance_ = JSObjectMakeArray(ctx_, argCount, args, nullptr); if (args) delete[] args; JSValueProtect(ctx_, instance_); }
JSValueRef NativeFunctionWithRetvalCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { JSCallbackWithRetval* callback = static_cast<JSCallbackWithRetval*>(JSObjectGetPrivate(function)); if (!callback) return JSValueMakeNull(ctx); JSContextRef old_ctx = GetJSContext(); SetJSContext(ctx); JSArgs args; for (size_t i = 0; i < argumentCount; ++i) args.push_back(arguments[i]); JSValueRef result = (*callback)(thisObject, args); SetJSContext(old_ctx); return result; }
JSValue::JSValue() : ctx_(GetJSContext()) { instance_ = JSValueMakeNull(ctx_); JSValueProtect(ctx_, instance_); }
JSValue::JSValue(const ultralight::String& val) : ctx_(GetJSContext()) { instance_ = JSValueMakeString(ctx_, JSString(val)); JSValueProtect(ctx_, instance_); }
JSValue::JSValue(const char* val) : ctx_(GetJSContext()) { instance_ = JSValueMakeString(ctx_, JSString(val)); JSValueProtect(ctx_, instance_); }
JSValue::JSValue(double val) : ctx_(GetJSContext()) { instance_ = JSValueMakeNumber(ctx_, val); JSValueProtect(ctx_, instance_); }
JSValue::JSValue(int64_t val) : ctx_(GetJSContext()) { instance_ = JSValueMakeNumber(ctx_, static_cast<double>(val)); JSValueProtect(ctx_, instance_); }
JSValue::JSValue(bool val) : ctx_(GetJSContext()) { instance_ = JSValueMakeBoolean(ctx_, val); JSValueProtect(ctx_, instance_); }
JSValue JSEval(const JSString& str) { return JSEvaluateScript(GetJSContext(), str, 0, 0, 0, 0); }
JSObject JSGlobalObject() { return JSContextGetGlobalObject(GetJSContext()); }
JSValue::JSValue(JSString val) : ctx_(GetJSContext()) { instance_ = JSValueMakeString(ctx_, val); JSValueProtect(ctx_, instance_); }
JSObject::JSObject(JSObjectRef obj) : ctx_(GetJSContext()) { instance_ = obj; JSValueProtect(ctx_, instance_); }
JSObject::JSObject() : ctx_(GetJSContext()) { instance_ = JSObjectMake(ctx_, nullptr, nullptr); JSValueProtect(ctx_, instance_); }
JSArray::JSArray(JSObjectRef array_obj) : ctx_(GetJSContext()) { instance_ = array_obj; JSValueProtect(ctx_, instance_); }
JSArray::JSArray() : ctx_(GetJSContext()) { instance_ = JSObjectMakeArray(ctx_, 0, nullptr, nullptr); JSValueProtect(ctx_, instance_); }
JSValue::JSValue(JSValueRef val) : ctx_(GetJSContext()) { instance_ = val; JSValueProtect(ctx_, instance_); }