Subj : Re: object serialization To : netscape.public.mozilla.jseng From : Jens Thiele Date : Wed Apr 07 2004 10:27 am >> the hash function is shit - is there any way to get a hashvalue for >> an object (perhaps based on its pointer/reference)? > However, about the hash function, I can help: > There is a MD5 hashing class within Mozlib library, part of my JS > Console extension (). Find it in the > "chrome://mozlib/content/lib/js/digest/" directory (after installing JS > Console, of course). You need only 2 files: generator.js and md5.js. > > You could use the DIGEST_MD5Generator class to hash your objects, based > on a unique field value, for instance using the Date function. For > instance: > > var object = {id:new Date}; //add your properties as you wish > var hashvalue = (new DIGEST_MD5Generator()).getHex(object.id); > > Will it fit? i hoped to get a hash for any existing object without adding properties i looked into JS_GetObjectID, JS_IdToValue, JS_ValueToId which at first seemed to be the right thing but perhaps it is not? any obvious bug there? ECMA_BEGIN_FUNC(hashObject) { ECMA_CHECK_NUM_ARGS(1); jsid id; if (!JSVAL_IS_OBJECT(argv[0])) ECMA_ERROR("object required as argument"); JSObject *o=JSVAL_TO_OBJECT(argv[0]); if (!JS_GetObjectId(cx,o,&id)) return JS_FALSE; if (!JS_IdToValue(cx,id,rval)) return JS_FALSE; return JS_TRUE; } hmm following will crash: if (!JS_GetObjectId(cx,o,&id)) return JS_FALSE; if (!JS_IdToValue(cx,id,rval)) return JS_FALSE; JS_TypeOfValue(cx,*rval); JS_PUBLIC_API(JSType) JS_TypeOfValue(JSContext *cx, jsval v) { JSType type; JSObject *obj; JSObjectOps *ops; JSClass *clasp; CHECK_REQUEST(cx); if (JSVAL_IS_OBJECT(v)) { /* XXX JSVAL_IS_OBJECT(v) is true for null too! Can we change ECMA? */ obj = JSVAL_TO_OBJECT(v); if (obj && (ops = obj->map->ops, CRASH since JSVAL_IS_OBJECT is true but the value is not an object puh Brendan? ;-) .