Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for (k in o) loop is broken #33

Closed
drsm opened this issue Jul 4, 2018 · 9 comments
Closed

for (k in o) loop is broken #33

drsm opened this issue Jul 4, 2018 · 9 comments
Assignees
Labels

Comments

@drsm
Copy link
Contributor

drsm commented Jul 4, 2018

>> var x = Object.create(null);
undefined
>> x.one = 1;
1
>> for (var a in x) console.log(a);
one
undefined
>> var y = Object.create(x);
undefined
>> for (var a in y) console.log(a);
undefined
>> 'one' in x
true
>> 'one' in y
true
>>
@xeioex xeioex self-assigned this Jul 10, 2018
@xeioex xeioex added the bug label Jul 10, 2018
@reyou
Copy link

reyou commented Dec 17, 2018

Is this fixed in place on latest package?

I have following code;

/etc/nginx/conf.d/api-requests-nginx.conf

server {    
    listen 8081;   
     location / {
     error_log  /etc/nginx/logs/info.txt;
     access_log  /etc/nginx/logs/info.txt;
     js_content routingApi;        
      }

and in JS:

// called after req.subrequest
function done(reply) {
 for(var propName in reply) {
            propValue = reply[propName];
            resText +=  propName + propValue;                       
             }
}

does not work. Meaning does not even go into loop.

Any idea on this?

@xeioex
Copy link
Contributor

xeioex commented Dec 17, 2018

Hi @reyou ,

Currently, in njs, you can iterate over only certain properties of request and reply.
Namely (r.headersIn, r.headersOut, r.args)

Because r and reply are not ordinary js objects, but rather wrappers around nginx request structure (see more here #64 (comment)). So, they are not native objects to njs, and special C code have to be written for each such iterator (for example).

To see reply internal properties of them you can use njs.dump() #69 (comment)

@lexborisov
Copy link
Contributor

lexborisov commented Apr 8, 2019

@drsm
Hi Artem!

Please, try this patch for the issue.

@drsm
Copy link
Contributor Author

drsm commented Apr 8, 2019

@lexborisov
Hi!
Thank you for the patch.
We have to flatten property tree there in order to bypass duplicate keys, not sure how to do it right:

>> var o = { a: 1, b: 1 }; var x = Object.create(o); x.b = 2; x.c = 3;
3
>> var a = []; for(var p in x) a.push(p); a
[
 'b',
 'c',
 'a',
 'b'
]

@lexborisov
Copy link
Contributor

@drsm

I solved the problem with duplicate keys. Please, try this three patches.

@drsm
Copy link
Contributor Author

drsm commented Apr 8, 2019

@lexborisov
The patch works fine for ordinary objects, thanks!

Still, with exotic ones there are problems:

>> var a = []; for(var p in Object('abc')) a.push(p); a
[
 
]
>> var a = []; for(var p in Array(3).fill()) a.push(p); a // OK
[
 '0',
 '1',
 '2'
]
>> var o = Object('abc'); var x = Object.create(o); x.a = 1; x.b = 2;
2
>> var a = []; for(var p in x) a.push(p); a
[
 'a',
 'b'
]
>> var o = Array(3).fill(); var x = Object.create(o); x[1] = 1; x[2] = 2;
2
>> var a = []; for(var p in x) a.push(p); a
[
 '1',
 '2'
]

I think at least the first one should be fixed too.

Upd:
and this:

> var a = []; for(var p in 'abc') a.push(p); a
[ '0', '1', '2' ]

@lexborisov
Copy link
Contributor

lexborisov commented Apr 8, 2019

@drsm

I understood the problem. The problem here is that the iterator is not implemented for string at all.

>> var a = []; for(var p in 'abc') a.push(p); a
[
 
]

I think that tomorrow we will solve this issue.
Thank you for the report!

@lexborisov
Copy link
Contributor

@drsm

It took a little more than one day. Not everything has been implemented yet, which I would like.
We decided to iteratively approach of implementation foreach function.

Please, see this patch.

@drsm
Copy link
Contributor Author

drsm commented Apr 18, 2019

@lexborisov
Looks good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants