diff --git a/js/src/xpconnect/src/XPCNativeWrapper.cpp b/js/src/xpconnect/src/XPCNativeWrapper.cpp index b22d6c455ff8..665af7e7cc45 100644 --- a/js/src/xpconnect/src/XPCNativeWrapper.cpp +++ b/js/src/xpconnect/src/XPCNativeWrapper.cpp @@ -645,8 +645,20 @@ XPC_NW_NewResolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, if (id == GetRTStringByIndex(cx, XPCJSRuntime::IDX_TO_STRING)) { *objp = obj; - return JS_DefineFunction(cx, obj, "toString", - XPC_NW_toString, 0, 0) != nsnull; + + // See the comment in XPC_NW_WrapFunction for why we create this function + // like this. + JSFunction *fun = JS_NewFunction(cx, XPC_NW_toString, 0, 0, nsnull, + "toString"); + if (!fun) { + return JS_FALSE; + } + + JSObject *funobj = JS_GetFunctionObject(fun); + STOBJ_SET_PARENT(funobj, obj); + + return JS_DefineProperty(cx, obj, "toString", OBJECT_TO_JSVAL(funobj), + nsnull, nsnull, 0); } if (!EnsureLegalActivity(cx, obj)) {