mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 09:02:43 +00:00 
			
		
		
		
	LibJS/Bytecode: Add Await and AsyncIteratorClose instructions
This commit is contained in:
		
							parent
							
								
									b645f87b7a
								
							
						
					
					
						commit
						d66eb4e3ba
					
				
					 4 changed files with 95 additions and 0 deletions
				
			
		|  | @ -13,6 +13,8 @@ | ||||||
| #define ENUMERATE_BYTECODE_OPS(O)    \ | #define ENUMERATE_BYTECODE_OPS(O)    \ | ||||||
|     O(Add)                           \ |     O(Add)                           \ | ||||||
|     O(Append)                        \ |     O(Append)                        \ | ||||||
|  |     O(AsyncIteratorClose)            \ | ||||||
|  |     O(Await)                         \ | ||||||
|     O(BitwiseAnd)                    \ |     O(BitwiseAnd)                    \ | ||||||
|     O(BitwiseNot)                    \ |     O(BitwiseNot)                    \ | ||||||
|     O(BitwiseOr)                     \ |     O(BitwiseOr)                     \ | ||||||
|  |  | ||||||
|  | @ -1130,12 +1130,15 @@ ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) | ||||||
|     auto yielded_value = interpreter.accumulator().value_or(js_undefined()); |     auto yielded_value = interpreter.accumulator().value_or(js_undefined()); | ||||||
|     auto object = Object::create(interpreter.realm(), nullptr); |     auto object = Object::create(interpreter.realm(), nullptr); | ||||||
|     object->define_direct_property("result", yielded_value, JS::default_attributes); |     object->define_direct_property("result", yielded_value, JS::default_attributes); | ||||||
|  | 
 | ||||||
|     if (m_continuation_label.has_value()) |     if (m_continuation_label.has_value()) | ||||||
|         // FIXME: If we get a pointer, which is not accurately representable as a double
 |         // FIXME: If we get a pointer, which is not accurately representable as a double
 | ||||||
|         //        will cause this to explode
 |         //        will cause this to explode
 | ||||||
|         object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes); |         object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes); | ||||||
|     else |     else | ||||||
|         object->define_direct_property("continuation", Value(0), JS::default_attributes); |         object->define_direct_property("continuation", Value(0), JS::default_attributes); | ||||||
|  | 
 | ||||||
