Subj : Re: Reserving another slot for private data To : Sterling Bates From : Brendan Eich Date : Thu Apr 22 2004 02:55 pm Sterling Bates wrote: > I need to store within each object a reference back to the Delphi > object. I can currently do so with standard JS objects, but I need the > same for strings and doubles, which occupy their standard private slot. > So I think I would use this slot like so: > > 1. Implement JSClass.reserveSlots to return 1 (since I only need one slot.) This won't work for strings and doubles, which are not objects, so have no slots (reserved, private, or otherwise). As an aside, you can reserve a constant number n of slots by including JSCLASS_HAS_RESERVED_SLOTS(n) in the class's flags initializer. The new reserveSlots hook is optional and only when the number of slots varies per object (but does not change once an object has been constructed). > 2. After the object is created, use JS_SetReservedSlot to assign the > slot value. > 3. When I need to get the Delphi object reference, call JS_GetReservedSlot. Yes, those are the APIs to use to access reserved slots (I thought you were going to list alternatives in 2 and 3; I'll do that below). > The problem is that conversion to & from a jsval could drop the > left-most bit in the pointer, which would be problematic. How so? The jsval is a tagged pointer to the object, string, or double. The reserved slots you want are in each object's slots array. A string is a primitive type; if you wrap it with a String object and hack the engine so that String objects have reserved slots, then you can indeed associate a pointer with the object -- but not with the primitive type value it "boxes". Note that boxing is not transparent. You can't use a String object in all ways as if it were a string datum. Ditto for numbers (doubles). > Is that > actually an issue, and how could I avoid this? I think you're confused by jsval tagging -- ignore it ;-). The problem you've posed has come up before for objects, and other than reserved slots, a perhaps better solution is to maintain your own hashtable mapping GC-things to whatever data you like. You can use JS_SetGCCallback and JS_IsAboutToBeFinalized from it to tell when to remove table entries. Why do you need the back-reference per string or double? Are *data* of those primitive types really always associated 1:1 with Delphi *objects*? /be .