Quantcast

C++ API getOwnPropertyNames?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

C++ API getOwnPropertyNames?

Kent Williams
Here's what I'm trying to do:

Given an JSObject, can I get the names of its properties?

Extra credit -- this is with an obsolete version of Spidermonkey (1.8.5)

Use case: Inside a JSNative function we look for properties in an
optional argument.

EG a JS_Native function takes one string argument, and an optional property:

var X = FindSomething_JSNative("a", { "look_really_hard" });

If none of the expected properties are defined, I want to print an error
message giving the name of the unrecognized properties.


_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: C++ API getOwnPropertyNames?

Miles Thornton
I'm still using 1.8.5 too so I can answer this...



I think you want to use JS_Enumerate(). Something like:



JSIdArray *IdArray;

jsval        val;

JSString  *str;



IdArray = JS_Enumerate(cx, obj);



for (i=0; i<IdArray->length; i++)

{

      JS_IdToValue(cx, IdArray->vector[i], &val);

      str = JS_ValueToString(cx, val);



// Then use JS_GetStringLength and JS_EncodeStringToBuffer to get property name as C char *.

// You can then use JS_GetProperty



}



Miles


Miles Thornton
Advanced Technology + Research

Arup
The Arup Campus  Blythe Valley Business Park  Solihull  W. Midlands  B90 8AE
d: +44 121 213 3308
www.arup.com <http://www.arup.com/>

____________________________________________________________
Electronic mail messages entering and leaving Arup  business
systems are scanned for acceptability of content and viruses
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: C++ API getOwnPropertyNames?

Benjamin Kircher
Hi!

On Wed, Aug 24, 2016 at 8:58 AM, Miles Thornton <[hidden email]>
wrote:

> I think you want to use JS_Enumerate(). Something like:


One important thing to remember here is to call JS_DestroyIdArray when you
are finished.

BK
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Loading...