@@ -52,6 +52,10 @@ class RubyToBlocksConverter {
5252 return this . _context . lists ;
5353 }
5454
55+ get broadcastMsgs ( ) {
56+ return this . _context . broadcastMsgs ;
57+ }
58+
5559 reset ( ) {
5660 this . _context = {
5761 currentNode : null ,
@@ -64,6 +68,7 @@ class RubyToBlocksConverter {
6468 localVariables : { } ,
6569 variables : { } ,
6670 lists : { } ,
71+ broadcastMsgs : { } ,
6772 procedures : { }
6873 } ;
6974 if ( this . vm && this . vm . runtime && this . vm . runtime . getTargetForStage ) {
@@ -73,6 +78,7 @@ class RubyToBlocksConverter {
7378
7479 targetCodeToBlocks ( target , code ) {
7580 this . reset ( ) ;
81+ this . _setTarget ( target ) ;
7682 this . _loadVariables ( target ) ;
7783 try {
7884 const root = RubyParser . $parse ( code ) ;
@@ -139,6 +145,13 @@ class RubyToBlocksConverter {
139145 } ) ;
140146 } ) ;
141147
148+ Object . keys ( this . _context . broadcastMsgs ) . forEach ( name => {
149+ const broadcastMsg = this . _context . broadcastMsgs [ name ] ;
150+ if ( ! stage . variables . hasOwnProperty ( broadcastMsg . id ) ) {
151+ stage . createVariable ( broadcastMsg . id , broadcastMsg . name , Variable . BROADCAST_MESSAGE_TYPE ) ;
152+ }
153+ } ) ;
154+
142155 Object . keys ( target . blocks . _blocks ) . forEach ( blockId => {
143156 target . blocks . deleteBlock ( blockId ) ;
144157 } ) ;
@@ -169,6 +182,7 @@ class RubyToBlocksConverter {
169182 'localVariables' ,
170183 'variables' ,
171184 'lists' ,
185+ 'broadcastMsgs' ,
172186 'procedures'
173187 ] ;
174188
@@ -202,6 +216,10 @@ class RubyToBlocksConverter {
202216 } ) ;
203217 }
204218
219+ _setTarget ( target ) {
220+ this . _context . target = target ;
221+ }
222+
205223 _loadVariables ( target ) {
206224 if ( ! target || ! target . variables ) {
207225 return ;
@@ -217,6 +235,8 @@ class RubyToBlocksConverter {
217235 let storeName ;
218236 if ( variable . type === Variable . SCALAR_TYPE ) {
219237 storeName = 'variables' ;
238+ } else if ( variable . type === Variable . BROADCAST_MESSAGE_TYPE ) {
239+ storeName = 'broadcastMsgs' ;
220240 } else {
221241 storeName = 'lists' ;
222242 }
@@ -394,14 +414,14 @@ class RubyToBlocksConverter {
394414 return block ;
395415 }
396416
397- _addField ( block , name , value ) {
417+ _addField ( block , name , value , attributes = { } ) {
398418 if ( ! this . _isBlock ( block ) ) {
399419 return ;
400420 }
401- block . fields [ name ] = {
421+ block . fields [ name ] = Object . assign ( {
402422 name : name ,
403423 value : value . toString ( )
404- } ;
424+ } , attributes ) ;
405425 }
406426
407427 _addInput ( block , name , inputBlock , shadowBlock ) {
@@ -456,7 +476,7 @@ class RubyToBlocksConverter {
456476 } ;
457477 }
458478
459- _findOrCreateVariableOrList ( name , type ) {
479+ _lookupOrCreateVariableOrList ( name , type ) {
460480 name = name . toString ( ) ;
461481 let scope ;
462482 let varName ;
@@ -493,15 +513,42 @@ class RubyToBlocksConverter {
493513 return variable ;
494514 }
495515
496- _findOrCreateVariable ( name ) {
497- return this . _findOrCreateVariableOrList ( name , Variable . SCALAR_TYPE ) ;
516+ _lookupOrCreateVariable ( name ) {
517+ return this . _lookupOrCreateVariableOrList ( name , Variable . SCALAR_TYPE ) ;
498518 }
499519
500- _findOrCreateList ( name ) {
501- return this . _findOrCreateVariableOrList ( name , Variable . LIST_TYPE ) ;
520+ _lookupOrCreateList ( name ) {
521+ return this . _lookupOrCreateVariableOrList ( name , Variable . LIST_TYPE ) ;
522+ }
523+
524+ _lookupOrCreateBroadcastMsg ( name ) {
525+ name = name . toString ( ) ;
526+ const key = name . toLowerCase ( ) ;
527+ let broadcastMsg = this . _context . broadcastMsgs [ key ] ;
528+ if ( ! broadcastMsg ) {
529+ broadcastMsg = {
530+ id : Blockly . utils . genUid ( ) ,
531+ name : name ,
532+ scope : 'global'
533+ } ;
534+ this . _context . broadcastMsgs [ key ] = broadcastMsg ;
535+ }
536+ return broadcastMsg ;
537+ }
538+
539+ _defaultBroadcastMsg ( ) {
540+ const defaultName = 'message1' ;
541+ const keys = Object . keys ( this . _context . broadcastMsgs ) ;
542+ if ( keys . length === 0 ) {
543+ return this . _lookupOrCreateBroadcastMsg ( defaultName ) ;
544+ }
545+ if ( this . _context . broadcastMsgs . hasOwnProperty ( defaultName ) ) {
546+ return this . _context . broadcastMsgs [ defaultName ] ;
547+ }
548+ return this . _context . broadcastMsgs [ keys [ 0 ] ] ;
502549 }
503550
504- _findProcedure ( name ) {
551+ _lookupProcedure ( name ) {
505552 name = name . toString ( ) ;
506553 return this . _context . procedures [ name ] ;
507554 }
@@ -931,7 +978,7 @@ class RubyToBlocksConverter {
931978 _onVar ( node , scope ) {
932979 this . _checkNumChildren ( node , 1 ) ;
933980
934- const variable = this . _findOrCreateVariable ( node . children [ 0 ] ) ;
981+ const variable = this . _lookupOrCreateVariable ( node . children [ 0 ] ) ;
935982 const block = this . _callConvertersHandler ( 'onVar' , scope , variable ) ;
936983 if ( block ) {
937984 return block ;
@@ -972,7 +1019,7 @@ class RubyToBlocksConverter {
9721019
9731020 const saved = this . _saveContext ( ) ;
9741021
975- const variable = this . _findOrCreateVariable ( node . children [ 0 ] ) ;
1022+ const variable = this . _lookupOrCreateVariable ( node . children [ 0 ] ) ;
9761023 const rh = this . _process ( node . children [ 1 ] ) ;
9771024 let block = this . _callConvertersHandler ( 'onVasgn' , scope , variable , rh ) ;
9781025 if ( ! block ) {
0 commit comments