feat(builtins): implement JsAsyncGenerator safe wrapper#4755
feat(builtins): implement JsAsyncGenerator safe wrapper#4755jedel1043 merged 4 commits intoboa-dev:mainfrom
Conversation
Test262 conformance changes
Tested PR commit: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4755 +/- ##
==========================================
+ Coverage 47.24% 57.18% +9.93%
==========================================
Files 476 554 +78
Lines 46892 60654 +13762
==========================================
+ Hits 22154 34682 +12528
- Misses 24738 25972 +1234 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| pub fn next<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue> | ||
| where | ||
| T: Into<JsValue>, | ||
| { | ||
| AsyncGenerator::next(&self.inner.clone().into(), &[value.into()], context) | ||
| } | ||
|
|
||
| /// Calls `AsyncGenerator.prototype.return()`. | ||
| /// | ||
| /// More information: | ||
| /// - [MDN documentation][mdn] | ||
| /// | ||
| /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator/return | ||
| pub fn r#return<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue> | ||
| where | ||
| T: Into<JsValue>, | ||
| { | ||
| AsyncGenerator::r#return(&self.inner.clone().into(), &[value.into()], context) | ||
| } | ||
|
|
||
| /// Calls `AsyncGenerator.prototype.throw()`. | ||
| /// | ||
| /// More information: | ||
| /// - [MDN documentation][mdn] | ||
| /// | ||
| /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator/throw | ||
| pub fn throw<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue> | ||
| where | ||
| T: Into<JsValue>, | ||
| { | ||
| AsyncGenerator::throw(&self.inner.clone().into(), &[value.into()], context) | ||
| } |
There was a problem hiding this comment.
All of these should return JsResult<JsPromise> to follow the protocol of AsyncGenerator
There was a problem hiding this comment.
yup i was waiting for that one, i try to handle it same way #4756 was handled , i made changes have a look @jedel1043 ?
| let async_gen = JsAsyncGenerator::from_object(obj).unwrap(); | ||
|
|
||
| // next() returns a Promise | ||
| let promise = async_gen.next(JsValue::undefined(), &mut context).unwrap(); |
There was a problem hiding this comment.
What you could do here is to call context.run_jobs() to resolve the promise, then you should be able to access the value 1 from its state.
There was a problem hiding this comment.
That way we at least show that promises can only go forward if you call context.run_jobs()
There was a problem hiding this comment.
yup let me handle this quickly
There was a problem hiding this comment.
@jedel1043 i made some changes u suggested me , have a look at it
examples/src/bin/jsasyncgenerator.rs
Outdated
| let promise = async_gen.next(JsValue::undefined(), &mut context).unwrap(); | ||
| drop(context.run_jobs()); | ||
| if let PromiseState::Fulfilled(val) = promise.state() { | ||
| println!("next resolved with: {}", val.display()); |
There was a problem hiding this comment.
Would be kinda better to assert_eq with a JsValue::from(1) to test that the function returns the correct value.
There was a problem hiding this comment.
Hi @jedel1043, updated the example to use assert_eq! to verify the resolved promise values — JsValue::from(1) for next and JsValue::from(42) for return. The assertions pass cleanly. is it what you mean to test the function or any other way ?
Implements the
JsAsyncGeneratorsafe Rust wrapper as part of #2098.Adds
core/engine/src/object/builtins/jsasyncgenerator.rswith:from_object(object: JsObject) -> JsResult<Self>next(value, context) -> JsResult<JsValue>return(value, context) -> JsResult<JsValue>throw(value, context) -> JsResult<JsValue>From<JsAsyncGenerator>forJsObjectandJsValueDereftoJsObjectTryFromJsimplFollows the same pattern as
JsGenerator.