You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've seen #1974 and #1961.
I'm having the same issue, I understand the explanation in the ticket as well, makes sense.
That being said, we're allowing any JsValue to be returned back to Rust land and the current API makes it a bit hard to traverse the object tree returned.
Is there a workaround currently?
Yes, you can combine OrdinaryObject::keys with JsArray::try_from_js to iterator over them and call JsObject::get with each one.
So, what's the problem?
Within the boa crate it can be done with one line of code, it is nice and simple...externally, it needs almost 30 lines.
Within Boa
obj.borrow().properties().shape.keys()
Externally
let keys = OrdinaryObject::keys(&JsValue::undefined(),&[JsValue::Object(obj.clone())],
context,)?;let keys = JsArray::try_from_js(&keys, context)?;letmut out = Map::new();letmut i:i32 = 0;let len = keys.length(context)? asi32;whileletOk(key) = keys.at(i, context){let key = match key {JsValue::Null => continue,JsValue::Undefined => continue,JsValue::Boolean(k) => {PropertyKey::String(if k {"true".into()}else{"false".into()})}JsValue::String(k) => PropertyKey::String(k),JsValue::Rational(k) => PropertyKey::String(k.to_string().into()),JsValue::Integer(k) => PropertyKey::String(k.to_string().into()),JsValue::BigInt(k) => PropertyKey::String(k.to_string().into()),JsValue::Object(_) => continue,JsValue::Symbol(_) => continue,};if obj.has_own_property(key.clone(), context)? {let val = /*map*/obj.get::<PropertyKey>(key.clone(), context)?;
out.insert(key.to_string(),js_to_json(val, context)?);}
i = i + 1;if i >= len {break;}}
What's being requested?
A JsObject.keys() or PropertyMap.keys() method which return the keys on a JsObject so that we can iterate over the keys and call .get on the JsObject.
Within boa it can just be the snippet above that to_json uses.
Anything else?
As a new boa user it was incredibly difficult to work out how to do this. The example above of how I ended up doing it is just the final thing I ended up with
I had a variant where I tried to get a JsMap from obj but it fails saying obj is not a map...I don't know the ECMAScript spec but it seems to me that I should be able to convert a JsObject to a JsMap
To work around the above I tried something like this:
let obj_copy = JsObject::from_proto_and_data(
context.intrinsics().constructors().map().prototype(),OrderedMap::<JsValue>::new(),);
obj_copy.copy_data_properties::<PropertyKey>(&JsValue::from(obj.clone()),vec![],
context,)?;let map = JsMap::from_object(obj_copy)?;let keys = map.keys(context)?;whileletOk(key) = keys.next(context)?
{//use key here to get value}
this failed spectacularly as it appears keys.next(context) never fails and lead to an infinite loop. I had a look and couldn't find any tests or usages of the JsMapIterator returned. It returns a JsResult<JsValue> so it never ends unless there's a failure?
The text was updated successfully, but these errors were encountered:
Hi @HalidOdat 👋🏾 !
Yes, that's exactly what I needed. Amazing turn around time as well.
FYI it seems we've committed to boa over alternatives now after evaluating a few options so in future I'm happy to contribute changes like this after some guidance (I wouldn't have thought to look at __own_property_keys__ or that own_property_keys was the way to go
Background
I've seen #1974 and #1961.
I'm having the same issue, I understand the explanation in the ticket as well, makes sense.
That being said, we're allowing any JsValue to be returned back to Rust land and the current API makes it a bit hard to traverse the object tree returned.
Is there a workaround currently?
Yes, you can combine
OrdinaryObject::keys
withJsArray::try_from_js
to iterator over them and callJsObject::get
with each one.So, what's the problem?
Within the boa crate it can be done with one line of code, it is nice and simple...externally, it needs almost 30 lines.
Within Boa
Externally
What's being requested?
A
JsObject.keys()
orPropertyMap.keys()
method which return the keys on a JsObject so that we can iterate over the keys and call.get
on theJsObject
.Within boa it can just be the snippet above that
to_json
uses.Anything else?
JsMap
fromobj
but it fails sayingobj
is not a map...I don't know the ECMAScript spec but it seems to me that I should be able to convert aJsObject
to aJsMap
this failed spectacularly as it appears
keys.next(context)
never fails and lead to an infinite loop. I had a look and couldn't find any tests or usages of theJsMapIterator
returned. It returns aJsResult<JsValue>
so it never ends unless there's a failure?The text was updated successfully, but these errors were encountered: