Subj : Re: Exceptions and Object Lifetimes To : Pete Diemert From : Brendan Eich Date : Tue Dec 16 2003 04:50 pm Pete Diemert wrote: >1) Exceptions - We have exposed our own object/funcs and so, would also >like to expose errors that our functions can generate as catchable >exceptions in JS. However, from looking at JSAPI.H we cannot see a clear >way to "throw" an exception from the host. We have tried to add entries to >JS.MSG and marked them as JSXN_ERR which seems to cause the exception object >to get generated in scope but alas nothing is catching in JS. Do we have to >actually construct JS script to do the throw then evaluate the script in >scope in order to get exceptions? > > I saw your followup saying you fixed your code. That's good, but it's bad that you still have to hack js.msg. That's our bug -- see http://bugzilla.mozilla.org/show_bug.cgi?id=215173. >2) Object Lifetimes - To make the object model in JS coherent with our own >internal object model we allow the JS object to keep a "reference count" on >our internal object so that JS controls its lifetime. This works fine but >we notice that the GC is VERY lazy and will choose to keep objects alive >after they are clearly never going to be accessed again, waiting for >shutdown or mem threshold, e.g.: > >function foo() >{ > var x = new SomeObject; > // x drops out of scope after func, never to be seen again but object >ref will remain >} > > > That's how GC works, in general -- you shouldn't be depending on prompt finalization. You should, however, run the GC more often, especially if there may be garbage to collect. JS_MaybeGC from the branch callback every 2^n backward branches or returns, for some n around 12 or 13, and JS_MaybeGC or even JS_GC when you're destroying any large "container" object (such as browser windows, in the DOM embedding), are good ideas. >The object does indeed get cleaned up at shutdown but we have a system that >may instantiate some fairly big footprint objects such as forms, databases, >recordsets, etc... Understanding that the GC reserves the right to >arbitrarily clean these objects is there some way we can get a callback >stating "object no longer in use by any call object" in lieu of a >JSFinalize() so that we can force the GC ourselves and prevent a rather >large uneeded working set? > > No, that would require reference counting. If you are tying up scarce resources, you need to require script writers to call a "release" method. There's no substitute. If you come to depend on a JS engine that does use ref-counting, you're depending on something not guaranteed by the language spec. So a mandatory method is the way to go. /be .