/
pegasus.c
5317 lines (4536 loc) · 156 KB
/
pegasus.c
1
2
3
4
5
6
7
8
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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* miniSphere JavaScript game engine
* Copyright (c) 2015-2018, Fat Cerberus
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of miniSphere nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
**/
#include "minisphere.h"
#include "pegasus.h"
#include "api.h"
#include "audio.h"
#include "color.h"
#include "compress.h"
#include "console.h"
#include "debugger.h"
#include "dispatch.h"
#include "encoding.h"
#include "font.h"
#include "galileo.h"
#include "image.h"
#include "input.h"
#include "jsal.h"
#include "profiler.h"
#include "sockets.h"
#include "unicode.h"
#include "xoroshiro.h"
#define API_VERSION 2
enum file_op
{
FILE_OP_READ,
FILE_OP_WRITE,
FILE_OP_UPDATE,
FILE_OP_MAX,
};
static const
struct x11_color
{
int api_level;
const char* name;
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
}
COLORS[] =
{
{ 1, "AliceBlue", 240, 248, 255, 255 },
{ 1, "AntiqueWhite", 250, 235, 215, 255 },
{ 1, "Aqua", 0, 255, 255, 255 },
{ 1, "Aquamarine", 127, 255, 212, 255 },
{ 1, "Azure", 240, 255, 255, 255 },
{ 1, "Beige", 245, 245, 220, 255 },
{ 1, "Bisque", 255, 228, 196, 255 },
{ 1, "Black", 0, 0, 0, 255 },
{ 1, "BlanchedAlmond", 255, 235, 205, 255 },
{ 1, "Blue", 0, 0, 255, 255 },
{ 1, "BlueViolet", 138, 43, 226, 255 },
{ 1, "Brown", 165, 42, 42, 255 },
{ 1, "BurlyWood", 222, 184, 135, 255 },
{ 1, "CadetBlue", 95, 158, 160, 255 },
{ 1, "Chartreuse", 127, 255, 0, 255 },
{ 1, "Chocolate", 210, 105, 30, 255 },
{ 1, "Coral", 255, 127, 80, 255 },
{ 1, "CornflowerBlue", 100, 149, 237, 255 },
{ 1, "Cornsilk", 255, 248, 220, 255 },
{ 1, "Crimson", 220, 20, 60, 255 },
{ 1, "Cyan", 0, 255, 255, 255 },
{ 1, "DarkBlue", 0, 0, 139, 255 },
{ 1, "DarkCyan", 0, 139, 139, 255 },
{ 1, "DarkGoldenrod", 184, 134, 11, 255 },
{ 1, "DarkGray", 169, 169, 169, 255 },
{ 1, "DarkGreen", 0, 100, 0, 255 },
{ 1, "DarkKhaki", 189, 183, 107, 255 },
{ 1, "DarkMagenta", 139, 0, 139, 255 },
{ 1, "DarkOliveGreen", 85, 107, 47, 255 },
{ 1, "DarkOrange", 255, 140, 0, 255 },
{ 1, "DarkOrchid", 153, 50, 204, 255 },
{ 1, "DarkRed", 139, 0, 0, 255 },
{ 1, "DarkSalmon", 233, 150, 122, 255 },
{ 1, "DarkSeaGreen", 143, 188, 143, 255 },
{ 1, "DarkSlateBlue", 72, 61, 139, 255 },
{ 1, "DarkSlateGray", 47, 79, 79, 255 },
{ 1, "DarkTurquoise", 0, 206, 209, 255 },
{ 1, "DarkViolet", 148, 0, 211, 255 },
{ 1, "DeepPink", 255, 20, 147, 255 },
{ 1, "DeepSkyBlue", 0, 191, 255, 255 },
{ 1, "DimGray", 105, 105, 105, 255 },
{ 1, "DodgerBlue", 30, 144, 255, 255 },
{ 1, "FireBrick", 178, 34, 34, 255 },
{ 1, "FloralWhite", 255, 250, 240, 255 },
{ 1, "ForestGreen", 34, 139, 34, 255 },
{ 1, "Fuchsia", 255, 0, 255, 255 },
{ 1, "Gainsboro", 220, 220, 220, 255 },
{ 1, "GhostWhite", 248, 248, 255, 255 },
{ 1, "Gold", 255, 215, 0, 255 },
{ 1, "Goldenrod", 218, 165, 32, 255 },
{ 1, "Gray", 128, 128, 128, 255 },
{ 1, "Green", 0, 128, 0, 255 },
{ 1, "GreenYellow", 173, 255, 47, 255 },
{ 1, "Honeydew", 240, 255, 240, 255 },
{ 1, "HotPink", 255, 105, 180, 255 },
{ 1, "IndianRed", 205, 92, 92, 255 },
{ 1, "Indigo", 75, 0, 130, 255 },
{ 1, "Ivory", 255, 255, 240, 255 },
{ 1, "Khaki", 240, 230, 140, 255 },
{ 1, "Lavender", 230, 230, 250, 255 },
{ 1, "LavenderBlush", 255, 240, 245, 255 },
{ 1, "LawnGreen", 124, 252, 0, 255 },
{ 1, "LemonChiffon", 255, 250, 205, 255 },
{ 1, "LightBlue", 173, 216, 230, 255 },
{ 1, "LightCoral", 240, 128, 128, 255 },
{ 1, "LightCyan", 224, 255, 255, 255 },
{ 1, "LightGoldenrodYellow", 250, 250, 210, 255 },
{ 1, "LightGray", 211, 211, 211, 255 },
{ 1, "LightGreen", 144, 238, 144, 255 },
{ 1, "LightPink", 255, 182, 193, 255 },
{ 1, "LightSalmon", 255, 160, 122, 255 },
{ 1, "LightSeaGreen", 32, 178, 170, 255 },
{ 1, "LightSkyBlue", 135, 206, 250, 255 },
{ 1, "LightSlateGray", 119, 136, 153, 255 },
{ 1, "LightSteelBlue", 176, 196, 222, 255 },
{ 1, "LightYellow", 255, 255, 224, 255 },
{ 1, "Lime", 0, 255, 0, 255 },
{ 1, "LimeGreen", 50, 205, 50, 255 },
{ 1, "Linen", 250, 240, 230, 255 },
{ 1, "Magenta", 255, 0, 255, 255 },
{ 1, "Maroon", 128, 0, 0, 255 },
{ 1, "MediumAquamarine", 102, 205, 170, 255 },
{ 1, "MediumBlue", 0, 0, 205, 255 },
{ 1, "MediumOrchid", 186, 85, 211, 255 },
{ 1, "MediumPurple", 147, 112, 219, 255 },
{ 1, "MediumSeaGreen", 60, 179, 113, 255 },
{ 1, "MediumSlateBlue", 123, 104, 238, 255 },
{ 1, "MediumSpringGreen", 0, 250, 154, 255 },
{ 1, "MediumTurquoise", 72, 209, 204, 255 },
{ 1, "MediumVioletRed", 199, 21, 133, 255 },
{ 1, "MidnightBlue", 25, 25, 112, 255 },
{ 1, "MintCream", 245, 255, 250, 255 },
{ 1, "MistyRose", 255, 228, 225, 255 },
{ 1, "Moccasin", 255, 228, 181, 255 },
{ 1, "NavajoWhite", 255, 222, 173, 255 },
{ 1, "Navy", 0, 0, 128, 255 },
{ 1, "OldLace", 253, 245, 230, 255 },
{ 1, "Olive", 128, 128, 0, 255 },
{ 1, "OliveDrab", 107, 142, 35, 255 },
{ 1, "Orange", 255, 165, 0, 255 },
{ 1, "OrangeRed", 255, 69, 0, 255 },
{ 1, "Orchid", 218, 112, 214, 255 },
{ 1, "PaleGoldenrod", 238, 232, 170, 255 },
{ 1, "PaleGreen", 152, 251, 152, 255 },
{ 1, "PaleTurquoise", 175, 238, 238, 255 },
{ 1, "PaleVioletRed", 219, 112, 147, 255 },
{ 1, "PapayaWhip", 225, 239, 213, 255 },
{ 1, "PeachPuff", 255, 218, 185, 255 },
{ 1, "Peru", 205, 133, 63, 255 },
{ 1, "Pink", 255, 192, 203, 255 },
{ 1, "Plum", 221, 160, 221, 255 },
{ 1, "PowderBlue", 176, 224, 230, 255 },
{ 1, "Purple", 128, 0, 128, 255 },
{ 1, "Red", 255, 0, 0, 255 },
{ 1, "RosyBrown", 188, 143, 143, 255 },
{ 1, "RoyalBlue", 65, 105, 225, 255 },
{ 1, "SaddleBrown", 139, 69, 19, 255 },
{ 1, "Salmon", 250, 128, 114, 255 },
{ 1, "SandyBrown", 244, 164, 96, 255 },
{ 1, "SeaGreen", 46, 139, 87, 255 },
{ 1, "Seashell", 255, 245, 238, 255 },
{ 1, "Sienna", 160, 82, 45, 255 },
{ 1, "Silver", 192, 192, 192, 255 },
{ 1, "SkyBlue", 135, 206, 235, 255 },
{ 1, "SlateBlue", 106, 90, 205, 255 },
{ 1, "SlateGray", 112, 128, 144, 255 },
{ 1, "Snow", 255, 250, 250, 255 },
{ 1, "SpringGreen", 0, 255, 127, 255 },
{ 1, "SteelBlue", 70, 130, 180, 255 },
{ 1, "Tan", 210, 180, 140, 255 },
{ 1, "Teal", 0, 128, 128, 255 },
{ 1, "Thistle", 216, 191, 216, 255 },
{ 1, "Tomato", 255, 99, 71, 255 },
{ 1, "Transparent", 0, 0, 0, 0 },
{ 1, "Turquoise", 64, 224, 208, 255 },
{ 1, "Violet", 238, 130, 238, 255 },
{ 1, "Wheat", 245, 222, 179, 255 },
{ 1, "White", 255, 255, 255, 255 },
{ 1, "WhiteSmoke", 245, 245, 245, 255 },
{ 1, "Yellow", 255, 255, 0, 255 },
{ 1, "YellowGreen", 154, 205, 50, 255 },
{ 2, "PurwaBlue", 155, 225, 255, 255 },
{ 2, "RebeccaPurple", 102, 51, 153, 255 },
{ 2, "StankyBean", 197, 162, 171, 255 },
{ 0, NULL, 0, 0, 0, 0 }
};
static bool js_require (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_get_APILevel (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_get_Compiler (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_get_Engine (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_get_Game (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_get_Version (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_get_frameRate (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_get_frameSkip (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_get_fullScreen (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_get_main (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_set_frameRate (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_set_frameSkip (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_set_fullScreen (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_abort (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_now (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_restart (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_setResolution (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_shutDown (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sphere_sleep (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_get_Color (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_is (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_mix (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_of (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Color (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_get_name (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_get_r (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_get_g (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_get_b (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_get_a (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_set_r (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_set_g (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_set_b (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_set_a (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_clone (int num_args, bool is_ctor, intptr_t magic);
static bool js_Color_fadeTo (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_DirectoryStream (int num_args, bool is_ctor, intptr_t magic);
static bool js_DirectoryStream_get_fileCount (int num_args, bool is_ctor, intptr_t magic);
static bool js_DirectoryStream_get_fileName (int num_args, bool is_ctor, intptr_t magic);
static bool js_DirectoryStream_get_position (int num_args, bool is_ctor, intptr_t magic);
static bool js_DirectoryStream_set_position (int num_args, bool is_ctor, intptr_t magic);
static bool js_DirectoryStream_iterator (int num_args, bool is_ctor, intptr_t magic);
static bool js_DirectoryStream_dispose (int num_args, bool is_ctor, intptr_t magic);
static bool js_DirectoryStream_next (int num_args, bool is_ctor, intptr_t magic);
static bool js_DirectoryStream_rewind (int num_args, bool is_ctor, intptr_t magic);
static bool js_Dispatch_cancelAll (int num_args, bool is_ctor, intptr_t magic);
static bool js_Dispatch_later (int num_args, bool is_ctor, intptr_t magic);
static bool js_Dispatch_now (int num_args, bool is_ctor, intptr_t magic);
static bool js_Dispatch_onExit (int num_args, bool is_ctor, intptr_t magic);
static bool js_Dispatch_onRender (int num_args, bool is_ctor, intptr_t magic);
static bool js_Dispatch_onUpdate (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_createDirectory (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_deleteFile (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_directoryExists (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_directoryOf (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_evaluateScript (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_fileExists (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_fileNameOf (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_fullPath (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_readFile (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_relativePath (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_rename (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_removeDirectory (int num_args, bool is_ctor, intptr_t magic);
static bool js_FS_writeFile (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_FileStream (int num_args, bool is_ctor, intptr_t magic);
static bool js_FileStream_dispose (int num_args, bool is_ctor, intptr_t magic);
static bool js_FileStream_get_fileName (int num_args, bool is_ctor, intptr_t magic);
static bool js_FileStream_get_fileSize (int num_args, bool is_ctor, intptr_t magic);
static bool js_FileStream_get_position (int num_args, bool is_ctor, intptr_t magic);
static bool js_FileStream_set_position (int num_args, bool is_ctor, intptr_t magic);
static bool js_FileStream_read (int num_args, bool is_ctor, intptr_t magic);
static bool js_FileStream_write (int num_args, bool is_ctor, intptr_t magic);
static bool js_Font_get_Default (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Font (int num_args, bool is_ctor, intptr_t magic);
static bool js_Font_get_fileName (int num_args, bool is_ctor, intptr_t magic);
static bool js_Font_get_height (int num_args, bool is_ctor, intptr_t magic);
static bool js_Font_drawText (int num_args, bool is_ctor, intptr_t magic);
static bool js_Font_getTextSize (int num_args, bool is_ctor, intptr_t magic);
static bool js_Font_wordWrap (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_IndexList (int num_args, bool is_ctor, intptr_t magic);
static bool js_JobToken_cancel (int num_args, bool is_ctor, intptr_t magic);
static bool js_JobToken_pause_resume (int num_args, bool is_ctor, intptr_t magic);
static bool js_Joystick_get_Null (int num_args, bool is_ctor, intptr_t magic);
static bool js_Joystick_getDevices (int num_args, bool is_ctor, intptr_t magic);
static bool js_Joystick_get_name (int num_args, bool is_ctor, intptr_t magic);
static bool js_Joystick_get_numAxes (int num_args, bool is_ctor, intptr_t magic);
static bool js_Joystick_get_numButtons (int num_args, bool is_ctor, intptr_t magic);
static bool js_Joystick_getPosition (int num_args, bool is_ctor, intptr_t magic);
static bool js_Joystick_isPressed (int num_args, bool is_ctor, intptr_t magic);
static bool js_Keyboard_get_Default (int num_args, bool is_ctor, intptr_t magic);
static bool js_Keyboard_get_capsLock (int num_args, bool is_ctor, intptr_t magic);
static bool js_Keyboard_get_numLock (int num_args, bool is_ctor, intptr_t magic);
static bool js_Keyboard_get_scrollLock (int num_args, bool is_ctor, intptr_t magic);
static bool js_Keyboard_charOf (int num_args, bool is_ctor, intptr_t magic);
static bool js_Keyboard_clearQueue (int num_args, bool is_ctor, intptr_t magic);
static bool js_Keyboard_getKey (int num_args, bool is_ctor, intptr_t magic);
static bool js_Keyboard_isPressed (int num_args, bool is_ctor, intptr_t magic);
static bool js_Mixer_get_Default (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Mixer (int num_args, bool is_ctor, intptr_t magic);
static bool js_Mixer_get_volume (int num_args, bool is_ctor, intptr_t magic);
static bool js_Mixer_set_volume (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Model (int num_args, bool is_ctor, intptr_t magic);
static bool js_Model_get_shader (int num_args, bool is_ctor, intptr_t magic);
static bool js_Model_get_transform (int num_args, bool is_ctor, intptr_t magic);
static bool js_Model_set_shader (int num_args, bool is_ctor, intptr_t magic);
static bool js_Model_set_transform (int num_args, bool is_ctor, intptr_t magic);
static bool js_Model_draw (int num_args, bool is_ctor, intptr_t magic);
static bool js_Mouse_get_Default (int num_args, bool is_ctor, intptr_t magic);
static bool js_Mouse_get_x (int num_args, bool is_ctor, intptr_t magic);
static bool js_Mouse_get_y (int num_args, bool is_ctor, intptr_t magic);
static bool js_Mouse_clearQueue (int num_args, bool is_ctor, intptr_t magic);
static bool js_Mouse_getEvent (int num_args, bool is_ctor, intptr_t magic);
static bool js_Mouse_isPressed (int num_args, bool is_ctor, intptr_t magic);
static bool js_RNG_fromSeed (int num_args, bool is_ctor, intptr_t magic);
static bool js_RNG_fromState (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_RNG (int num_args, bool is_ctor, intptr_t magic);
static bool js_RNG_get_state (int num_args, bool is_ctor, intptr_t magic);
static bool js_RNG_set_state (int num_args, bool is_ctor, intptr_t magic);
static bool js_RNG_iterator (int num_args, bool is_ctor, intptr_t magic);
static bool js_RNG_next (int num_args, bool is_ctor, intptr_t magic);
static bool js_SSj_flipScreen (int num_args, bool is_ctor, intptr_t magic);
static bool js_SSj_instrument (int num_args, bool is_ctor, intptr_t magic);
static bool js_SSj_log (int num_args, bool is_ctor, intptr_t magic);
static bool js_SSj_now (int num_args, bool is_ctor, intptr_t magic);
static bool js_SSj_profile (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Sample (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sample_get_fileName (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sample_play (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sample_stopAll (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Server (int num_args, bool is_ctor, intptr_t magic);
static bool js_Server_close (int num_args, bool is_ctor, intptr_t magic);
static bool js_Server_accept (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_get_Default (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Shader (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_clone (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_setBoolean (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_setColorVector (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_setFloat (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_setFloatArray (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_setFloatVector (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_setInt (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_setIntArray (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_setIntVector (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shader_setMatrix (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shape_drawImmediate (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Shape (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shape_get_indexList (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shape_get_texture (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shape_get_vertexList (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shape_set_indexList (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shape_set_texture (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shape_set_vertexList (int num_args, bool is_ctor, intptr_t magic);
static bool js_Shape_draw (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Socket (int num_args, bool is_ctor, intptr_t magic);
static bool js_Socket_get_bytesPending (int num_args, bool is_ctor, intptr_t magic);
static bool js_Socket_get_connected (int num_args, bool is_ctor, intptr_t magic);
static bool js_Socket_get_remoteAddress (int num_args, bool is_ctor, intptr_t magic);
static bool js_Socket_get_remotePort (int num_args, bool is_ctor, intptr_t magic);
static bool js_Socket_close (int num_args, bool is_ctor, intptr_t magic);
static bool js_Socket_connectTo (int num_args, bool is_ctor, intptr_t magic);
static bool js_Socket_read (int num_args, bool is_ctor, intptr_t magic);
static bool js_Socket_write (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Sound (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_get_fileName (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_get_length (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_get_pan (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_get_playing (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_get_position (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_get_repeat (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_get_speed (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_get_volume (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_set_pan (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_set_position (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_set_repeat (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_set_speed (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_set_volume (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_pause (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_play (int num_args, bool is_ctor, intptr_t magic);
static bool js_Sound_stop (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_SoundStream (int num_args, bool is_ctor, intptr_t magic);
static bool js_SoundStream_get_length (int num_args, bool is_ctor, intptr_t magic);
static bool js_SoundStream_play (int num_args, bool is_ctor, intptr_t magic);
static bool js_SoundStream_pause (int num_args, bool is_ctor, intptr_t magic);
static bool js_SoundStream_stop (int num_args, bool is_ctor, intptr_t magic);
static bool js_SoundStream_write (int num_args, bool is_ctor, intptr_t magic);
static bool js_Surface_get_Screen (int num_args, bool is_ctor, intptr_t magic);
static bool js_Surface_get_blendOp (int num_args, bool is_ctor, intptr_t magic);
static bool js_Surface_get_height (int num_args, bool is_ctor, intptr_t magic);
static bool js_Surface_get_transform (int num_args, bool is_ctor, intptr_t magic);
static bool js_Surface_get_width (int num_args, bool is_ctor, intptr_t magic);
static bool js_Surface_set_blendOp (int num_args, bool is_ctor, intptr_t magic);
static bool js_Surface_set_transform (int num_args, bool is_ctor, intptr_t magic);
static bool js_Surface_clipTo (int num_args, bool is_ctor, intptr_t magic);
static bool js_Surface_toTexture (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_TextDecoder (int num_args, bool is_ctor, intptr_t magic);
static bool js_TextDecoder_get_encoding (int num_args, bool is_ctor, intptr_t magic);
static bool js_TextDecoder_get_fatal (int num_args, bool is_ctor, intptr_t magic);
static bool js_TextDecoder_get_ignoreBOM (int num_args, bool is_ctor, intptr_t magic);
static bool js_TextDecoder_decode (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_TextEncoder (int num_args, bool is_ctor, intptr_t magic);
static bool js_TextEncoder_get_encoding (int num_args, bool is_ctor, intptr_t magic);
static bool js_TextEncoder_encode (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Texture (int num_args, bool is_ctor, intptr_t magic);
static bool js_Texture_get_fileName (int num_args, bool is_ctor, intptr_t magic);
static bool js_Texture_get_height (int num_args, bool is_ctor, intptr_t magic);
static bool js_Texture_get_width (int num_args, bool is_ctor, intptr_t magic);
static bool js_Texture_download (int num_args, bool is_ctor, intptr_t magic);
static bool js_Texture_upload (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_Transform (int num_args, bool is_ctor, intptr_t magic);
static bool js_Transform_get_matrix (int num_args, bool is_ctor, intptr_t magic);
static bool js_Transform_set_matrix (int num_args, bool is_ctor, intptr_t magic);
static bool js_Transform_compose (int num_args, bool is_ctor, intptr_t magic);
static bool js_Transform_identity (int num_args, bool is_ctor, intptr_t magic);
static bool js_Transform_project2D (int num_args, bool is_ctor, intptr_t magic);
static bool js_Transform_project3D (int num_args, bool is_ctor, intptr_t magic);
static bool js_Transform_rotate (int num_args, bool is_ctor, intptr_t magic);
static bool js_Transform_scale (int num_args, bool is_ctor, intptr_t magic);
static bool js_Transform_translate (int num_args, bool is_ctor, intptr_t magic);
static bool js_new_VertexList (int num_args, bool is_ctor, intptr_t magic);
static bool js_Z_deflate (int num_args, bool is_ctor, intptr_t magic);
static bool js_Z_inflate (int num_args, bool is_ctor, intptr_t magic);
static void js_Color_finalize (void* host_ptr);
static void js_DirectoryStream_finalize (void* host_ptr);
static void js_FileStream_finalize (void* host_ptr);
static void js_Font_finalize (void* host_ptr);
static void js_IndexList_finalize (void* host_ptr);
static void js_JobToken_finalize (void* host_ptr);
static void js_Joystick_finalize (void* host_ptr);
static void js_Mixer_finalize (void* host_ptr);
static void js_Model_finalize (void* host_ptr);
static void js_RNG_finalize (void* host_ptr);
static void js_Sample_finalize (void* host_ptr);
static void js_Server_finalize (void* host_ptr);
static void js_Shader_finalize (void* host_ptr);
static void js_Shape_finalize (void* host_ptr);
static void js_Socket_finalize (void* host_ptr);
static void js_Sound_finalize (void* host_ptr);
static void js_SoundStream_finalize (void* host_ptr);
static void js_TextDecoder_finalize (void* host_ptr);
static void js_TextEncoder_finalize (void* host_ptr);
static void js_Texture_finalize (void* host_ptr);
static void js_Transform_finalize (void* host_ptr);
static void js_VertexList_finalize (void* host_ptr);
static void cache_value_to_this (const char* key);
static void create_joystick_objects (void);
static path_t* find_module_file (const char* id, const char* origin, const char* sys_origin, bool es6_mode);
static bool handle_main_event_loop (int num_args, bool is_ctor, intptr_t magic);
static void handle_module_import (void);
static void jsal_pegasus_push_color (color_t color, bool in_ctor);
static void jsal_pegasus_push_job_token (int64_t token);
static void jsal_pegasus_push_require (const char* module_id);
static color_t jsal_pegasus_require_color (int index);
static script_t* jsal_pegasus_require_script (int index);
static path_t* load_package_json (const char* filename);
static int s_api_level;
static int s_api_level_nominal;
static mixer_t* s_def_mixer;
static int s_frame_rate = 60;
static int s_next_module_id = 1;
static js_ref_t* s_screen_obj;
static bool s_shutting_down = false;
static js_ref_t* s_key_color;
static js_ref_t* s_key_done;
static js_ref_t* s_key_get;
static js_ref_t* s_key_inBackground;
static js_ref_t* s_key_priority;
static js_ref_t* s_key_set;
static js_ref_t* s_key_stack;
static js_ref_t* s_key_u;
static js_ref_t* s_key_v;
static js_ref_t* s_key_value;
static js_ref_t* s_key_x;
static js_ref_t* s_key_y;
static js_ref_t* s_key_z;
void
pegasus_init(int api_level)
{
const struct x11_color* p;
console_log(1, "initializing Sphere v%d L%d API", API_VERSION, api_level);
s_api_level = api_level;
s_def_mixer = mixer_new(44100, 16, 2);
// only advertise highest stable API level; games must test for experimental
// features on an individual basis.
s_api_level_nominal = s_api_level;
if (s_api_level_nominal > SPHERE_API_LEVEL_STABLE)
s_api_level_nominal = SPHERE_API_LEVEL_STABLE;
jsal_on_import_module(handle_module_import);
s_key_color = jsal_new_key("color");
s_key_done = jsal_new_key("done");
s_key_get = jsal_new_key("get");
s_key_inBackground = jsal_new_key("inBackground");
s_key_priority = jsal_new_key("priority");
s_key_set = jsal_new_key("set");
s_key_stack = jsal_new_key("stack");
s_key_u = jsal_new_key("u");
s_key_v = jsal_new_key("v");
s_key_value = jsal_new_key("value");
s_key_x = jsal_new_key("x");
s_key_y = jsal_new_key("y");
s_key_z = jsal_new_key("z");
// initialize CommonJS cache and global require()
jsal_push_hidden_stash();
jsal_push_new_bare_object();
jsal_put_prop_string(-2, "moduleCache");
jsal_pop(1);
jsal_push_global_object();
jsal_pegasus_push_require(NULL);
jsal_to_propdesc_value(true, false, true);
jsal_def_prop_string(-2, "require");
jsal_pop(1);
// initialize the Sphere v2 API
api_define_function(NULL, "print", js_SSj_log, KI_LOG_NORMAL);
api_define_static_prop("Sphere", "APILevel", js_Sphere_get_APILevel, NULL);
api_define_static_prop("Sphere", "Compiler", js_Sphere_get_Compiler, NULL);
api_define_static_prop("Sphere", "Engine", js_Sphere_get_Engine, NULL);
api_define_static_prop("Sphere", "Game", js_Sphere_get_Game, NULL);
api_define_static_prop("Sphere", "Version", js_Sphere_get_Version, NULL);
api_define_static_prop("Sphere", "frameRate", js_Sphere_get_frameRate, js_Sphere_set_frameRate);
api_define_static_prop("Sphere", "frameSkip", js_Sphere_get_frameSkip, js_Sphere_set_frameSkip);
api_define_static_prop("Sphere", "fullScreen", js_Sphere_get_fullScreen, js_Sphere_set_fullScreen);
api_define_static_prop("Sphere", "main", js_Sphere_get_main, NULL);
api_define_function("Sphere", "abort", js_Sphere_abort, 0);
api_define_function("Sphere", "now", js_Sphere_now, 0);
api_define_function("Sphere", "restart", js_Sphere_restart, 0);
api_define_function("Sphere", "setResolution", js_Sphere_setResolution, 0);
api_define_function("Sphere", "shutDown", js_Sphere_shutDown, 0);
api_define_function("Sphere", "sleep", js_Sphere_sleep, 0);
api_define_class("Color", PEGASUS_COLOR, js_new_Color, js_Color_finalize, 0);
api_define_function("Color", "is", js_Color_is, 0);
api_define_function("Color", "mix", js_Color_mix, 0);
api_define_function("Color", "of", js_Color_of, 0);
api_define_property("Color", "name", false, js_Color_get_name, NULL);
api_define_property("Color", "r", true, js_Color_get_r, js_Color_set_r);
api_define_property("Color", "g", true, js_Color_get_g, js_Color_set_g);
api_define_property("Color", "b", true, js_Color_get_b, js_Color_set_b);
api_define_property("Color", "a", true, js_Color_get_a, js_Color_set_a);
api_define_method("Color", "clone", js_Color_clone, 0);
api_define_method("Color", "fadeTo", js_Color_fadeTo, 0);
api_define_class("DirectoryStream", PEGASUS_DIR_STREAM, js_new_DirectoryStream, js_DirectoryStream_finalize, 0);
api_define_property("DirectoryStream", "fileCount", false, js_DirectoryStream_get_fileCount, NULL);
api_define_property("DirectoryStream", "fileName", false, js_DirectoryStream_get_fileName, NULL);
api_define_property("DirectoryStream", "position", false, js_DirectoryStream_get_position, js_DirectoryStream_set_position);
api_define_method("DirectoryStream", "@@iterator", js_DirectoryStream_iterator, 0);
api_define_method("DirectoryStream", "dispose", js_DirectoryStream_dispose, 0);
api_define_method("DirectoryStream", "next", js_DirectoryStream_next, 0);
api_define_method("DirectoryStream", "rewind", js_DirectoryStream_rewind, 0);
api_define_function("Dispatch", "cancelAll", js_Dispatch_cancelAll, 0);
api_define_function("Dispatch", "later", js_Dispatch_later, 0);
api_define_function("Dispatch", "now", js_Dispatch_now, 0);
api_define_function("Dispatch", "onRender", js_Dispatch_onRender, 0);
api_define_function("Dispatch", "onUpdate", js_Dispatch_onUpdate, 0);
api_define_class("FileStream", PEGASUS_FILE_STREAM, js_new_FileStream, js_FileStream_finalize, 0);
api_define_method("FileStream", "dispose", js_FileStream_dispose, 0);
api_define_property("FileStream", "fileName", false, js_FileStream_get_fileName, NULL);
api_define_property("FileStream", "fileSize", false, js_FileStream_get_fileSize, NULL);
api_define_property("FileStream", "position", false, js_FileStream_get_position, js_FileStream_set_position);
api_define_method("FileStream", "read", js_FileStream_read, 0);
api_define_method("FileStream", "write", js_FileStream_write, 0);
api_define_class("Font", PEGASUS_FONT, js_new_Font, js_Font_finalize, 0);
api_define_static_prop("Font", "Default", js_Font_get_Default, NULL);
api_define_property("Font", "fileName", false, js_Font_get_fileName, NULL);
api_define_property("Font", "height", false, js_Font_get_height, NULL);
api_define_method("Font", "drawText", js_Font_drawText, 0);
api_define_method("Font", "getTextSize", js_Font_getTextSize, 0);
api_define_method("Font", "wordWrap", js_Font_wordWrap, 0);
api_define_function("FS", "createDirectory", js_FS_createDirectory, 0);
api_define_function("FS", "deleteFile", js_FS_deleteFile, 0);
api_define_function("FS", "directoryExists", js_FS_directoryExists, 0);
api_define_function("FS", "evaluateScript", js_FS_evaluateScript, 0);
api_define_function("FS", "fileExists", js_FS_fileExists, 0);
api_define_function("FS", "fullPath", js_FS_fullPath, 0);
api_define_function("FS", "readFile", js_FS_readFile, 0);
api_define_function("FS", "relativePath", js_FS_relativePath, 0);
api_define_function("FS", "removeDirectory", js_FS_removeDirectory, 0);
api_define_function("FS", "rename", js_FS_rename, 0);
api_define_function("FS", "writeFile", js_FS_writeFile, 0);
api_define_class("IndexList", PEGASUS_INDEX_LIST, js_new_IndexList, js_IndexList_finalize, 0);
api_define_class("JobToken", PEGASUS_JOB_TOKEN, NULL, js_JobToken_finalize, 0);
api_define_method("JobToken", "cancel", js_JobToken_cancel, 0);
api_define_class("Joystick", PEGASUS_JOYSTICK, NULL, js_Joystick_finalize, 0);
api_define_static_prop("Joystick", "Null", js_Joystick_get_Null, NULL);
api_define_function("Joystick", "getDevices", js_Joystick_getDevices, 0);
api_define_property("Joystick", "name", false, js_Joystick_get_name, NULL);
api_define_property("Joystick", "numAxes", false, js_Joystick_get_numAxes, NULL);
api_define_property("Joystick", "numButtons", false, js_Joystick_get_numButtons, NULL);
api_define_method("Joystick", "getPosition", js_Joystick_getPosition, 0);
api_define_method("Joystick", "isPressed", js_Joystick_isPressed, 0);
api_define_class("Keyboard", PEGASUS_KEYBOARD, NULL, NULL, 0);
api_define_static_prop("Keyboard", "Default", js_Keyboard_get_Default, NULL);
api_define_property("Keyboard", "capsLock", false, js_Keyboard_get_capsLock, NULL);
api_define_property("Keyboard", "numLock", false, js_Keyboard_get_numLock, NULL);
api_define_property("Keyboard", "scrollLock", false, js_Keyboard_get_scrollLock, NULL);
api_define_method("Keyboard", "charOf", js_Keyboard_charOf, 0);
api_define_method("Keyboard", "clearQueue", js_Keyboard_clearQueue, 0);
api_define_method("Keyboard", "getKey", js_Keyboard_getKey, 0);
api_define_method("Keyboard", "isPressed", js_Keyboard_isPressed, 0);
api_define_class("Mixer", PEGASUS_MIXER, js_new_Mixer, js_Mixer_finalize, 0);
api_define_static_prop("Mixer", "Default", js_Mixer_get_Default, NULL);
api_define_property("Mixer", "volume", false, js_Mixer_get_volume, js_Mixer_set_volume);
api_define_class("Model", PEGASUS_MODEL, js_new_Model, js_Model_finalize, 0);
api_define_property("Model", "shader", false, js_Model_get_shader, js_Model_set_shader);
api_define_property("Model", "transform", false, js_Model_get_transform, js_Model_set_transform);
api_define_method("Model", "draw", js_Model_draw, 0);
api_define_class("Mouse", PEGASUS_MOUSE, NULL, NULL, 0);
api_define_static_prop("Mouse", "Default", js_Mouse_get_Default, NULL);
api_define_property("Mouse", "x", false, js_Mouse_get_x, NULL);
api_define_property("Mouse", "y", false, js_Mouse_get_y, NULL);
api_define_method("Mouse", "clearQueue", js_Mouse_clearQueue, 0);
api_define_method("Mouse", "getEvent", js_Mouse_getEvent, 0);
api_define_method("Mouse", "isPressed", js_Mouse_isPressed, 0);
api_define_class("RNG", PEGASUS_RNG, js_new_RNG, js_RNG_finalize, 0);
api_define_function("RNG", "fromSeed", js_RNG_fromSeed, 0);
api_define_function("RNG", "fromState", js_RNG_fromState, 0);
api_define_property("RNG", "state", false, js_RNG_get_state, js_RNG_set_state);
api_define_method("RNG", "@@iterator", js_RNG_iterator, 0);
api_define_method("RNG", "next", js_RNG_next, 0);
api_define_function("SSj", "flipScreen", js_SSj_flipScreen, 0);
api_define_function("SSj", "instrument", js_SSj_instrument, 0);
api_define_function("SSj", "log", js_SSj_log, KI_LOG_NORMAL);
api_define_function("SSj", "now", js_SSj_now, 0);
api_define_function("SSj", "profile", js_SSj_profile, 0);
api_define_function("SSj", "trace", js_SSj_log, KI_LOG_TRACE);
api_define_class("Sample", PEGASUS_SAMPLE, js_new_Sample, js_Sample_finalize, 0);
api_define_property("Sample", "fileName", false, js_Sample_get_fileName, NULL);
api_define_method("Sample", "play", js_Sample_play, 0);
api_define_method("Sample", "stopAll", js_Sample_stopAll, 0);
api_define_class("Server", PEGASUS_SERVER, js_new_Server, js_Server_finalize, 0);
api_define_method("Server", "close", js_Server_close, 0);
api_define_method("Server", "accept", js_Server_accept, 0);
api_define_class("Shader", PEGASUS_SHADER, js_new_Shader, js_Shader_finalize, 0);
api_define_static_prop("Shader", "Default", js_Shader_get_Default, NULL);
api_define_method("Shader", "clone", js_Shader_clone, 0);
api_define_method("Shader", "setBoolean", js_Shader_setBoolean, 0);
api_define_method("Shader", "setColorVector", js_Shader_setColorVector, 0);
api_define_method("Shader", "setFloat", js_Shader_setFloat, 0);
api_define_method("Shader", "setFloatArray", js_Shader_setFloatArray, 0);
api_define_method("Shader", "setFloatVector", js_Shader_setFloatVector, 0);
api_define_method("Shader", "setInt", js_Shader_setInt, 0);
api_define_method("Shader", "setIntArray", js_Shader_setIntArray, 0);
api_define_method("Shader", "setIntVector", js_Shader_setIntVector, 0);
api_define_method("Shader", "setMatrix", js_Shader_setMatrix, 0);
api_define_class("Shape", PEGASUS_SHAPE, js_new_Shape, js_Shape_finalize, 0);
api_define_property("Shape", "indexList", false, js_Shape_get_indexList, js_Shape_set_indexList);
api_define_property("Shape", "texture", false, js_Shape_get_texture, js_Shape_set_texture);
api_define_property("Shape", "vertexList", false, js_Shape_get_vertexList, js_Shape_set_vertexList);
api_define_method("Shape", "draw", js_Shape_draw, 0);
api_define_class("Socket", PEGASUS_SOCKET, js_new_Socket, js_Socket_finalize, 0);
api_define_property("Socket", "bytesPending", false, js_Socket_get_bytesPending, NULL);
api_define_property("Socket", "connected", false, js_Socket_get_connected, NULL);
api_define_property("Socket", "remoteAddress", false, js_Socket_get_remoteAddress, NULL);
api_define_property("Socket", "remotePort", false, js_Socket_get_remotePort, NULL);
api_define_method("Socket", "close", js_Socket_close, 0);
api_define_method("Socket", "connectTo", js_Socket_connectTo, 0);
api_define_method("Socket", "read", js_Socket_read, 0);
api_define_method("Socket", "write", js_Socket_write, 0);
api_define_class("Sound", PEGASUS_SOUND, js_new_Sound, js_Sound_finalize, 0);
api_define_property("Sound", "fileName", false, js_Sound_get_fileName, NULL);
api_define_property("Sound", "length", false, js_Sound_get_length, NULL);
api_define_property("Sound", "pan", false, js_Sound_get_pan, js_Sound_set_pan);
api_define_property("Sound", "playing", false, js_Sound_get_playing, NULL);
api_define_property("Sound", "position", false, js_Sound_get_position, js_Sound_set_position);
api_define_property("Sound", "repeat", false, js_Sound_get_repeat, js_Sound_set_repeat);
api_define_property("Sound", "speed", false, js_Sound_get_speed, js_Sound_set_speed);
api_define_property("Sound", "volume", false, js_Sound_get_volume, js_Sound_set_volume);
api_define_method("Sound", "pause", js_Sound_pause, 0);
api_define_method("Sound", "play", js_Sound_play, 0);
api_define_method("Sound", "stop", js_Sound_stop, 0);
api_define_class("SoundStream", PEGASUS_SOUND_STREAM, js_new_SoundStream, js_SoundStream_finalize, 0);
api_define_property("SoundStream", "length", false, js_SoundStream_get_length, NULL);
api_define_method("SoundStream", "pause", js_SoundStream_pause, 0);
api_define_method("SoundStream", "play", js_SoundStream_play, 0);
api_define_method("SoundStream", "stop", js_SoundStream_stop, 0);
api_define_method("SoundStream", "write", js_SoundStream_write, 0);
api_define_class("TextDecoder", PEGASUS_TEXT_DEC, js_new_TextDecoder, js_TextDecoder_finalize, 0);
api_define_property("TextDecoder", "encoding", false, js_TextDecoder_get_encoding, NULL);
api_define_property("TextDecoder", "fatal", false, js_TextDecoder_get_fatal, NULL);
api_define_property("TextDecoder", "ignoreBOM", false, js_TextDecoder_get_ignoreBOM, NULL);
api_define_method("TextDecoder", "decode", js_TextDecoder_decode, 0);
api_define_class("TextEncoder", PEGASUS_TEXT_ENC, js_new_TextEncoder, js_TextEncoder_finalize, 0);
api_define_property("TextEncoder", "encoding", false, js_TextEncoder_get_encoding, NULL);
api_define_method("TextEncoder", "encode", js_TextEncoder_encode, 0);
api_define_class("Texture", PEGASUS_TEXTURE, js_new_Texture, js_Texture_finalize, PEGASUS_TEXTURE);
api_define_property("Texture", "fileName", false, js_Texture_get_fileName, NULL);
api_define_property("Texture", "height", false, js_Texture_get_height, NULL);
api_define_property("Texture", "width", false, js_Texture_get_width, NULL);
api_define_class("Transform", PEGASUS_TRANSFORM, js_new_Transform, js_Transform_finalize, 0);
api_define_property("Transform", "matrix", false, js_Transform_get_matrix, NULL);
api_define_method("Transform", "compose", js_Transform_compose, 0);
api_define_method("Transform", "identity", js_Transform_identity, 0);
api_define_method("Transform", "project2D", js_Transform_project2D, 0);
api_define_method("Transform", "project3D", js_Transform_project3D, 0);
api_define_method("Transform", "rotate", js_Transform_rotate, 0);
api_define_method("Transform", "scale", js_Transform_scale, 0);
api_define_method("Transform", "translate", js_Transform_translate, 0);
api_define_class("VertexList", PEGASUS_VERTEX_LIST, js_new_VertexList, js_VertexList_finalize, 0);
api_define_subclass("Surface", PEGASUS_SURFACE, PEGASUS_TEXTURE, js_new_Texture, js_Texture_finalize, PEGASUS_SURFACE);
api_define_static_prop("Surface", "Screen", js_Surface_get_Screen, NULL);
api_define_property("Surface", "height", false, js_Surface_get_height, 0);
api_define_property("Surface", "transform", false, js_Surface_get_transform, js_Surface_set_transform);
api_define_property("Surface", "width", false, js_Surface_get_width, 0);
api_define_method("Surface", "clipTo", js_Surface_clipTo, 0);
api_define_method("Surface", "toTexture", js_Surface_toTexture, 0);
api_define_const("FileOp", "Read", FILE_OP_READ);
api_define_const("FileOp", "Write", FILE_OP_WRITE);
api_define_const("FileOp", "Update", FILE_OP_UPDATE);
api_define_const("Key", "Alt", ALLEGRO_KEY_ALT);
api_define_const("Key", "AltGr", ALLEGRO_KEY_ALTGR);
api_define_const("Key", "Apostrophe", ALLEGRO_KEY_QUOTE);
api_define_const("Key", "Backslash", ALLEGRO_KEY_BACKSLASH);
api_define_const("Key", "Backspace", ALLEGRO_KEY_BACKSPACE);
api_define_const("Key", "CapsLock", ALLEGRO_KEY_CAPSLOCK);
api_define_const("Key", "CloseBrace", ALLEGRO_KEY_CLOSEBRACE);
api_define_const("Key", "Comma", ALLEGRO_KEY_COMMA);
api_define_const("Key", "Delete", ALLEGRO_KEY_DELETE);
api_define_const("Key", "Down", ALLEGRO_KEY_DOWN);
api_define_const("Key", "End", ALLEGRO_KEY_END);
api_define_const("Key", "Enter", ALLEGRO_KEY_ENTER);
api_define_const("Key", "Equals", ALLEGRO_KEY_EQUALS);
api_define_const("Key", "Escape", ALLEGRO_KEY_ESCAPE);
api_define_const("Key", "F1", ALLEGRO_KEY_F1);
api_define_const("Key", "F2", ALLEGRO_KEY_F2);
api_define_const("Key", "F3", ALLEGRO_KEY_F3);
api_define_const("Key", "F4", ALLEGRO_KEY_F4);
api_define_const("Key", "F5", ALLEGRO_KEY_F5);
api_define_const("Key", "F6", ALLEGRO_KEY_F6);
api_define_const("Key", "F7", ALLEGRO_KEY_F7);
api_define_const("Key", "F8", ALLEGRO_KEY_F8);
api_define_const("Key", "F9", ALLEGRO_KEY_F9);
api_define_const("Key", "F10", ALLEGRO_KEY_F10);
api_define_const("Key", "F11", ALLEGRO_KEY_F11);
api_define_const("Key", "F12", ALLEGRO_KEY_F12);
api_define_const("Key", "Home", ALLEGRO_KEY_HOME);
api_define_const("Key", "Hyphen", ALLEGRO_KEY_MINUS);
api_define_const("Key", "Insert", ALLEGRO_KEY_INSERT);
api_define_const("Key", "LCtrl", ALLEGRO_KEY_LCTRL);
api_define_const("Key", "LShift", ALLEGRO_KEY_LSHIFT);
api_define_const("Key", "Left", ALLEGRO_KEY_LEFT);
api_define_const("Key", "NumLock", ALLEGRO_KEY_NUMLOCK);
api_define_const("Key", "OpenBrace", ALLEGRO_KEY_OPENBRACE);
api_define_const("Key", "PageDown", ALLEGRO_KEY_PGDN);
api_define_const("Key", "PageUp", ALLEGRO_KEY_PGUP);
api_define_const("Key", "Period", ALLEGRO_KEY_FULLSTOP);
api_define_const("Key", "RCtrl", ALLEGRO_KEY_RCTRL);
api_define_const("Key", "RShift", ALLEGRO_KEY_RSHIFT);
api_define_const("Key", "Right", ALLEGRO_KEY_RIGHT);
api_define_const("Key", "ScrollLock", ALLEGRO_KEY_SCROLLLOCK);
api_define_const("Key", "Semicolon", ALLEGRO_KEY_SEMICOLON);
api_define_const("Key", "Slash", ALLEGRO_KEY_SLASH);
api_define_const("Key", "Space", ALLEGRO_KEY_SPACE);
api_define_const("Key", "Tab", ALLEGRO_KEY_TAB);
api_define_const("Key", "Tilde", ALLEGRO_KEY_TILDE);
api_define_const("Key", "Up", ALLEGRO_KEY_UP);
api_define_const("Key", "A", ALLEGRO_KEY_A);
api_define_const("Key", "B", ALLEGRO_KEY_B);
api_define_const("Key", "C", ALLEGRO_KEY_C);
api_define_const("Key", "D", ALLEGRO_KEY_D);
api_define_const("Key", "E", ALLEGRO_KEY_E);
api_define_const("Key", "F", ALLEGRO_KEY_F);
api_define_const("Key", "G", ALLEGRO_KEY_G);
api_define_const("Key", "H", ALLEGRO_KEY_H);
api_define_const("Key", "I", ALLEGRO_KEY_I);
api_define_const("Key", "J", ALLEGRO_KEY_J);
api_define_const("Key", "K", ALLEGRO_KEY_K);
api_define_const("Key", "L", ALLEGRO_KEY_L);
api_define_const("Key", "M", ALLEGRO_KEY_M);
api_define_const("Key", "N", ALLEGRO_KEY_N);
api_define_const("Key", "O", ALLEGRO_KEY_O);
api_define_const("Key", "P", ALLEGRO_KEY_P);
api_define_const("Key", "Q", ALLEGRO_KEY_Q);
api_define_const("Key", "R", ALLEGRO_KEY_R);
api_define_const("Key", "S", ALLEGRO_KEY_S);
api_define_const("Key", "T", ALLEGRO_KEY_T);
api_define_const("Key", "U", ALLEGRO_KEY_U);
api_define_const("Key", "V", ALLEGRO_KEY_V);
api_define_const("Key", "W", ALLEGRO_KEY_W);
api_define_const("Key", "X", ALLEGRO_KEY_X);
api_define_const("Key", "Y", ALLEGRO_KEY_Y);
api_define_const("Key", "Z", ALLEGRO_KEY_Z);
api_define_const("Key", "D1", ALLEGRO_KEY_1);
api_define_const("Key", "D2", ALLEGRO_KEY_2);
api_define_const("Key", "D3", ALLEGRO_KEY_3);
api_define_const("Key", "D4", ALLEGRO_KEY_4);
api_define_const("Key", "D5", ALLEGRO_KEY_5);
api_define_const("Key", "D6", ALLEGRO_KEY_6);
api_define_const("Key", "D7", ALLEGRO_KEY_7);
api_define_const("Key", "D8", ALLEGRO_KEY_8);
api_define_const("Key", "D9", ALLEGRO_KEY_9);
api_define_const("Key", "D0", ALLEGRO_KEY_0);
api_define_const("Key", "NumPad1", ALLEGRO_KEY_PAD_1);
api_define_const("Key", "NumPad2", ALLEGRO_KEY_PAD_2);
api_define_const("Key", "NumPad3", ALLEGRO_KEY_PAD_3);
api_define_const("Key", "NumPad4", ALLEGRO_KEY_PAD_4);
api_define_const("Key", "NumPad5", ALLEGRO_KEY_PAD_5);
api_define_const("Key", "NumPad6", ALLEGRO_KEY_PAD_6);
api_define_const("Key", "NumPad7", ALLEGRO_KEY_PAD_7);
api_define_const("Key", "NumPad8", ALLEGRO_KEY_PAD_8);
api_define_const("Key", "NumPad9", ALLEGRO_KEY_PAD_9);
api_define_const("Key", "NumPad0", ALLEGRO_KEY_PAD_0);
api_define_const("Key", "NumPadEnter", ALLEGRO_KEY_PAD_ENTER);
api_define_const("Key", "Add", ALLEGRO_KEY_PAD_PLUS);
api_define_const("Key", "Decimal", ALLEGRO_KEY_PAD_DELETE);
api_define_const("Key", "Divide", ALLEGRO_KEY_PAD_SLASH);
api_define_const("Key", "Multiply", ALLEGRO_KEY_PAD_ASTERISK);
api_define_const("Key", "Subtract", ALLEGRO_KEY_PAD_MINUS);
api_define_const("MouseKey", "Left", MOUSE_KEY_LEFT);
api_define_const("MouseKey", "Right", MOUSE_KEY_RIGHT);
api_define_const("MouseKey", "Middle", MOUSE_KEY_MIDDLE);
api_define_const("MouseKey", "WheelUp", MOUSE_KEY_WHEEL_UP);
api_define_const("MouseKey", "WheelDown", MOUSE_KEY_WHEEL_DOWN);
api_define_const("ShapeType", "Fan", SHAPE_TRI_FAN);
api_define_const("ShapeType", "Lines", SHAPE_LINES);
api_define_const("ShapeType", "LineLoop", SHAPE_LINE_LOOP);
api_define_const("ShapeType", "LineStrip", SHAPE_LINE_STRIP);
api_define_const("ShapeType", "Points", SHAPE_POINTS);
api_define_const("ShapeType", "Triangles", SHAPE_TRIANGLES);
api_define_const("ShapeType", "TriStrip", SHAPE_TRI_STRIP);
if (api_level >= 2) {
api_define_function("Dispatch", "onExit", js_Dispatch_onExit, 0);
api_define_function("FS", "directoryOf", js_FS_directoryOf, 0);
api_define_function("FS", "fileNameOf", js_FS_fileNameOf, 0);
api_define_method("JobToken", "pause", js_JobToken_pause_resume, (intptr_t)true);
api_define_method("JobToken", "resume", js_JobToken_pause_resume, (intptr_t)false);
api_define_function("Shape", "drawImmediate", js_Shape_drawImmediate, 0);
api_define_property("Surface", "blendOp", false, js_Surface_get_blendOp, js_Surface_set_blendOp);
api_define_method("Texture", "download", js_Texture_download, 0);
api_define_method("Texture", "upload", js_Texture_upload, 0);
api_define_function("Z", "deflate", js_Z_deflate, 0);
api_define_function("Z", "inflate", js_Z_inflate, 0);
api_define_const("BlendOp", "AlphaBlend", BLEND_NORMAL);
api_define_const("BlendOp", "Add", BLEND_ADD);
api_define_const("BlendOp", "Average", BLEND_AVERAGE);
api_define_const("BlendOp", "CopyAlpha", BLEND_COPY_ALPHA);
api_define_const("BlendOp", "CopyRGB", BLEND_COPY_RGB);
api_define_const("BlendOp", "Invert", BLEND_INVERT);
api_define_const("BlendOp", "Multiply", BLEND_MULTIPLY);
api_define_const("BlendOp", "Replace", BLEND_REPLACE);
api_define_const("BlendOp", "Subtract", BLEND_SUBTRACT);
}
// keep a local reference to Surface.Screen
jsal_get_global_string("Surface");
jsal_get_prop_string(-1, "Screen");
s_screen_obj = jsal_ref(-1);
jsal_pop(2);
// pre-create joystick objects for Joystick.getDevices(). this ensures that
// multiple requests for the device list return the same Joystick object(s).
create_joystick_objects();
// register predefined X11 colors
jsal_get_global_string("Color");
p = COLORS;
while (p->name != NULL) {
if (s_api_level >= p->api_level) {
jsal_push_new_function(js_Color_get_Color, "get", 0, (intptr_t)(p - COLORS));
jsal_to_propdesc_get(false, true);
jsal_def_prop_string(-2, p->name);
}
++p;
}
jsal_pop(1);
}
void
pegasus_uninit(void)
{
jsal_unref(s_screen_obj);
jsal_unref(s_key_color);
jsal_unref(s_key_done);
jsal_unref(s_key_get);
jsal_unref(s_key_inBackground);
jsal_unref(s_key_priority);
jsal_unref(s_key_set);
jsal_unref(s_key_stack);
jsal_unref(s_key_u);
jsal_unref(s_key_v);
jsal_unref(s_key_value);
jsal_unref(s_key_x);
jsal_unref(s_key_y);
jsal_unref(s_key_z);
mixer_unref(s_def_mixer);
}
bool
pegasus_start_event_loop(void)
{
if (jsal_try(handle_main_event_loop, 0)) {
jsal_pop(1); // don't need return value
return true;
}
else {
// leave the error for the caller, don't pop it off
return false;
}
}
bool
pegasus_try_require(const char* filename)
{
// HERE BE DRAGONS!
// this function is horrendous. JSAL's stack-based API is powerful, but gets very
// messy very quickly when dealing with object properties. I tried to add comments
// to illuminate what's going on, but it's still likely to be confusing for someone
// not familiar with Lua or similar APIs. proceed with caution.
// notes:
// - the final value of 'module.exports' is left on top of the JSAL value stack.
// - 'module.id' is set to the given filename. in order to guarantee proper cache
// behavior, the filename should be canonicalized first.
// - this is a protected call. if the module being loaded throws, this function will
// return false and the error will be left on top of the stack for the caller to deal
// with.
lstring_t* code_string;
path_t* dir_path;
path_t* file_path;
const char* file_type;
bool is_esm_module = false;
bool is_module_loaded;
size_t source_size;
char* source;
file_path = path_new(filename);
dir_path = path_strip(path_dup(file_path));
file_type = game_file_type(g_game, filename);
// evaluate `javascript-module` file type as an ES module
is_esm_module = strcmp(file_type, "javascript-module") == 0;
if (is_esm_module) {
source = game_read_file(g_game, filename, &source_size);
code_string = lstr_from_utf8(source, source_size, true);
free(source);
jsal_push_lstring_t(code_string);
debugger_add_source(filename, code_string);
is_module_loaded = jsal_try_eval_module(filename, debugger_source_name(filename));
lstr_free(code_string);
if (!is_module_loaded)
goto on_error;
goto have_module;
}
// is the requested module already in the cache?
jsal_push_hidden_stash();
jsal_get_prop_string(-1, "moduleCache");
if (jsal_get_prop_string(-1, filename)) {
jsal_remove(-2);
jsal_remove(-2);
goto have_module;
}
else {
jsal_pop(3);
}
console_log(1, "evaluating JS module '%s'", filename);
source = game_read_file(g_game, filename, &source_size);
code_string = lstr_from_utf8(source, source_size, true);
free(source);
// construct a CommonJS module object for the new module