Beginning on October 25th, 2016, Persona will no longer be an option for authentication on BMO. For more details see Persona Deprecated.
Last Comment Bug 317485 - Implement getObjectId for js
: Implement getObjectId for js
Product: Core
Classification: Components
Component: XPConnect (show other bugs)
: Trunk
: x86 Windows XP
: -- normal (vote)
: ---
Assigned To: Alex Fritze
: Andrew Overholt [:overholt]
Depends on:
Blocks: 317491
  Show dependency treegraph
Reported: 2005-11-22 15:10 PST by Alex Fritze
Modified: 2011-04-22 12:00 PDT (History)
13 users (show)
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---

patch v.1 (2.43 KB, patch)
2005-11-22 15:13 PST, Alex Fritze
shaver: review-
Details | Diff | Splinter Review

Description Alex Fritze 2005-11-22 15:10:36 PST
For zap we need a js mechanism of producing hash table keys for objects.
Patch coming up.
Comment 1 Alex Fritze 2005-11-22 15:13:30 PST
Created attachment 203994 [details] [diff] [review]
patch v.1

This patch implements Components.utils.getObjectId(obj) which returns a unique identifier for obj, good for the lifetime of obj.
Comment 2 Mike Shaver (:shaver -- probably not reading bugmail closely) 2005-12-10 09:35:16 PST
Comment on attachment 203994 [details] [diff] [review]
patch v.1

>+    JSObject *obj;
>+    if (!JS_ValueToObject(cx, argv[0], &obj))
>+        return NS_ERROR_FAILURE;
>+    jsid id;
>+    if (!JS_GetObjectId(cx, obj, &id))
>+        return NS_ERROR_FAILURE;

Do these not signal an exception?  I think they do, in which case
we should take care that callers see that exception, and not just
a generic NS_ERROR_FAILURE.  I don't recall whether or not you need
to return a special nsresult to signal that to XPConnect, but your
testing should show it clearly. =)

>+    char buf[80];
>+    sprintf(buf,"%p", id);
>+    JSString *str = JS_NewStringCopyZ(cx, buf);
>+    *retval = STRING_TO_JSVAL(str);

Better to use simply

  JS_IdToValue(cx, id, retval)

here and document that the id is a primitive.  If you really want
it to be a string, then JS_ValueToString afterwards will slake
your thirst.
Comment 3 James Turner 2007-07-23 08:13:30 PDT
Trying to unpick this (it's been a while since I did any SpiderMonkey development), I can't see any specific references to JS_ValueToObject signalling an exception. What's the definitive way to check? JS_GetPendingException? nsXPCWrappedJSClass::CheckForException looks like it has some related code, to convert the JSException to an XPCException and convert that to an nsresult, but it's, uh, quite a substantial chunk of code.

The JS_IdToValue path is better, I'll check with Alex how vital it is that we use a string.
Comment 4 Mike Shaver (:shaver -- probably not reading bugmail closely) 2007-07-24 11:25:40 PDT
JS_GetPendingException is the right way to check, as with all other JSAPI calls that can return JS_FALSE to signal failure.  We should make that clear in the JSAPI docs on MDC if we haven't already.

In the years since this bug was filed, I think ECMA TG1 has specified a similar method to live on Object.prototype proper; might be worth looking into that spec and just doing it for Spidermonkey.
Comment 5 Jonathan Watt [:jwatt] 2007-10-23 02:51:32 PDT
ES4 will define a global function intrinsic::hashcode that computes a hash value for any other value. Since this function will be in the 'intrinsic' namespace, it would be good to implement Components.utils.getObjectId as an interim solution for gecko 1.9 (namespaces in JavaScript will only be implemented for Mozilla 2 I believe).
Comment 6 Jason Orendorff [:jorendorff] 2011-04-22 12:00:35 PDT
This is finally possible in JS, thanks to WeakMap (bug 547941).

var getObjectId = (function () {
    var nextId = 0;
    var map = new WeakMap;
    return function getObjectId(obj) {
        var i = map.get(obj);
        if (i === undefined) {
            i = nextId++;
            map.set(obj, i);
        return i;

Not that you would do this in order to produce hash codes; WeakMap is itself an identity hash table on object keys.

Note You need to log in before you can comment on or make changes to this bug.