|  |     object->define_direct_property("isAwait", Value(false), JS::default_attributes); | ||||||
|     interpreter.do_return(object); |     interpreter.do_return(object); | ||||||
|     return {}; |     return {}; | ||||||
| } | } | ||||||
|  | @ -1146,6 +1149,25 @@ void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to | ||||||
|         m_continuation_label = Label { to }; |         m_continuation_label = Label { to }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | ThrowCompletionOr<void> Await::execute_impl(Bytecode::Interpreter& interpreter) const | ||||||
|  | { | ||||||
|  |     auto yielded_value = interpreter.accumulator().value_or(js_undefined()); | ||||||
|  |     auto object = Object::create(interpreter.realm(), nullptr); | ||||||
|  |     object->define_direct_property("result", yielded_value, JS::default_attributes); | ||||||
|  |     // FIXME: If we get a pointer, which is not accurately representable as a double
 | ||||||
|  |     //        will cause this to explode
 | ||||||
|  |     object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label.block()))), JS::default_attributes); | ||||||
|  |     object->define_direct_property("isAwait", Value(true), JS::default_attributes); | ||||||
|  |     interpreter.do_return(object); | ||||||
|  |     return {}; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void Await::replace_references_impl(BasicBlock const& from, BasicBlock const& to) | ||||||
|  | { | ||||||
|  |     if (&m_continuation_label.block() == &from) | ||||||
|  |         m_continuation_label = Label { to }; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const | ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const | ||||||
| { | { | ||||||
|     auto& vm = interpreter.vm(); |     auto& vm = interpreter.vm(); | ||||||
|  | @ -1353,6 +1375,17 @@ ThrowCompletionOr<void> IteratorClose::execute_impl(Bytecode::Interpreter& inter | ||||||
|     return {}; |     return {}; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | ThrowCompletionOr<void> AsyncIteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const | ||||||
|  | { | ||||||
|  |     auto& vm = interpreter.vm(); | ||||||
|  |     auto iterator_object = TRY(interpreter.accumulator().to_object(vm)); | ||||||
|  |     auto iterator = object_to_iterator(vm, iterator_object); | ||||||
|  | 
 | ||||||
|  |     // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
 | ||||||
|  |     TRY(async_iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} })); | ||||||
|  |     return {}; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const | ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const | ||||||
| { | { | ||||||
|     auto& vm = interpreter.vm(); |     auto& vm = interpreter.vm(); | ||||||
|  | @ -1806,6 +1839,11 @@ DeprecatedString Yield::to_deprecated_string_impl(Bytecode::Executable const&) c | ||||||
|     return DeprecatedString::formatted("Yield return"); |     return DeprecatedString::formatted("Yield return"); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | DeprecatedString Await::to_deprecated_string_impl(Bytecode::Executable const&) const | ||||||
|  | { | ||||||
|  |     return DeprecatedString::formatted("Await continuation:@{}", m_continuation_label.block().name()); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| DeprecatedString GetByValue::to_deprecated_string_impl(Bytecode::Executable const&) const | DeprecatedString GetByValue::to_deprecated_string_impl(Bytecode::Executable const&) const | ||||||
| { | { | ||||||
|     return DeprecatedString::formatted("GetByValue base:{}", m_base); |     return DeprecatedString::formatted("GetByValue base:{}", m_base); | ||||||
|  | @ -1873,6 +1911,15 @@ DeprecatedString IteratorClose::to_deprecated_string_impl(Bytecode::Executable c | ||||||
|     return DeprecatedString::formatted("IteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string); |     return DeprecatedString::formatted("IteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | DeprecatedString AsyncIteratorClose::to_deprecated_string_impl(Bytecode::Executable const&) const | ||||||
|  | { | ||||||
|  |     if (!m_completion_value.has_value()) | ||||||
|  |         return DeprecatedString::formatted("AsyncIteratorClose completion_type={} completion_value=<empty>", to_underlying(m_completion_type)); | ||||||
|  | 
 | ||||||
|  |     auto completion_value_string = m_completion_value->to_string_without_side_effects().release_value_but_fixme_should_propagate_errors(); | ||||||
|  |     return DeprecatedString::formatted("AsyncIteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| DeprecatedString IteratorNext::to_deprecated_string_impl(Executable const&) const | DeprecatedString IteratorNext::to_deprecated_string_impl(Executable const&) const | ||||||
| { | { | ||||||
|     return "IteratorNext"; |     return "IteratorNext"; | ||||||
|  |  | ||||||
|  | @ -1377,6 +1377,27 @@ private: | ||||||
|     Optional<Label> m_continuation_label; |     Optional<Label> m_continuation_label; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | class Await final : public Instruction { | ||||||
|  | public: | ||||||
|  |     constexpr static bool IsTerminator = true; | ||||||
|  | 
 | ||||||
|  |     explicit Await(Label continuation_label) | ||||||
|  |         : Instruction(Type::Await) | ||||||
|  |         , m_continuation_label(continuation_label) | ||||||
|  |     { | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; | ||||||
|  |     DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; | ||||||
|  |     void replace_references_impl(BasicBlock const&, BasicBlock const&); | ||||||
|  |     void replace_references_impl(Register, Register) { } | ||||||
|  | 
 | ||||||
|  |     auto& continuation() const { return m_continuation_label; } | ||||||
|  | 
 | ||||||
|  | private: | ||||||
|  |     Label m_continuation_label; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| class PushDeclarativeEnvironment final : public Instruction { | class PushDeclarativeEnvironment final : public Instruction { | ||||||
| public: | public: | ||||||
|     explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables) |     explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables) | ||||||
|  | @ -1460,6 +1481,25 @@ private: | ||||||
|     Optional<Value> m_completion_value; |     Optional<Value> m_completion_value; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | class AsyncIteratorClose final : public Instruction { | ||||||
|  | public: | ||||||
|  |     AsyncIteratorClose(Completion::Type completion_type, Optional<Value> completion_value) | ||||||
|  |         : Instruction(Type::AsyncIteratorClose) | ||||||
|  |         , m_completion_type(completion_type) | ||||||
|  |         , m_completion_value(completion_value) | ||||||
|  |     { | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; | ||||||
|  |     DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; | ||||||
|  |     void replace_references_impl(BasicBlock const&, BasicBlock const&) { } | ||||||
|  |     void replace_references_impl(Register, Register) { } | ||||||
|  | 
 | ||||||
|  | private: | ||||||
|  |     Completion::Type m_completion_type { Completion::Type::Normal }; | ||||||
|  |     Optional<Value> m_completion_value; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| class IteratorNext final : public Instruction { | class IteratorNext final : public Instruction { | ||||||
| public: | public: | ||||||
|     IteratorNext() |     IteratorNext() | ||||||
|  |  | ||||||
|  | @ -92,6 +92,12 @@ static void generate_cfg_for_block(BasicBlock const& current_block, PassPipeline | ||||||
|             } |             } | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |         case Await: { | ||||||
|  |             auto const& continuation = static_cast<Op::Await const&>(instruction).continuation(); | ||||||
|  |             executable.exported_blocks->set(&continuation.block()); | ||||||
|  |             enter_label(continuation, current_block); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|         case EnterUnwindContext: { |         case EnterUnwindContext: { | ||||||
|             auto entry_point = static_cast<Op::EnterUnwindContext const&>(instruction).entry_point(); |             auto entry_point = static_cast<Op::EnterUnwindContext const&>(instruction).entry_point(); | ||||||
|             auto handler_target = static_cast<Op::EnterUnwindContext const&>(instruction).handler_target(); |             auto handler_target = static_cast<Op::EnterUnwindContext const&>(instruction).handler_target(); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Luke Wilde
						Luke Wilde