@kubohiroya/turbowarp-named-functions 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +148 -6
- package/README.md +190 -15
- package/dist/composition.js +371 -25
- package/dist/extension-manifest.json +144 -0
- package/dist/turbowarp-named-functions.js +1476 -904
- package/dist/types/block-literals.d.ts +16 -0
- package/dist/types/composition.d.ts +11 -1
- package/dist/types/extension.d.ts +17 -1
- package/dist/types/function-registry.d.ts +8 -1
- package/dist/types/runtime-types.d.ts +2 -0
- package/dist/types/schema-builder.d.ts +52 -0
- package/package.json +6 -6
- package/src/block-definitions.json +199 -2
- package/src/block-literals.ts +62 -0
- package/src/composition.ts +40 -5
- package/src/extension.ts +64 -2
- package/src/function-registry.ts +43 -32
- package/src/runtime-types.ts +2 -0
- package/src/schema-builder.ts +458 -0
|
@@ -7,909 +7,1481 @@
|
|
|
7
7
|
(function (Scratch) {
|
|
8
8
|
'use strict';
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
10
|
+
//#region src/config.ts
|
|
11
|
+
var extensionConfig = {
|
|
12
|
+
id: "kubohiroyanamedfunctions",
|
|
13
|
+
slug: "turbowarp-named-functions",
|
|
14
|
+
name: "TurboWarp-Named-Functions",
|
|
15
|
+
description: "Named JSON functions that scripts can call by name, synchronously or asynchronously.",
|
|
16
|
+
author: "Hiroya Kubo",
|
|
17
|
+
license: "MPL-2.0",
|
|
18
|
+
unsandboxed: true,
|
|
19
|
+
docsURI: "https://kubohiroya.github.io/turbowarp-named-functions/",
|
|
20
|
+
blockIconURI: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0OCA0OCI+PHJlY3QgeD0iNCIgeT0iOCIgd2lkdGg9IjQwIiBoZWlnaHQ9IjMyIiByeD0iNiIgZmlsbD0iIzhBNTVENyIvPjx0ZXh0IHg9IjI0IiB5PSIzMSIgZm9udC1mYW1pbHk9Im1vbm9zcGFjZSIgZm9udC1zaXplPSIxNiIgZm9udC13ZWlnaHQ9ImJvbGQiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IiNmZmYiPmYoeCk8L3RleHQ+PC9zdmc+"
|
|
21
|
+
};
|
|
22
|
+
var block_editor_fixed_inputs_schema_default = {
|
|
23
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
24
|
+
$id: "https://kubohiroya.github.io/turbowarp-named-functions/schemas/block-editor-fixed-inputs.schema.json",
|
|
25
|
+
$defs: { "defineFunction": {
|
|
26
|
+
"type": "object",
|
|
27
|
+
"properties": { "EXPORT": {
|
|
28
|
+
"type": "string",
|
|
29
|
+
"enum": ["none", "tool"]
|
|
30
|
+
} },
|
|
31
|
+
"required": ["EXPORT"],
|
|
32
|
+
"additionalProperties": true
|
|
33
|
+
} }
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/fixed-input-schema.ts
|
|
37
|
+
var source = {
|
|
38
|
+
repository: "turbowarp-named-functions",
|
|
39
|
+
path: "schemas/block-editor-fixed-inputs.schema.json",
|
|
40
|
+
symbol: "#/$defs/defineFunction/properties/EXPORT"
|
|
41
|
+
};
|
|
42
|
+
/** The on-disk owner Schema is canonical; this is its single-field projection. */
|
|
43
|
+
var fixedInputSchemaBundle = {
|
|
44
|
+
identity: {
|
|
45
|
+
ownerId: "turbowarp-named-functions",
|
|
46
|
+
contractId: "defineFunction.EXPORT",
|
|
47
|
+
revision: "fixed-inputs-v1",
|
|
48
|
+
variantId: "default"
|
|
49
|
+
},
|
|
50
|
+
dialect: "2020-12",
|
|
51
|
+
root: {
|
|
52
|
+
retrievalUri: "https://kubohiroya.github.io/turbowarp-named-functions/schemas/defineFunction-EXPORT.schema.json",
|
|
53
|
+
document: block_editor_fixed_inputs_schema_default.$defs.defineFunction.properties.EXPORT,
|
|
54
|
+
artifact: {
|
|
55
|
+
role: "canonical-schema",
|
|
56
|
+
source
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
resources: [],
|
|
60
|
+
provenance: {
|
|
61
|
+
canonicalSources: [source, {
|
|
62
|
+
repository: "turbowarp-named-functions",
|
|
63
|
+
path: "src/function-registry.ts",
|
|
64
|
+
symbol: "readDefinition"
|
|
65
|
+
}],
|
|
66
|
+
generatedFiles: [{
|
|
67
|
+
repository: "turbowarp-named-functions",
|
|
68
|
+
path: "site/schemas/block-editor-fixed-inputs.schema.json"
|
|
69
|
+
}],
|
|
70
|
+
referenceSchemas: [],
|
|
71
|
+
generationSources: [{
|
|
72
|
+
repository: "turbowarp-named-functions",
|
|
73
|
+
path: "scripts/generate-fixed-input-docs.mjs"
|
|
74
|
+
}]
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/fixed-input-editors.ts
|
|
79
|
+
var choices = ["none", "tool"];
|
|
80
|
+
var fixedInputEditorDefinitions = [{
|
|
81
|
+
apiVersion: 1,
|
|
82
|
+
extensionId: extensionConfig.id,
|
|
83
|
+
editorId: "defineFunction-fixed-inputs",
|
|
84
|
+
opcodes: [`${extensionConfig.id}_defineFunction`],
|
|
85
|
+
contractId: "turbowarp-named-functions:defineFunction.EXPORT",
|
|
86
|
+
contractRevision: "fixed-inputs-v1",
|
|
87
|
+
bindings: [{
|
|
88
|
+
argument: "EXPORT",
|
|
89
|
+
schemaBundle: fixedInputSchemaBundle,
|
|
90
|
+
control: {
|
|
91
|
+
kind: "enum",
|
|
92
|
+
label: "EXPORT",
|
|
93
|
+
choices: [{
|
|
94
|
+
"value": "none",
|
|
95
|
+
"label": "none"
|
|
96
|
+
}, {
|
|
97
|
+
"value": "tool",
|
|
98
|
+
"label": "tool"
|
|
99
|
+
}]
|
|
100
|
+
},
|
|
101
|
+
read(value) {
|
|
102
|
+
if (typeof value !== "string") throw new TypeError("Expected a literal export mode.");
|
|
103
|
+
return value;
|
|
104
|
+
},
|
|
105
|
+
encode(value) {
|
|
106
|
+
if (typeof value !== "string" || !choices.includes(value)) throw new TypeError("Unsupported export mode.");
|
|
107
|
+
return value;
|
|
108
|
+
},
|
|
109
|
+
validateCandidate(value) {
|
|
110
|
+
return typeof value === "string" && choices.includes(value) ? [] : [{
|
|
111
|
+
path: "/inputs/EXPORT",
|
|
112
|
+
message: "Unsupported export mode."
|
|
113
|
+
}];
|
|
114
|
+
}
|
|
115
|
+
}],
|
|
116
|
+
validationContext: () => ({
|
|
117
|
+
consumerId: extensionConfig.id,
|
|
118
|
+
nowMs: Date.now(),
|
|
119
|
+
settings: null
|
|
120
|
+
})
|
|
121
|
+
}];
|
|
122
|
+
/** Register metadata only; the GUI startup capability owns editor activation. */
|
|
123
|
+
function registerFixedInputEditors(scratch, definitions, runtimeOverride) {
|
|
124
|
+
const host = scratch;
|
|
125
|
+
const api = host.gui?.blockEditors;
|
|
126
|
+
if (!definitions.length || api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== "function") return () => {};
|
|
127
|
+
const runtime = runtimeOverride ?? host.vm?.runtime;
|
|
128
|
+
const removers = [];
|
|
129
|
+
let active = true;
|
|
130
|
+
const dispose = () => {
|
|
131
|
+
if (!active) return;
|
|
132
|
+
active = false;
|
|
133
|
+
runtime?.off?.("RUNTIME_DISPOSED", dispose);
|
|
134
|
+
for (const remove of removers.splice(0).reverse()) remove();
|
|
135
|
+
};
|
|
136
|
+
try {
|
|
137
|
+
for (const definition of definitions) {
|
|
138
|
+
const remove = api.registerFixedInputEditor.call(api, definition);
|
|
139
|
+
if (typeof remove !== "function") throw new TypeError("Invalid fixed input registration disposer.");
|
|
140
|
+
removers.push(remove);
|
|
141
|
+
}
|
|
142
|
+
runtime?.on?.("RUNTIME_DISPOSED", dispose);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
dispose();
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
return dispose;
|
|
148
|
+
}
|
|
149
|
+
var block_definitions_default = {
|
|
150
|
+
extensionName: "Named Functions",
|
|
151
|
+
blocks: [
|
|
152
|
+
{
|
|
153
|
+
"opcode": "defineFunction",
|
|
154
|
+
"blockType": "HAT",
|
|
155
|
+
"text": "define function [NAME] description [DESCRIPTION] args schema [SCHEMA] returns schema [RETURNS] export as [EXPORT]",
|
|
156
|
+
"description": "Defines a named function. NAME, DESCRIPTION, and SCHEMA must be literal text. SCHEMA and RETURNS each take JSON Schema text or the name of a `define schema` hat, optionally pinned as Name@1.0.0. RETURNS is optional; leave it blank to disable return validation.",
|
|
157
|
+
"descriptionJa": "名前を持つ関数を定義します。NAME、DESCRIPTION、SCHEMAには文字列を直接書く必要があります。SCHEMAとRETURNSには、JSON Schema文字列、または`define schema`ハットの名前(Name@1.0.0の形でバージョン固定も可)を書けます。RETURNSは任意で、空欄なら戻り値を検証しません。",
|
|
158
|
+
"arguments": {
|
|
159
|
+
"NAME": {
|
|
160
|
+
"type": "STRING",
|
|
161
|
+
"defaultValue": "add"
|
|
162
|
+
},
|
|
163
|
+
"DESCRIPTION": {
|
|
164
|
+
"type": "STRING",
|
|
165
|
+
"defaultValue": "Adds two numbers."
|
|
166
|
+
},
|
|
167
|
+
"SCHEMA": {
|
|
168
|
+
"type": "STRING",
|
|
169
|
+
"defaultValue": "{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"number\"},\"b\":{\"type\":\"number\"}},\"required\":[\"a\",\"b\"]}"
|
|
170
|
+
},
|
|
171
|
+
"RETURNS": {
|
|
172
|
+
"type": "STRING",
|
|
173
|
+
"defaultValue": ""
|
|
174
|
+
},
|
|
175
|
+
"EXPORT": {
|
|
176
|
+
"type": "STRING",
|
|
177
|
+
"defaultValue": "none",
|
|
178
|
+
"menu": "exportModes"
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
"opcode": "defineSchema",
|
|
184
|
+
"blockType": "HAT",
|
|
185
|
+
"text": "define schema [NAME] version [VERSION]",
|
|
186
|
+
"description": "Declares a named JSON Schema from the property blocks stacked under it. NAME and VERSION must be literal text, and VERSION must be a complete SemVer value. The stack is read as a declaration and never run; the result is the same canonical JSON Schema that schema text produces.",
|
|
187
|
+
"descriptionJa": "下に積まれたpropertyブロックから、名前付きJSON Schemaを宣言します。NAMEとVERSIONには文字列を直接書く必要があり、VERSIONは完全なSemVer値でなければなりません。この積み木は宣言として読まれるだけで実行されず、結果はSchema文字列と同じcanonical JSON Schemaになります。",
|
|
188
|
+
"arguments": {
|
|
189
|
+
"NAME": {
|
|
190
|
+
"type": "STRING",
|
|
191
|
+
"defaultValue": "Player"
|
|
192
|
+
},
|
|
193
|
+
"VERSION": {
|
|
194
|
+
"type": "STRING",
|
|
195
|
+
"defaultValue": "1.0.0"
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
"opcode": "schemaProperty",
|
|
201
|
+
"blockType": "COMMAND",
|
|
202
|
+
"text": "property [NAME] type [TYPE] [PRESENCE]",
|
|
203
|
+
"description": "Inside a `define schema` stack, declares one property of the schema.",
|
|
204
|
+
"descriptionJa": "`define schema`の積み木の中で、Schemaのプロパティを1つ宣言します。",
|
|
205
|
+
"arguments": {
|
|
206
|
+
"NAME": {
|
|
207
|
+
"type": "STRING",
|
|
208
|
+
"defaultValue": "name"
|
|
209
|
+
},
|
|
210
|
+
"TYPE": {
|
|
211
|
+
"type": "STRING",
|
|
212
|
+
"defaultValue": "string",
|
|
213
|
+
"menu": "propertyTypes"
|
|
214
|
+
},
|
|
215
|
+
"PRESENCE": {
|
|
216
|
+
"type": "STRING",
|
|
217
|
+
"defaultValue": "required",
|
|
218
|
+
"menu": "presenceModes"
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
"opcode": "schemaPropertySchema",
|
|
224
|
+
"blockType": "COMMAND",
|
|
225
|
+
"text": "property [NAME] schema [SCHEMA] [PRESENCE]",
|
|
226
|
+
"description": "Declares a property whose value follows another named schema. SCHEMA is a `define schema` name, optionally pinned as Name@1.0.0.",
|
|
227
|
+
"descriptionJa": "別の名前付きSchemaに従う値を持つプロパティを宣言します。SCHEMAには`define schema`の名前を書き、Name@1.0.0の形でバージョンを固定できます。",
|
|
228
|
+
"arguments": {
|
|
229
|
+
"NAME": {
|
|
230
|
+
"type": "STRING",
|
|
231
|
+
"defaultValue": "profile"
|
|
232
|
+
},
|
|
233
|
+
"SCHEMA": {
|
|
234
|
+
"type": "STRING",
|
|
235
|
+
"defaultValue": "Profile"
|
|
236
|
+
},
|
|
237
|
+
"PRESENCE": {
|
|
238
|
+
"type": "STRING",
|
|
239
|
+
"defaultValue": "required",
|
|
240
|
+
"menu": "presenceModes"
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
"opcode": "schemaPropertyArraySchema",
|
|
246
|
+
"blockType": "COMMAND",
|
|
247
|
+
"text": "property [NAME] array of schema [SCHEMA] [PRESENCE]",
|
|
248
|
+
"description": "Declares a property holding an array whose items follow another named schema.",
|
|
249
|
+
"descriptionJa": "項目が別の名前付きSchemaに従う配列を持つプロパティを宣言します。",
|
|
250
|
+
"arguments": {
|
|
251
|
+
"NAME": {
|
|
252
|
+
"type": "STRING",
|
|
253
|
+
"defaultValue": "items"
|
|
254
|
+
},
|
|
255
|
+
"SCHEMA": {
|
|
256
|
+
"type": "STRING",
|
|
257
|
+
"defaultValue": "Item"
|
|
258
|
+
},
|
|
259
|
+
"PRESENCE": {
|
|
260
|
+
"type": "STRING",
|
|
261
|
+
"defaultValue": "optional",
|
|
262
|
+
"menu": "presenceModes"
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
"opcode": "schemaConstraint",
|
|
268
|
+
"blockType": "COMMAND",
|
|
269
|
+
"text": "limit [KEYWORD] to [VALUE]",
|
|
270
|
+
"description": "Adds a limit to the property declared just above. `minItems` and `maxItems` apply to an array property itself; the other limits apply to each of its items.",
|
|
271
|
+
"descriptionJa": "すぐ上で宣言したプロパティに制約を加えます。`minItems`と`maxItems`は配列そのものに、その他の制約は配列の各項目に適用されます。",
|
|
272
|
+
"arguments": {
|
|
273
|
+
"KEYWORD": {
|
|
274
|
+
"type": "STRING",
|
|
275
|
+
"defaultValue": "minimum",
|
|
276
|
+
"menu": "constraintKeywords"
|
|
277
|
+
},
|
|
278
|
+
"VALUE": {
|
|
279
|
+
"type": "NUMBER",
|
|
280
|
+
"defaultValue": "0"
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
"opcode": "schemaAllowedValues",
|
|
286
|
+
"blockType": "COMMAND",
|
|
287
|
+
"text": "allowed values [VALUES]",
|
|
288
|
+
"description": "Restricts the property declared just above to an enum. VALUES is a JSON array, or a comma-separated list of strings.",
|
|
289
|
+
"descriptionJa": "すぐ上で宣言したプロパティの値をenumに制限します。VALUESにはJSON配列、またはカンマ区切りの文字列を書きます。",
|
|
290
|
+
"arguments": { "VALUES": {
|
|
291
|
+
"type": "STRING",
|
|
292
|
+
"defaultValue": "red, green, blue"
|
|
293
|
+
} }
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
"opcode": "schemaAdditionalProperties",
|
|
297
|
+
"blockType": "COMMAND",
|
|
298
|
+
"text": "allow undeclared properties [ALLOW]",
|
|
299
|
+
"description": "Sets whether the schema accepts properties it does not declare. Left out, undeclared properties are accepted, as in JSON Schema.",
|
|
300
|
+
"descriptionJa": "宣言していないプロパティをSchemaが受け入れるかどうかを設定します。省略した場合は、JSON Schemaと同じく受け入れます。",
|
|
301
|
+
"arguments": { "ALLOW": {
|
|
302
|
+
"type": "STRING",
|
|
303
|
+
"defaultValue": "no",
|
|
304
|
+
"menu": "allowModes"
|
|
305
|
+
} }
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
"opcode": "schemaJson",
|
|
309
|
+
"blockType": "REPORTER",
|
|
310
|
+
"text": "schema [NAME] JSON",
|
|
311
|
+
"description": "Reports the canonical JSON Schema a `define schema` hat lowers to, or an empty string if it is unknown or invalid.",
|
|
312
|
+
"descriptionJa": "`define schema`ハットが変換されたcanonical JSON Schemaを返します。未定義または不正な場合は空文字列を返します。",
|
|
313
|
+
"arguments": { "NAME": {
|
|
314
|
+
"type": "STRING",
|
|
315
|
+
"defaultValue": "Player"
|
|
316
|
+
} }
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
"opcode": "isSchemaDefined",
|
|
320
|
+
"blockType": "BOOLEAN",
|
|
321
|
+
"text": "schema [NAME] defined?",
|
|
322
|
+
"description": "True when a valid `define schema` hat has that name. A name may be pinned as Name@1.0.0.",
|
|
323
|
+
"descriptionJa": "その名前の正しい`define schema`ハットがあればtrueを返します。Name@1.0.0の形でバージョンを固定できます。",
|
|
324
|
+
"arguments": { "NAME": {
|
|
325
|
+
"type": "STRING",
|
|
326
|
+
"defaultValue": "Player"
|
|
327
|
+
} }
|
|
328
|
+
},
|
|
329
|
+
{
|
|
330
|
+
"opcode": "functionArgument",
|
|
331
|
+
"blockType": "REPORTER",
|
|
332
|
+
"text": "function argument [PATH]",
|
|
333
|
+
"description": "Inside a function, reports the argument at a dotted path such as a or items.0.name.",
|
|
334
|
+
"descriptionJa": "関数の中で、aやitems.0.nameのようなドット区切りのパスにある引数を返します。",
|
|
335
|
+
"arguments": { "PATH": {
|
|
336
|
+
"type": "STRING",
|
|
337
|
+
"defaultValue": "a"
|
|
338
|
+
} }
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
"opcode": "functionArgumentsJson",
|
|
342
|
+
"blockType": "REPORTER",
|
|
343
|
+
"text": "function arguments JSON",
|
|
344
|
+
"description": "Inside a function, reports all arguments as JSON text.",
|
|
345
|
+
"descriptionJa": "関数の中で、すべての引数をJSONテキストとして返します。",
|
|
346
|
+
"arguments": {}
|
|
347
|
+
},
|
|
348
|
+
{
|
|
349
|
+
"opcode": "returnValue",
|
|
350
|
+
"blockType": "COMMAND",
|
|
351
|
+
"text": "return [VALUE]",
|
|
352
|
+
"description": "Inside a function, returns a value and ends the script. JSON text is returned as JSON; other text is returned as a string.",
|
|
353
|
+
"descriptionJa": "関数の中で値を返し、スクリプトを終了します。JSONテキストはJSONとして、それ以外のテキストは文字列として返します。",
|
|
354
|
+
"arguments": { "VALUE": {
|
|
355
|
+
"type": "STRING",
|
|
356
|
+
"defaultValue": "0"
|
|
357
|
+
} }
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
"opcode": "callFunction",
|
|
361
|
+
"blockType": "REPORTER",
|
|
362
|
+
"text": "call function [NAME] with [ARGS]",
|
|
363
|
+
"description": "Calls a function and waits for its result. ARGS is JSON text and is checked against the args schema. Takes at least one frame.",
|
|
364
|
+
"descriptionJa": "関数を呼び出し、結果を待って返します。ARGSはJSONテキストで、args schemaで検査されます。少なくとも1フレームかかります。",
|
|
365
|
+
"arguments": {
|
|
366
|
+
"NAME": {
|
|
367
|
+
"type": "STRING",
|
|
368
|
+
"defaultValue": "add"
|
|
369
|
+
},
|
|
370
|
+
"ARGS": {
|
|
371
|
+
"type": "STRING",
|
|
372
|
+
"defaultValue": "{\"a\":1,\"b\":2}"
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
},
|
|
376
|
+
{
|
|
377
|
+
"opcode": "startFunction",
|
|
378
|
+
"blockType": "REPORTER",
|
|
379
|
+
"text": "start function [NAME] with [ARGS]",
|
|
380
|
+
"description": "Starts a function without waiting and reports a promise reference as JSON text.",
|
|
381
|
+
"descriptionJa": "関数を待たずに開始し、Promiseへの参照をJSONテキストで返します。",
|
|
382
|
+
"arguments": {
|
|
383
|
+
"NAME": {
|
|
384
|
+
"type": "STRING",
|
|
385
|
+
"defaultValue": "add"
|
|
386
|
+
},
|
|
387
|
+
"ARGS": {
|
|
388
|
+
"type": "STRING",
|
|
389
|
+
"defaultValue": "{\"a\":1,\"b\":2}"
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
"opcode": "awaitResult",
|
|
395
|
+
"blockType": "REPORTER",
|
|
396
|
+
"text": "await [PROMISE]",
|
|
397
|
+
"description": "Waits for a promise reference from start function and reports its result.",
|
|
398
|
+
"descriptionJa": "start functionが返したPromiseへの参照を待ち、その結果を返します。",
|
|
399
|
+
"arguments": { "PROMISE": {
|
|
400
|
+
"type": "STRING",
|
|
401
|
+
"defaultValue": "{\"$promise\":\"p_1\"}"
|
|
402
|
+
} }
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
"opcode": "awaitAll",
|
|
406
|
+
"blockType": "REPORTER",
|
|
407
|
+
"text": "await all [PROMISES]",
|
|
408
|
+
"description": "Waits for a JSON array of promise references and reports a JSON array of results. Failed calls become {\"$error\": message}.",
|
|
409
|
+
"descriptionJa": "Promiseへの参照のJSON配列をすべて待ち、結果のJSON配列を返します。失敗した呼び出しは{\"$error\": メッセージ}になります。",
|
|
410
|
+
"arguments": { "PROMISES": {
|
|
411
|
+
"type": "STRING",
|
|
412
|
+
"defaultValue": "[]"
|
|
413
|
+
} }
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
"opcode": "isSettled",
|
|
417
|
+
"blockType": "BOOLEAN",
|
|
418
|
+
"text": "[PROMISE] settled?",
|
|
419
|
+
"description": "Reports whether a started function has finished, without waiting.",
|
|
420
|
+
"descriptionJa": "開始した関数が終わったかを、待たずに返します。",
|
|
421
|
+
"arguments": { "PROMISE": {
|
|
422
|
+
"type": "STRING",
|
|
423
|
+
"defaultValue": "{\"$promise\":\"p_1\"}"
|
|
424
|
+
} }
|
|
425
|
+
},
|
|
426
|
+
{
|
|
427
|
+
"opcode": "isDefined",
|
|
428
|
+
"blockType": "BOOLEAN",
|
|
429
|
+
"text": "function [NAME] defined?",
|
|
430
|
+
"description": "Reports whether a valid define function hat with this name exists.",
|
|
431
|
+
"descriptionJa": "この名前の正しいdefine functionハットがあるかを返します。",
|
|
432
|
+
"arguments": { "NAME": {
|
|
433
|
+
"type": "STRING",
|
|
434
|
+
"defaultValue": "add"
|
|
435
|
+
} }
|
|
436
|
+
},
|
|
437
|
+
{
|
|
438
|
+
"opcode": "lastError",
|
|
439
|
+
"blockType": "REPORTER",
|
|
440
|
+
"text": "last function error",
|
|
441
|
+
"description": "Reports the most recent function error, or an empty string.",
|
|
442
|
+
"descriptionJa": "直近の関数のエラーを返します。エラーがなければ空文字列を返します。",
|
|
443
|
+
"arguments": {}
|
|
444
|
+
}
|
|
445
|
+
],
|
|
446
|
+
menus: {
|
|
447
|
+
"exportModes": {
|
|
448
|
+
"acceptReporters": false,
|
|
449
|
+
"items": ["none", "tool"]
|
|
450
|
+
},
|
|
451
|
+
"propertyTypes": {
|
|
452
|
+
"acceptReporters": false,
|
|
453
|
+
"items": [
|
|
454
|
+
"string",
|
|
455
|
+
"number",
|
|
456
|
+
"integer",
|
|
457
|
+
"boolean",
|
|
458
|
+
"null",
|
|
459
|
+
"object",
|
|
460
|
+
"array",
|
|
461
|
+
"array of string",
|
|
462
|
+
"array of number",
|
|
463
|
+
"array of integer",
|
|
464
|
+
"array of boolean",
|
|
465
|
+
"array of object"
|
|
466
|
+
]
|
|
467
|
+
},
|
|
468
|
+
"presenceModes": {
|
|
469
|
+
"acceptReporters": false,
|
|
470
|
+
"items": ["required", "optional"]
|
|
471
|
+
},
|
|
472
|
+
"constraintKeywords": {
|
|
473
|
+
"acceptReporters": false,
|
|
474
|
+
"items": [
|
|
475
|
+
"minimum",
|
|
476
|
+
"maximum",
|
|
477
|
+
"minLength",
|
|
478
|
+
"maxLength",
|
|
479
|
+
"minItems",
|
|
480
|
+
"maxItems"
|
|
481
|
+
]
|
|
482
|
+
},
|
|
483
|
+
"allowModes": {
|
|
484
|
+
"acceptReporters": false,
|
|
485
|
+
"items": ["no", "yes"]
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
//#endregion
|
|
490
|
+
//#region src/schema-validator.ts
|
|
491
|
+
/**
|
|
492
|
+
* A small JSON Schema validator for function arguments and returns. It interprets the schema at run time and
|
|
493
|
+
* never generates code, so it also works where `eval` is unavailable (for example Cloudflare Workers).
|
|
494
|
+
*
|
|
495
|
+
* Supported keywords: type, properties, required, additionalProperties (boolean), items, enum,
|
|
496
|
+
* const, minimum, maximum, minLength, maxLength, minItems, maxItems. Other keywords, such as
|
|
497
|
+
* description and title, are ignored.
|
|
498
|
+
*/
|
|
499
|
+
function validateAgainstSchema(value, schema, path = "$") {
|
|
500
|
+
const rule = asRecord(schema);
|
|
501
|
+
if (!rule) throw new Error(`${path} must be a JSON Schema object`);
|
|
502
|
+
assertSchemaNode(rule, path);
|
|
503
|
+
return validateValue(value, rule, path);
|
|
504
|
+
}
|
|
505
|
+
/** Reject malformed schemas before a function definition or call can use them. */
|
|
506
|
+
function validateSchemaDefinition(schema, label = "schema") {
|
|
507
|
+
const rule = asRecord(schema);
|
|
508
|
+
if (!rule) throw new Error(`${label} must be a JSON object`);
|
|
509
|
+
assertSchemaNode(rule, label);
|
|
510
|
+
}
|
|
511
|
+
function validateValue(value, rule, path) {
|
|
512
|
+
const errors = [];
|
|
513
|
+
if (rule.type !== void 0) {
|
|
514
|
+
const types = Array.isArray(rule.type) ? rule.type : [rule.type];
|
|
515
|
+
if (!types.some((type) => matchesType(value, type))) return [`${path} must be ${types.join(" or ")}`];
|
|
516
|
+
}
|
|
517
|
+
if (Array.isArray(rule.enum) && !rule.enum.some((candidate) => deepEqual(candidate, value))) errors.push(`${path} must be one of ${rule.enum.map((item) => JSON.stringify(item)).join(", ")}`);
|
|
518
|
+
if ("const" in rule && !deepEqual(rule.const, value)) errors.push(`${path} must be ${JSON.stringify(rule.const)}`);
|
|
519
|
+
if (typeof value === "number") {
|
|
520
|
+
if (typeof rule.minimum === "number" && value < rule.minimum) errors.push(`${path} must be >= ${rule.minimum}`);
|
|
521
|
+
if (typeof rule.maximum === "number" && value > rule.maximum) errors.push(`${path} must be <= ${rule.maximum}`);
|
|
522
|
+
}
|
|
523
|
+
if (typeof value === "string") {
|
|
524
|
+
const length = [...value].length;
|
|
525
|
+
if (typeof rule.minLength === "number" && length < rule.minLength) errors.push(`${path} must have at least ${rule.minLength} characters`);
|
|
526
|
+
if (typeof rule.maxLength === "number" && length > rule.maxLength) errors.push(`${path} must have at most ${rule.maxLength} characters`);
|
|
527
|
+
}
|
|
528
|
+
if (Array.isArray(value)) {
|
|
529
|
+
if (typeof rule.minItems === "number" && value.length < rule.minItems) errors.push(`${path} must have at least ${rule.minItems} items`);
|
|
530
|
+
if (typeof rule.maxItems === "number" && value.length > rule.maxItems) errors.push(`${path} must have at most ${rule.maxItems} items`);
|
|
531
|
+
if (rule.items !== void 0) value.forEach((item, index) => errors.push(...validateValue(item, rule.items, `${path}[${index}]`)));
|
|
532
|
+
}
|
|
533
|
+
const object = asRecord(value);
|
|
534
|
+
if (object) {
|
|
535
|
+
const properties = asRecord(rule.properties) ?? {};
|
|
536
|
+
if (Array.isArray(rule.required)) {
|
|
537
|
+
for (const name of rule.required) if (typeof name === "string" && !Object.hasOwn(object, name)) errors.push(`${path}.${name} is required`);
|
|
538
|
+
}
|
|
539
|
+
for (const [name, child] of Object.entries(object)) if (Object.hasOwn(properties, name)) errors.push(...validateValue(child, properties[name], `${path}.${name}`));
|
|
540
|
+
else if (rule.additionalProperties === false) errors.push(`${path}.${name} is not allowed`);
|
|
541
|
+
}
|
|
542
|
+
return errors;
|
|
543
|
+
}
|
|
544
|
+
function assertSchemaNode(rule, path) {
|
|
545
|
+
const validTypes = /* @__PURE__ */ new Set([
|
|
546
|
+
"object",
|
|
547
|
+
"array",
|
|
548
|
+
"string",
|
|
549
|
+
"number",
|
|
550
|
+
"integer",
|
|
551
|
+
"boolean",
|
|
552
|
+
"null"
|
|
553
|
+
]);
|
|
554
|
+
if (rule.type !== void 0) {
|
|
555
|
+
const types = Array.isArray(rule.type) ? rule.type : [rule.type];
|
|
556
|
+
if (types.length === 0 || types.some((type) => typeof type !== "string" || !validTypes.has(type))) throw new Error(`${path}.type must be a supported JSON Schema type or non-empty type array`);
|
|
557
|
+
}
|
|
558
|
+
if (rule.properties !== void 0) {
|
|
559
|
+
const properties = asRecord(rule.properties);
|
|
560
|
+
if (!properties) throw new Error(`${path}.properties must be an object`);
|
|
561
|
+
for (const [name, child] of Object.entries(properties)) {
|
|
562
|
+
const childRule = asRecord(child);
|
|
563
|
+
if (!childRule) throw new Error(`${path}.properties.${name} must be a JSON Schema object`);
|
|
564
|
+
assertSchemaNode(childRule, `${path}.properties.${name}`);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (rule.required !== void 0 && (!Array.isArray(rule.required) || rule.required.some((name) => typeof name !== "string"))) throw new Error(`${path}.required must be an array of strings`);
|
|
568
|
+
if (rule.additionalProperties !== void 0 && typeof rule.additionalProperties !== "boolean") throw new Error(`${path}.additionalProperties must be a boolean`);
|
|
569
|
+
if (rule.items !== void 0) {
|
|
570
|
+
const items = asRecord(rule.items);
|
|
571
|
+
if (!items) throw new Error(`${path}.items must be a JSON Schema object`);
|
|
572
|
+
assertSchemaNode(items, `${path}.items`);
|
|
573
|
+
}
|
|
574
|
+
if (rule.enum !== void 0 && (!Array.isArray(rule.enum) || rule.enum.length === 0)) throw new Error(`${path}.enum must be a non-empty array`);
|
|
575
|
+
for (const keyword of ["minimum", "maximum"]) if (rule[keyword] !== void 0 && (typeof rule[keyword] !== "number" || !Number.isFinite(rule[keyword]))) throw new Error(`${path}.${keyword} must be a finite number`);
|
|
576
|
+
for (const keyword of [
|
|
577
|
+
"minLength",
|
|
578
|
+
"maxLength",
|
|
579
|
+
"minItems",
|
|
580
|
+
"maxItems"
|
|
581
|
+
]) {
|
|
582
|
+
const value = rule[keyword];
|
|
583
|
+
if (value !== void 0 && (typeof value !== "number" || !Number.isInteger(value) || value < 0)) throw new Error(`${path}.${keyword} must be a non-negative integer`);
|
|
584
|
+
}
|
|
585
|
+
if (typeof rule.minimum === "number" && typeof rule.maximum === "number" && rule.minimum > rule.maximum) throw new Error(`${path}.minimum cannot exceed maximum`);
|
|
586
|
+
if (typeof rule.minLength === "number" && typeof rule.maxLength === "number" && rule.minLength > rule.maxLength) throw new Error(`${path}.minLength cannot exceed maxLength`);
|
|
587
|
+
if (typeof rule.minItems === "number" && typeof rule.maxItems === "number" && rule.minItems > rule.maxItems) throw new Error(`${path}.minItems cannot exceed maxItems`);
|
|
588
|
+
}
|
|
589
|
+
function matchesType(value, type) {
|
|
590
|
+
switch (type) {
|
|
591
|
+
case "object": return asRecord(value) !== null;
|
|
592
|
+
case "array": return Array.isArray(value);
|
|
593
|
+
case "string": return typeof value === "string";
|
|
594
|
+
case "number": return typeof value === "number" && Number.isFinite(value);
|
|
595
|
+
case "integer": return typeof value === "number" && Number.isInteger(value);
|
|
596
|
+
case "boolean": return typeof value === "boolean";
|
|
597
|
+
case "null": return value === null;
|
|
598
|
+
default: return false;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
function deepEqual(left, right) {
|
|
602
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
603
|
+
}
|
|
604
|
+
function asRecord(value) {
|
|
605
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
606
|
+
}
|
|
607
|
+
var FunctionDispatcher = class {
|
|
608
|
+
constructor(runtime, options) {
|
|
609
|
+
this.runtime = runtime;
|
|
610
|
+
this.options = options;
|
|
611
|
+
this.queue = [];
|
|
612
|
+
this.running = /* @__PURE__ */ new Map();
|
|
613
|
+
this.byThread = /* @__PURE__ */ new Map();
|
|
614
|
+
this.starting = null;
|
|
615
|
+
this.step = 0;
|
|
616
|
+
this.onAfterExecute = () => this.afterStep();
|
|
617
|
+
this.onStopAll = () => this.cancelAll("The project was stopped.");
|
|
618
|
+
this.timeoutMs = options.timeoutMs ?? 3e4;
|
|
619
|
+
this.setTimer = options.setTimer ?? ((callback, ms) => setTimeout(callback, ms));
|
|
620
|
+
this.clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle));
|
|
621
|
+
runtime.on("AFTER_EXECUTE", this.onAfterExecute);
|
|
622
|
+
runtime.on("PROJECT_STOP_ALL", this.onStopAll);
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Starts a named function. `caller` is the thread of the block making the call, if any; it is used
|
|
626
|
+
* to detect reentrant calls.
|
|
627
|
+
*/
|
|
628
|
+
invoke(name, args, caller, returnSchema) {
|
|
629
|
+
if (!this.options.knownNames().has(name)) return Promise.reject(/* @__PURE__ */ new Error(`Unknown function: ${name}`));
|
|
630
|
+
const parentChain = caller ? this.byThread.get(caller)?.chain ?? [] : [];
|
|
631
|
+
if (parentChain.includes(name)) return Promise.reject(/* @__PURE__ */ new Error(`Reentrant call: ${[...parentChain, name].join(" -> ")}. Use a custom block for recursion.`));
|
|
632
|
+
return new Promise((resolve, reject) => {
|
|
633
|
+
const invocation = {
|
|
634
|
+
name,
|
|
635
|
+
args,
|
|
636
|
+
...returnSchema === void 0 ? {} : { returnSchema },
|
|
637
|
+
chain: [...parentChain, name],
|
|
638
|
+
resolve,
|
|
639
|
+
reject,
|
|
640
|
+
thread: null,
|
|
641
|
+
startedAtStep: -1,
|
|
642
|
+
timer: null,
|
|
643
|
+
settled: false
|
|
644
|
+
};
|
|
645
|
+
invocation.timer = this.setTimer(() => this.settle(invocation, /* @__PURE__ */ new Error(`Function ${name} timed out.`)), this.timeoutMs);
|
|
646
|
+
this.queue.push(invocation);
|
|
647
|
+
this.pump();
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
/** Hat predicate: true only for the script that should run the invocation being started. */
|
|
651
|
+
matchHat(name, thread) {
|
|
652
|
+
const invocation = this.starting;
|
|
653
|
+
if (!invocation || !thread || invocation.name !== name.trim()) return false;
|
|
654
|
+
invocation.thread = thread;
|
|
655
|
+
this.byThread.set(thread, invocation);
|
|
656
|
+
this.starting = null;
|
|
657
|
+
return true;
|
|
658
|
+
}
|
|
659
|
+
argumentsFor(thread) {
|
|
660
|
+
const invocation = thread ? this.byThread.get(thread) : void 0;
|
|
661
|
+
if (!invocation) throw new Error("This block can only be used inside a running function.");
|
|
662
|
+
return invocation.args;
|
|
663
|
+
}
|
|
664
|
+
returnFrom(thread, value) {
|
|
665
|
+
const invocation = thread ? this.byThread.get(thread) : void 0;
|
|
666
|
+
if (!invocation) throw new Error("return can only be used inside a running function.");
|
|
667
|
+
this.settleWithValue(invocation, value);
|
|
668
|
+
}
|
|
669
|
+
cancelAll(reason) {
|
|
670
|
+
for (const invocation of [...this.queue, ...this.running.values()]) this.settle(invocation, new Error(reason));
|
|
671
|
+
if (this.starting) this.settle(this.starting, new Error(reason));
|
|
672
|
+
}
|
|
673
|
+
get pendingCount() {
|
|
674
|
+
return this.queue.length + this.running.size;
|
|
675
|
+
}
|
|
676
|
+
release() {
|
|
677
|
+
this.cancelAll("Named functions were released.");
|
|
678
|
+
this.runtime.off?.("AFTER_EXECUTE", this.onAfterExecute);
|
|
679
|
+
this.runtime.off?.("PROJECT_STOP_ALL", this.onStopAll);
|
|
680
|
+
}
|
|
681
|
+
pump() {
|
|
682
|
+
if (this.starting) return;
|
|
683
|
+
const index = this.queue.findIndex((invocation) => !this.running.has(invocation.name));
|
|
684
|
+
if (index < 0) return;
|
|
685
|
+
const [invocation] = this.queue.splice(index, 1);
|
|
686
|
+
if (!invocation) return;
|
|
687
|
+
this.running.set(invocation.name, invocation);
|
|
688
|
+
this.starting = invocation;
|
|
689
|
+
invocation.startedAtStep = this.step;
|
|
690
|
+
this.runtime.startHats(this.options.hatOpcode);
|
|
691
|
+
}
|
|
692
|
+
afterStep() {
|
|
693
|
+
this.step += 1;
|
|
694
|
+
const starting = this.starting;
|
|
695
|
+
if (starting && this.step - starting.startedAtStep > 2) this.settle(starting, /* @__PURE__ */ new Error(`Function ${starting.name} did not start. Is its script already running?`));
|
|
696
|
+
for (const invocation of this.running.values()) if (invocation.thread && !this.runtime.threads.includes(invocation.thread)) this.settleWithValue(invocation, null);
|
|
697
|
+
this.pump();
|
|
698
|
+
}
|
|
699
|
+
settle(invocation, error, value) {
|
|
700
|
+
if (invocation.settled) return;
|
|
701
|
+
invocation.settled = true;
|
|
702
|
+
this.clearTimer(invocation.timer);
|
|
703
|
+
const queued = this.queue.indexOf(invocation);
|
|
704
|
+
if (queued >= 0) this.queue.splice(queued, 1);
|
|
705
|
+
if (this.running.get(invocation.name) === invocation) this.running.delete(invocation.name);
|
|
706
|
+
if (invocation.thread) this.byThread.delete(invocation.thread);
|
|
707
|
+
if (this.starting === invocation) this.starting = null;
|
|
708
|
+
if (error) invocation.reject(error);
|
|
709
|
+
else invocation.resolve(value);
|
|
710
|
+
}
|
|
711
|
+
settleWithValue(invocation, value) {
|
|
712
|
+
if (invocation.returnSchema !== void 0) {
|
|
713
|
+
const errors = validateAgainstSchema(value, invocation.returnSchema);
|
|
714
|
+
if (errors.length > 0) {
|
|
715
|
+
this.settle(invocation, /* @__PURE__ */ new Error(`Invalid return value for ${invocation.name}: ${errors.join("; ")}`));
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
this.settle(invocation, null, value);
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
/** Resolves a dotted path such as `items.0.name` inside parsed JSON arguments. */
|
|
723
|
+
function readArgumentPath(args, path) {
|
|
724
|
+
const trimmed = path.trim();
|
|
725
|
+
if (trimmed.length === 0) return args;
|
|
726
|
+
let current = args;
|
|
727
|
+
for (const segment of trimmed.split(".")) {
|
|
728
|
+
if (current === null || typeof current !== "object") return void 0;
|
|
729
|
+
current = current[segment];
|
|
730
|
+
}
|
|
731
|
+
return current;
|
|
732
|
+
}
|
|
733
|
+
/** Converts a value for a Scratch reporter: objects and arrays become JSON text. */
|
|
734
|
+
function toScratchValue(value) {
|
|
735
|
+
if (value === void 0 || value === null) return "";
|
|
736
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
|
|
737
|
+
return JSON.stringify(value);
|
|
738
|
+
}
|
|
739
|
+
/** Parses block text: JSON text becomes JSON, anything else stays a string. */
|
|
740
|
+
function parseJsonOrText(text) {
|
|
741
|
+
const trimmed = text.trim();
|
|
742
|
+
if (trimmed.length === 0) return "";
|
|
743
|
+
try {
|
|
744
|
+
return JSON.parse(trimmed);
|
|
745
|
+
} catch {
|
|
746
|
+
return text;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
//#endregion
|
|
750
|
+
//#region src/block-literals.ts
|
|
751
|
+
function readLiteralInput(block, blocks, inputName) {
|
|
752
|
+
const input = block.inputs?.[inputName];
|
|
753
|
+
if (!input) throw new Error(`${inputName} is missing`);
|
|
754
|
+
if (input.block && input.block !== input.shadow) throw new Error(`${inputName} must be literal text, not a reporter block`);
|
|
755
|
+
const shadowId = input.shadow ?? input.block;
|
|
756
|
+
const shadow = shadowId ? blocks[shadowId] : void 0;
|
|
757
|
+
const field = shadow?.fields ? Object.values(shadow.fields)[0] : void 0;
|
|
758
|
+
if (!field) throw new Error(`${inputName} is missing`);
|
|
759
|
+
return String(field.value ?? "");
|
|
760
|
+
}
|
|
761
|
+
function readOptionalLiteralInput(block, blocks, inputName) {
|
|
762
|
+
if (!block.inputs?.[inputName]) return void 0;
|
|
763
|
+
return readLiteralInput(block, blocks, inputName);
|
|
764
|
+
}
|
|
765
|
+
/** Reads a dropdown that does not accept reporters; the VM stores it as a field on the block. */
|
|
766
|
+
function readMenuField(block, fieldName) {
|
|
767
|
+
const field = block.fields?.[fieldName];
|
|
768
|
+
if (!field) throw new Error(`${fieldName} is missing`);
|
|
769
|
+
return String(field.value ?? "");
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* The blocks stacked under `block`, in order. Shadow blocks are skipped, and a `next` chain that
|
|
773
|
+
* loops or leaves the container stops the walk instead of hanging.
|
|
774
|
+
*/
|
|
775
|
+
function stackBelow(block, blocks) {
|
|
776
|
+
const stack = [];
|
|
777
|
+
const seen = /* @__PURE__ */ new Set([block.id]);
|
|
778
|
+
let nextId = block.next ?? null;
|
|
779
|
+
while (nextId) {
|
|
780
|
+
if (seen.has(nextId)) break;
|
|
781
|
+
seen.add(nextId);
|
|
782
|
+
const next = blocks[nextId];
|
|
783
|
+
if (!next) break;
|
|
784
|
+
if (next.shadow !== true) stack.push(next);
|
|
785
|
+
nextId = next.next ?? null;
|
|
786
|
+
}
|
|
787
|
+
return stack;
|
|
788
|
+
}
|
|
789
|
+
//#endregion
|
|
790
|
+
//#region src/function-registry.ts
|
|
791
|
+
var FUNCTION_NAME_PATTERN = /^[A-Za-z0-9_-]{1,64}$/u;
|
|
792
|
+
var MAX_DESCRIPTION_LENGTH = 1024;
|
|
793
|
+
/**
|
|
794
|
+
* Reads every `define function` hat in the project. NAME, DESCRIPTION, and SCHEMA are part of the
|
|
795
|
+
* tool contract sent to the model before any script runs, so they must be literal text.
|
|
796
|
+
*/
|
|
797
|
+
function scanFunctionDefinitions(targets, hatOpcode, options = {}) {
|
|
798
|
+
const functions = [];
|
|
799
|
+
const errors = [];
|
|
800
|
+
const seen = /* @__PURE__ */ new Map();
|
|
801
|
+
for (const target of targets) {
|
|
802
|
+
if (target.isOriginal === false) continue;
|
|
803
|
+
const targetName = target.getName?.() ?? (target.isStage ? "Stage" : "sprite");
|
|
804
|
+
const blocks = target.blocks._blocks;
|
|
805
|
+
for (const block of Object.values(blocks)) {
|
|
806
|
+
if (block.opcode !== hatOpcode || block.topLevel === false) continue;
|
|
807
|
+
const where = `${targetName} (block ${block.id})`;
|
|
808
|
+
try {
|
|
809
|
+
const definition = readDefinition(block, blocks, targetName, options);
|
|
810
|
+
const previous = seen.get(definition.name);
|
|
811
|
+
if (previous !== void 0) throw new Error(`function "${definition.name}" is already defined in ${previous}`);
|
|
812
|
+
seen.set(definition.name, where);
|
|
813
|
+
functions.push(definition);
|
|
814
|
+
} catch (error) {
|
|
815
|
+
errors.push(`${where}: ${error instanceof Error ? error.message : String(error)}`);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
functions.sort((left, right) => left.name < right.name ? -1 : left.name > right.name ? 1 : 0);
|
|
820
|
+
return {
|
|
821
|
+
functions,
|
|
822
|
+
errors
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
function toFunctionTools(functions) {
|
|
826
|
+
return functions.filter((definition) => definition.exportAs === "tool").map((definition) => ({
|
|
827
|
+
type: "function",
|
|
828
|
+
name: definition.name,
|
|
829
|
+
description: definition.description,
|
|
830
|
+
parameters: definition.parameters
|
|
831
|
+
}));
|
|
832
|
+
}
|
|
833
|
+
function readDefinition(block, blocks, targetName, options) {
|
|
834
|
+
const name = readLiteralInput(block, blocks, "NAME").trim();
|
|
835
|
+
if (!FUNCTION_NAME_PATTERN.test(name)) throw new Error("function name must be 1-64 letters, digits, \"_\" or \"-\"");
|
|
836
|
+
const description = readLiteralInput(block, blocks, "DESCRIPTION").trim();
|
|
837
|
+
if (description.length > 1024) throw new Error(`description must be at most ${MAX_DESCRIPTION_LENGTH} characters`);
|
|
838
|
+
const parameters = parseSchema(readLiteralInput(block, blocks, "SCHEMA"), "args schema", true, options);
|
|
839
|
+
const returnsText = readOptionalLiteralInput(block, blocks, "RETURNS");
|
|
840
|
+
const returns = returnsText === void 0 || returnsText.trim() === "" ? void 0 : parseSchema(returnsText, "returns schema", false, options);
|
|
841
|
+
const exportAs = String(block.fields?.EXPORT?.value ?? "none") === "tool" ? "tool" : "none";
|
|
842
|
+
if (exportAs === "tool" && description.length === 0) throw new Error("a function exported as a tool needs a description");
|
|
843
|
+
return {
|
|
844
|
+
name,
|
|
845
|
+
description,
|
|
846
|
+
parameters,
|
|
847
|
+
...returns ? { returns } : {},
|
|
848
|
+
exportAs,
|
|
849
|
+
targetName,
|
|
850
|
+
blockId: block.id
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* SCHEMA and RETURNS hold either JSON Schema text or, when the consumer supports it, the name of a
|
|
855
|
+
* `define schema` hat. Text that is not JSON is treated as a name, so existing JSON projects are
|
|
856
|
+
* unaffected.
|
|
857
|
+
*/
|
|
858
|
+
function parseSchema(text, label, requireObjectType, options) {
|
|
859
|
+
let value;
|
|
860
|
+
try {
|
|
861
|
+
value = JSON.parse(text);
|
|
862
|
+
} catch {
|
|
863
|
+
if (options.resolveSchema) return resolveNamed(text, label, requireObjectType, options.resolveSchema);
|
|
864
|
+
throw new Error(`${label} must be valid JSON`);
|
|
865
|
+
}
|
|
866
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error(`${label} must be a JSON object`);
|
|
867
|
+
const schema = value;
|
|
868
|
+
if (requireObjectType && schema.type !== "object") throw new Error("args schema must have \"type\": \"object\"");
|
|
869
|
+
validateSchemaDefinition(schema, label);
|
|
870
|
+
return schema;
|
|
871
|
+
}
|
|
872
|
+
function resolveNamed(text, label, requireObjectType, resolveSchema) {
|
|
873
|
+
const schema = resolveSchema(text, label);
|
|
874
|
+
if (requireObjectType && schema.type !== "object") throw new Error("args schema must have \"type\": \"object\"");
|
|
875
|
+
validateSchemaDefinition(schema, label);
|
|
876
|
+
return schema;
|
|
877
|
+
}
|
|
878
|
+
//#endregion
|
|
879
|
+
//#region src/schema-builder.ts
|
|
880
|
+
/**
|
|
881
|
+
* Lowers `define schema` block stacks into the canonical JSON Schema objects consumed by
|
|
882
|
+
* `validateAgainstSchema`.
|
|
883
|
+
*
|
|
884
|
+
* The builder is declarative: it reads literal block fields before any script runs and never
|
|
885
|
+
* executes the blocks it reads. Every schema it produces is checked with `validateSchemaDefinition`,
|
|
886
|
+
* so a block-built schema and a hand-written JSON one go through one validator and one diagnostic
|
|
887
|
+
* format.
|
|
888
|
+
*/
|
|
889
|
+
var SCHEMA_NAME_PATTERN = /^[A-Za-z0-9_.-]{1,128}$/u;
|
|
890
|
+
/** Complete SemVer, as the named-data schema identity contract requires. */
|
|
891
|
+
var SCHEMA_VERSION_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/u;
|
|
892
|
+
var PROPERTY_TYPES = {
|
|
893
|
+
string: () => ({ type: "string" }),
|
|
894
|
+
number: () => ({ type: "number" }),
|
|
895
|
+
integer: () => ({ type: "integer" }),
|
|
896
|
+
boolean: () => ({ type: "boolean" }),
|
|
897
|
+
null: () => ({ type: "null" }),
|
|
898
|
+
object: () => ({ type: "object" }),
|
|
899
|
+
array: () => ({ type: "array" }),
|
|
900
|
+
"array of string": () => ({
|
|
901
|
+
type: "array",
|
|
902
|
+
items: { type: "string" }
|
|
903
|
+
}),
|
|
904
|
+
"array of number": () => ({
|
|
905
|
+
type: "array",
|
|
906
|
+
items: { type: "number" }
|
|
907
|
+
}),
|
|
908
|
+
"array of integer": () => ({
|
|
909
|
+
type: "array",
|
|
910
|
+
items: { type: "integer" }
|
|
911
|
+
}),
|
|
912
|
+
"array of boolean": () => ({
|
|
913
|
+
type: "array",
|
|
914
|
+
items: { type: "boolean" }
|
|
915
|
+
}),
|
|
916
|
+
"array of object": () => ({
|
|
917
|
+
type: "array",
|
|
918
|
+
items: { type: "object" }
|
|
919
|
+
})
|
|
920
|
+
};
|
|
921
|
+
var CONSTRAINT_KEYWORDS = /* @__PURE__ */ new Set([
|
|
922
|
+
"minimum",
|
|
923
|
+
"maximum",
|
|
924
|
+
"minLength",
|
|
925
|
+
"maxLength",
|
|
926
|
+
"minItems",
|
|
927
|
+
"maxItems"
|
|
928
|
+
]);
|
|
929
|
+
/** Keywords that describe the array itself rather than each of its items. */
|
|
930
|
+
var ARRAY_LEVEL_KEYWORDS = /* @__PURE__ */ new Set(["minItems", "maxItems"]);
|
|
931
|
+
/**
|
|
932
|
+
* Reads every `define schema` hat in the project and lowers it to a canonical JSON Schema.
|
|
933
|
+
* A hat that cannot be read is reported in `errors` and left out of `schemas`.
|
|
934
|
+
*/
|
|
935
|
+
function scanSchemaDefinitions(targets, opcodes) {
|
|
936
|
+
const drafts = [];
|
|
937
|
+
const errors = [];
|
|
938
|
+
const seen = /* @__PURE__ */ new Map();
|
|
939
|
+
for (const target of targets) {
|
|
940
|
+
if (target.isOriginal === false) continue;
|
|
941
|
+
const targetName = target.getName?.() ?? (target.isStage ? "Stage" : "sprite");
|
|
942
|
+
const blocks = target.blocks._blocks;
|
|
943
|
+
for (const block of Object.values(blocks)) {
|
|
944
|
+
if (block.opcode !== opcodes.hat || block.topLevel === false) continue;
|
|
945
|
+
const where = `${targetName} (block ${block.id})`;
|
|
946
|
+
try {
|
|
947
|
+
const draft = readDraft(block, blocks, opcodes, targetName);
|
|
948
|
+
const key = `${draft.name}@${draft.version}`;
|
|
949
|
+
const previous = seen.get(key);
|
|
950
|
+
if (previous !== void 0) throw new Error(`schema "${key}" is already defined in ${previous}`);
|
|
951
|
+
seen.set(key, where);
|
|
952
|
+
drafts.push(draft);
|
|
953
|
+
} catch (error) {
|
|
954
|
+
errors.push(`${where}: ${messageOf$2(error)}`);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
const byKey = new Map(drafts.map((draft) => [`${draft.name}@${draft.version}`, draft]));
|
|
959
|
+
const schemas = [];
|
|
960
|
+
for (const draft of drafts) try {
|
|
961
|
+
const schema = lowerDraft(draft, byKey, []);
|
|
962
|
+
validateSchemaDefinition(schema, `schema ${draft.name}`);
|
|
963
|
+
schemas.push({
|
|
964
|
+
name: draft.name,
|
|
965
|
+
version: draft.version,
|
|
966
|
+
schema,
|
|
967
|
+
targetName: draft.targetName,
|
|
968
|
+
blockId: draft.blockId
|
|
969
|
+
});
|
|
970
|
+
} catch (error) {
|
|
971
|
+
errors.push(`${draft.targetName} (block ${draft.blockId}): ${messageOf$2(error)}`);
|
|
972
|
+
}
|
|
973
|
+
schemas.sort((left, right) => compare(`${left.name}@${left.version}`, `${right.name}@${right.version}`));
|
|
974
|
+
return {
|
|
975
|
+
schemas,
|
|
976
|
+
errors
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
/** Parses `Name` or `Name@1.2.3`. */
|
|
980
|
+
function parseSchemaReference(text, label) {
|
|
981
|
+
const trimmed = text.trim();
|
|
982
|
+
const at = trimmed.lastIndexOf("@");
|
|
983
|
+
const name = at > 0 ? trimmed.slice(0, at) : trimmed;
|
|
984
|
+
const version = at > 0 ? trimmed.slice(at + 1) : void 0;
|
|
985
|
+
if (!SCHEMA_NAME_PATTERN.test(name)) throw new Error(`${label} must be 1-128 letters, digits, ".", "_" or "-"`);
|
|
986
|
+
if (version !== void 0 && !SCHEMA_VERSION_PATTERN.test(version)) throw new Error(`${label} version "${version}" must be a complete SemVer value such as 1.0.0`);
|
|
987
|
+
return version === void 0 ? { name } : {
|
|
988
|
+
name,
|
|
989
|
+
version
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* Resolves a `Name` or `Name@1.2.3` reference against scanned schemas. An unknown name or version
|
|
994
|
+
* fails; a name with several versions and no pinned version is ambiguous rather than guessed.
|
|
995
|
+
*/
|
|
996
|
+
function resolveSchemaReference(schemas, text, label) {
|
|
997
|
+
const reference = parseSchemaReference(text, label);
|
|
998
|
+
const matches = schemas.filter((candidate) => candidate.name === reference.name);
|
|
999
|
+
if (matches.length === 0) throw new Error(`${label} refers to unknown schema "${reference.name}"`);
|
|
1000
|
+
if (reference.version !== void 0) {
|
|
1001
|
+
const exact = matches.find((candidate) => candidate.version === reference.version);
|
|
1002
|
+
if (!exact) {
|
|
1003
|
+
const available = matches.map((candidate) => candidate.version).join(", ");
|
|
1004
|
+
throw new Error(`${label} refers to unknown version "${reference.version}" of schema "${reference.name}" (defined: ${available})`);
|
|
1005
|
+
}
|
|
1006
|
+
return exact.schema;
|
|
1007
|
+
}
|
|
1008
|
+
if (matches.length > 1) {
|
|
1009
|
+
const available = matches.map((candidate) => candidate.version).join(", ");
|
|
1010
|
+
throw new Error(`${label} refers to schema "${reference.name}", which has several versions (${available}); pin one as "${reference.name}@${matches[0]?.version}"`);
|
|
1011
|
+
}
|
|
1012
|
+
return matches[0].schema;
|
|
1013
|
+
}
|
|
1014
|
+
function readDraft(hat, blocks, opcodes, targetName) {
|
|
1015
|
+
const name = readLiteralInput(hat, blocks, "NAME").trim();
|
|
1016
|
+
if (!SCHEMA_NAME_PATTERN.test(name)) throw new Error("schema name must be 1-128 letters, digits, \".\", \"_\" or \"-\"");
|
|
1017
|
+
const version = readLiteralInput(hat, blocks, "VERSION").trim();
|
|
1018
|
+
if (!SCHEMA_VERSION_PATTERN.test(version)) throw new Error(`schema version "${version}" must be a complete SemVer value such as 1.0.0`);
|
|
1019
|
+
const draft = {
|
|
1020
|
+
name,
|
|
1021
|
+
version,
|
|
1022
|
+
properties: [],
|
|
1023
|
+
targetName,
|
|
1024
|
+
blockId: hat.id
|
|
1025
|
+
};
|
|
1026
|
+
const declared = /* @__PURE__ */ new Set();
|
|
1027
|
+
for (const block of stackBelow(hat, blocks)) if (block.opcode === opcodes.property) draft.properties.push(readProperty(block, blocks, declared));
|
|
1028
|
+
else if (block.opcode === opcodes.propertySchema || block.opcode === opcodes.propertyArraySchema) draft.properties.push(readReferenceProperty(block, blocks, declared, block.opcode === opcodes.propertyArraySchema));
|
|
1029
|
+
else if (block.opcode === opcodes.constraint) applyConstraint(block, blocks, draft);
|
|
1030
|
+
else if (block.opcode === opcodes.allowedValues) applyAllowedValues(block, blocks, draft);
|
|
1031
|
+
else if (block.opcode === opcodes.additionalProperties) {
|
|
1032
|
+
if (draft.additionalProperties !== void 0) throw new Error("\"allow undeclared properties\" is used more than once in this schema");
|
|
1033
|
+
draft.additionalProperties = readMenuField(block, "ALLOW") === "yes";
|
|
1034
|
+
} else throw new Error(`${block.opcode} cannot be used inside a "define schema" stack`);
|
|
1035
|
+
return draft;
|
|
1036
|
+
}
|
|
1037
|
+
function readProperty(block, blocks, declared) {
|
|
1038
|
+
const name = readPropertyName(block, blocks, declared);
|
|
1039
|
+
const typeName = readMenuField(block, "TYPE");
|
|
1040
|
+
const build = PROPERTY_TYPES[typeName];
|
|
1041
|
+
if (!build) throw new Error(`property "${name}" has unsupported type "${typeName}"`);
|
|
1042
|
+
return {
|
|
1043
|
+
name,
|
|
1044
|
+
required: readPresence(block),
|
|
1045
|
+
schema: build()
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
function readReferenceProperty(block, blocks, declared, asArray) {
|
|
1049
|
+
const name = readPropertyName(block, blocks, declared);
|
|
1050
|
+
const reference = parseSchemaReference(readLiteralInput(block, blocks, "SCHEMA"), `property "${name}" schema`);
|
|
1051
|
+
return {
|
|
1052
|
+
name,
|
|
1053
|
+
required: readPresence(block),
|
|
1054
|
+
reference,
|
|
1055
|
+
referenceIsArray: asArray
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
function readPropertyName(block, blocks, declared) {
|
|
1059
|
+
const name = readLiteralInput(block, blocks, "NAME").trim();
|
|
1060
|
+
if (name.length === 0) throw new Error("a property needs a name");
|
|
1061
|
+
if (declared.has(name)) throw new Error(`property "${name}" is declared more than once`);
|
|
1062
|
+
declared.add(name);
|
|
1063
|
+
return name;
|
|
1064
|
+
}
|
|
1065
|
+
function readPresence(block) {
|
|
1066
|
+
return readMenuField(block, "PRESENCE") === "required";
|
|
1067
|
+
}
|
|
1068
|
+
function applyConstraint(block, blocks, draft) {
|
|
1069
|
+
const keyword = readMenuField(block, "KEYWORD");
|
|
1070
|
+
if (!CONSTRAINT_KEYWORDS.has(keyword)) throw new Error(`unsupported limit "${keyword}"`);
|
|
1071
|
+
const target = constraintTarget(draft, `limit "${keyword}"`, keyword);
|
|
1072
|
+
const raw = readLiteralInput(block, blocks, "VALUE").trim();
|
|
1073
|
+
const value = Number(raw);
|
|
1074
|
+
if (raw.length === 0 || !Number.isFinite(value)) throw new Error(`limit "${keyword}" needs a number, not "${raw}"`);
|
|
1075
|
+
if (target[keyword] !== void 0) throw new Error(`limit "${keyword}" is set more than once on property "${lastProperty(draft).name}"`);
|
|
1076
|
+
target[keyword] = value;
|
|
1077
|
+
}
|
|
1078
|
+
function applyAllowedValues(block, blocks, draft) {
|
|
1079
|
+
const target = constraintTarget(draft, "\"allowed values\"", "enum");
|
|
1080
|
+
const property = lastProperty(draft);
|
|
1081
|
+
if (target.enum !== void 0) throw new Error(`"allowed values" is set more than once on property "${property.name}"`);
|
|
1082
|
+
const values = parseAllowedValues(readLiteralInput(block, blocks, "VALUES"), property.name);
|
|
1083
|
+
const type = target.type;
|
|
1084
|
+
if (type !== void 0) {
|
|
1085
|
+
for (const value of values) if (validateAgainstSchema(value, { type }).length > 0) throw new Error(`allowed value ${JSON.stringify(value)} does not match the declared type of property "${property.name}"`);
|
|
1086
|
+
}
|
|
1087
|
+
target.enum = values;
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Where a limit or an allowed-values list belongs. For an array property the item keywords describe
|
|
1091
|
+
* each item, while `minItems`/`maxItems` describe the array.
|
|
1092
|
+
*/
|
|
1093
|
+
function constraintTarget(draft, label, keyword) {
|
|
1094
|
+
const property = lastProperty(draft, label);
|
|
1095
|
+
if (!property.schema) throw new Error(`${label} cannot be used on property "${property.name}", which uses another schema`);
|
|
1096
|
+
if (!(property.schema.type === "array")) {
|
|
1097
|
+
if (ARRAY_LEVEL_KEYWORDS.has(keyword)) throw new Error(`${label} can only be used on an array property, not "${property.name}"`);
|
|
1098
|
+
return property.schema;
|
|
1099
|
+
}
|
|
1100
|
+
if (ARRAY_LEVEL_KEYWORDS.has(keyword)) return property.schema;
|
|
1101
|
+
const items = property.schema.items;
|
|
1102
|
+
if (items === void 0 || typeof items !== "object" || items === null || Array.isArray(items)) throw new Error(`${label} needs property "${property.name}" to declare an item type, such as "array of number"`);
|
|
1103
|
+
return items;
|
|
1104
|
+
}
|
|
1105
|
+
function lastProperty(draft, label = "this block") {
|
|
1106
|
+
const property = draft.properties.at(-1);
|
|
1107
|
+
if (!property) throw new Error(`${label} must come after a property block`);
|
|
1108
|
+
return property;
|
|
1109
|
+
}
|
|
1110
|
+
/** JSON array text, or a comma-separated list of strings. */
|
|
1111
|
+
function parseAllowedValues(text, propertyName) {
|
|
1112
|
+
const trimmed = text.trim();
|
|
1113
|
+
if (trimmed.length === 0) throw new Error(`"allowed values" for property "${propertyName}" is empty`);
|
|
1114
|
+
if (trimmed.startsWith("[")) {
|
|
1115
|
+
let parsed;
|
|
1116
|
+
try {
|
|
1117
|
+
parsed = JSON.parse(trimmed);
|
|
1118
|
+
} catch {
|
|
1119
|
+
throw new Error(`"allowed values" for property "${propertyName}" must be valid JSON`);
|
|
1120
|
+
}
|
|
1121
|
+
if (!Array.isArray(parsed) || parsed.length === 0) throw new Error(`"allowed values" for property "${propertyName}" must be a non-empty JSON array`);
|
|
1122
|
+
return parsed;
|
|
1123
|
+
}
|
|
1124
|
+
const values = trimmed.split(",").map((value) => value.trim()).filter((value) => value.length > 0);
|
|
1125
|
+
if (values.length === 0) throw new Error(`"allowed values" for property "${propertyName}" is empty`);
|
|
1126
|
+
return values;
|
|
1127
|
+
}
|
|
1128
|
+
function lowerDraft(draft, byKey, stack) {
|
|
1129
|
+
const key = `${draft.name}@${draft.version}`;
|
|
1130
|
+
if (stack.includes(key)) throw new Error(`schema reference cycle: ${[...stack, key].join(" -> ")}`);
|
|
1131
|
+
if (stack.length >= 16) throw new Error(`schema references nest deeper than 16 levels`);
|
|
1132
|
+
const nextStack = [...stack, key];
|
|
1133
|
+
const properties = {};
|
|
1134
|
+
const required = [];
|
|
1135
|
+
for (const property of draft.properties) {
|
|
1136
|
+
properties[property.name] = property.reference ? lowerReference(property, draft, byKey, nextStack) : property.schema;
|
|
1137
|
+
if (property.required) required.push(property.name);
|
|
1138
|
+
}
|
|
1139
|
+
return {
|
|
1140
|
+
type: "object",
|
|
1141
|
+
properties,
|
|
1142
|
+
...required.length > 0 ? { required } : {},
|
|
1143
|
+
...draft.additionalProperties === void 0 ? {} : { additionalProperties: draft.additionalProperties }
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
function lowerReference(property, draft, byKey, stack) {
|
|
1147
|
+
const reference = property.reference;
|
|
1148
|
+
const lowered = lowerDraft(findDraft(byKey, reference, `property "${property.name}" of schema ${draft.name}`), byKey, stack);
|
|
1149
|
+
return property.referenceIsArray ? {
|
|
1150
|
+
type: "array",
|
|
1151
|
+
items: lowered
|
|
1152
|
+
} : lowered;
|
|
1153
|
+
}
|
|
1154
|
+
function findDraft(byKey, reference, label) {
|
|
1155
|
+
if (reference.version !== void 0) {
|
|
1156
|
+
const exact = byKey.get(`${reference.name}@${reference.version}`);
|
|
1157
|
+
if (!exact) throw new Error(`${label} refers to unknown schema "${reference.name}@${reference.version}"`);
|
|
1158
|
+
return exact;
|
|
1159
|
+
}
|
|
1160
|
+
const matches = [...byKey.values()].filter((candidate) => candidate.name === reference.name);
|
|
1161
|
+
if (matches.length === 0) throw new Error(`${label} refers to unknown schema "${reference.name}"`);
|
|
1162
|
+
if (matches.length > 1) {
|
|
1163
|
+
const available = matches.map((candidate) => candidate.version).join(", ");
|
|
1164
|
+
throw new Error(`${label} refers to schema "${reference.name}", which has several versions (${available}); pin one as "${reference.name}@${matches[0]?.version}"`);
|
|
1165
|
+
}
|
|
1166
|
+
return matches[0];
|
|
1167
|
+
}
|
|
1168
|
+
function compare(left, right) {
|
|
1169
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
1170
|
+
}
|
|
1171
|
+
function messageOf$2(error) {
|
|
1172
|
+
return error instanceof Error ? error.message : String(error);
|
|
1173
|
+
}
|
|
1174
|
+
//#endregion
|
|
1175
|
+
//#region src/composition.ts
|
|
1176
|
+
/**
|
|
1177
|
+
* Composition API: named functions without TurboWarp block definitions.
|
|
1178
|
+
*
|
|
1179
|
+
* A consumer extension owns its `define function` hat (and its opcode) and forwards the hat
|
|
1180
|
+
* predicate and the argument/return blocks here. Importing this module does not register a
|
|
1181
|
+
* TurboWarp extension and does not touch the `Scratch` global.
|
|
1182
|
+
*/
|
|
1183
|
+
function createNamedFunctions(options) {
|
|
1184
|
+
return new NamedFunctionsImpl(options);
|
|
1185
|
+
}
|
|
1186
|
+
var NamedFunctionsImpl = class {
|
|
1187
|
+
constructor(options) {
|
|
1188
|
+
this.options = options;
|
|
1189
|
+
this.promises = /* @__PURE__ */ new Map();
|
|
1190
|
+
this.waiting = [];
|
|
1191
|
+
this.inFlight = 0;
|
|
1192
|
+
this.nextPromiseId = 1;
|
|
1193
|
+
this.maxInFlight = Math.max(1, options.maxInFlight ?? 8);
|
|
1194
|
+
this.maxRetained = Math.max(1, options.maxRetainedPromises ?? 256);
|
|
1195
|
+
const dispatcherOptions = {
|
|
1196
|
+
hatOpcode: options.functionHatOpcode,
|
|
1197
|
+
knownNames: () => new Set(this.scan().functions.map((definition) => definition.name)),
|
|
1198
|
+
...options.timeoutMs === void 0 ? {} : { timeoutMs: options.timeoutMs },
|
|
1199
|
+
...options.setTimer ? { setTimer: options.setTimer } : {},
|
|
1200
|
+
...options.clearTimer ? { clearTimer: options.clearTimer } : {}
|
|
1201
|
+
};
|
|
1202
|
+
this.dispatcher = new FunctionDispatcher(options.runtime, dispatcherOptions);
|
|
1203
|
+
options.runtime.on("PROJECT_STOP_ALL", () => this.promises.clear());
|
|
1204
|
+
}
|
|
1205
|
+
scan() {
|
|
1206
|
+
if (!this.options.schemaBlockOpcodes) return scanFunctionDefinitions(this.options.runtime.targets, this.options.functionHatOpcode);
|
|
1207
|
+
const schemas = this.schemas().schemas;
|
|
1208
|
+
return scanFunctionDefinitions(this.options.runtime.targets, this.options.functionHatOpcode, { resolveSchema: (reference, label) => resolveSchemaReference(schemas, reference, label) });
|
|
1209
|
+
}
|
|
1210
|
+
schemas() {
|
|
1211
|
+
const opcodes = this.options.schemaBlockOpcodes;
|
|
1212
|
+
if (!opcodes) return {
|
|
1213
|
+
schemas: [],
|
|
1214
|
+
errors: []
|
|
1215
|
+
};
|
|
1216
|
+
return scanSchemaDefinitions(this.options.runtime.targets, opcodes);
|
|
1217
|
+
}
|
|
1218
|
+
tools() {
|
|
1219
|
+
const errors = [...this.schemas().errors, ...this.scan().errors];
|
|
1220
|
+
if (errors.length > 0) throw new Error(`Invalid function definitions: ${errors.join("; ")}`);
|
|
1221
|
+
return toFunctionTools(this.scan().functions);
|
|
1222
|
+
}
|
|
1223
|
+
isDefined(name) {
|
|
1224
|
+
return this.scan().functions.some((definition) => definition.name === name.trim());
|
|
1225
|
+
}
|
|
1226
|
+
call(name, args, options = {}) {
|
|
1227
|
+
const trimmed = name.trim();
|
|
1228
|
+
const definition = this.scan().functions.find((candidate) => candidate.name === trimmed);
|
|
1229
|
+
if (!definition) return Promise.reject(/* @__PURE__ */ new Error(`Unknown function: ${trimmed}`));
|
|
1230
|
+
if (options.exportedOnly && definition.exportAs !== "tool") return Promise.reject(/* @__PURE__ */ new Error(`Function ${trimmed} is not exported as a tool.`));
|
|
1231
|
+
const errors = validateAgainstSchema(args, definition.parameters);
|
|
1232
|
+
if (errors.length > 0) return Promise.reject(/* @__PURE__ */ new Error(`Invalid arguments for ${trimmed}: ${errors.join("; ")}`));
|
|
1233
|
+
return this.dispatcher.invoke(trimmed, args, options.caller, definition.returns);
|
|
1234
|
+
}
|
|
1235
|
+
start(name, args, options = {}) {
|
|
1236
|
+
const id = `p_${this.nextPromiseId++}`;
|
|
1237
|
+
const promise = this.acquireSlot().then(() => this.call(name, args, options).finally(() => this.releaseSlot()));
|
|
1238
|
+
const entry = {
|
|
1239
|
+
promise,
|
|
1240
|
+
settled: false
|
|
1241
|
+
};
|
|
1242
|
+
promise.then(() => entry.settled = true, () => entry.settled = true);
|
|
1243
|
+
this.promises.set(id, entry);
|
|
1244
|
+
this.evictSettled();
|
|
1245
|
+
return { $promise: id };
|
|
1246
|
+
}
|
|
1247
|
+
await(ref) {
|
|
1248
|
+
const entry = this.lookup(ref);
|
|
1249
|
+
return entry ? entry.promise : Promise.reject(/* @__PURE__ */ new Error("Unknown or expired promise reference."));
|
|
1250
|
+
}
|
|
1251
|
+
async awaitAll(refs) {
|
|
1252
|
+
if (!Array.isArray(refs)) throw new Error("await all needs a JSON array of promise references.");
|
|
1253
|
+
return (await Promise.allSettled(refs.map((ref) => this.await(ref)))).map((result) => result.status === "fulfilled" ? result.value : { $error: messageOf$1(result.reason) });
|
|
1254
|
+
}
|
|
1255
|
+
isSettled(ref) {
|
|
1256
|
+
return this.lookup(ref)?.settled ?? false;
|
|
1257
|
+
}
|
|
1258
|
+
matchHat(name, thread) {
|
|
1259
|
+
return this.dispatcher.matchHat(name, thread);
|
|
1260
|
+
}
|
|
1261
|
+
argumentsFor(thread) {
|
|
1262
|
+
return this.dispatcher.argumentsFor(thread);
|
|
1263
|
+
}
|
|
1264
|
+
returnFrom(thread, value) {
|
|
1265
|
+
this.dispatcher.returnFrom(thread, value);
|
|
1266
|
+
}
|
|
1267
|
+
cancelAll(reason) {
|
|
1268
|
+
this.dispatcher.cancelAll(reason);
|
|
1269
|
+
}
|
|
1270
|
+
release() {
|
|
1271
|
+
this.dispatcher.release();
|
|
1272
|
+
this.promises.clear();
|
|
1273
|
+
}
|
|
1274
|
+
lookup(ref) {
|
|
1275
|
+
const id = typeof ref === "object" && ref !== null && typeof ref.$promise === "string" ? ref.$promise : void 0;
|
|
1276
|
+
return id ? this.promises.get(id) : void 0;
|
|
1277
|
+
}
|
|
1278
|
+
acquireSlot() {
|
|
1279
|
+
if (this.inFlight < this.maxInFlight) {
|
|
1280
|
+
this.inFlight += 1;
|
|
1281
|
+
return Promise.resolve();
|
|
1282
|
+
}
|
|
1283
|
+
return new Promise((resolve) => this.waiting.push(() => {
|
|
1284
|
+
this.inFlight += 1;
|
|
1285
|
+
resolve();
|
|
1286
|
+
}));
|
|
1287
|
+
}
|
|
1288
|
+
releaseSlot() {
|
|
1289
|
+
this.inFlight -= 1;
|
|
1290
|
+
this.waiting.shift()?.();
|
|
1291
|
+
}
|
|
1292
|
+
evictSettled() {
|
|
1293
|
+
if (this.promises.size <= this.maxRetained) return;
|
|
1294
|
+
for (const [id, entry] of this.promises) {
|
|
1295
|
+
if (this.promises.size <= this.maxRetained) break;
|
|
1296
|
+
if (entry.settled) this.promises.delete(id);
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
};
|
|
1300
|
+
function messageOf$1(error) {
|
|
1301
|
+
return error instanceof Error ? error.message : String(error);
|
|
1302
|
+
}
|
|
1303
|
+
//#endregion
|
|
1304
|
+
//#region src/extension.ts
|
|
1305
|
+
var blockDefinitions = block_definitions_default.blocks;
|
|
1306
|
+
var menuDefinitions = block_definitions_default.menus;
|
|
1307
|
+
var DEFINE_FUNCTION_OPCODE = `${extensionConfig.id}_defineFunction`;
|
|
1308
|
+
/** Opcodes of the blocks that make up a `define schema` stack. */
|
|
1309
|
+
var SCHEMA_BLOCK_OPCODES = {
|
|
1310
|
+
hat: `${extensionConfig.id}_defineSchema`,
|
|
1311
|
+
property: `${extensionConfig.id}_schemaProperty`,
|
|
1312
|
+
propertySchema: `${extensionConfig.id}_schemaPropertySchema`,
|
|
1313
|
+
propertyArraySchema: `${extensionConfig.id}_schemaPropertyArraySchema`,
|
|
1314
|
+
constraint: `${extensionConfig.id}_schemaConstraint`,
|
|
1315
|
+
allowedValues: `${extensionConfig.id}_schemaAllowedValues`,
|
|
1316
|
+
additionalProperties: `${extensionConfig.id}_schemaAdditionalProperties`
|
|
1317
|
+
};
|
|
1318
|
+
/** Block surface over the named-functions composition. */
|
|
1319
|
+
var NamedFunctionsExtension = class {
|
|
1320
|
+
constructor(deps) {
|
|
1321
|
+
this.lastErrorMessage = "";
|
|
1322
|
+
this.unregisterFixedInputEditors = registerFixedInputEditors(typeof Scratch === "undefined" ? {} : Scratch, fixedInputEditorDefinitions, deps.runtime);
|
|
1323
|
+
try {
|
|
1324
|
+
this.functions = createNamedFunctions({
|
|
1325
|
+
...deps,
|
|
1326
|
+
functionHatOpcode: DEFINE_FUNCTION_OPCODE,
|
|
1327
|
+
schemaBlockOpcodes: SCHEMA_BLOCK_OPCODES
|
|
1328
|
+
});
|
|
1329
|
+
} catch (error) {
|
|
1330
|
+
this.unregisterFixedInputEditors();
|
|
1331
|
+
throw error;
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
dispose() {
|
|
1335
|
+
this.unregisterFixedInputEditors();
|
|
1336
|
+
}
|
|
1337
|
+
getInfo() {
|
|
1338
|
+
return {
|
|
1339
|
+
id: extensionConfig.id,
|
|
1340
|
+
name: Scratch.translate(block_definitions_default.extensionName),
|
|
1341
|
+
docsURI: extensionConfig.docsURI,
|
|
1342
|
+
blockIconURI: extensionConfig.blockIconURI,
|
|
1343
|
+
blocks: blockDefinitions.map((block) => this.toScratchBlock(block)),
|
|
1344
|
+
menus: Object.fromEntries(Object.entries(menuDefinitions).map(([id, menu]) => [id, {
|
|
1345
|
+
acceptReporters: menu.acceptReporters,
|
|
1346
|
+
items: [...menu.items]
|
|
1347
|
+
}]))
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1350
|
+
defineFunction(args, util) {
|
|
1351
|
+
return this.functions.matchHat(Scratch.Cast.toString(args.NAME), util?.thread);
|
|
1352
|
+
}
|
|
1353
|
+
/**
|
|
1354
|
+
* A `define schema` stack is a declaration, not a script: the hat never activates, and the blocks
|
|
1355
|
+
* under it do nothing if a user clicks the stack.
|
|
1356
|
+
*/
|
|
1357
|
+
defineSchema() {
|
|
1358
|
+
return false;
|
|
1359
|
+
}
|
|
1360
|
+
schemaProperty() {}
|
|
1361
|
+
schemaPropertySchema() {}
|
|
1362
|
+
schemaPropertyArraySchema() {}
|
|
1363
|
+
schemaConstraint() {}
|
|
1364
|
+
schemaAllowedValues() {}
|
|
1365
|
+
schemaAdditionalProperties() {}
|
|
1366
|
+
schemaJson(args) {
|
|
1367
|
+
return this.guard("", () => {
|
|
1368
|
+
const scan = this.functions.schemas();
|
|
1369
|
+
try {
|
|
1370
|
+
return JSON.stringify(resolveSchemaReference(scan.schemas, Scratch.Cast.toString(args.NAME), "schema name"));
|
|
1371
|
+
} catch (error) {
|
|
1372
|
+
if (scan.errors.length === 0) throw error;
|
|
1373
|
+
throw new Error(`${messageOf(error)} (invalid schema definitions: ${scan.errors.join("; ")})`);
|
|
1374
|
+
}
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
isSchemaDefined(args) {
|
|
1378
|
+
try {
|
|
1379
|
+
resolveSchemaReference(this.functions.schemas().schemas, Scratch.Cast.toString(args.NAME), "schema name");
|
|
1380
|
+
return true;
|
|
1381
|
+
} catch {
|
|
1382
|
+
return false;
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
functionArgument(args, util) {
|
|
1386
|
+
return this.guard("", () => toScratchValue(readArgumentPath(this.functions.argumentsFor(util?.thread), Scratch.Cast.toString(args.PATH))));
|
|
1387
|
+
}
|
|
1388
|
+
functionArgumentsJson(_args, util) {
|
|
1389
|
+
return this.guard("", () => JSON.stringify(this.functions.argumentsFor(util?.thread) ?? null));
|
|
1390
|
+
}
|
|
1391
|
+
returnValue(args, util) {
|
|
1392
|
+
this.guard(void 0, () => {
|
|
1393
|
+
this.functions.returnFrom(util?.thread, parseJsonOrText(Scratch.Cast.toString(args.VALUE)));
|
|
1394
|
+
util?.stopThisScript?.();
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
async callFunction(args, util) {
|
|
1398
|
+
try {
|
|
1399
|
+
const result = await this.functions.call(Scratch.Cast.toString(args.NAME), parseArguments(Scratch.Cast.toString(args.ARGS)), util?.thread ? { caller: util.thread } : {});
|
|
1400
|
+
this.lastErrorMessage = "";
|
|
1401
|
+
return toScratchValue(result);
|
|
1402
|
+
} catch (error) {
|
|
1403
|
+
this.lastErrorMessage = messageOf(error);
|
|
1404
|
+
return "";
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
startFunction(args, util) {
|
|
1408
|
+
return this.guard("", () => {
|
|
1409
|
+
const ref = this.functions.start(Scratch.Cast.toString(args.NAME), parseArguments(Scratch.Cast.toString(args.ARGS)), util?.thread ? { caller: util.thread } : {});
|
|
1410
|
+
return JSON.stringify(ref);
|
|
1411
|
+
});
|
|
1412
|
+
}
|
|
1413
|
+
async awaitResult(args) {
|
|
1414
|
+
try {
|
|
1415
|
+
const result = await this.functions.await(parseJsonOrText(Scratch.Cast.toString(args.PROMISE)));
|
|
1416
|
+
this.lastErrorMessage = "";
|
|
1417
|
+
return toScratchValue(result);
|
|
1418
|
+
} catch (error) {
|
|
1419
|
+
this.lastErrorMessage = messageOf(error);
|
|
1420
|
+
return "";
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
async awaitAll(args) {
|
|
1424
|
+
try {
|
|
1425
|
+
const results = await this.functions.awaitAll(parseJsonOrText(Scratch.Cast.toString(args.PROMISES)));
|
|
1426
|
+
this.lastErrorMessage = "";
|
|
1427
|
+
return JSON.stringify(results);
|
|
1428
|
+
} catch (error) {
|
|
1429
|
+
this.lastErrorMessage = messageOf(error);
|
|
1430
|
+
return "";
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
isSettled(args) {
|
|
1434
|
+
return this.functions.isSettled(parseJsonOrText(Scratch.Cast.toString(args.PROMISE)));
|
|
1435
|
+
}
|
|
1436
|
+
isDefined(args) {
|
|
1437
|
+
return this.functions.isDefined(Scratch.Cast.toString(args.NAME));
|
|
1438
|
+
}
|
|
1439
|
+
lastError() {
|
|
1440
|
+
return this.lastErrorMessage;
|
|
1441
|
+
}
|
|
1442
|
+
guard(fallback, action) {
|
|
1443
|
+
try {
|
|
1444
|
+
const result = action();
|
|
1445
|
+
this.lastErrorMessage = "";
|
|
1446
|
+
return result;
|
|
1447
|
+
} catch (error) {
|
|
1448
|
+
this.lastErrorMessage = messageOf(error);
|
|
1449
|
+
return fallback;
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
toScratchBlock(block) {
|
|
1453
|
+
const scratchBlock = {
|
|
1454
|
+
opcode: block.opcode,
|
|
1455
|
+
blockType: Scratch.BlockType[block.blockType],
|
|
1456
|
+
text: Scratch.translate(block.text),
|
|
1457
|
+
arguments: Object.fromEntries(Object.entries(block.arguments).map(([name, argument]) => [name, {
|
|
1458
|
+
type: Scratch.ArgumentType[argument.type],
|
|
1459
|
+
defaultValue: argument.defaultValue,
|
|
1460
|
+
...argument.menu ? { menu: argument.menu } : {}
|
|
1461
|
+
}]))
|
|
1462
|
+
};
|
|
1463
|
+
if (block.blockType === "HAT") scratchBlock.isEdgeActivated = false;
|
|
1464
|
+
return scratchBlock;
|
|
1465
|
+
}
|
|
1466
|
+
};
|
|
1467
|
+
/** ARGS text: empty means `{}`; otherwise it must be JSON. */
|
|
1468
|
+
function parseArguments(text) {
|
|
1469
|
+
if (text.trim().length === 0) return {};
|
|
1470
|
+
try {
|
|
1471
|
+
return JSON.parse(text);
|
|
1472
|
+
} catch {
|
|
1473
|
+
throw new Error("ARGS must be JSON text, for example {\"a\":1}.");
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
function messageOf(error) {
|
|
1477
|
+
return error instanceof Error ? error.message : String(error);
|
|
1478
|
+
}
|
|
1479
|
+
//#endregion
|
|
1480
|
+
//#region src/index.ts
|
|
1481
|
+
if (extensionConfig.unsandboxed && !Scratch.extensions.unsandboxed) throw new Error(`${extensionConfig.name} must run unsandboxed.`);
|
|
1482
|
+
var runtime = Scratch.vm?.runtime;
|
|
1483
|
+
if (!runtime) throw new Error(`${extensionConfig.name} requires access to the TurboWarp VM runtime.`);
|
|
1484
|
+
Scratch.extensions.register(new NamedFunctionsExtension({ runtime }));
|
|
1485
|
+
//#endregion
|
|
914
1486
|
|
|
915
1487
|
})(Scratch);
|