-
-
Notifications
You must be signed in to change notification settings - Fork 488
/
js.c
1335 lines (1033 loc) · 36.3 KB
/
js.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
// MIT License
// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "core/core.h"
#include "tools.h"
#include <ctype.h>
#include <string.h>
#include <quickjs.h>
extern bool parse_note(const char* noteStr, s32* note, s32* octave);
static inline tic_core* getCore(JSContext *ctx)
{
return JS_GetContextOpaque(ctx);
}
static s32 getInteger(JSContext *ctx, JSValueConst val)
{
s32 res;
JS_ToInt32(ctx, &res, val);
return res;
}
static s32 getInteger2(JSContext *ctx, JSValueConst val, s32 defValue)
{
s32 res;
if(JS_IsUndefined(val))
{
res = defValue;
}
else
{
JS_ToInt32(ctx, &res, val);
}
return res;
}
static float getNumber(JSContext *ctx, JSValueConst val)
{
double res;
JS_ToFloat64(ctx, &res, val);
return res;
}
static void js_dump_obj(JSContext *ctx, FILE *f, JSValueConst val)
{
const char *str;
tic_core* core = getCore(ctx);
str = JS_ToCString(ctx, val);
if (str)
{
core->data->error(core->data->data, str);
JS_FreeCString(ctx, str);
}
else
{
core->data->error(core->data->data, "[exception]\n");
}
}
static void js_std_dump_error1(JSContext *ctx, JSValueConst exception_val)
{
JSValue val;
bool is_error;
is_error = JS_IsError(ctx, exception_val);
js_dump_obj(ctx, stdout, exception_val);
if (is_error)
{
val = JS_GetPropertyStr(ctx, exception_val, "stack");
if (!JS_IsUndefined(val))
{
js_dump_obj(ctx, stdout, val);
}
JS_FreeValue(ctx, val);
}
}
static void js_std_dump_error(JSContext *ctx)
{
JSValue exception_val;
exception_val = JS_GetException(ctx);
js_std_dump_error1(ctx, exception_val);
JS_FreeValue(ctx, exception_val);
}
static void closeJavascript(tic_mem* tic)
{
tic_core* core = (tic_core*)tic;
JSContext *ctx = core->currentVM;
if(ctx)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
core->currentVM = NULL;
}
}
static JSValue js_print(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
const char* text = JS_ToCStringLen(ctx, NULL, argv[0]);
s32 x = getInteger2(ctx, argv[1], 0);
s32 y = getInteger2(ctx, argv[2], 0);
s32 color = getInteger2(ctx, argv[3], TIC_DEFAULT_COLOR);
bool fixed = JS_ToBool(ctx, argv[4]);
s32 scale = getInteger2(ctx, argv[5], 1);
bool alt = JS_ToBool(ctx, argv[6]);
s32 size = core->api.print(tic, text ? text : "nil", x, y, color, fixed, scale, alt);
JS_FreeCString(ctx, text);
return JS_NewInt32(ctx, size);
}
static JSValue js_cls(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
s32 color = getInteger2(ctx, argv[0], tic_color_black);
core->api.cls(tic, color);
return JS_UNDEFINED;
}
static JSValue js_pix(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger2(ctx, argv[0], 0);
s32 y = getInteger2(ctx, argv[1], 0);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
if(JS_IsUndefined(argv[2]))
{
return JS_NewInt32(ctx, core->api.pix(tic, x, y, 0, true));
}
else
{
s32 color = getInteger(ctx, argv[2]);
core->api.pix(tic, x, y, color, false);
}
return JS_UNDEFINED;
}
static JSValue js_line(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
float x0 = getNumber(ctx, argv[0]);
float y0 = getNumber(ctx, argv[1]);
float x1 = getNumber(ctx, argv[2]);
float y1 = getNumber(ctx, argv[3]);
s32 color = getInteger2(ctx, argv[4], tic_color_black);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.line(tic, x0, y0, x1, y1, color);
return JS_UNDEFINED;
}
static JSValue js_rect(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger2(ctx, argv[0], 0);
s32 y = getInteger2(ctx, argv[1], 0);
s32 w = getInteger2(ctx, argv[2], 0);
s32 h = getInteger2(ctx, argv[3], 0);
s32 color = getInteger2(ctx, argv[4], 0);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.rect(tic, x, y, w, h, color);
return JS_UNDEFINED;
}
static JSValue js_rectb(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger2(ctx, argv[0], 0);
s32 y = getInteger2(ctx, argv[1], 0);
s32 w = getInteger2(ctx, argv[2], 0);
s32 h = getInteger2(ctx, argv[3], 0);
s32 color = getInteger2(ctx, argv[4], 0);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.rectb(tic, x, y, w, h, color);
return JS_UNDEFINED;
}
static JSValue js_spr(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
static u8 colors[TIC_PALETTE_SIZE];
s32 count = 0;
s32 index = getInteger2(ctx, argv[0], 0);
s32 x = getInteger2(ctx, argv[1], 0);
s32 y = getInteger2(ctx, argv[2], 0);
if(JS_IsArray(ctx, argv[3]))
{
for(s32 i = 0; i < TIC_PALETTE_SIZE; i++)
{
JSValue val = JS_GetPropertyUint32(ctx, argv[3], i);
colors[i] = getInteger2(ctx, val, -1);
count++;
}
}
else
{
colors[0] = getInteger2(ctx, argv[3], -1);
count = 1;
}
s32 scale = getInteger2(ctx, argv[4], 1);
tic_flip flip = JS_IsBool(argv[5])
? JS_ToBool(ctx, argv[5]) ? tic_horz_flip : tic_no_flip
: getInteger2(ctx, argv[5], tic_no_flip);
tic_rotate rotate = getInteger2(ctx, argv[6], tic_no_rotate);
s32 w = getInteger2(ctx, argv[7], 1);
s32 h = getInteger2(ctx, argv[8], 1);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.spr(tic, index, x, y, w, h, colors, count, scale, flip, rotate);
return JS_UNDEFINED;
}
static JSValue js_btn(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
return JS_IsUndefined(argv[0])
? JS_NewUint32(ctx, core->api.btn(tic, -1))
: JS_NewBool(ctx, core->api.btn(tic, getInteger(ctx, argv[0]) & 0x1f));
}
static JSValue js_btnp(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
if(JS_IsUndefined(argv[0]))
{
return JS_NewUint32(ctx, core->api.btnp(tic, -1, -1, -1));
}
s32 index = getInteger(ctx, argv[0]);
u32 hold = getInteger2(ctx, argv[1], -1);
u32 period = getInteger2(ctx, argv[2], -1);
return JS_NewBool(ctx, core->api.btnp(tic, index & 0x1f, hold, period));
}
static JSValue js_key(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx);
tic_mem* tic = &core->memory;
if (JS_IsUndefined(argv[0]))
return JS_NewBool(ctx, core->api.key(tic, tic_key_unknown));
tic_key key = getInteger(ctx, argv[0]);
if(key < tic_keys_count)
return JS_NewBool(ctx, core->api.key(tic, key));
else
{
JSValue err = JS_NewError(ctx);
JS_SetPropertyStr(ctx, err, "message", JS_NewString(ctx, "unknown keyboard code"));
JS_Throw(ctx, err);
}
return JS_UNDEFINED;
}
static void throwError(JSContext* ctx, const char* message)
{
JSValue err = JS_NewError(ctx);
JS_SetPropertyStr(ctx, err, "message", JS_NewString(ctx, message));
JS_Throw(ctx, err);
}
static JSValue js_keyp(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx);
tic_mem* tic = &core->memory;
if (JS_IsUndefined(argv[0]))
return JS_NewBool(ctx, core->api.keyp(tic, tic_key_unknown, -1, -1));
tic_key key = getInteger(ctx, argv[0]);
if(key < tic_keys_count)
{
u32 hold = getInteger2(ctx, argv[1], -1);
u32 period = getInteger2(ctx, argv[2], -1);
return JS_NewBool(ctx, core->api.keyp(tic, key, hold, period));
}
else
{
throwError(ctx, "unknown keyboard code");
}
return JS_UNDEFINED;
}
static JSValue js_sfx(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
s32 index = getInteger2(ctx, argv[0], -1);
s32 note = -1;
s32 octave = -1;
s32 speed = SFX_DEF_SPEED;
if (index < SFX_COUNT)
{
if(index >= 0)
{
tic_sample* effect = tic->ram->sfx.samples.data + index;
note = effect->note;
octave = effect->octave;
speed = effect->speed;
if(!JS_IsUndefined(argv[1]))
{
if(JS_IsString(argv[1]))
{
const char *noteStr = JS_ToCStringLen(ctx, NULL, argv[1]);
if(!parse_note(noteStr, ¬e, &octave))
{
throwError(ctx, "invalid note, should be like C#4");
}
JS_FreeCString(ctx, noteStr);
}
else
{
s32 id = getInteger(ctx, argv[1]);
note = id % NOTES;
octave = id / NOTES;
}
}
}
}
else
{
throwError(ctx, "unknown sfx index");
}
s32 duration = getInteger2(ctx, argv[2], -1);
s32 channel = getInteger2(ctx, argv[3], 0);
s32 volumes[TIC80_SAMPLE_CHANNELS];
if(JS_IsArray(ctx, argv[4]))
{
for(s32 i = 0; i < COUNT_OF(volumes); i++)
{
JSValue val = JS_GetPropertyUint32(ctx, argv[4], i);
volumes[i] = getInteger(ctx, val);
}
}
else volumes[0] = volumes[1] = getInteger2(ctx, argv[4], MAX_VOLUME);
speed = getInteger2(ctx, argv[5], speed);
if (channel >= 0 && channel < TIC_SOUND_CHANNELS)
{
core->api.sfx(tic, index, note, octave, duration, channel, volumes[0] & 0xf, volumes[1] & 0xf, speed);
}
else throwError(ctx, "unknown channel");
return JS_UNDEFINED;
}
typedef struct
{
JSContext* ctx;
JSValueConst func;
} RemapData;
static void remapCallback(void* data, s32 x, s32 y, RemapResult* result)
{
RemapData* remap = (RemapData*)data;
JSContext* ctx = remap->ctx;
JSValue res = JS_Call(ctx, remap->func, JS_UNDEFINED, 3,
(JSValueConst[])
{
JS_NewInt32(ctx, result->index),
JS_NewInt32(ctx, x),
JS_NewInt32(ctx, y),
});
if(JS_IsArray(ctx, res))
{
result->index = JS_IsUndefined(res) ? 0 : getInteger(ctx, JS_GetPropertyUint32(ctx, res, 0));
result->flip = getInteger2(ctx, JS_GetPropertyUint32(ctx, res, 1), result->flip);
result->rotate = getInteger2(ctx, JS_GetPropertyUint32(ctx, res, 2), result->rotate);
}
else
{
result->index = JS_IsUndefined(res) ? 0 : getInteger(ctx, res);
}
}
static JSValue js_map(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger2(ctx, argv[0], 0);
s32 y = getInteger2(ctx, argv[1], 0);
s32 w = getInteger2(ctx, argv[2], TIC_MAP_SCREEN_WIDTH);
s32 h = getInteger2(ctx, argv[3], TIC_MAP_SCREEN_HEIGHT);
s32 sx = getInteger2(ctx, argv[4], 0);
s32 sy = getInteger2(ctx, argv[5], 0);
s32 scale = getInteger2(ctx, argv[7], 1);
static u8 colors[TIC_PALETTE_SIZE];
s32 count = 0;
if(JS_IsArray(ctx, argv[6]))
{
for(s32 i = 0; i < TIC_PALETTE_SIZE; i++)
{
JSValue val = JS_GetPropertyUint32(ctx, argv[6], i);
colors[i] = getInteger2(ctx, val, -1);
count++;
}
}
else
{
colors[0] = getInteger2(ctx, argv[6], 0);
count = 1;
}
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
if(JS_IsFunction(ctx, argv[8]))
{
RemapData data = {ctx, argv[8]};
core->api.map((tic_mem*)getCore(ctx), x, y, w, h, sx, sy, colors, count, scale, remapCallback, &data);
}
else
{
core->api.map(tic, x, y, w, h, sx, sy, colors, count, scale, NULL, NULL);
}
return JS_UNDEFINED;
}
static JSValue js_mget(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger2(ctx, argv[0], 0);
s32 y = getInteger2(ctx, argv[1], 0);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
u8 value = core->api.mget(tic, x, y);
return JS_NewInt32(ctx, value);
}
static JSValue js_mset(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger2(ctx, argv[0], 0);
s32 y = getInteger2(ctx, argv[1], 0);
u8 value = getInteger2(ctx, argv[2], 0);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.mset(tic, x, y, value);
return JS_UNDEFINED;
}
static JSValue js_peek(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 address = getInteger(ctx, argv[0]);
s32 bits = getInteger2(ctx, argv[1], BITS_IN_BYTE);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
return JS_NewInt32(ctx, core->api.peek(tic, address, bits));
}
static JSValue js_poke(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 address = getInteger(ctx, argv[0]);
u8 value = getInteger(ctx, argv[1]);
s32 bits = getInteger2(ctx, argv[2], BITS_IN_BYTE);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.poke(tic, address, value, bits);
return JS_UNDEFINED;
}
static JSValue js_peek1(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 address = getInteger(ctx, argv[0]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
return JS_NewInt32(ctx, core->api.peek1(tic, address));
}
static JSValue js_poke1(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 address = getInteger(ctx, argv[0]);
u8 value = getInteger(ctx, argv[1]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.poke1(tic, address, value);
return JS_UNDEFINED;
}
static JSValue js_peek2(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 address = getInteger(ctx, argv[0]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
return JS_NewInt32(ctx, core->api.peek2(tic, address));
}
static JSValue js_poke2(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 address = getInteger(ctx, argv[0]);
u8 value = getInteger(ctx, argv[1]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.poke2(tic, address, value);
return JS_UNDEFINED;
}
static JSValue js_peek4(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 address = getInteger(ctx, argv[0]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
return JS_NewInt32(ctx, core->api.peek4(tic, address));
}
static JSValue js_poke4(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 address = getInteger(ctx, argv[0]);
u8 value = getInteger(ctx, argv[1]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.poke4(tic, address, value);
return JS_UNDEFINED;
}
static JSValue js_memcpy(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 dest = getInteger(ctx, argv[0]);
s32 src = getInteger(ctx, argv[1]);
s32 size = getInteger(ctx, argv[2]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.memcpy(tic, dest, src, size);
return JS_UNDEFINED;
}
static JSValue js_memset(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 dest = getInteger(ctx, argv[0]);
u8 value = getInteger(ctx, argv[1]);
s32 size = getInteger(ctx, argv[2]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.memset(tic, dest, value, size);
return JS_UNDEFINED;
}
static JSValue js_trace(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
const char* text = JS_ToCString(ctx, argv[0]);
u8 color = getInteger2(ctx, argv[1], TIC_DEFAULT_COLOR);
core->api.trace(tic, text, color);
JS_FreeCString(ctx, text);
return JS_UNDEFINED;
}
static JSValue js_pmem(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
u32 index = getInteger(ctx, argv[0]);
if(index < TIC_PERSISTENT_SIZE)
{
u32 val = core->api.pmem(tic, index, 0, false);
if(!JS_IsUndefined(argv[1]))
{
core->api.pmem(tic, index, getInteger(ctx, argv[1]), true);
}
return JS_NewInt32(ctx, val);
}
else throwError(ctx, "invalid persistent tic index");
return JS_UNDEFINED;
}
static JSValue js_time(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
return JS_NewFloat64(ctx, core->api.time(tic));
}
static JSValue js_tstamp(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
return JS_NewInt32(ctx, core->api.tstamp(tic));
}
static JSValue js_exit(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.exit(tic);
return JS_UNDEFINED;
}
static JSValue js_font(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
const char* text = JS_ToCString(ctx, argv[0]);
s32 x = getInteger(ctx, argv[1]);
s32 y = getInteger(ctx, argv[2]);
u8 chromakey = getInteger(ctx, argv[3]);
s32 width = getInteger2(ctx, argv[4], TIC_SPRITESIZE);
s32 height = getInteger2(ctx, argv[5], TIC_SPRITESIZE);
bool fixed = JS_ToBool(ctx, argv[6]);
s32 scale = getInteger2(ctx, argv[7], 1);
bool alt = JS_ToBool(ctx, argv[8]);
s32 size = scale ? core->api.font(tic, text, x, y, &chromakey, 1, width, height, fixed, scale, alt) : 0;
JS_FreeCString(ctx, text);
return JS_NewInt32(ctx, size);
}
static JSValue js_mouse(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx);
const tic80_mouse* mouse = &core->memory.ram->input.mouse;
JSValue arr = JS_NewArray(ctx);
tic_point pos = core->api.mouse((tic_mem*)core);
JS_SetPropertyUint32(ctx, arr, 0, JS_NewInt32(ctx, pos.x));
JS_SetPropertyUint32(ctx, arr, 1, JS_NewInt32(ctx, pos.y));
JS_SetPropertyUint32(ctx, arr, 2, JS_NewBool(ctx, mouse->left));
JS_SetPropertyUint32(ctx, arr, 3, JS_NewBool(ctx, mouse->middle));
JS_SetPropertyUint32(ctx, arr, 4, JS_NewBool(ctx, mouse->right));
JS_SetPropertyUint32(ctx, arr, 5, JS_NewInt32(ctx, mouse->scrollx));
JS_SetPropertyUint32(ctx, arr, 6, JS_NewInt32(ctx, mouse->scrolly));
return arr;
}
static JSValue js_circ(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger(ctx, argv[0]);
s32 y = getInteger(ctx, argv[1]);
s32 radius = getInteger(ctx, argv[2]);
s32 color = getInteger(ctx, argv[3]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.circ(tic, x, y, radius, color);
return JS_UNDEFINED;
}
static JSValue js_circb(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger(ctx, argv[0]);
s32 y = getInteger(ctx, argv[1]);
s32 radius = getInteger(ctx, argv[2]);
s32 color = getInteger(ctx, argv[3]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.circb(tic, x, y, radius, color);
return JS_UNDEFINED;
}
static JSValue js_elli(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger(ctx, argv[0]);
s32 y = getInteger(ctx, argv[1]);
s32 a = getInteger(ctx, argv[2]);
s32 b = getInteger(ctx, argv[3]);
s32 color = getInteger(ctx, argv[4]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.elli(tic, x, y, a, b, color);
return JS_UNDEFINED;
}
static JSValue js_ellib(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger(ctx, argv[0]);
s32 y = getInteger(ctx, argv[1]);
s32 a = getInteger(ctx, argv[2]);
s32 b = getInteger(ctx, argv[3]);
s32 color = getInteger(ctx, argv[4]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.ellib(tic, x, y, a, b, color);
return JS_UNDEFINED;
}
static JSValue js_paint(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger(ctx, argv[0]);
s32 y = getInteger(ctx, argv[1]);
s32 color = getInteger(ctx, argv[2]);
s32 bordercolor = getInteger2(ctx, argv[3], -1);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.paint(tic, x, y, color, bordercolor);
return JS_UNDEFINED;
}
static JSValue js_tri(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
float pt[6];
for(s32 i = 0; i < COUNT_OF(pt); i++)
pt[i] = getNumber(ctx, argv[i]);
s32 color = getInteger(ctx, argv[6]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.tri(tic, pt[0], pt[1], pt[2], pt[3], pt[4], pt[5], color);
return JS_UNDEFINED;
}
static JSValue js_trib(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
float pt[6];
for(s32 i = 0; i < COUNT_OF(pt); i++)
pt[i] = getNumber(ctx, argv[i]);
s32 color = getInteger(ctx, argv[6]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.trib(tic, pt[0], pt[1], pt[2], pt[3], pt[4], pt[5], color);
return JS_UNDEFINED;
}
#if defined(BUILD_DEPRECATED)
static JSValue js_textri(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
float pt[12];
for (s32 i = 0; i < COUNT_OF(pt); i++)
pt[i] = getNumber(ctx, argv[i]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
bool use_map = JS_ToBool(ctx, argv[12]);
static u8 colors[TIC_PALETTE_SIZE];
s32 count = 0;
if(JS_IsArray(ctx, argv[13]))
{
for(s32 i = 0; i < TIC_PALETTE_SIZE; i++)
{
JSValue val = JS_GetPropertyUint32(ctx, argv[13], i);
colors[i] = getInteger2(ctx, val, -1);
count++;
}
}
else
{
colors[0] = getInteger2(ctx, argv[13], 0);
count = 1;
}
core->api.textri(tic,
pt[0], pt[1], // xy 1
pt[2], pt[3], // xy 2
pt[4], pt[5], // xy 3
pt[6], pt[7], // uv 1
pt[8], pt[9], // uv 2
pt[10], pt[11], // uv 3
use_map, // use_map
colors, count); // chroma
return JS_UNDEFINED;
}
#endif
static JSValue js_ttri(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
float pt[12];
for (s32 i = 0; i < COUNT_OF(pt); i++)
pt[i] = getNumber(ctx, argv[i]);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
tic_texture_src src = getInteger(ctx, argv[12]);
static u8 colors[TIC_PALETTE_SIZE];
s32 count = 0;
if(JS_IsArray(ctx, argv[13]))
{
for(s32 i = 0; i < TIC_PALETTE_SIZE; i++)
{
JSValue val = JS_GetPropertyUint32(ctx, argv[13], i);
colors[i] = getInteger2(ctx, val, -1);
count++;
}
}
else
{
colors[0] = getInteger2(ctx, argv[13], 0);
count = 1;
}
float z[3];
bool depth = true;
for (s32 i = 0, index = 14; i < COUNT_OF(z); i++, index++)
{
if(JS_IsUndefined(argv[index])) depth = false;
else z[i] = getNumber(ctx, argv[index]);
}
core->api.ttri(tic, pt[0], pt[1], // xy 1
pt[2], pt[3], // xy 2
pt[4], pt[5], // xy 3
pt[6], pt[7], // uv 1
pt[8], pt[9], // uv 2
pt[10], pt[11], // uv 3
src, // texture source
colors, count, // chroma
z[0], z[1], z[2], depth); // depth
return JS_UNDEFINED;
}
static JSValue js_clip(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
s32 x = getInteger(ctx, argv[0]);
s32 y = getInteger(ctx, argv[1]);
s32 w = getInteger2(ctx, argv[2], TIC80_WIDTH);
s32 h = getInteger2(ctx, argv[3], TIC80_HEIGHT);
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
core->api.clip(tic, x, y, w, h);
return JS_UNDEFINED;
}
static JSValue js_music(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
s32 track = getInteger2(ctx, argv[0], -1);
core->api.music(tic, -1, 0, 0, false, false, -1, -1);
if(track >= 0)
{
if(track > MUSIC_TRACKS - 1)
{
throwError(ctx, "invalid music track index");
return JS_UNDEFINED;
}
s32 frame = getInteger2(ctx, argv[1], -1);
s32 row = getInteger2(ctx, argv[2], -1);
bool loop = JS_IsUndefined(argv[3]) ? true : JS_ToBool(ctx, argv[3]);
bool sustain = JS_ToBool(ctx, argv[4]);
s32 tempo = getInteger2(ctx, argv[5], -1);
s32 speed = getInteger2(ctx, argv[6], -1);
core->api.music(tic, track, frame, row, loop, sustain, tempo, speed);
}
return JS_UNDEFINED;
}
static JSValue js_vbank(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx);
tic_mem* tic = (tic_mem*)core;
s32 prev = core->state.vbank.id;
if(!JS_IsUndefined(argv[0]))
core->api.vbank(tic, getInteger2(ctx, argv[0], 0));
return JS_NewUint32(ctx, prev);
}
static JSValue js_sync(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
u32 mask = getInteger2(ctx, argv[0], 0);
s32 bank = getInteger2(ctx, argv[1], 0);
bool toCart = JS_ToBool(ctx, argv[2]);
if(bank >= 0 && bank < TIC_BANKS)
core->api.sync(tic, mask, bank, toCart);
else
throwError(ctx, "sync() error, invalid bank");
return JS_UNDEFINED;
}
static JSValue js_reset(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx);
core->state.initialized = false;
return JS_UNDEFINED;
}
static JSValue js_fget(JSContext *ctx, JSValueConst this_val, s32 argc, JSValueConst *argv)
{
tic_core* core = getCore(ctx); tic_mem* tic = (tic_mem*)core;
u32 index = getInteger2(ctx, argv[0], 0);
u32 flag = getInteger2(ctx, argv[1], 0);
bool value = core->api.fget(tic, index, flag);