-
-
Notifications
You must be signed in to change notification settings - Fork 401
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
Value::to_json destroy the order of items in an Array #690
Comments
Iterating through the indexed keys is better so we should have: if obj.borrow().is_array() {
let keys: Vec<u32> = obj.borrow().index_property_keys().collect().sort();
let mut array: Vec<JSONValue> = Vec::with_capacity(keys.len());
for key in keys {
let value = self.get_field(key);
if value.is_undefined() || value.is_function() || value.is_symbol() {
array.push(JSONValue::Null);
} else {
array.push(value).to_json(interpreter)?);
}
}
Ok(JSONValue::Array(array))
} we also dont do What do you think? @sele9 |
I think the "sort" is not really needed. If it is an Array the order should be from 0 .. length currently i'am using: let len = obj
.borrow()
.keys()
.into_iter()
.filter(|k| match k {
PropertyKey::Index(_) => true,
_ => false,
})
.count();
let mut arr: Vec<JSONValue> = Vec::with_capacity(len);
for key in 0..len {
let k = PropertyKey::Index(key as u32);
let value = self.get_field(k);
if value.is_undefined() || value.is_function() || value.is_symbol()
{
arr.push(JSONValue::Null);
} else {
arr.push(value.to_json(interpreter)?);
}
}
Ok(JSONValue::Array(arr)) |
This is how its implemented in the spec, but there is a problem with this approach, with sparse arrays for example: let x = [];
x[4294967294] = 1;
JSON.stringify(x) with this implementation it will iterate through from 0 to |
okay i'll update the PR |
The compiler complains about the type, because sort does not return a value. obj.borrow().index_property_keys().collect().sort(); 241 | let keys : Vec = obj.borrow().index_property_keys().collect().sort(); |
let mut keys : Vec<u32> = obj.borrow().index_property_keys().cloned().collect();
keys.sort(); would work |
Describe the bug
The order of array items is sometimes mixed after the conversion from boa::Value to serde_json::Value.
To Reproduce
I've added a few lines of code here:
boa/boa/src/value/mod.rs
Line 242 in edfafc4
The debugger shows me the following keys:
This JavaScript code reproduces the issue:
where
$t
is a globally registered function implemented in rust andself
the equivalent ofglobalThis
Expected behavior
It should return in the same Order as defined in javascript.
Build environment (please complete the following information):
Additional context
I replaced these lines:
boa/boa/src/value/mod.rs
Lines 240 to 253 in edfafc4
with:
it works as expected but maybe there is a more performant way?
The text was updated successfully, but these errors were encountered: