[quickjs-devel] Re: Iterate over JSValue/Object

  • From: "Sam Chang" <dmarc-noreply@xxxxxxxxxxxxx> (Redacted sender "sam.chang" for DMARC)
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Tue, 20 Aug 2019 17:09:11 +0800

use JS_FreeAtom

sample code:

https://github.com/quickjs-ios/quickjs-ios/blob/28c94847b7d14e388d2f02e7dd8f62d1134968f9/QuickJS-iOS/Classes/QJSContext.m#L540
 
<https://github.com/quickjs-ios/quickjs-ios/blob/28c94847b7d14e388d2f02e7dd8f62d1134968f9/QuickJS-iOS/Classes/QJSContext.m#L540>


On Aug 20, 2019, at 4:29 PM, Serghei Amelian (Redacted sender "serghei" for 
DMARC) <dmarc-noreply@xxxxxxxxxxxxx> wrote:

Hi Alexander,

Thank you, it was useful, I figure out how to do it, I attached an example.

Now, it seems that the list of properties allocated by 
JS_GetOwnPropertyNames() must be freed somehow, but I don't know what 
function to use for this operation.

Thanks.

On Mon August 19 2019 15:12:05 Alexander Rodin wrote:
Hi Serghei,

How it can be iterated an JSValue of type Object? I need to parse on
C-host an object/array received from js.

You can use `JS_GetOwnPropertyNames` and `JS_GetPropertyInternal` to
iterate over object properties.

See
https://github.com/theduke/quickjs-rs/blob/4a87e54c66e2d4225be69c258f044f39
6d2f8ac1/src/bindings.rs#L239-L288
<https://github.com/theduke/quickjs-rs/blob/4a87e54c66e2d4225be69c258f044f3
96d2f8ac1/src/bindings.rs#L239-L288> for an example of iterating over an
object and
https://github.com/theduke/quickjs-rs/blob/4a87e54c66e2d4225be69c258f044f39
6d2f8ac1/src/bindings.rs#L202-L237
<https://github.com/theduke/quickjs-rs/blob/4a87e54c66e2d4225be69c258f044f3
96d2f8ac1/src/bindings.rs#L202-L237> for an example of iterating over an
array. Although it is not C, but Rust, the idea should still be clear if
you look at `unsafe` blocks.

Best regards,
Alexander



-- 
Serghei
<enumeration.c>

Other related posts: