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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkScript2.h"
#include "SkData.h"
#include "SkFloatingPoint.h"
#include "SkMath.h"
#include "SkParse.h"
#include "SkScriptCallBack.h"
#include "SkScriptRuntime.h"
#include "SkString.h"
#include "SkOpArray.h"
const SkScriptEngine2::OperatorAttributes SkScriptEngine2::gOpAttributes[] = {
{ SkOperand2::kNoType, SkOperand2::kNoType, kNoBias, kResultIsNotBoolean },
{ SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar | SkOperand2::kString),
SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar | SkOperand2::kString), kTowardsString, kResultIsNotBoolean }, // kAdd
{ SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kBitAnd
{ SkOperand2::kNoType, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kBitNot
{ SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kBitOr
{ SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar),
SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), kNoBias, kResultIsNotBoolean }, // kDivide
{ SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar | SkOperand2::kString),
SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar |SkOperand2:: kString), kTowardsNumber,
kResultIsBoolean }, // kEqual
{ SkOperand2::kS32, SkOperand2::kNoType, kNoBias, kResultIsNotBoolean }, // kFlipOps
{ SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar | SkOperand2::kString),
SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar | SkOperand2::kString), kTowardsNumber,
kResultIsBoolean }, // kGreaterEqual
{ SkOperand2::kNoType, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kLogicalAnd (really, ToBool)
{ SkOperand2::kNoType, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kLogicalNot
{ SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kLogicalOr
{ SkOperand2::kNoType, SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), kNoBias, kResultIsNotBoolean }, // kMinus
{ SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar),
SkOperand2::OpType(SkOperand2::kS32 |SkOperand2:: kScalar), kNoBias, kResultIsNotBoolean }, // kModulo
{ SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar),
SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), kNoBias, kResultIsNotBoolean }, // kMultiply
{ SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kShiftLeft
{ SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kShiftRight
{ SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar),
SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), kNoBias, kResultIsNotBoolean }, // kSubtract
{ SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean } // kXor
};
#define kBracketPrecedence 16
#define kIfElsePrecedence 15
const signed char SkScriptEngine2::gPrecedence[] = {
17, // kUnassigned,
6, // kAdd,
10, // kBitAnd,
4, // kBitNot,
12, // kBitOr,
5, // kDivide,
9, // kEqual,
-1, // kFlipOps,
8, // kGreaterEqual,
13, // kLogicalAnd,
4, // kLogicalNot,
14, // kLogicalOr,
4, // kMinus,
5, // kModulo,
5, // kMultiply,
7, // kShiftLeft,
7, // kShiftRight, // signed
6, // kSubtract,
11, // kXor
kBracketPrecedence, // kArrayOp
kIfElsePrecedence, // kElse
kIfElsePrecedence, // kIf
kBracketPrecedence, // kParen
};
const SkScriptEngine2::TypeOp SkScriptEngine2::gTokens[] = {
kNop, // unassigned
kAddInt, // kAdd,
kBitAndInt, // kBitAnd,
kBitNotInt, // kBitNot,
kBitOrInt, // kBitOr,
kDivideInt, // kDivide,
kEqualInt, // kEqual,
kFlipOpsOp, // kFlipOps,
kGreaterEqualInt, // kGreaterEqual,
kLogicalAndInt, // kLogicalAnd,
kLogicalNotInt, // kLogicalNot,
kLogicalOrInt, // kLogicalOr,
kMinusInt, // kMinus,
kModuloInt, // kModulo,
kMultiplyInt, // kMultiply,
kShiftLeftInt, // kShiftLeft,
kShiftRightInt, // kShiftRight, // signed
kSubtractInt, // kSubtract,
kXorInt // kXor
};
static inline bool is_between(int c, int min, int max)
{
return (unsigned)(c - min) <= (unsigned)(max - min);
}
static inline bool is_ws(int c)
{
return is_between(c, 1, 32);
}
static int token_length(const char* start) {
char ch = start[0];
if (! is_between(ch, 'a' , 'z') && ! is_between(ch, 'A', 'Z') && ch != '_' && ch != '$')
return -1;
int length = 0;
do
ch = start[++length];
while (is_between(ch, 'a' , 'z') || is_between(ch, 'A', 'Z') || is_between(ch, '0', '9') ||
ch == '_' || ch == '$');
return length;
}
SkScriptEngine2::SkScriptEngine2(SkOperand2::OpType returnType) : fActiveStream(&fStream),
fTokenLength(0), fReturnType(returnType), fError(kNoError),
fAccumulatorType(SkOperand2::kNoType),
fBranchPopAllowed(true), fConstExpression(true), fOperandInUse(false)
{
Branch branch(kUnassigned, 0, 0);
fBranchStack.push(branch);
*fOpStack.push() = (Op) kParen;
}
SkScriptEngine2::~SkScriptEngine2() {
for (SkString** stringPtr = fTrackString.begin(); stringPtr < fTrackString.end(); stringPtr++)
delete *stringPtr;
for (SkOpArray** arrayPtr = fTrackArray.begin(); arrayPtr < fTrackArray.end(); arrayPtr++)
delete *arrayPtr;
}
void SkScriptEngine2::addToken(SkScriptEngine2::TypeOp op) {
int limit = fBranchStack.count() - 1;
for (int index = 0; index < limit; index++) {
Branch& branch = fBranchStack.index(index);
if (branch.fPrimed == Branch::kIsPrimed)
resolveBranch(branch);
}
if (fBranchPopAllowed) {
while (fBranchStack.top().fDone == Branch::kIsDone)
fBranchStack.pop();
}
unsigned char charOp = (unsigned char) op;
fActiveStream->write(&charOp, sizeof(charOp));
}
void SkScriptEngine2::addTokenConst(SkScriptValue2* value, AddTokenRegister reg,
SkOperand2::OpType toType, SkScriptEngine2::TypeOp op) {
if (value->fIsConstant == SkScriptValue2::kConstant && convertTo(toType, value))
return;
addTokenValue(*value, reg);
addToken(op);
value->fIsWritten = SkScriptValue2::kWritten;
value->fType = toType;
}
void SkScriptEngine2::addTokenInt(int integer) {
fActiveStream->write(&integer, sizeof(integer));
}
void SkScriptEngine2::addTokenScalar(SkScalar scalar) {
fActiveStream->write(&scalar, sizeof(scalar));
}
void SkScriptEngine2::addTokenString(const SkString& string) {
int size = string.size();
addTokenInt(size);
fActiveStream->write(string.c_str(), size);
}
void SkScriptEngine2::addTokenValue(const SkScriptValue2& value, AddTokenRegister reg) {
if (value.isConstant() == false) {
if (reg == kAccumulator) {
if (fAccumulatorType == SkOperand2::kNoType)
addToken(kAccumulatorPop);
} else {
; // !!! incomplete?
}
return;
}
if (reg == kAccumulator && fAccumulatorType != SkOperand2::kNoType)
addToken(kAccumulatorPush);
switch (value.fType) {
case SkOperand2::kS32:
addToken(reg == kAccumulator ? kIntegerAccumulator : kIntegerOperand);
addTokenInt(value.fOperand.fS32);
if (reg == kAccumulator)
fAccumulatorType = SkOperand2::kS32;
else
fOperandInUse = true;
break;
case SkOperand2::kScalar:
addToken(reg == kAccumulator ? kScalarAccumulator : kScalarOperand);
addTokenScalar(value.fOperand.fScalar);
if (reg == kAccumulator)
fAccumulatorType = SkOperand2::kScalar;
else
fOperandInUse = true;
break;
case SkOperand2::kString:
addToken(reg == kAccumulator ? kStringAccumulator : kStringOperand);
addTokenString(*value.fOperand.fString);
if (reg == kAccumulator)
fAccumulatorType = SkOperand2::kString;
else
fOperandInUse = true;
break;
default:
SkASSERT(0); //!!! not implemented yet
}
}
int SkScriptEngine2::arithmeticOp(char ch, char nextChar, bool lastPush) {
Op op = kUnassigned;
bool reverseOperands = false;
bool negateResult = false;
int advance = 1;
switch (ch) {
case '+':
// !!! ignoring unary plus as implemented here has the side effect of
// suppressing errors like +"hi"
if (lastPush == false) // unary plus, don't push an operator
return advance;
op = kAdd;
break;
case '-':
op = lastPush ? kSubtract : kMinus;
break;
case '*':
op = kMultiply;
break;
case '/':
op = kDivide;
break;
case '>':
if (nextChar == '>') {
op = kShiftRight;
goto twoChar;
}
op = kGreaterEqual;
if (nextChar == '=')
goto twoChar;
reverseOperands = negateResult = true;
break;
case '<':
if (nextChar == '<') {
op = kShiftLeft;
goto twoChar;
}
op = kGreaterEqual;
reverseOperands = nextChar == '=';
negateResult = ! reverseOperands;
advance += reverseOperands;
break;
case '=':
if (nextChar == '=') {
op = kEqual;
goto twoChar;
}
break;
case '!':
if (nextChar == '=') {
op = kEqual;
negateResult = true;
twoChar:
advance++;
break;
}
op = kLogicalNot;
break;
case '?':
op =(Op) kIf;
break;
case ':':
op = (Op) kElse;
break;
case '^':
op = kXor;
break;
case '(':
*fOpStack.push() = (Op) kParen;
return advance;
case '&':
SkASSERT(nextChar != '&');
op = kBitAnd;
break;
case '|':
SkASSERT(nextChar != '|');
op = kBitOr;
break;
case '%':
op = kModulo;
break;
case '~':
op = kBitNot;
break;
}
if (op == kUnassigned)
return 0;
signed char precedence = gPrecedence[op];
do {
int idx = 0;
Op compare;
do {
compare = fOpStack.index(idx);
if ((compare & kArtificialOp) == 0)
break;
idx++;
} while (true);
signed char topPrecedence = gPrecedence[compare];
SkASSERT(topPrecedence != -1);
if (topPrecedence > precedence || (topPrecedence == precedence &&
gOpAttributes[op].fLeftType == SkOperand2::kNoType)) {
break;
}
processOp();
} while (true);
if (negateResult)
*fOpStack.push() = (Op) (kLogicalNot | kArtificialOp);
fOpStack.push(op);
if (reverseOperands)
*fOpStack.push() = (Op) (kFlipOps | kArtificialOp);
return advance;
}
bool SkScriptEngine2::convertParams(SkTDArray<SkScriptValue2>* params,
const SkOperand2::OpType* paramTypes, int paramCount) {
int count = params->count();
if (count > paramCount) {
SkASSERT(0);
return false; // too many parameters passed
}
for (int index = 0; index < count; index++)
convertTo(paramTypes[index], &(*params)[index]);
return true;
}
bool SkScriptEngine2::convertTo(SkOperand2::OpType toType, SkScriptValue2* value ) {
SkOperand2::OpType type = value->fType;
if (type == toType)
return true;
if (type == SkOperand2::kObject) {
if (handleUnbox(value) == false)
return false;
return convertTo(toType, value);
}
return ConvertTo(this, toType, value);
}
bool SkScriptEngine2::evaluateDot(const char*& script) {
size_t fieldLength = token_length(++script); // skip dot
SkASSERT(fieldLength > 0); // !!! add error handling
const char* field = script;
script += fieldLength;
bool success = handleProperty();
if (success == false) {
fError = kCouldNotFindReferencedID;
goto error;
}
return evaluateDotParam(script, field, fieldLength);
error:
return false;
}
bool SkScriptEngine2::evaluateDotParam(const char*& script, const char* field, size_t fieldLength) {
SkScriptValue2& top = fValueStack.top();
if (top.fType != SkOperand2::kObject)
return false;
void* object = top.fOperand.fObject;
fValueStack.pop();
char ch; // see if it is a simple member or a function
while (is_ws(ch = script[0]))
script++;
bool success = true;
if (ch != '(')
success = handleMember(field, fieldLength, object);
else {
SkTDArray<SkScriptValue2> params;
*fBraceStack.push() = kFunctionBrace;
success = functionParams(&script, ¶ms);
if (success)
success = handleMemberFunction(field, fieldLength, object, ¶ms);
}
return success;
}
bool SkScriptEngine2::evaluateScript(const char** scriptPtr, SkScriptValue2* value) {
// fArrayOffset = 0; // no support for structures for now
bool success;
const char* inner;
if (strncmp(*scriptPtr, "#script:", sizeof("#script:") - 1) == 0) {
*scriptPtr += sizeof("#script:") - 1;
if (fReturnType == SkOperand2::kNoType || fReturnType == SkOperand2::kString) {
success = innerScript(scriptPtr, value);
SkASSERT(success);
inner = value->fOperand.fString->c_str();
scriptPtr = &inner;
}
}
success = innerScript(scriptPtr, value);
const char* script = *scriptPtr;
char ch;
while (is_ws(ch = script[0]))
script++;
if (ch != '\0') {
// error may trigger on scripts like "50,0" that were intended to be written as "[50, 0]"
return false;
}
return success;
}
void SkScriptEngine2::forget(SkOpArray* array) {
if (array->getType() == SkOperand2::kString) {
for (int index = 0; index < array->count(); index++) {
SkString* string = (*array)[index].fString;
int found = fTrackString.find(string);
if (found >= 0)
fTrackString.remove(found);
}
return;
}
if (array->getType() == SkOperand2::kArray) {
for (int index = 0; index < array->count(); index++) {
SkOpArray* child = (*array)[index].fArray;
forget(child); // forgets children of child
int found = fTrackArray.find(child);
if (found >= 0)
fTrackArray.remove(found);
}
}
}
bool SkScriptEngine2::functionParams(const char** scriptPtr, SkTDArray<SkScriptValue2>* params) {
(*scriptPtr)++; // skip open paren
*fOpStack.push() = (Op) kParen;
*fBraceStack.push() = kFunctionBrace;
do {
SkScriptValue2 value;
bool success = innerScript(scriptPtr, &value);
SkASSERT(success);
if (success == false)
return false;
*params->append() = value;
} while ((*scriptPtr)[-1] == ',');
fBraceStack.pop();
fOpStack.pop(); // pop paren
(*scriptPtr)++; // advance beyond close paren
return true;
}
size_t SkScriptEngine2::getTokenOffset() {
return fActiveStream->getOffset();
}
SkOperand2::OpType SkScriptEngine2::getUnboxType(SkOperand2 scriptValue) {
for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) {
if ((*callBack)->getType() != SkScriptCallBack::kUnbox)
continue;
return (*callBack)->getReturnType(0, &scriptValue);
}
return SkOperand2::kObject;
}
bool SkScriptEngine2::innerScript(const char** scriptPtr, SkScriptValue2* value) {
const char* script = *scriptPtr;
char ch;
bool lastPush = false;
bool success = true;
int opBalance = fOpStack.count();
int baseBrace = fBraceStack.count();
int branchBalance = fBranchStack.count();
while ((ch = script[0]) != '\0') {
if (is_ws(ch)) {
script++;
continue;
}
SkScriptValue2 operand;
const char* dotCheck;
if (fBraceStack.count() > baseBrace) {
if (fBraceStack.top() == kArrayBrace) {
SkScriptValue2 tokenValue;
success = innerScript(&script, &tokenValue); // terminate and return on comma, close brace
SkASSERT(success);
{
SkOperand2::OpType type = fReturnType;
if (fReturnType == SkOperand2::kNoType) {
// !!! short sighted; in the future, allow each returned array component to carry
// its own type, and let caller do any needed conversions
if (value->fOperand.fArray->count() == 0)
value->fOperand.fArray->setType(type = tokenValue.fType);
else
type = value->fOperand.fArray->getType();
}
if (tokenValue.fType != type)
convertTo(type, &tokenValue);
*value->fOperand.fArray->append() = tokenValue.fOperand;
}
lastPush = false;
continue;
} else {
SkASSERT(token_length(script) > 0);
}
}
if (lastPush != false && fTokenLength > 0) {
if (ch == '(') {
*fBraceStack.push() = kFunctionBrace;
SkString functionName(fToken, fTokenLength);
if (handleFunction(&script) == false)
return false;
lastPush = true;
continue;
} else if (ch == '[') {
if (handleProperty() == false) {
SkASSERT(0);
return false;
}
if (handleArrayIndexer(&script) == false)
return false;
lastPush = true;
continue;
} else if (ch != '.') {
if (handleProperty() == false) {
SkASSERT(0);
return false;
}
lastPush = true;
continue;
}
}
if (ch == '0' && (script[1] & ~0x20) == 'X') {
SkASSERT(lastPush == false);
script += 2;
script = SkParse::FindHex(script, (uint32_t*) &operand.fOperand.fS32);
SkASSERT(script);
goto intCommon;
}
if (lastPush == false && ch == '.')
goto scalarCommon;
if (ch >= '0' && ch <= '9') {
SkASSERT(lastPush == false);
dotCheck = SkParse::FindS32(script, &operand.fOperand.fS32);
if (dotCheck[0] != '.') {
script = dotCheck;
intCommon:
operand.fType = SkOperand2::kS32;
} else {
scalarCommon:
script = SkParse::FindScalar(script, &operand.fOperand.fScalar);
operand.fType = SkOperand2::kScalar;
}
operand.fIsConstant = SkScriptValue2::kConstant;
fValueStack.push(operand);
lastPush = true;
continue;
}
int length = token_length(script);
if (length > 0) {
SkASSERT(lastPush == false);
fToken = script;
fTokenLength = length;
script += length;
lastPush = true;
continue;
}
char startQuote = ch;
if (startQuote == '\'' || startQuote == '\"') {
SkASSERT(lastPush == false);
operand.fOperand.fString = new SkString();
++script;
const char* stringStart = script;
do { // measure string
if (script[0] == '\\')
++script;
++script;
SkASSERT(script[0]); // !!! throw an error
} while (script[0] != startQuote);
operand.fOperand.fString->set(stringStart, script - stringStart);
script = stringStart;
char* stringWrite = operand.fOperand.fString->writable_str();
do { // copy string
if (script[0] == '\\')
++script;
*stringWrite++ = script[0];
++script;
SkASSERT(script[0]); // !!! throw an error
} while (script[0] != startQuote);
++script;
track(operand.fOperand.fString);
operand.fType = SkOperand2::kString;
operand.fIsConstant = SkScriptValue2::kConstant;
fValueStack.push(operand);
lastPush = true;
continue;
}
if (ch == '.') {
if (fTokenLength == 0) {
SkDEBUGCODE(SkScriptValue2 scriptValue;)
SkDEBUGCODE(scriptValue.fOperand.fObject = NULL);
int tokenLength = token_length(++script);
const char* token = script;
script += tokenLength;
SkASSERT(fValueStack.count() > 0); // !!! add error handling
SkScriptValue2 top;
fValueStack.pop(&top);
addTokenInt(top.fType);
addToken(kBoxToken);
top.fType = SkOperand2::kObject;
top.fIsConstant = SkScriptValue2::kVariable;
fConstExpression = false;
fValueStack.push(top);
success = evaluateDotParam(script, token, tokenLength);
SkASSERT(success);
lastPush = true;
continue;
}
// get next token, and evaluate immediately
success = evaluateDot(script);
if (success == false) {
// SkASSERT(0);
return false;
}
lastPush = true;
continue;
}
if (ch == '[') {
if (lastPush == false) {
script++;
*fBraceStack.push() = kArrayBrace;
operand.fOperand.fArray = value->fOperand.fArray = new SkOpArray(fReturnType);
track(value->fOperand.fArray);
operand.fType = SkOperand2::kArray;
operand.fIsConstant = SkScriptValue2::kVariable;
fValueStack.push(operand);
continue;
}
if (handleArrayIndexer(&script) == false)
return false;
lastPush = true;
continue;
}
#if 0 // structs not supported for now
if (ch == '{') {
if (lastPush == false) {
script++;
*fBraceStack.push() = kStructBrace;
operand.fS32 = 0;
*fTypeStack.push() = (SkOpType) kStruct;
fOperandStack.push(operand);
continue;
}
SkASSERT(0); // braces in other contexts aren't supported yet
}
#endif
if (ch == ')' && fBraceStack.count() > 0) {
BraceStyle braceStyle = fBraceStack.top();
if (braceStyle == kFunctionBrace) {
fBraceStack.pop();
break;
}
}
if (ch == ',' || ch == ']') {
if (ch != ',') {
BraceStyle match;
fBraceStack.pop(&match);
SkASSERT(match == kArrayBrace);
}
script++;
// !!! see if brace or bracket is correct closer
break;
}
char nextChar = script[1];
int advance = logicalOp(ch, nextChar);
if (advance == 0)
advance = arithmeticOp(ch, nextChar, lastPush);
if (advance == 0) // unknown token
return false;
if (advance > 0)
script += advance;
lastPush = ch == ']' || ch == ')';
}
if (fTokenLength > 0) {
success = handleProperty();
SkASSERT(success);
}
int branchIndex = 0;
branchBalance = fBranchStack.count() - branchBalance;
fBranchPopAllowed = false;
while (branchIndex < branchBalance) {
Branch& branch = fBranchStack.index(branchIndex++);
if (branch.fPrimed == Branch::kIsPrimed)
break;
Op branchOp = branch.fOperator;
SkOperand2::OpType lastType = fValueStack.top().fType;
addTokenValue(fValueStack.top(), kAccumulator);
fValueStack.pop();
if (branchOp == kLogicalAnd || branchOp == kLogicalOr) {
if (branch.fOperator == kLogicalAnd)
branch.prime();
addToken(kToBool);
} else {
resolveBranch(branch);
SkScriptValue2 operand;
operand.fType = lastType;
// !!! note that many branching expressions could be constant
// today, we always evaluate branches as returning variables
operand.fIsConstant = SkScriptValue2::kVariable;
fValueStack.push(operand);
}
if (branch.fDone == Branch::kIsNotDone)
branch.prime();
}
fBranchPopAllowed = true;
while (fBranchStack.top().fDone == Branch::kIsDone)
fBranchStack.pop();
while (fOpStack.count() > opBalance) { // leave open paren
if (processOp() == false)
return false;
}
SkOperand2::OpType topType = fValueStack.count() > 0 ? fValueStack.top().fType : SkOperand2::kNoType;
if (topType != fReturnType &&
topType == SkOperand2::kString && fReturnType != SkOperand2::kNoType) { // if result is a string, give handle property a chance to convert it to the property value
SkString* string = fValueStack.top().fOperand.fString;
fToken = string->c_str();
fTokenLength = string->size();
fValueStack.pop();
success = handleProperty();
if (success == false) { // if it couldn't convert, return string (error?)
SkScriptValue2 operand;
operand.fType = SkOperand2::kString;
operand.fOperand.fString = string;
operand.fIsConstant = SkScriptValue2::kVariable; // !!! ?
fValueStack.push(operand);
}
}
if (fStream.getOffset() > 0) {
addToken(kEnd);
SkAutoDataUnref data(fStream.copyToData());
#ifdef SK_DEBUG
decompile(data.bytes(), data.size());
#endif
SkScriptRuntime runtime(fCallBackArray);
runtime.executeTokens((unsigned char*) data.bytes());
SkScriptValue2 value1;
runtime.getResult(&value1.fOperand);
value1.fType = fReturnType;
fValueStack.push(value1);
}
if (value) {
if (fValueStack.count() == 0)
return false;
fValueStack.pop(value);
if (value->fType != fReturnType && value->fType == SkOperand2::kObject &&
fReturnType != SkOperand2::kNoType)
convertTo(fReturnType, value);
}
// if (fBranchStack.top().fOpStackDepth > fOpStack.count())
// resolveBranch();
*scriptPtr = script;
return true; // no error
}
bool SkScriptEngine2::handleArrayIndexer(const char** scriptPtr) {
SkScriptValue2 scriptValue;
(*scriptPtr)++;
*fOpStack.push() = (Op) kParen;
*fBraceStack.push() = kArrayBrace;
SkOperand2::OpType saveType = fReturnType;
fReturnType = SkOperand2::kS32;
bool success = innerScript(scriptPtr, &scriptValue);
fReturnType = saveType;
SkASSERT(success);
success = convertTo(SkOperand2::kS32, &scriptValue);
SkASSERT(success);
int index = scriptValue.fOperand.fS32;
fValueStack.pop(&scriptValue);
if (scriptValue.fType == SkOperand2::kObject) {
success = handleUnbox(&scriptValue);
SkASSERT(success);
SkASSERT(scriptValue.fType == SkOperand2::kArray);
}
scriptValue.fType = scriptValue.fOperand.fArray->getType();
// SkASSERT(index >= 0);
if ((unsigned) index >= (unsigned) scriptValue.fOperand.fArray->count()) {
fError = kArrayIndexOutOfBounds;
return false;
}
scriptValue.fOperand = scriptValue.fOperand.fArray->begin()[index];
scriptValue.fIsConstant = SkScriptValue2::kVariable;
fValueStack.push(scriptValue);
fOpStack.pop(); // pop paren
return success;
}
bool SkScriptEngine2::handleFunction(const char** scriptPtr) {
const char* functionName = fToken;
size_t functionNameLen = fTokenLength;
fTokenLength = 0;
SkTDArray<SkScriptValue2> params;
bool success = functionParams(scriptPtr, ¶ms);
if (success == false)
goto done;
{
for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) {
if ((*callBack)->getType() != SkScriptCallBack::kFunction)
continue;
SkScriptValue2 callbackResult;
success = (*callBack)->getReference(functionName, functionNameLen, &callbackResult);
if (success) {
callbackResult.fType = (*callBack)->getReturnType(callbackResult.fOperand.fReference, NULL);
callbackResult.fIsConstant = SkScriptValue2::kVariable;
fValueStack.push(callbackResult);
goto done;
}
}
}
return false;
done:
fOpStack.pop();
return success;
}
bool SkScriptEngine2::handleMember(const char* field, size_t len, void* object) {
bool success = true;
for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) {
if ((*callBack)->getType() != SkScriptCallBack::kMember)
continue;
SkScriptValue2 callbackResult;
success = (*callBack)->getReference(field, len, &callbackResult);
if (success) {
if (callbackResult.fType == SkOperand2::kString)
track(callbackResult.fOperand.fString);
callbackResult.fIsConstant = SkScriptValue2::kVariable;
fValueStack.push(callbackResult);
goto done;
}
}
return false;
done:
return success;
}
bool SkScriptEngine2::handleMemberFunction(const char* field, size_t len, void* object,
SkTDArray<SkScriptValue2>* params) {
bool success = true;
for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) {
if ((*callBack)->getType() != SkScriptCallBack::kMemberFunction)
continue;
SkScriptValue2 callbackResult;
success = (*callBack)->getReference(field, len, &callbackResult);
if (success) {
if (callbackResult.fType == SkOperand2::kString)
track(callbackResult.fOperand.fString);
callbackResult.fIsConstant = SkScriptValue2::kVariable;
fValueStack.push(callbackResult);
goto done;
}
}
return false;
done:
return success;
}
bool SkScriptEngine2::handleProperty() {
bool success = true;
for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) {
if ((*callBack)->getType() != SkScriptCallBack::kProperty)
continue;
SkScriptValue2 callbackResult;
success = (*callBack)->getReference(fToken, fTokenLength, &callbackResult);
if (success) {
if (callbackResult.fType == SkOperand2::kString && callbackResult.fOperand.fString == NULL) {
callbackResult.fOperand.fString = new SkString(fToken, fTokenLength);
track(callbackResult.fOperand.fString);
}
callbackResult.fIsConstant = SkScriptValue2::kVariable;
fValueStack.push(callbackResult);
goto done;
}
}
done:
fTokenLength = 0;
return success;
}
bool SkScriptEngine2::handleUnbox(SkScriptValue2* scriptValue) {
bool success = true;
for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) {
if ((*callBack)->getType() != SkScriptCallBack::kUnbox)
continue;
SkScriptCallBackConvert* callBackConvert = (SkScriptCallBackConvert*) *callBack;
success = callBackConvert->convert(scriptValue->fType, &scriptValue->fOperand);
if (success) {
if (scriptValue->fType == SkOperand2::kString)
track(scriptValue->fOperand.fString);
goto done;
}
}
return false;
done:
return success;
}
// note that entire expression is treated as if it were enclosed in parens
// an open paren is always the first thing in the op stack
int SkScriptEngine2::logicalOp(char ch, char nextChar) {
int advance = 1;
Op op;
signed char precedence;
switch (ch) {
case ')':
op = (Op) kParen;
break;
case ']':
op = (Op) kArrayOp;
break;
case '?':
op = (Op) kIf;
break;
case ':':
op = (Op) kElse;
break;
case '&':
if (nextChar != '&')
goto noMatch;
op = kLogicalAnd;
advance = 2;
break;
case '|':
if (nextChar != '|')
goto noMatch;
op = kLogicalOr;
advance = 2;
break;
default:
noMatch:
return 0;
}
precedence = gPrecedence[op];
int branchIndex = 0;
fBranchPopAllowed = false;
do {
while (gPrecedence[fOpStack.top() & ~kArtificialOp] < precedence)
processOp();
Branch& branch = fBranchStack.index(branchIndex++);
Op branchOp = branch.fOperator;
if (gPrecedence[branchOp] >= precedence)
break;
addTokenValue(fValueStack.top(), kAccumulator);
fValueStack.pop();
if (branchOp == kLogicalAnd || branchOp == kLogicalOr) {
if (branch.fOperator == kLogicalAnd)
branch.prime();
addToken(kToBool);
} else
resolveBranch(branch);
if (branch.fDone == Branch::kIsNotDone)
branch.prime();
} while (true);
fBranchPopAllowed = true;
while (fBranchStack.top().fDone == Branch::kIsDone)
fBranchStack.pop();
processLogicalOp(op);
return advance;
}
void SkScriptEngine2::processLogicalOp(Op op) {
switch (op) {
case kParen:
case kArrayOp:
SkASSERT(fOpStack.count() > 1 && fOpStack.top() == op); // !!! add error handling
if (op == kParen)
fOpStack.pop();
else {
SkScriptValue2 value;
fValueStack.pop(&value);
SkASSERT(value.fType == SkOperand2::kS32 || value.fType == SkOperand2::kScalar); // !!! add error handling (although, could permit strings eventually)
int index = value.fType == SkOperand2::kScalar ? SkScalarFloor(value.fOperand.fScalar) :
value.fOperand.fS32;
SkScriptValue2 arrayValue;
fValueStack.pop(&arrayValue);
SkASSERT(arrayValue.fType == SkOperand2::kArray); // !!! add error handling
SkOpArray* array = arrayValue.fOperand.fArray;
SkOperand2 operand;
bool success = array->getIndex(index, &operand);
SkASSERT(success); // !!! add error handling
SkScriptValue2 resultValue;
resultValue.fType = array->getType();
resultValue.fOperand = operand;
resultValue.fIsConstant = SkScriptValue2::kVariable;
fValueStack.push(resultValue);
}
break;
case kIf: {
if (fAccumulatorType == SkOperand2::kNoType) {
addTokenValue(fValueStack.top(), kAccumulator);
fValueStack.pop();
}
SkASSERT(fAccumulatorType != SkOperand2::kString); // !!! add error handling
addToken(kIfOp);
Branch branch(op, fOpStack.count(), getTokenOffset());
*fBranchStack.push() = branch;
addTokenInt(0); // placeholder for future branch
fAccumulatorType = SkOperand2::kNoType;
} break;
case kElse: {
addTokenValue(fValueStack.top(), kAccumulator);
fValueStack.pop();
addToken(kElseOp);
size_t newOffset = getTokenOffset();
addTokenInt(0); // placeholder for future branch
Branch& branch = fBranchStack.top();
resolveBranch(branch);
branch.fOperator = op;
branch.fDone = Branch::kIsNotDone;
SkASSERT(branch.fOpStackDepth == fOpStack.count());
branch.fOffset = newOffset;
fAccumulatorType = SkOperand2::kNoType;
} break;
case kLogicalAnd:
case kLogicalOr: {
Branch& oldTop = fBranchStack.top();
Branch::Primed wasPrime = oldTop.fPrimed;
Branch::Done wasDone = oldTop.fDone;
oldTop.fPrimed = Branch::kIsNotPrimed;
oldTop.fDone = Branch::kIsNotDone;
if (fAccumulatorType == SkOperand2::kNoType) {
SkASSERT(fValueStack.top().fType == SkOperand2::kS32); // !!! add error handling, and conversion to int?
addTokenValue(fValueStack.top(), kAccumulator);
fValueStack.pop();
} else {
SkASSERT(fAccumulatorType == SkOperand2::kS32);
}
// if 'and', write beq goto opcode after end of predicate (after to bool)
// if 'or', write bne goto to bool
addToken(op == kLogicalAnd ? kLogicalAndInt : kLogicalOrInt);
Branch branch(op, fOpStack.count(), getTokenOffset());
addTokenInt(0); // placeholder for future branch
oldTop.fPrimed = wasPrime;
oldTop.fDone = wasDone;
*fBranchStack.push() = branch;
fAccumulatorType = SkOperand2::kNoType;
} break;
default:
SkASSERT(0);
}
}
bool SkScriptEngine2::processOp() {
Op op;
fOpStack.pop(&op);
op = (Op) (op & ~kArtificialOp);
const OperatorAttributes* attributes = &gOpAttributes[op];
SkScriptValue2 value1;
memset(&value1, 0, sizeof(SkScriptValue2));
SkScriptValue2 value2;
fValueStack.pop(&value2);
value2.fIsWritten = SkScriptValue2::kUnwritten;
// SkScriptEngine2::SkTypeOp convert1[3];
// SkScriptEngine2::SkTypeOp convert2[3];
// SkScriptEngine2::SkTypeOp* convert2Ptr = convert2;
bool constantOperands = value2.fIsConstant == SkScriptValue2::kConstant;
if (attributes->fLeftType != SkOperand2::kNoType) {
fValueStack.pop(&value1);
constantOperands &= value1.fIsConstant == SkScriptValue2::kConstant;
value1.fIsWritten = SkScriptValue2::kUnwritten;
if (op == kFlipOps) {
SkTSwap(value1, value2);
fOpStack.pop(&op);
op = (Op) (op & ~kArtificialOp);
attributes = &gOpAttributes[op];
if (constantOperands == false)
addToken(kFlipOpsOp);
}
if (value1.fType == SkOperand2::kObject && (value1.fType & attributes->fLeftType) == 0) {
value1.fType = getUnboxType(value1.fOperand);
addToken(kUnboxToken);
}
}
if (value2.fType == SkOperand2::kObject && (value2.fType & attributes->fLeftType) == 0) {
value1.fType = getUnboxType(value2.fOperand);
addToken(kUnboxToken2);
}
if (attributes->fLeftType != SkOperand2::kNoType) {
if (value1.fType != value2.fType) {
if ((attributes->fLeftType & SkOperand2::kString) && attributes->fBias & kTowardsString &&
((value1.fType | value2.fType) & SkOperand2::kString)) {
if (value1.fType == SkOperand2::kS32 || value1.fType == SkOperand2::kScalar) {
addTokenConst(&value1, kAccumulator, SkOperand2::kString,
value1.fType == SkOperand2::kS32 ? kIntToString : kScalarToString);
}
if (value2.fType == SkOperand2::kS32 || value2.fType == SkOperand2::kScalar) {
addTokenConst(&value2, kOperand, SkOperand2::kString,
value2.fType == SkOperand2::kS32 ? kIntToString2 : kScalarToString2);
}
} else if (attributes->fLeftType & SkOperand2::kScalar && ((value1.fType | value2.fType) &
SkOperand2::kScalar)) {
if (value1.fType == SkOperand2::kS32)
addTokenConst(&value1, kAccumulator, SkOperand2::kScalar, kIntToScalar);
if (value2.fType == SkOperand2::kS32)
addTokenConst(&value2, kOperand, SkOperand2::kScalar, kIntToScalar2);
}
}
if ((value1.fType & attributes->fLeftType) == 0 || value1.fType != value2.fType) {
if (value1.fType == SkOperand2::kString)
addTokenConst(&value1, kAccumulator, SkOperand2::kScalar, kStringToScalar);
if (value1.fType == SkOperand2::kScalar && (attributes->fLeftType == SkOperand2::kS32 ||
value2.fType == SkOperand2::kS32))
addTokenConst(&value1, kAccumulator, SkOperand2::kS32, kScalarToInt);
}
}
AddTokenRegister rhRegister = attributes->fLeftType != SkOperand2::kNoType ?
kOperand : kAccumulator;
if ((value2.fType & attributes->fRightType) == 0 || value1.fType != value2.fType) {
if (value2.fType == SkOperand2::kString)
addTokenConst(&value2, rhRegister, SkOperand2::kScalar, kStringToScalar2);
if (value2.fType == SkOperand2::kScalar && (attributes->fRightType == SkOperand2::kS32 ||
value1.fType == SkOperand2::kS32))
addTokenConst(&value2, rhRegister, SkOperand2::kS32, kScalarToInt2);
}
TypeOp typeOp = gTokens[op];
if (value2.fType == SkOperand2::kScalar)
typeOp = (TypeOp) (typeOp + 1);
else if (value2.fType == SkOperand2::kString)
typeOp = (TypeOp) (typeOp + 2);
SkDynamicMemoryWStream stream;
SkOperand2::OpType saveType = SkOperand2::kNoType;
SkBool saveOperand = false;
if (constantOperands) {
fActiveStream = &stream;
saveType = fAccumulatorType;
saveOperand = fOperandInUse;
fAccumulatorType = SkOperand2::kNoType;
fOperandInUse = false;
}
if (attributes->fLeftType != SkOperand2::kNoType) { // two operands
if (value1.fIsWritten == SkScriptValue2::kUnwritten)
addTokenValue(value1, kAccumulator);
}
if (value2.fIsWritten == SkScriptValue2::kUnwritten)
addTokenValue(value2, rhRegister);
addToken(typeOp);
if (constantOperands) {
addToken(kEnd);
SkAutoDataUnref data(fStream.copyToData());
#ifdef SK_DEBUG
decompile(data.bytes(), data.size());
#endif
SkScriptRuntime runtime(fCallBackArray);
runtime.executeTokens((unsigned char*)data.bytes());
runtime.getResult(&value1.fOperand);
if (attributes->fResultIsBoolean == kResultIsBoolean)
value1.fType = SkOperand2::kS32;
else if (attributes->fLeftType == SkOperand2::kNoType) // unary operand
value1.fType = value2.fType;
fValueStack.push(value1);
if (value1.fType == SkOperand2::kString)
runtime.untrack(value1.fOperand.fString);
else if (value1.fType == SkOperand2::kArray)
runtime.untrack(value1.fOperand.fArray);
fActiveStream = &fStream;
fAccumulatorType = saveType;
fOperandInUse = saveOperand;
return true;
}
value2.fIsConstant = SkScriptValue2::kVariable;
fValueStack.push(value2);
return true;
}
void SkScriptEngine2::Branch::resolve(SkDynamicMemoryWStream* stream, size_t off) {
SkASSERT(fDone == kIsNotDone);
fPrimed = kIsNotPrimed;
fDone = kIsDone;
SkASSERT(off > fOffset + sizeof(size_t));
size_t offset = off - fOffset - sizeof(offset);
stream->write(&offset, fOffset, sizeof(offset));
}
void SkScriptEngine2::resolveBranch(SkScriptEngine2::Branch& branch) {
branch.resolve(fActiveStream, getTokenOffset());
}
bool SkScriptEngine2::ConvertTo(SkScriptEngine2* engine, SkOperand2::OpType toType, SkScriptValue2* value ) {
SkASSERT(value);
SkOperand2::OpType type = value->fType;
if (type == toType)
return true;
SkOperand2& operand = value->fOperand;
bool success = true;
switch (toType) {
case SkOperand2::kS32:
if (type == SkOperand2::kScalar)
operand.fS32 = SkScalarFloor(operand.fScalar);
else {
SkASSERT(type == SkOperand2::kString);
success = SkParse::FindS32(operand.fString->c_str(), &operand.fS32) != NULL;
}
break;
case SkOperand2::kScalar:
if (type == SkOperand2::kS32)
operand.fScalar = IntToScalar(operand.fS32);
else {
SkASSERT(type == SkOperand2::kString);
success = SkParse::FindScalar(operand.fString->c_str(), &operand.fScalar) != NULL;
}
break;
case SkOperand2::kString: {
SkString* strPtr = new SkString();
SkASSERT(engine);
engine->track(strPtr);
if (type == SkOperand2::kS32)
strPtr->appendS32(operand.fS32);
else {
SkASSERT(type == SkOperand2::kScalar);
strPtr->appendScalar(operand.fScalar);
}
operand.fString = strPtr;
} break;
case SkOperand2::kArray: {
SkOpArray* array = new SkOpArray(type);
*array->append() = operand;
engine->track(array);
operand.fArray = array;
} break;
default:
SkASSERT(0);
}
value->fType = toType;
return success;
}
SkScalar SkScriptEngine2::IntToScalar(int32_t s32) {
SkScalar scalar;
if (s32 == (int32_t) SK_NaN32)
scalar = SK_ScalarNaN;
else if (SkAbs32(s32) == SK_MaxS32)
scalar = SkSign32(s32) * SK_ScalarMax;
else
scalar = SkIntToScalar(s32);
return scalar;
}
bool SkScriptEngine2::ValueToString(const SkScriptValue2& value, SkString* string) {
switch (value.fType) {
case SkOperand2::kS32:
string->reset();
string->appendS32(value.fOperand.fS32);
break;
case SkOperand2::kScalar:
string->reset();
string->appendScalar(value.fOperand.fScalar);
break;
case SkOperand2::kString:
string->set(*value.fOperand.fString);
break;
default:
SkASSERT(0);
return false;
}
return true; // no error
}
#ifdef SK_DEBUG
#define testInt(expression) { #expression, SkOperand2::kS32, expression, 0, NULL }
#ifdef SK_SCALAR_IS_FLOAT
#define testScalar(expression) { #expression, SkOperand2::kScalar, 0, (float) expression, NULL }
#define testRemainder(exp1, exp2) { #exp1 "%" #exp2, SkOperand2::kScalar, 0, fmodf(exp1, exp2), NULL }
#else
#define testScalar(expression) { #expression, SkOperand2::kScalar, 0, (int) ((expression) * 65536.0f), NULL }
#define testRemainder(exp1, exp2) { #exp1 "%" #exp2, SkOperand2::kScalar, 0, (int) (fmod(exp1, exp2) * 65536.0f), NULL }
#endif
#define testTrue(expression) { #expression, SkOperand2::kS32, 1, 0, NULL }
#define testFalse(expression) { #expression, SkOperand2::kS32, 0, 0, NULL }
static const SkScriptNAnswer2 scriptTests[] = {
testInt(1||(0&&3)),
testScalar(- -5.5- -1.5),
testScalar(1.0+5),
testInt((6+7)*8),
testInt(3*(4+5)),
testScalar(1.0+2.0),
testScalar(3.0-1.0),
testScalar(6-1.0),
testScalar(2.5*6.),
testScalar(0.5*4),
testScalar(4.5/.5),
testScalar(9.5/19),
testRemainder(9.5, 0.5),
testRemainder(9.,2),
testRemainder(9,2.5),
testRemainder(-9,2.5),
testTrue(-9==-9.0),
testTrue(-9.==-4.0-5),
testTrue(-9.*1==-4-5),
testFalse(-9!=-9.0),
testFalse(-9.!=-4.0-5),
testFalse(-9.*1!=-4-5),
testInt(0x123),
testInt(0XABC),
testInt(0xdeadBEEF),
{ "'123'+\"456\"", SkOperand2::kString, 0, 0, "123456" },
{ "123+\"456\"", SkOperand2::kString, 0, 0, "123456" },
{ "'123'+456", SkOperand2::kString, 0, 0, "123456" },
{ "'123'|\"456\"", SkOperand2::kS32, 123|456, 0, NULL },
{ "123|\"456\"", SkOperand2::kS32, 123|456, 0, NULL },
{ "'123'|456", SkOperand2::kS32, 123|456, 0, NULL },
{ "'2'<11", SkOperand2::kS32, 1, 0, NULL },
{ "2<'11'", SkOperand2::kS32, 1, 0, NULL },
{ "'2'<'11'", SkOperand2::kS32, 0, 0, NULL },
testInt(123),
testInt(-345),
testInt(+678),
testInt(1+2+3),
testInt(3*4+5),
testInt(6+7*8),
testInt(-1-2-8/4),
testInt(-9%4),
testInt(9%-4),
testInt(-9%-4),
testInt(123|978),
testInt(123&978),
testInt(123^978),
testInt(2<<4),
testInt(99>>3),
testInt(~55),
testInt(~~55),
testInt(!55),
testInt(!!55),
// both int
testInt(2<2),
testInt(2<11),
testInt(20<11),
testInt(2<=2),
testInt(2<=11),
testInt(20<=11),
testInt(2>2),
testInt(2>11),
testInt(20>11),
testInt(2>=2),
testInt(2>=11),
testInt(20>=11),
testInt(2==2),
testInt(2==11),
testInt(20==11),
testInt(2!=2),
testInt(2!=11),
testInt(20!=11),
// left int, right scalar
testInt(2<2.),
testInt(2<11.),
testInt(20<11.),
testInt(2<=2.),
testInt(2<=11.),
testInt(20<=11.),
testInt(2>2.),
testInt(2>11.),
testInt(20>11.),
testInt(2>=2.),
testInt(2>=11.),
testInt(20>=11.),
testInt(2==2.),
testInt(2==11.),
testInt(20==11.),
testInt(2!=2.),
testInt(2!=11.),
testInt(20!=11.),
// left scalar, right int
testInt(2.<2),
testInt(2.<11),
testInt(20.<11),
testInt(2.<=2),
testInt(2.<=11),
testInt(20.<=11),
testInt(2.>2),
testInt(2.>11),
testInt(20.>11),
testInt(2.>=2),
testInt(2.>=11),
testInt(20.>=11),
testInt(2.==2),
testInt(2.==11),
testInt(20.==11),
testInt(2.!=2),
testInt(2.!=11),
testInt(20.!=11),
// both scalar
testInt(2.<11.),
testInt(20.<11.),
testInt(2.<=2.),
testInt(2.<=11.),
testInt(20.<=11.),
testInt(2.>2.),
testInt(2.>11.),
testInt(20.>11.),
testInt(2.>=2.),
testInt(2.>=11.),
testInt(20.>=11.),
testInt(2.==2.),
testInt(2.==11.),
testInt(20.==11.),
testInt(2.!=2.),
testInt(2.!=11.),
testInt(20.!=11.),
// int, string (string is int)
testFalse(2<'2'),
testTrue(2<'11'),
testFalse(20<'11'),
testTrue(2<='2'),
testTrue(2<='11'),
testFalse(20<='11'),
testFalse(2>'2'),
testFalse(2>'11'),
testTrue(20>'11'),
testTrue(2>='2'),
testFalse(2>='11'),
testTrue(20>='11'),
testTrue(2=='2'),
testFalse(2=='11'),
testFalse(2!='2'),
testTrue(2!='11'),
// int, string (string is scalar)
testFalse(2<'2.'),
testTrue(2<'11.'),
testFalse(20<'11.'),
testTrue(2=='2.'),
testFalse(2=='11.'),
// scalar, string
testFalse(2.<'2.'),
testTrue(2.<'11.'),
testFalse(20.<'11.'),
testTrue(2.=='2.'),
testFalse(2.=='11.'),
// string, int
testFalse('2'<2),
testTrue('2'<11),
testFalse('20'<11),
testTrue('2'==2),
testFalse('2'==11),
// string, scalar
testFalse('2'<2.),
testTrue('2'<11.),
testFalse('20'<11.),
testTrue('2'==2.),
testFalse('2'==11.),
// string, string
testFalse('2'<'2'),
testFalse('2'<'11'),
testFalse('20'<'11'),
testTrue('2'=='2'),
testFalse('2'=='11'),
// logic
testInt(1?2:3),
testInt(0?2:3),
testInt((1&&2)||3),
testInt((1&&0)||3),
testInt((1&&0)||0),
testInt(1||(0&&3)),
testInt(0||(0&&3)),
testInt(0||(1&&3)),
testInt(0&&1?2:3)
, { "123.5", SkOperand2::kScalar, 0, SkIntToScalar(123) + SK_Scalar1/2, NULL }
};
#define SkScriptNAnswer_testCount SK_ARRAY_COUNT(scriptTests)
void SkScriptEngine2::UnitTest() {
#if defined(SK_SUPPORT_UNITTEST)
ValidateDecompileTable();
for (int index = 0; index < SkScriptNAnswer_testCount; index++) {
SkScriptEngine2 engine(scriptTests[index].fType);
SkScriptValue2 value;
const char* script = scriptTests[index].fScript;
const char* scriptPtr = script;
SkASSERT(engine.evaluateScript(&scriptPtr, &value) == true);
SkASSERT(value.fType == scriptTests[index].fType);
SkScalar error;
switch (value.fType) {
case SkOperand2::kS32:
if (value.fOperand.fS32 != scriptTests[index].fIntAnswer)
SkDEBUGF(("script '%s' == value %d != expected answer %d\n", script, value.fOperand.fS32, scriptTests[index].fIntAnswer));
SkASSERT(value.fOperand.fS32 == scriptTests[index].fIntAnswer);
break;
case SkOperand2::kScalar:
error = SkScalarAbs(value.fOperand.fScalar - scriptTests[index].fScalarAnswer);
if (error >= SK_Scalar1 / 10000)
SkDEBUGF(("script '%s' == value %g != expected answer %g\n", script, value.fOperand.fScalar / (1.0f * SK_Scalar1), scriptTests[index].fScalarAnswer / (1.0f * SK_Scalar1)));
SkASSERT(error < SK_Scalar1 / 10000);
break;
case SkOperand2::kString:
SkASSERT(value.fOperand.fString->equals(scriptTests[index].fStringAnswer));
break;
default:
SkASSERT(0);
}
}
#endif
}
#endif
|