@@ -479,4 +479,144 @@ describe("dereference", () => {
479479 expect ( regions [ 1 ] . name ) . toEqual ( "outer-data" ) ;
480480 expect ( regions . named ( "outer-data" ) ) . toHaveLength ( 2 ) ;
481481 } ) ;
482+
483+ it ( "works for inline template definitions" , async ( ) => {
484+ const pointer : Pointer = {
485+ templates : {
486+ "memory-range" : {
487+ expect : [ "offset" , "length" ] ,
488+ for : {
489+ location : "memory" ,
490+ offset : "offset" ,
491+ length : "length"
492+ }
493+ }
494+ } ,
495+ in : {
496+ define : {
497+ "offset" : 0 ,
498+ "length" : 32
499+ } ,
500+ in : {
501+ template : "memory-range"
502+ }
503+ }
504+ } ;
505+
506+ const cursor = await dereference ( pointer ) ;
507+ const { regions } = await cursor . view ( state ) ;
508+
509+ expect ( regions ) . toHaveLength ( 1 ) ;
510+ expect ( regions [ 0 ] . offset ) . toEqual ( Data . fromNumber ( 0 ) ) ;
511+ expect ( regions [ 0 ] . length ) . toEqual ( Data . fromNumber ( 32 ) ) ;
512+ } ) ;
513+
514+ it ( "inline templates take precedence over external templates" , async ( ) => {
515+ // External template defines a slot-based region
516+ const externalTemplates : Pointer . Templates = {
517+ "my-region" : {
518+ expect : [ "value" ] ,
519+ for : {
520+ name : "external" ,
521+ location : "storage" ,
522+ slot : "value"
523+ }
524+ }
525+ } ;
526+
527+ // Inline template overrides with memory-based region
528+ const pointer : Pointer = {
529+ templates : {
530+ "my-region" : {
531+ expect : [ "value" ] ,
532+ for : {
533+ name : "inline" ,
534+ location : "memory" ,
535+ offset : "value" ,
536+ length : 32
537+ }
538+ }
539+ } ,
540+ in : {
541+ define : { "value" : 64 } ,
542+ in : {
543+ template : "my-region"
544+ }
545+ }
546+ } ;
547+
548+ const cursor = await dereference ( pointer , { templates : externalTemplates } ) ;
549+ const { regions } = await cursor . view ( state ) ;
550+
551+ expect ( regions ) . toHaveLength ( 1 ) ;
552+ expect ( regions [ 0 ] . name ) . toEqual ( "inline" ) ;
553+ expect ( regions [ 0 ] . location ) . toEqual ( "memory" ) ;
554+ } ) ;
555+
556+ it ( "works for nested inline templates" , async ( ) => {
557+ const pointer : Pointer = {
558+ templates : {
559+ "outer-template" : {
560+ expect : [ "base" ] ,
561+ for : {
562+ templates : {
563+ "inner-template" : {
564+ expect : [ "offset" ] ,
565+ for : {
566+ name : "data" ,
567+ location : "memory" ,
568+ offset : "offset" ,
569+ length : 32
570+ }
571+ }
572+ } ,
573+ in : {
574+ define : { "offset" : "base" } ,
575+ in : { template : "inner-template" }
576+ }
577+ }
578+ }
579+ } ,
580+ in : {
581+ define : { "base" : 128 } ,
582+ in : { template : "outer-template" }
583+ }
584+ } ;
585+
586+ const cursor = await dereference ( pointer ) ;
587+ const { regions } = await cursor . view ( state ) ;
588+
589+ expect ( regions ) . toHaveLength ( 1 ) ;
590+ expect ( regions [ 0 ] . name ) . toEqual ( "data" ) ;
591+ expect ( regions [ 0 ] . offset ) . toEqual ( Data . fromNumber ( 128 ) ) ;
592+ } ) ;
593+
594+ it ( "inline templates with yields work correctly" , async ( ) => {
595+ const pointer : Pointer = {
596+ templates : {
597+ "named-slot" : {
598+ expect : [ "slot" ] ,
599+ for : {
600+ name : "value" ,
601+ location : "storage" ,
602+ slot : "slot"
603+ }
604+ }
605+ } ,
606+ in : {
607+ define : { "slot" : 5 } ,
608+ in : {
609+ template : "named-slot" ,
610+ yields : { "value" : "my-slot-value" }
611+ }
612+ }
613+ } ;
614+
615+ const cursor = await dereference ( pointer ) ;
616+ const { regions } = await cursor . view ( state ) ;
617+
618+ expect ( regions ) . toHaveLength ( 1 ) ;
619+ expect ( regions [ 0 ] . name ) . toEqual ( "my-slot-value" ) ;
620+ expect ( regions . lookup [ "my-slot-value" ] ) . toBeDefined ( ) ;
621+ } ) ;
482622} ) ;
0 commit comments