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
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkScript2_DEFINED
#define SkScript2_DEFINED
#include "SkOperand2.h"
#include "SkStream.h"
#include "SkTDArray.h"
#include "SkTDArray_Experimental.h"
#include "SkTDict.h"
#include "SkTDStack.h"
typedef SkLongArray(SkString*) SkTDStringArray;
class SkAnimateMaker;
class SkScriptCallBack;
class SkScriptEngine2 {
public:
enum Error {
kNoError,
kArrayIndexOutOfBounds,
kCouldNotFindReferencedID,
kFunctionCallFailed,
kMemberOpFailed,
kPropertyOpFailed
};
enum Attrs {
kConstant,
kVariable
};
SkScriptEngine2(SkOperand2::OpType returnType);
~SkScriptEngine2();
bool convertTo(SkOperand2::OpType , SkScriptValue2* );
bool evaluateScript(const char** script, SkScriptValue2* value);
void forget(SkOpArray* array);
Error getError() { return fError; }
SkOperand2::OpType getReturnType() { return fReturnType; }
void track(SkOpArray* array) {
SkASSERT(fTrackArray.find(array) < 0);
*fTrackArray.append() = array; }
void track(SkString* string) {
SkASSERT(fTrackString.find(string) < 0);
*fTrackString.append() = string;
}
static bool ConvertTo(SkScriptEngine2* , SkOperand2::OpType toType, SkScriptValue2* value);
static SkScalar IntToScalar(int32_t );
static bool ValueToString(const SkScriptValue2& value, SkString* string);
enum Op { // used by tokenizer attribute table
kUnassigned,
kAdd,
kBitAnd,
kBitNot,
kBitOr,
kDivide,
kEqual,
kFlipOps,
kGreaterEqual,
kLogicalAnd,
kLogicalNot,
kLogicalOr,
kMinus,
kModulo,
kMultiply,
kShiftLeft,
kShiftRight, // signed
kSubtract,
kXor,
// following not in attribute table
kArrayOp,
kElse,
kIf,
kParen,
kLastLogicalOp,
kArtificialOp = 0x20
};
enum TypeOp { // generated by tokenizer
kNop, // should never get generated
kAccumulatorPop,
kAccumulatorPush,
kAddInt,
kAddScalar,
kAddString, // string concat
kArrayIndex,
kArrayParam,
kArrayToken,
kBitAndInt,
kBitNotInt,
kBitOrInt,
kBoxToken,
kCallback,
kDivideInt,
kDivideScalar,
kDotOperator,
kElseOp,
kEnd,
kEqualInt,
kEqualScalar,
kEqualString,
kFunctionCall,
kFlipOpsOp,
kFunctionToken,
kGreaterEqualInt,
kGreaterEqualScalar,
kGreaterEqualString,
kIfOp,
kIntToScalar,
kIntToScalar2,
kIntToString,
kIntToString2,
kIntegerAccumulator,
kIntegerOperand,
kLogicalAndInt,
kLogicalNotInt,
kLogicalOrInt,
kMemberOp,
kMinusInt,
kMinusScalar,
kModuloInt,
kModuloScalar,
kMultiplyInt,
kMultiplyScalar,
kPropertyOp,
kScalarAccumulator,
kScalarOperand,
kScalarToInt,
kScalarToInt2,
kScalarToString,
kScalarToString2,
kShiftLeftInt,
kShiftRightInt, // signed
kStringAccumulator,
kStringOperand,
kStringToInt,
kStringToScalar,
kStringToScalar2,
kStringTrack,
kSubtractInt,
kSubtractScalar,
kToBool,
kUnboxToken,
kUnboxToken2,
kXorInt,
kLastTypeOp
};
enum OpBias {
kNoBias,
kTowardsNumber = 0,
kTowardsString
};
protected:
enum BraceStyle {
// kStructBrace,
kArrayBrace,
kFunctionBrace
};
enum AddTokenRegister {
kAccumulator,
kOperand
};
enum ResultIsBoolean {
kResultIsNotBoolean,
kResultIsBoolean
};
struct OperatorAttributes {
unsigned int fLeftType : 3; // SkOpType union, but only lower values
unsigned int fRightType : 3; // SkOpType union, but only lower values
OpBias fBias : 1;
ResultIsBoolean fResultIsBoolean : 1;
};
struct Branch {
Branch() {
}
Branch(Op op, int depth, unsigned offset) : fOffset(offset), fOpStackDepth(depth), fOperator(op),
fPrimed(kIsNotPrimed), fDone(kIsNotDone) {
}
enum Primed {
kIsNotPrimed,
kIsPrimed
};
enum Done {
kIsNotDone,
kIsDone,
};
unsigned fOffset : 16; // offset in generated stream where branch needs to go
int fOpStackDepth : 7; // depth when operator was found
Op fOperator : 6; // operand which generated branch
mutable Primed fPrimed : 1; // mark when next instruction generates branch
Done fDone : 1; // mark when branch is complete
void prime() { fPrimed = kIsPrimed; }
void resolve(SkDynamicMemoryWStream* , size_t offset);
};
static const OperatorAttributes gOpAttributes[];
static const signed char gPrecedence[];
static const TypeOp gTokens[];
void addToken(TypeOp );
void addTokenConst(SkScriptValue2* , AddTokenRegister , SkOperand2::OpType , TypeOp );
void addTokenInt(int );
void addTokenScalar(SkScalar );
void addTokenString(const SkString& );
void addTokenValue(const SkScriptValue2& , AddTokenRegister );
int arithmeticOp(char ch, char nextChar, bool lastPush);
bool convertParams(SkTDArray<SkScriptValue2>* ,
const SkOperand2::OpType* paramTypes, int paramTypeCount);
void convertToString(SkOperand2* operand, SkOperand2::OpType type) {
SkScriptValue2 scriptValue;
scriptValue.fOperand = *operand;
scriptValue.fType = type;
convertTo(SkOperand2::kString, &scriptValue);
*operand = scriptValue.fOperand;
}
bool evaluateDot(const char*& script);
bool evaluateDotParam(const char*& script, const char* field, size_t fieldLength);
bool functionParams(const char** scriptPtr, SkTDArray<SkScriptValue2>* params);
size_t getTokenOffset();
SkOperand2::OpType getUnboxType(SkOperand2 scriptValue);
bool handleArrayIndexer(const char** scriptPtr);
bool handleFunction(const char** scriptPtr);
bool handleMember(const char* field, size_t len, void* object);
bool handleMemberFunction(const char* field, size_t len, void* object,
SkTDArray<SkScriptValue2>* params);
bool handleProperty();
bool handleUnbox(SkScriptValue2* scriptValue);
bool innerScript(const char** scriptPtr, SkScriptValue2* value);
int logicalOp(char ch, char nextChar);
void processLogicalOp(Op op);
bool processOp();
void resolveBranch(Branch& );
// void setAnimateMaker(SkAnimateMaker* maker) { fMaker = maker; }
SkDynamicMemoryWStream fStream;
SkDynamicMemoryWStream* fActiveStream;
SkTDStack<BraceStyle> fBraceStack; // curly, square, function paren
SkTDStack<Branch> fBranchStack; // logical operators, slot to store forward branch
SkLongArray(SkScriptCallBack*) fCallBackArray;
SkTDStack<Op> fOpStack;
SkTDStack<SkScriptValue2> fValueStack;
// SkAnimateMaker* fMaker;
SkLongArray(SkOpArray*) fTrackArray;
SkTDStringArray fTrackString;
const char* fToken; // one-deep stack
size_t fTokenLength;
SkOperand2::OpType fReturnType;
Error fError;
SkOperand2::OpType fAccumulatorType; // tracking for code generation
SkBool fBranchPopAllowed;
SkBool fConstExpression;
SkBool fOperandInUse;
private:
#ifdef SK_DEBUG
public:
void decompile(const unsigned char* , size_t );
static void UnitTest();
static void ValidateDecompileTable();
#endif
};
#ifdef SK_DEBUG
struct SkScriptNAnswer2 {
const char* fScript;
SkOperand2::OpType fType;
int32_t fIntAnswer;
SkScalar fScalarAnswer;
const char* fStringAnswer;
};
#endif
#endif // SkScript2_DEFINED
|