22
33use std:: collections:: { HashMap , HashSet } ;
44
5- use arraystring:: { typenum:: U255 , ArrayString } ;
65use baml_types:: { ir_type:: TypeIR , BamlMap , BamlMediaType , BamlValueWithMeta , TypeValue } ;
76use baml_vm:: {
87 BamlVmProgram , BinOp , Bytecode , Class , CmpOp , Enum , Function , FunctionKind , GlobalIndex ,
@@ -152,6 +151,7 @@ fn compile_thir_to_bytecode(
152151 kind : FunctionKind :: Llm ,
153152 locals_in_scope : vec ! [ func. parameters. iter( ) . map( |p| p. name. clone( ) ) . collect( ) ] ,
154153 span : func. span . clone ( ) ,
154+ block_notifications : Vec :: new ( ) ,
155155 } ) ;
156156
157157 let object_index = objects. insert ( bytecode_llm_function) ;
@@ -235,6 +235,7 @@ fn compile_thir_to_bytecode(
235235 kind : FunctionKind :: Native ( func) ,
236236 locals_in_scope : vec ! [ ] , // TODO.
237237 span : Span :: fake_builtin_baml ( ) ,
238+ block_notifications : Vec :: new ( ) ,
238239 } ) ;
239240
240241 let object_index = objects. insert ( native_function) ;
@@ -247,6 +248,7 @@ fn compile_thir_to_bytecode(
247248 kind : FunctionKind :: Future ,
248249 locals_in_scope : vec ! [ ] ,
249250 span : Span :: fake_builtin_baml ( ) ,
251+ block_notifications : Vec :: new ( ) ,
250252 } ) ) ) ) ;
251253
252254 let mut resolved_class_names = HashMap :: new ( ) ;
@@ -431,6 +433,9 @@ struct HirCompiler<'g> {
431433 /// `AllocInstance` instructions that have a placeholder, which must be resolved when location
432434 /// of the class object is resolved.
433435 class_alloc_patch_list : & ' g mut Vec < AllocInstancePatch > ,
436+
437+ /// Block notifications for the current function being compiled.
438+ block_notifications : Vec < baml_vm:: bytecode:: BlockNotification > ,
434439}
435440
436441#[ derive( Debug ) ]
@@ -470,6 +475,7 @@ impl<'g> HirCompiler<'g> {
470475 scopes : Vec :: new ( ) ,
471476 current_source_line : 0 ,
472477 locals_in_scope : Vec :: new ( ) ,
478+ block_notifications : Vec :: new ( ) ,
473479 }
474480 }
475481
@@ -508,6 +514,7 @@ impl<'g> HirCompiler<'g> {
508514 } ) ) ,
509515
510516 span : func. span . clone ( ) ,
517+ block_notifications : self . block_notifications . clone ( ) ,
511518 } )
512519 }
513520
@@ -1585,18 +1592,24 @@ impl<'g> HirCompiler<'g> {
15851592 self . emit ( Instruction :: LoadConst ( const_index) ) ;
15861593 }
15871594
1588- fn emit_annotated_block ( & mut self , v : & str ) {
1589- self . emit_string_literal ( v) ;
1595+ fn emit_annotated_block ( & mut self , annotation : & str ) {
1596+ // Create the notification metadata
1597+ let notification = baml_vm:: bytecode:: BlockNotification {
1598+ function_name : String :: new ( ) , // Will be populated at runtime from Function::name
1599+ block_name : annotation. to_string ( ) ,
1600+ level : self . scopes . len ( ) , // Current scope depth (1-based)
1601+ block_type : baml_vm:: bytecode:: BlockNotificationType :: Statement ,
1602+ is_enter : true ,
1603+ } ;
15901604
1591- self . emit ( Instruction :: NotifyBlock (
1592- baml_vm:: bytecode:: BlockNotification {
1593- function_name : ArrayString :: < U255 > :: from_str_truncate ( v) ,
1594- block_name : ArrayString :: < U255 > :: from_str_truncate ( v) ,
1595- level : 1 ,
1596- block_type : baml_vm:: bytecode:: BlockNotificationType :: Statement ,
1597- is_enter : true ,
1598- } ,
1599- ) ) ;
1605+ // Add to the function's notification list
1606+ let notification_index = self . block_notifications . len ( ) ;
1607+ self . block_notifications . push ( notification) ;
1608+
1609+ // Emit instruction with just the index
1610+ self . emit ( Instruction :: NotifyBlock ( notification_index) ) ;
1611+
1612+ // TODO: Emit exit notification when leaving the block
16001613 }
16011614
16021615 /// Emits a single instruction and returns the index of the instruction.
0 commit comments