Subj : Re: Another JS question To : netscape.public.mozilla.jseng From : Issac Goldstand Date : Tue May 04 2004 12:04 pm Continuing discussion on netscape.public.mozilla.jseng: ----- Original Message ----- Sent: Monday, May 03, 2004 11:06 PM Subject: Re: Another JS question > Issac Goldstand wrote: > > >Brendan, > > First of all, thanks tons for putting up with me a few weeks ago with the > >"bugs" I was finding in the SpiderMonkey API (which was really just > >forgetting to set /OPT:NOICF on Win32)... > > > >I've since made a lot of progress and finding myself making good progress... > > > >I'm currently trying to make glue to return objects from JS in Perl... I > >have a single conversion function which converts a jsval to whatever type > >best suits it. To do this, it does a simple switch on JS_TypeOfValue. > >Numbers and strings were an easy start, and I qwuickly started working on > >arrays. Currently, I do something like: > > > > case JSTYPE_OBJECT: > > { > > JSObject* obj; > > if (!(JS_ValueToObject(cx,v,&obj))) { > > return UNDEFINED; > > } > > if (JS_IsArrayObject(cx,obj)) { > > > > DoArrayCallback(cx,obj); > > } else { > > return UNDEFINED; > > } > > > > > > Nit: return UNDEFINED early in both cases, and don't write else after > return when you transpose the then and else parts in the second if > statement. > I return UNDEFINED in the else block as a placeholder - I hope to implement some intelligent logic for that in the future. > Bug: JS_ValueToObject can return null in obj and true as its return > value -- you need to check and defend, as null is not a valid object to > pass to JS_IsArrayObject, etc. ??? But I thought " JS_ValueToObject converts a specified JS value, v, to a JS object. The converted object is stored in the object pointed to by objp. If the conversion is successful, JS_ValueToObject returns JS_TRUE. Otherwise it returns JS_FALSE. " I would think that null would be an unsuccessful conversion > >var a = new Array(43); > >a["AB"] = new Array(); > >a["AB"][0] = new areaCode("780 ", new Array(new > >nxx("any","Any Location "), new nxx("Edmonton","Edmonton"))); > >a["AR"] = new Array(); > >a["AR"][0] = new areaCode("501 ", new Array(new > >nxx("any","Any Location "), new nxx("Ferndale","Ferndale"), new > >nxx("Palarm","Palarm"), new nxx("Pinnacle","Pinnacle"))); > > > >a["AR"][1] = new areaCode("479 ", new Array(new > >nxx("any","Any Location "), new nxx("Bentonville","Bentonville"), > >new nxx("Fayetteville","Fayetteville"))); > > > >... continues for another 41 states > > > > > > a is not an Array. You are not indexing -- using non-negative integer > property ids. Why not use an Object instead? You could even use > convenient object literal notation (a = {AB: [...], AR: [...], ...]). > I'm not sure I understood that... In any case, I can't change the javascript source as I'm having it passed to me - not generating it myself. > >If I then do JS_GetProperty(cx,global,"a",&rval) and pass rval to my > >conversion function, it returns a 43member array with everything UNDEFINED. > >Debugging shows me that the type is 0 (I think that's JS_VOID) for each > >recursive call beyond the top-level array. My question is what is the > >"proper" way to recurse into a data structure like this? > > > >One thought that I had was: I currently return each element of the array by > >calling JS_GetArrayLength and using a for loop with JS_GetElement. I > >thought maybe the "hash-like" nature of the top level of the array wouldn't > >work with that. > > > > It does not. Arrays are objects that track their length (one greater > than the greatest non-negative property id). They don't allocate an > index aliasing the named properties you add to them (they did long ago, > in JS1.0, but that mode is not part of the ECMA standard). > > > Frankly, I'd rather identify hash-like arrays anyway and > >return a proper hash-table for them. > > > > > > Use an object. > > To enumerate it, use a for..in loop. > Is there any way to identify an object that follows this "hash-like" convention, so I can add a section in the conversion function to return a native hash as the return value? > >PS - If you're not the correct address for questions like this, I'll gladly > >send them somewhere else if you could point me to the best place. > > > > You've read news://news.mozilla.org/netscape.public.mozilla.jseng, > right? That's the place for SpiderMonkey embedding questions. Don't > mail me, I may not answer, and others won't learn from my answers if I > do reply. I didn't read it. I knew it was there. I couldn't access it. It troubled me for a bit, but I never really applied myself to seeing why it didn't work. When you told me that this was really the "proper" forum for these discussions I tried again, found out that my firewall was blocking nntp access to any server not explicitly whitelisted and remedied the situation. Thanks :-) Issac > /be > .