Subj : Re: why does it not print Hello twice? To : Jens Thiele From : Brendan Eich Date : Thu Apr 15 2004 01:08 am This is a multi-part message in MIME format. --------------030508080100010606040509 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Jens Thiele wrote: > I am stuck: > > var w=function(text){ > return (function(id,o,n){print(text);return n;}); > }("Hello"); > w(); > foo={x:1} > foo.watch("x",w); > foo.x=10; > > Is watch somehow different? > Is it a bug? Yes and yes. Thanks again, this is a very old bug! I'm filing http://bugzilla.mozilla.org/show_bug.cgi?id=240577 and fixing now, using the attached patch. /be --------------030508080100010606040509 Content-Type: text/x-patch; name="jsobj.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="jsobj.patch" Index: jsobj.c =================================================================== RCS file: /cvsroot/mozilla/js/src/jsobj.c,v retrieving revision 3.158 diff -p -u -8 -r3.158 jsobj.c --- jsobj.c 14 Apr 2004 02:36:37 -0000 3.158 +++ jsobj.c 15 Apr 2004 07:05:02 -0000 @@ -1261,36 +1261,42 @@ obj_watch_handler(JSContext *cx, JSObjec ok = js_InternalCall(cx, obj, OBJECT_TO_JSVAL(funobj), 3, argv, nvp); StopResolving(cx, &key, JSRESFLAG_WATCH, entry, generation); return ok; } static JSBool obj_watch(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { + JSObject *funobj; JSFunction *fun; jsval userid, value; jsid propid; uintN attrs; - fun = js_ValueToFunction(cx, &argv[1], 0); - if (!fun) - return JS_FALSE; - argv[1] = OBJECT_TO_JSVAL(fun->object); + if (JSVAL_IS_FUNCTION(cx, argv[1])) { + funobj = JSVAL_TO_OBJECT(argv[1]); + } else { + fun = js_ValueToFunction(cx, &argv[1], 0); + if (!fun) + return JS_FALSE; + funobj = fun->object; + } + argv[1] = OBJECT_TO_JSVAL(funobj); /* Compute the unique int/atom symbol id needed by js_LookupProperty. */ userid = argv[0]; if (!JS_ValueToId(cx, userid, &propid)) return JS_FALSE; if (!OBJ_CHECK_ACCESS(cx, obj, propid, JSACC_WATCH, &value, &attrs)) return JS_FALSE; if (attrs & JSPROP_READONLY) return JS_TRUE; - return JS_SetWatchPoint(cx, obj, userid, obj_watch_handler, fun->object); + return JS_SetWatchPoint(cx, obj, userid, obj_watch_handler, funobj); } static JSBool obj_unwatch(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { return JS_ClearWatchPoint(cx, obj, argv[0], NULL, NULL); } --------------030508080100010606040509-- .