michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkScript2.h" michael@0: #include "SkData.h" michael@0: #include "SkFloatingPoint.h" michael@0: #include "SkMath.h" michael@0: #include "SkParse.h" michael@0: #include "SkScriptCallBack.h" michael@0: #include "SkScriptRuntime.h" michael@0: #include "SkString.h" michael@0: #include "SkOpArray.h" michael@0: michael@0: const SkScriptEngine2::OperatorAttributes SkScriptEngine2::gOpAttributes[] = { michael@0: { SkOperand2::kNoType, SkOperand2::kNoType, kNoBias, kResultIsNotBoolean }, michael@0: { SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar | SkOperand2::kString), michael@0: SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar | SkOperand2::kString), kTowardsString, kResultIsNotBoolean }, // kAdd michael@0: { SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kBitAnd michael@0: { SkOperand2::kNoType, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kBitNot michael@0: { SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kBitOr michael@0: { SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), michael@0: SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), kNoBias, kResultIsNotBoolean }, // kDivide michael@0: { SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar | SkOperand2::kString), michael@0: SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar |SkOperand2:: kString), kTowardsNumber, michael@0: kResultIsBoolean }, // kEqual michael@0: { SkOperand2::kS32, SkOperand2::kNoType, kNoBias, kResultIsNotBoolean }, // kFlipOps michael@0: { SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar | SkOperand2::kString), michael@0: SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar | SkOperand2::kString), kTowardsNumber, michael@0: kResultIsBoolean }, // kGreaterEqual michael@0: { SkOperand2::kNoType, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kLogicalAnd (really, ToBool) michael@0: { SkOperand2::kNoType, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kLogicalNot michael@0: { SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kLogicalOr michael@0: { SkOperand2::kNoType, SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), kNoBias, kResultIsNotBoolean }, // kMinus michael@0: { SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), michael@0: SkOperand2::OpType(SkOperand2::kS32 |SkOperand2:: kScalar), kNoBias, kResultIsNotBoolean }, // kModulo michael@0: { SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), michael@0: SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), kNoBias, kResultIsNotBoolean }, // kMultiply michael@0: { SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kShiftLeft michael@0: { SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean }, // kShiftRight michael@0: { SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), michael@0: SkOperand2::OpType(SkOperand2::kS32 | SkOperand2::kScalar), kNoBias, kResultIsNotBoolean }, // kSubtract michael@0: { SkOperand2::kS32, SkOperand2::kS32, kNoBias, kResultIsNotBoolean } // kXor michael@0: }; michael@0: michael@0: #define kBracketPrecedence 16 michael@0: #define kIfElsePrecedence 15 michael@0: michael@0: const signed char SkScriptEngine2::gPrecedence[] = { michael@0: 17, // kUnassigned, michael@0: 6, // kAdd, michael@0: 10, // kBitAnd, michael@0: 4, // kBitNot, michael@0: 12, // kBitOr, michael@0: 5, // kDivide, michael@0: 9, // kEqual, michael@0: -1, // kFlipOps, michael@0: 8, // kGreaterEqual, michael@0: 13, // kLogicalAnd, michael@0: 4, // kLogicalNot, michael@0: 14, // kLogicalOr, michael@0: 4, // kMinus, michael@0: 5, // kModulo, michael@0: 5, // kMultiply, michael@0: 7, // kShiftLeft, michael@0: 7, // kShiftRight, // signed michael@0: 6, // kSubtract, michael@0: 11, // kXor michael@0: kBracketPrecedence, // kArrayOp michael@0: kIfElsePrecedence, // kElse michael@0: kIfElsePrecedence, // kIf michael@0: kBracketPrecedence, // kParen michael@0: }; michael@0: michael@0: const SkScriptEngine2::TypeOp SkScriptEngine2::gTokens[] = { michael@0: kNop, // unassigned michael@0: kAddInt, // kAdd, michael@0: kBitAndInt, // kBitAnd, michael@0: kBitNotInt, // kBitNot, michael@0: kBitOrInt, // kBitOr, michael@0: kDivideInt, // kDivide, michael@0: kEqualInt, // kEqual, michael@0: kFlipOpsOp, // kFlipOps, michael@0: kGreaterEqualInt, // kGreaterEqual, michael@0: kLogicalAndInt, // kLogicalAnd, michael@0: kLogicalNotInt, // kLogicalNot, michael@0: kLogicalOrInt, // kLogicalOr, michael@0: kMinusInt, // kMinus, michael@0: kModuloInt, // kModulo, michael@0: kMultiplyInt, // kMultiply, michael@0: kShiftLeftInt, // kShiftLeft, michael@0: kShiftRightInt, // kShiftRight, // signed michael@0: kSubtractInt, // kSubtract, michael@0: kXorInt // kXor michael@0: }; michael@0: michael@0: static inline bool is_between(int c, int min, int max) michael@0: { michael@0: return (unsigned)(c - min) <= (unsigned)(max - min); michael@0: } michael@0: michael@0: static inline bool is_ws(int c) michael@0: { michael@0: return is_between(c, 1, 32); michael@0: } michael@0: michael@0: static int token_length(const char* start) { michael@0: char ch = start[0]; michael@0: if (! is_between(ch, 'a' , 'z') && ! is_between(ch, 'A', 'Z') && ch != '_' && ch != '$') michael@0: return -1; michael@0: int length = 0; michael@0: do michael@0: ch = start[++length]; michael@0: while (is_between(ch, 'a' , 'z') || is_between(ch, 'A', 'Z') || is_between(ch, '0', '9') || michael@0: ch == '_' || ch == '$'); michael@0: return length; michael@0: } michael@0: michael@0: SkScriptEngine2::SkScriptEngine2(SkOperand2::OpType returnType) : fActiveStream(&fStream), michael@0: fTokenLength(0), fReturnType(returnType), fError(kNoError), michael@0: fAccumulatorType(SkOperand2::kNoType), michael@0: fBranchPopAllowed(true), fConstExpression(true), fOperandInUse(false) michael@0: { michael@0: Branch branch(kUnassigned, 0, 0); michael@0: fBranchStack.push(branch); michael@0: *fOpStack.push() = (Op) kParen; michael@0: } michael@0: michael@0: SkScriptEngine2::~SkScriptEngine2() { michael@0: for (SkString** stringPtr = fTrackString.begin(); stringPtr < fTrackString.end(); stringPtr++) michael@0: delete *stringPtr; michael@0: for (SkOpArray** arrayPtr = fTrackArray.begin(); arrayPtr < fTrackArray.end(); arrayPtr++) michael@0: delete *arrayPtr; michael@0: } michael@0: michael@0: void SkScriptEngine2::addToken(SkScriptEngine2::TypeOp op) { michael@0: int limit = fBranchStack.count() - 1; michael@0: for (int index = 0; index < limit; index++) { michael@0: Branch& branch = fBranchStack.index(index); michael@0: if (branch.fPrimed == Branch::kIsPrimed) michael@0: resolveBranch(branch); michael@0: } michael@0: if (fBranchPopAllowed) { michael@0: while (fBranchStack.top().fDone == Branch::kIsDone) michael@0: fBranchStack.pop(); michael@0: } michael@0: unsigned char charOp = (unsigned char) op; michael@0: fActiveStream->write(&charOp, sizeof(charOp)); michael@0: } michael@0: michael@0: void SkScriptEngine2::addTokenConst(SkScriptValue2* value, AddTokenRegister reg, michael@0: SkOperand2::OpType toType, SkScriptEngine2::TypeOp op) { michael@0: if (value->fIsConstant == SkScriptValue2::kConstant && convertTo(toType, value)) michael@0: return; michael@0: addTokenValue(*value, reg); michael@0: addToken(op); michael@0: value->fIsWritten = SkScriptValue2::kWritten; michael@0: value->fType = toType; michael@0: } michael@0: michael@0: void SkScriptEngine2::addTokenInt(int integer) { michael@0: fActiveStream->write(&integer, sizeof(integer)); michael@0: } michael@0: michael@0: void SkScriptEngine2::addTokenScalar(SkScalar scalar) { michael@0: fActiveStream->write(&scalar, sizeof(scalar)); michael@0: } michael@0: michael@0: void SkScriptEngine2::addTokenString(const SkString& string) { michael@0: int size = string.size(); michael@0: addTokenInt(size); michael@0: fActiveStream->write(string.c_str(), size); michael@0: } michael@0: michael@0: void SkScriptEngine2::addTokenValue(const SkScriptValue2& value, AddTokenRegister reg) { michael@0: if (value.isConstant() == false) { michael@0: if (reg == kAccumulator) { michael@0: if (fAccumulatorType == SkOperand2::kNoType) michael@0: addToken(kAccumulatorPop); michael@0: } else { michael@0: ; // !!! incomplete? michael@0: } michael@0: return; michael@0: } michael@0: if (reg == kAccumulator && fAccumulatorType != SkOperand2::kNoType) michael@0: addToken(kAccumulatorPush); michael@0: switch (value.fType) { michael@0: case SkOperand2::kS32: michael@0: addToken(reg == kAccumulator ? kIntegerAccumulator : kIntegerOperand); michael@0: addTokenInt(value.fOperand.fS32); michael@0: if (reg == kAccumulator) michael@0: fAccumulatorType = SkOperand2::kS32; michael@0: else michael@0: fOperandInUse = true; michael@0: break; michael@0: case SkOperand2::kScalar: michael@0: addToken(reg == kAccumulator ? kScalarAccumulator : kScalarOperand); michael@0: addTokenScalar(value.fOperand.fScalar); michael@0: if (reg == kAccumulator) michael@0: fAccumulatorType = SkOperand2::kScalar; michael@0: else michael@0: fOperandInUse = true; michael@0: break; michael@0: case SkOperand2::kString: michael@0: addToken(reg == kAccumulator ? kStringAccumulator : kStringOperand); michael@0: addTokenString(*value.fOperand.fString); michael@0: if (reg == kAccumulator) michael@0: fAccumulatorType = SkOperand2::kString; michael@0: else michael@0: fOperandInUse = true; michael@0: break; michael@0: default: michael@0: SkASSERT(0); //!!! not implemented yet michael@0: } michael@0: } michael@0: michael@0: int SkScriptEngine2::arithmeticOp(char ch, char nextChar, bool lastPush) { michael@0: Op op = kUnassigned; michael@0: bool reverseOperands = false; michael@0: bool negateResult = false; michael@0: int advance = 1; michael@0: switch (ch) { michael@0: case '+': michael@0: // !!! ignoring unary plus as implemented here has the side effect of michael@0: // suppressing errors like +"hi" michael@0: if (lastPush == false) // unary plus, don't push an operator michael@0: return advance; michael@0: op = kAdd; michael@0: break; michael@0: case '-': michael@0: op = lastPush ? kSubtract : kMinus; michael@0: break; michael@0: case '*': michael@0: op = kMultiply; michael@0: break; michael@0: case '/': michael@0: op = kDivide; michael@0: break; michael@0: case '>': michael@0: if (nextChar == '>') { michael@0: op = kShiftRight; michael@0: goto twoChar; michael@0: } michael@0: op = kGreaterEqual; michael@0: if (nextChar == '=') michael@0: goto twoChar; michael@0: reverseOperands = negateResult = true; michael@0: break; michael@0: case '<': michael@0: if (nextChar == '<') { michael@0: op = kShiftLeft; michael@0: goto twoChar; michael@0: } michael@0: op = kGreaterEqual; michael@0: reverseOperands = nextChar == '='; michael@0: negateResult = ! reverseOperands; michael@0: advance += reverseOperands; michael@0: break; michael@0: case '=': michael@0: if (nextChar == '=') { michael@0: op = kEqual; michael@0: goto twoChar; michael@0: } michael@0: break; michael@0: case '!': michael@0: if (nextChar == '=') { michael@0: op = kEqual; michael@0: negateResult = true; michael@0: twoChar: michael@0: advance++; michael@0: break; michael@0: } michael@0: op = kLogicalNot; michael@0: break; michael@0: case '?': michael@0: op =(Op) kIf; michael@0: break; michael@0: case ':': michael@0: op = (Op) kElse; michael@0: break; michael@0: case '^': michael@0: op = kXor; michael@0: break; michael@0: case '(': michael@0: *fOpStack.push() = (Op) kParen; michael@0: return advance; michael@0: case '&': michael@0: SkASSERT(nextChar != '&'); michael@0: op = kBitAnd; michael@0: break; michael@0: case '|': michael@0: SkASSERT(nextChar != '|'); michael@0: op = kBitOr; michael@0: break; michael@0: case '%': michael@0: op = kModulo; michael@0: break; michael@0: case '~': michael@0: op = kBitNot; michael@0: break; michael@0: } michael@0: if (op == kUnassigned) michael@0: return 0; michael@0: signed char precedence = gPrecedence[op]; michael@0: do { michael@0: int idx = 0; michael@0: Op compare; michael@0: do { michael@0: compare = fOpStack.index(idx); michael@0: if ((compare & kArtificialOp) == 0) michael@0: break; michael@0: idx++; michael@0: } while (true); michael@0: signed char topPrecedence = gPrecedence[compare]; michael@0: SkASSERT(topPrecedence != -1); michael@0: if (topPrecedence > precedence || (topPrecedence == precedence && michael@0: gOpAttributes[op].fLeftType == SkOperand2::kNoType)) { michael@0: break; michael@0: } michael@0: processOp(); michael@0: } while (true); michael@0: if (negateResult) michael@0: *fOpStack.push() = (Op) (kLogicalNot | kArtificialOp); michael@0: fOpStack.push(op); michael@0: if (reverseOperands) michael@0: *fOpStack.push() = (Op) (kFlipOps | kArtificialOp); michael@0: michael@0: return advance; michael@0: } michael@0: michael@0: bool SkScriptEngine2::convertParams(SkTDArray* params, michael@0: const SkOperand2::OpType* paramTypes, int paramCount) { michael@0: int count = params->count(); michael@0: if (count > paramCount) { michael@0: SkASSERT(0); michael@0: return false; // too many parameters passed michael@0: } michael@0: for (int index = 0; index < count; index++) michael@0: convertTo(paramTypes[index], &(*params)[index]); michael@0: return true; michael@0: } michael@0: michael@0: bool SkScriptEngine2::convertTo(SkOperand2::OpType toType, SkScriptValue2* value ) { michael@0: SkOperand2::OpType type = value->fType; michael@0: if (type == toType) michael@0: return true; michael@0: if (type == SkOperand2::kObject) { michael@0: if (handleUnbox(value) == false) michael@0: return false; michael@0: return convertTo(toType, value); michael@0: } michael@0: return ConvertTo(this, toType, value); michael@0: } michael@0: michael@0: bool SkScriptEngine2::evaluateDot(const char*& script) { michael@0: size_t fieldLength = token_length(++script); // skip dot michael@0: SkASSERT(fieldLength > 0); // !!! add error handling michael@0: const char* field = script; michael@0: script += fieldLength; michael@0: bool success = handleProperty(); michael@0: if (success == false) { michael@0: fError = kCouldNotFindReferencedID; michael@0: goto error; michael@0: } michael@0: return evaluateDotParam(script, field, fieldLength); michael@0: error: michael@0: return false; michael@0: } michael@0: michael@0: bool SkScriptEngine2::evaluateDotParam(const char*& script, const char* field, size_t fieldLength) { michael@0: SkScriptValue2& top = fValueStack.top(); michael@0: if (top.fType != SkOperand2::kObject) michael@0: return false; michael@0: void* object = top.fOperand.fObject; michael@0: fValueStack.pop(); michael@0: char ch; // see if it is a simple member or a function michael@0: while (is_ws(ch = script[0])) michael@0: script++; michael@0: bool success = true; michael@0: if (ch != '(') michael@0: success = handleMember(field, fieldLength, object); michael@0: else { michael@0: SkTDArray params; michael@0: *fBraceStack.push() = kFunctionBrace; michael@0: success = functionParams(&script, ¶ms); michael@0: if (success) michael@0: success = handleMemberFunction(field, fieldLength, object, ¶ms); michael@0: } michael@0: return success; michael@0: } michael@0: michael@0: bool SkScriptEngine2::evaluateScript(const char** scriptPtr, SkScriptValue2* value) { michael@0: // fArrayOffset = 0; // no support for structures for now michael@0: bool success; michael@0: const char* inner; michael@0: if (strncmp(*scriptPtr, "#script:", sizeof("#script:") - 1) == 0) { michael@0: *scriptPtr += sizeof("#script:") - 1; michael@0: if (fReturnType == SkOperand2::kNoType || fReturnType == SkOperand2::kString) { michael@0: success = innerScript(scriptPtr, value); michael@0: SkASSERT(success); michael@0: inner = value->fOperand.fString->c_str(); michael@0: scriptPtr = &inner; michael@0: } michael@0: } michael@0: success = innerScript(scriptPtr, value); michael@0: const char* script = *scriptPtr; michael@0: char ch; michael@0: while (is_ws(ch = script[0])) michael@0: script++; michael@0: if (ch != '\0') { michael@0: // error may trigger on scripts like "50,0" that were intended to be written as "[50, 0]" michael@0: return false; michael@0: } michael@0: return success; michael@0: } michael@0: michael@0: void SkScriptEngine2::forget(SkOpArray* array) { michael@0: if (array->getType() == SkOperand2::kString) { michael@0: for (int index = 0; index < array->count(); index++) { michael@0: SkString* string = (*array)[index].fString; michael@0: int found = fTrackString.find(string); michael@0: if (found >= 0) michael@0: fTrackString.remove(found); michael@0: } michael@0: return; michael@0: } michael@0: if (array->getType() == SkOperand2::kArray) { michael@0: for (int index = 0; index < array->count(); index++) { michael@0: SkOpArray* child = (*array)[index].fArray; michael@0: forget(child); // forgets children of child michael@0: int found = fTrackArray.find(child); michael@0: if (found >= 0) michael@0: fTrackArray.remove(found); michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool SkScriptEngine2::functionParams(const char** scriptPtr, SkTDArray* params) { michael@0: (*scriptPtr)++; // skip open paren michael@0: *fOpStack.push() = (Op) kParen; michael@0: *fBraceStack.push() = kFunctionBrace; michael@0: do { michael@0: SkScriptValue2 value; michael@0: bool success = innerScript(scriptPtr, &value); michael@0: SkASSERT(success); michael@0: if (success == false) michael@0: return false; michael@0: *params->append() = value; michael@0: } while ((*scriptPtr)[-1] == ','); michael@0: fBraceStack.pop(); michael@0: fOpStack.pop(); // pop paren michael@0: (*scriptPtr)++; // advance beyond close paren michael@0: return true; michael@0: } michael@0: michael@0: size_t SkScriptEngine2::getTokenOffset() { michael@0: return fActiveStream->getOffset(); michael@0: } michael@0: michael@0: SkOperand2::OpType SkScriptEngine2::getUnboxType(SkOperand2 scriptValue) { michael@0: for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) { michael@0: if ((*callBack)->getType() != SkScriptCallBack::kUnbox) michael@0: continue; michael@0: return (*callBack)->getReturnType(0, &scriptValue); michael@0: } michael@0: return SkOperand2::kObject; michael@0: } michael@0: michael@0: bool SkScriptEngine2::innerScript(const char** scriptPtr, SkScriptValue2* value) { michael@0: const char* script = *scriptPtr; michael@0: char ch; michael@0: bool lastPush = false; michael@0: bool success = true; michael@0: int opBalance = fOpStack.count(); michael@0: int baseBrace = fBraceStack.count(); michael@0: int branchBalance = fBranchStack.count(); michael@0: while ((ch = script[0]) != '\0') { michael@0: if (is_ws(ch)) { michael@0: script++; michael@0: continue; michael@0: } michael@0: SkScriptValue2 operand; michael@0: const char* dotCheck; michael@0: if (fBraceStack.count() > baseBrace) { michael@0: if (fBraceStack.top() == kArrayBrace) { michael@0: SkScriptValue2 tokenValue; michael@0: success = innerScript(&script, &tokenValue); // terminate and return on comma, close brace michael@0: SkASSERT(success); michael@0: { michael@0: SkOperand2::OpType type = fReturnType; michael@0: if (fReturnType == SkOperand2::kNoType) { michael@0: // !!! short sighted; in the future, allow each returned array component to carry michael@0: // its own type, and let caller do any needed conversions michael@0: if (value->fOperand.fArray->count() == 0) michael@0: value->fOperand.fArray->setType(type = tokenValue.fType); michael@0: else michael@0: type = value->fOperand.fArray->getType(); michael@0: } michael@0: if (tokenValue.fType != type) michael@0: convertTo(type, &tokenValue); michael@0: *value->fOperand.fArray->append() = tokenValue.fOperand; michael@0: } michael@0: lastPush = false; michael@0: continue; michael@0: } else { michael@0: SkASSERT(token_length(script) > 0); michael@0: } michael@0: } michael@0: if (lastPush != false && fTokenLength > 0) { michael@0: if (ch == '(') { michael@0: *fBraceStack.push() = kFunctionBrace; michael@0: SkString functionName(fToken, fTokenLength); michael@0: michael@0: if (handleFunction(&script) == false) michael@0: return false; michael@0: lastPush = true; michael@0: continue; michael@0: } else if (ch == '[') { michael@0: if (handleProperty() == false) { michael@0: SkASSERT(0); michael@0: return false; michael@0: } michael@0: if (handleArrayIndexer(&script) == false) michael@0: return false; michael@0: lastPush = true; michael@0: continue; michael@0: } else if (ch != '.') { michael@0: if (handleProperty() == false) { michael@0: SkASSERT(0); michael@0: return false; michael@0: } michael@0: lastPush = true; michael@0: continue; michael@0: } michael@0: } michael@0: if (ch == '0' && (script[1] & ~0x20) == 'X') { michael@0: SkASSERT(lastPush == false); michael@0: script += 2; michael@0: script = SkParse::FindHex(script, (uint32_t*) &operand.fOperand.fS32); michael@0: SkASSERT(script); michael@0: goto intCommon; michael@0: } michael@0: if (lastPush == false && ch == '.') michael@0: goto scalarCommon; michael@0: if (ch >= '0' && ch <= '9') { michael@0: SkASSERT(lastPush == false); michael@0: dotCheck = SkParse::FindS32(script, &operand.fOperand.fS32); michael@0: if (dotCheck[0] != '.') { michael@0: script = dotCheck; michael@0: intCommon: michael@0: operand.fType = SkOperand2::kS32; michael@0: } else { michael@0: scalarCommon: michael@0: script = SkParse::FindScalar(script, &operand.fOperand.fScalar); michael@0: operand.fType = SkOperand2::kScalar; michael@0: } michael@0: operand.fIsConstant = SkScriptValue2::kConstant; michael@0: fValueStack.push(operand); michael@0: lastPush = true; michael@0: continue; michael@0: } michael@0: int length = token_length(script); michael@0: if (length > 0) { michael@0: SkASSERT(lastPush == false); michael@0: fToken = script; michael@0: fTokenLength = length; michael@0: script += length; michael@0: lastPush = true; michael@0: continue; michael@0: } michael@0: char startQuote = ch; michael@0: if (startQuote == '\'' || startQuote == '\"') { michael@0: SkASSERT(lastPush == false); michael@0: operand.fOperand.fString = new SkString(); michael@0: ++script; michael@0: const char* stringStart = script; michael@0: do { // measure string michael@0: if (script[0] == '\\') michael@0: ++script; michael@0: ++script; michael@0: SkASSERT(script[0]); // !!! throw an error michael@0: } while (script[0] != startQuote); michael@0: operand.fOperand.fString->set(stringStart, script - stringStart); michael@0: script = stringStart; michael@0: char* stringWrite = operand.fOperand.fString->writable_str(); michael@0: do { // copy string michael@0: if (script[0] == '\\') michael@0: ++script; michael@0: *stringWrite++ = script[0]; michael@0: ++script; michael@0: SkASSERT(script[0]); // !!! throw an error michael@0: } while (script[0] != startQuote); michael@0: ++script; michael@0: track(operand.fOperand.fString); michael@0: operand.fType = SkOperand2::kString; michael@0: operand.fIsConstant = SkScriptValue2::kConstant; michael@0: fValueStack.push(operand); michael@0: lastPush = true; michael@0: continue; michael@0: } michael@0: if (ch == '.') { michael@0: if (fTokenLength == 0) { michael@0: int tokenLength = token_length(++script); michael@0: const char* token = script; michael@0: script += tokenLength; michael@0: SkASSERT(fValueStack.count() > 0); // !!! add error handling michael@0: SkScriptValue2 top; michael@0: fValueStack.pop(&top); michael@0: michael@0: addTokenInt(top.fType); michael@0: addToken(kBoxToken); michael@0: top.fType = SkOperand2::kObject; michael@0: top.fIsConstant = SkScriptValue2::kVariable; michael@0: fConstExpression = false; michael@0: fValueStack.push(top); michael@0: success = evaluateDotParam(script, token, tokenLength); michael@0: SkASSERT(success); michael@0: lastPush = true; michael@0: continue; michael@0: } michael@0: // get next token, and evaluate immediately michael@0: success = evaluateDot(script); michael@0: if (success == false) { michael@0: // SkASSERT(0); michael@0: return false; michael@0: } michael@0: lastPush = true; michael@0: continue; michael@0: } michael@0: if (ch == '[') { michael@0: if (lastPush == false) { michael@0: script++; michael@0: *fBraceStack.push() = kArrayBrace; michael@0: operand.fOperand.fArray = value->fOperand.fArray = new SkOpArray(fReturnType); michael@0: track(value->fOperand.fArray); michael@0: michael@0: operand.fType = SkOperand2::kArray; michael@0: operand.fIsConstant = SkScriptValue2::kVariable; michael@0: fValueStack.push(operand); michael@0: continue; michael@0: } michael@0: if (handleArrayIndexer(&script) == false) michael@0: return false; michael@0: lastPush = true; michael@0: continue; michael@0: } michael@0: #if 0 // structs not supported for now michael@0: if (ch == '{') { michael@0: if (lastPush == false) { michael@0: script++; michael@0: *fBraceStack.push() = kStructBrace; michael@0: operand.fS32 = 0; michael@0: *fTypeStack.push() = (SkOpType) kStruct; michael@0: fOperandStack.push(operand); michael@0: continue; michael@0: } michael@0: SkASSERT(0); // braces in other contexts aren't supported yet michael@0: } michael@0: #endif michael@0: if (ch == ')' && fBraceStack.count() > 0) { michael@0: BraceStyle braceStyle = fBraceStack.top(); michael@0: if (braceStyle == kFunctionBrace) { michael@0: fBraceStack.pop(); michael@0: break; michael@0: } michael@0: } michael@0: if (ch == ',' || ch == ']') { michael@0: if (ch != ',') { michael@0: BraceStyle match; michael@0: fBraceStack.pop(&match); michael@0: SkASSERT(match == kArrayBrace); michael@0: } michael@0: script++; michael@0: // !!! see if brace or bracket is correct closer michael@0: break; michael@0: } michael@0: char nextChar = script[1]; michael@0: int advance = logicalOp(ch, nextChar); michael@0: if (advance == 0) michael@0: advance = arithmeticOp(ch, nextChar, lastPush); michael@0: if (advance == 0) // unknown token michael@0: return false; michael@0: if (advance > 0) michael@0: script += advance; michael@0: lastPush = ch == ']' || ch == ')'; michael@0: } michael@0: if (fTokenLength > 0) { michael@0: success = handleProperty(); michael@0: SkASSERT(success); michael@0: } michael@0: int branchIndex = 0; michael@0: branchBalance = fBranchStack.count() - branchBalance; michael@0: fBranchPopAllowed = false; michael@0: while (branchIndex < branchBalance) { michael@0: Branch& branch = fBranchStack.index(branchIndex++); michael@0: if (branch.fPrimed == Branch::kIsPrimed) michael@0: break; michael@0: Op branchOp = branch.fOperator; michael@0: SkOperand2::OpType lastType = fValueStack.top().fType; michael@0: addTokenValue(fValueStack.top(), kAccumulator); michael@0: fValueStack.pop(); michael@0: if (branchOp == kLogicalAnd || branchOp == kLogicalOr) { michael@0: if (branch.fOperator == kLogicalAnd) michael@0: branch.prime(); michael@0: addToken(kToBool); michael@0: } else { michael@0: resolveBranch(branch); michael@0: SkScriptValue2 operand; michael@0: operand.fType = lastType; michael@0: // !!! note that many branching expressions could be constant michael@0: // today, we always evaluate branches as returning variables michael@0: operand.fIsConstant = SkScriptValue2::kVariable; michael@0: fValueStack.push(operand); michael@0: } michael@0: if (branch.fDone == Branch::kIsNotDone) michael@0: branch.prime(); michael@0: } michael@0: fBranchPopAllowed = true; michael@0: while (fBranchStack.top().fDone == Branch::kIsDone) michael@0: fBranchStack.pop(); michael@0: while (fOpStack.count() > opBalance) { // leave open paren michael@0: if (processOp() == false) michael@0: return false; michael@0: } michael@0: SkOperand2::OpType topType = fValueStack.count() > 0 ? fValueStack.top().fType : SkOperand2::kNoType; michael@0: if (topType != fReturnType && michael@0: topType == SkOperand2::kString && fReturnType != SkOperand2::kNoType) { // if result is a string, give handle property a chance to convert it to the property value michael@0: SkString* string = fValueStack.top().fOperand.fString; michael@0: fToken = string->c_str(); michael@0: fTokenLength = string->size(); michael@0: fValueStack.pop(); michael@0: success = handleProperty(); michael@0: if (success == false) { // if it couldn't convert, return string (error?) michael@0: SkScriptValue2 operand; michael@0: operand.fType = SkOperand2::kString; michael@0: operand.fOperand.fString = string; michael@0: operand.fIsConstant = SkScriptValue2::kVariable; // !!! ? michael@0: fValueStack.push(operand); michael@0: } michael@0: } michael@0: if (fStream.getOffset() > 0) { michael@0: addToken(kEnd); michael@0: SkAutoDataUnref data(fStream.copyToData()); michael@0: #ifdef SK_DEBUG michael@0: decompile(data->bytes(), data->size()); michael@0: #endif michael@0: SkScriptRuntime runtime(fCallBackArray); michael@0: runtime.executeTokens((unsigned char*) data->bytes()); michael@0: SkScriptValue2 value1; michael@0: runtime.getResult(&value1.fOperand); michael@0: value1.fType = fReturnType; michael@0: fValueStack.push(value1); michael@0: } michael@0: if (value) { michael@0: if (fValueStack.count() == 0) michael@0: return false; michael@0: fValueStack.pop(value); michael@0: if (value->fType != fReturnType && value->fType == SkOperand2::kObject && michael@0: fReturnType != SkOperand2::kNoType) michael@0: convertTo(fReturnType, value); michael@0: } michael@0: // if (fBranchStack.top().fOpStackDepth > fOpStack.count()) michael@0: // resolveBranch(); michael@0: *scriptPtr = script; michael@0: return true; // no error michael@0: } michael@0: michael@0: bool SkScriptEngine2::handleArrayIndexer(const char** scriptPtr) { michael@0: SkScriptValue2 scriptValue; michael@0: (*scriptPtr)++; michael@0: *fOpStack.push() = (Op) kParen; michael@0: *fBraceStack.push() = kArrayBrace; michael@0: SkOperand2::OpType saveType = fReturnType; michael@0: fReturnType = SkOperand2::kS32; michael@0: bool success = innerScript(scriptPtr, &scriptValue); michael@0: fReturnType = saveType; michael@0: SkASSERT(success); michael@0: success = convertTo(SkOperand2::kS32, &scriptValue); michael@0: SkASSERT(success); michael@0: int index = scriptValue.fOperand.fS32; michael@0: fValueStack.pop(&scriptValue); michael@0: if (scriptValue.fType == SkOperand2::kObject) { michael@0: success = handleUnbox(&scriptValue); michael@0: SkASSERT(success); michael@0: SkASSERT(scriptValue.fType == SkOperand2::kArray); michael@0: } michael@0: scriptValue.fType = scriptValue.fOperand.fArray->getType(); michael@0: // SkASSERT(index >= 0); michael@0: if ((unsigned) index >= (unsigned) scriptValue.fOperand.fArray->count()) { michael@0: fError = kArrayIndexOutOfBounds; michael@0: return false; michael@0: } michael@0: scriptValue.fOperand = scriptValue.fOperand.fArray->begin()[index]; michael@0: scriptValue.fIsConstant = SkScriptValue2::kVariable; michael@0: fValueStack.push(scriptValue); michael@0: fOpStack.pop(); // pop paren michael@0: return success; michael@0: } michael@0: michael@0: bool SkScriptEngine2::handleFunction(const char** scriptPtr) { michael@0: const char* functionName = fToken; michael@0: size_t functionNameLen = fTokenLength; michael@0: fTokenLength = 0; michael@0: SkTDArray params; michael@0: bool success = functionParams(scriptPtr, ¶ms); michael@0: if (success == false) michael@0: goto done; michael@0: { michael@0: for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) { michael@0: if ((*callBack)->getType() != SkScriptCallBack::kFunction) michael@0: continue; michael@0: SkScriptValue2 callbackResult; michael@0: success = (*callBack)->getReference(functionName, functionNameLen, &callbackResult); michael@0: if (success) { michael@0: callbackResult.fType = (*callBack)->getReturnType(callbackResult.fOperand.fReference, NULL); michael@0: callbackResult.fIsConstant = SkScriptValue2::kVariable; michael@0: fValueStack.push(callbackResult); michael@0: goto done; michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: done: michael@0: fOpStack.pop(); michael@0: return success; michael@0: } michael@0: michael@0: bool SkScriptEngine2::handleMember(const char* field, size_t len, void* object) { michael@0: bool success = true; michael@0: for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) { michael@0: if ((*callBack)->getType() != SkScriptCallBack::kMember) michael@0: continue; michael@0: SkScriptValue2 callbackResult; michael@0: success = (*callBack)->getReference(field, len, &callbackResult); michael@0: if (success) { michael@0: if (callbackResult.fType == SkOperand2::kString) michael@0: track(callbackResult.fOperand.fString); michael@0: callbackResult.fIsConstant = SkScriptValue2::kVariable; michael@0: fValueStack.push(callbackResult); michael@0: goto done; michael@0: } michael@0: } michael@0: return false; michael@0: done: michael@0: return success; michael@0: } michael@0: michael@0: bool SkScriptEngine2::handleMemberFunction(const char* field, size_t len, void* object, michael@0: SkTDArray* params) { michael@0: bool success = true; michael@0: for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) { michael@0: if ((*callBack)->getType() != SkScriptCallBack::kMemberFunction) michael@0: continue; michael@0: SkScriptValue2 callbackResult; michael@0: success = (*callBack)->getReference(field, len, &callbackResult); michael@0: if (success) { michael@0: if (callbackResult.fType == SkOperand2::kString) michael@0: track(callbackResult.fOperand.fString); michael@0: callbackResult.fIsConstant = SkScriptValue2::kVariable; michael@0: fValueStack.push(callbackResult); michael@0: goto done; michael@0: } michael@0: } michael@0: return false; michael@0: done: michael@0: return success; michael@0: } michael@0: michael@0: bool SkScriptEngine2::handleProperty() { michael@0: bool success = true; michael@0: for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) { michael@0: if ((*callBack)->getType() != SkScriptCallBack::kProperty) michael@0: continue; michael@0: SkScriptValue2 callbackResult; michael@0: success = (*callBack)->getReference(fToken, fTokenLength, &callbackResult); michael@0: if (success) { michael@0: if (callbackResult.fType == SkOperand2::kString && callbackResult.fOperand.fString == NULL) { michael@0: callbackResult.fOperand.fString = new SkString(fToken, fTokenLength); michael@0: track(callbackResult.fOperand.fString); michael@0: } michael@0: callbackResult.fIsConstant = SkScriptValue2::kVariable; michael@0: fValueStack.push(callbackResult); michael@0: goto done; michael@0: } michael@0: } michael@0: done: michael@0: fTokenLength = 0; michael@0: return success; michael@0: } michael@0: michael@0: bool SkScriptEngine2::handleUnbox(SkScriptValue2* scriptValue) { michael@0: bool success = true; michael@0: for (SkScriptCallBack** callBack = fCallBackArray.begin(); callBack < fCallBackArray.end(); callBack++) { michael@0: if ((*callBack)->getType() != SkScriptCallBack::kUnbox) michael@0: continue; michael@0: SkScriptCallBackConvert* callBackConvert = (SkScriptCallBackConvert*) *callBack; michael@0: success = callBackConvert->convert(scriptValue->fType, &scriptValue->fOperand); michael@0: if (success) { michael@0: if (scriptValue->fType == SkOperand2::kString) michael@0: track(scriptValue->fOperand.fString); michael@0: goto done; michael@0: } michael@0: } michael@0: return false; michael@0: done: michael@0: return success; michael@0: } michael@0: michael@0: // note that entire expression is treated as if it were enclosed in parens michael@0: // an open paren is always the first thing in the op stack michael@0: michael@0: int SkScriptEngine2::logicalOp(char ch, char nextChar) { michael@0: int advance = 1; michael@0: Op op; michael@0: signed char precedence; michael@0: switch (ch) { michael@0: case ')': michael@0: op = (Op) kParen; michael@0: break; michael@0: case ']': michael@0: op = (Op) kArrayOp; michael@0: break; michael@0: case '?': michael@0: op = (Op) kIf; michael@0: break; michael@0: case ':': michael@0: op = (Op) kElse; michael@0: break; michael@0: case '&': michael@0: if (nextChar != '&') michael@0: goto noMatch; michael@0: op = kLogicalAnd; michael@0: advance = 2; michael@0: break; michael@0: case '|': michael@0: if (nextChar != '|') michael@0: goto noMatch; michael@0: op = kLogicalOr; michael@0: advance = 2; michael@0: break; michael@0: default: michael@0: noMatch: michael@0: return 0; michael@0: } michael@0: precedence = gPrecedence[op]; michael@0: int branchIndex = 0; michael@0: fBranchPopAllowed = false; michael@0: do { michael@0: while (gPrecedence[fOpStack.top() & ~kArtificialOp] < precedence) michael@0: processOp(); michael@0: Branch& branch = fBranchStack.index(branchIndex++); michael@0: Op branchOp = branch.fOperator; michael@0: if (gPrecedence[branchOp] >= precedence) michael@0: break; michael@0: addTokenValue(fValueStack.top(), kAccumulator); michael@0: fValueStack.pop(); michael@0: if (branchOp == kLogicalAnd || branchOp == kLogicalOr) { michael@0: if (branch.fOperator == kLogicalAnd) michael@0: branch.prime(); michael@0: addToken(kToBool); michael@0: } else michael@0: resolveBranch(branch); michael@0: if (branch.fDone == Branch::kIsNotDone) michael@0: branch.prime(); michael@0: } while (true); michael@0: fBranchPopAllowed = true; michael@0: while (fBranchStack.top().fDone == Branch::kIsDone) michael@0: fBranchStack.pop(); michael@0: processLogicalOp(op); michael@0: return advance; michael@0: } michael@0: michael@0: void SkScriptEngine2::processLogicalOp(Op op) { michael@0: switch (op) { michael@0: case kParen: michael@0: case kArrayOp: michael@0: SkASSERT(fOpStack.count() > 1 && fOpStack.top() == op); // !!! add error handling michael@0: if (op == kParen) michael@0: fOpStack.pop(); michael@0: else { michael@0: SkScriptValue2 value; michael@0: fValueStack.pop(&value); michael@0: SkASSERT(value.fType == SkOperand2::kS32 || value.fType == SkOperand2::kScalar); // !!! add error handling (although, could permit strings eventually) michael@0: int index = value.fType == SkOperand2::kScalar ? SkScalarFloorToInt(value.fOperand.fScalar) : michael@0: value.fOperand.fS32; michael@0: SkScriptValue2 arrayValue; michael@0: fValueStack.pop(&arrayValue); michael@0: SkASSERT(arrayValue.fType == SkOperand2::kArray); // !!! add error handling michael@0: SkOpArray* array = arrayValue.fOperand.fArray; michael@0: SkOperand2 operand; michael@0: SkDEBUGCODE(bool success = ) array->getIndex(index, &operand); michael@0: SkASSERT(success); // !!! add error handling michael@0: SkScriptValue2 resultValue; michael@0: resultValue.fType = array->getType(); michael@0: resultValue.fOperand = operand; michael@0: resultValue.fIsConstant = SkScriptValue2::kVariable; michael@0: fValueStack.push(resultValue); michael@0: } michael@0: break; michael@0: case kIf: { michael@0: if (fAccumulatorType == SkOperand2::kNoType) { michael@0: addTokenValue(fValueStack.top(), kAccumulator); michael@0: fValueStack.pop(); michael@0: } michael@0: SkASSERT(fAccumulatorType != SkOperand2::kString); // !!! add error handling michael@0: addToken(kIfOp); michael@0: Branch branch(op, fOpStack.count(), getTokenOffset()); michael@0: *fBranchStack.push() = branch; michael@0: addTokenInt(0); // placeholder for future branch michael@0: fAccumulatorType = SkOperand2::kNoType; michael@0: } break; michael@0: case kElse: { michael@0: addTokenValue(fValueStack.top(), kAccumulator); michael@0: fValueStack.pop(); michael@0: addToken(kElseOp); michael@0: size_t newOffset = getTokenOffset(); michael@0: addTokenInt(0); // placeholder for future branch michael@0: Branch& branch = fBranchStack.top(); michael@0: resolveBranch(branch); michael@0: branch.fOperator = op; michael@0: branch.fDone = Branch::kIsNotDone; michael@0: SkASSERT(branch.fOpStackDepth == fOpStack.count()); michael@0: branch.fOffset = newOffset; michael@0: fAccumulatorType = SkOperand2::kNoType; michael@0: } break; michael@0: case kLogicalAnd: michael@0: case kLogicalOr: { michael@0: Branch& oldTop = fBranchStack.top(); michael@0: Branch::Primed wasPrime = oldTop.fPrimed; michael@0: Branch::Done wasDone = oldTop.fDone; michael@0: oldTop.fPrimed = Branch::kIsNotPrimed; michael@0: oldTop.fDone = Branch::kIsNotDone; michael@0: if (fAccumulatorType == SkOperand2::kNoType) { michael@0: SkASSERT(fValueStack.top().fType == SkOperand2::kS32); // !!! add error handling, and conversion to int? michael@0: addTokenValue(fValueStack.top(), kAccumulator); michael@0: fValueStack.pop(); michael@0: } else { michael@0: SkASSERT(fAccumulatorType == SkOperand2::kS32); michael@0: } michael@0: // if 'and', write beq goto opcode after end of predicate (after to bool) michael@0: // if 'or', write bne goto to bool michael@0: addToken(op == kLogicalAnd ? kLogicalAndInt : kLogicalOrInt); michael@0: Branch branch(op, fOpStack.count(), getTokenOffset()); michael@0: addTokenInt(0); // placeholder for future branch michael@0: oldTop.fPrimed = wasPrime; michael@0: oldTop.fDone = wasDone; michael@0: *fBranchStack.push() = branch; michael@0: fAccumulatorType = SkOperand2::kNoType; michael@0: } break; michael@0: default: michael@0: SkASSERT(0); michael@0: } michael@0: } michael@0: michael@0: bool SkScriptEngine2::processOp() { michael@0: Op op; michael@0: fOpStack.pop(&op); michael@0: op = (Op) (op & ~kArtificialOp); michael@0: const OperatorAttributes* attributes = &gOpAttributes[op]; michael@0: SkScriptValue2 value1; michael@0: memset(&value1, 0, sizeof(SkScriptValue2)); michael@0: SkScriptValue2 value2; michael@0: fValueStack.pop(&value2); michael@0: value2.fIsWritten = SkScriptValue2::kUnwritten; michael@0: // SkScriptEngine2::SkTypeOp convert1[3]; michael@0: // SkScriptEngine2::SkTypeOp convert2[3]; michael@0: // SkScriptEngine2::SkTypeOp* convert2Ptr = convert2; michael@0: bool constantOperands = value2.fIsConstant == SkScriptValue2::kConstant; michael@0: if (attributes->fLeftType != SkOperand2::kNoType) { michael@0: fValueStack.pop(&value1); michael@0: constantOperands &= value1.fIsConstant == SkScriptValue2::kConstant; michael@0: value1.fIsWritten = SkScriptValue2::kUnwritten; michael@0: if (op == kFlipOps) { michael@0: SkTSwap(value1, value2); michael@0: fOpStack.pop(&op); michael@0: op = (Op) (op & ~kArtificialOp); michael@0: attributes = &gOpAttributes[op]; michael@0: if (constantOperands == false) michael@0: addToken(kFlipOpsOp); michael@0: } michael@0: if (value1.fType == SkOperand2::kObject && (value1.fType & attributes->fLeftType) == 0) { michael@0: value1.fType = getUnboxType(value1.fOperand); michael@0: addToken(kUnboxToken); michael@0: } michael@0: } michael@0: if (value2.fType == SkOperand2::kObject && (value2.fType & attributes->fLeftType) == 0) { michael@0: value1.fType = getUnboxType(value2.fOperand); michael@0: addToken(kUnboxToken2); michael@0: } michael@0: if (attributes->fLeftType != SkOperand2::kNoType) { michael@0: if (value1.fType != value2.fType) { michael@0: if ((attributes->fLeftType & SkOperand2::kString) && attributes->fBias & kTowardsString && michael@0: ((value1.fType | value2.fType) & SkOperand2::kString)) { michael@0: if (value1.fType == SkOperand2::kS32 || value1.fType == SkOperand2::kScalar) { michael@0: addTokenConst(&value1, kAccumulator, SkOperand2::kString, michael@0: value1.fType == SkOperand2::kS32 ? kIntToString : kScalarToString); michael@0: } michael@0: if (value2.fType == SkOperand2::kS32 || value2.fType == SkOperand2::kScalar) { michael@0: addTokenConst(&value2, kOperand, SkOperand2::kString, michael@0: value2.fType == SkOperand2::kS32 ? kIntToString2 : kScalarToString2); michael@0: } michael@0: } else if (attributes->fLeftType & SkOperand2::kScalar && ((value1.fType | value2.fType) & michael@0: SkOperand2::kScalar)) { michael@0: if (value1.fType == SkOperand2::kS32) michael@0: addTokenConst(&value1, kAccumulator, SkOperand2::kScalar, kIntToScalar); michael@0: if (value2.fType == SkOperand2::kS32) michael@0: addTokenConst(&value2, kOperand, SkOperand2::kScalar, kIntToScalar2); michael@0: } michael@0: } michael@0: if ((value1.fType & attributes->fLeftType) == 0 || value1.fType != value2.fType) { michael@0: if (value1.fType == SkOperand2::kString) michael@0: addTokenConst(&value1, kAccumulator, SkOperand2::kScalar, kStringToScalar); michael@0: if (value1.fType == SkOperand2::kScalar && (attributes->fLeftType == SkOperand2::kS32 || michael@0: value2.fType == SkOperand2::kS32)) michael@0: addTokenConst(&value1, kAccumulator, SkOperand2::kS32, kScalarToInt); michael@0: } michael@0: } michael@0: AddTokenRegister rhRegister = attributes->fLeftType != SkOperand2::kNoType ? michael@0: kOperand : kAccumulator; michael@0: if ((value2.fType & attributes->fRightType) == 0 || value1.fType != value2.fType) { michael@0: if (value2.fType == SkOperand2::kString) michael@0: addTokenConst(&value2, rhRegister, SkOperand2::kScalar, kStringToScalar2); michael@0: if (value2.fType == SkOperand2::kScalar && (attributes->fRightType == SkOperand2::kS32 || michael@0: value1.fType == SkOperand2::kS32)) michael@0: addTokenConst(&value2, rhRegister, SkOperand2::kS32, kScalarToInt2); michael@0: } michael@0: TypeOp typeOp = gTokens[op]; michael@0: if (value2.fType == SkOperand2::kScalar) michael@0: typeOp = (TypeOp) (typeOp + 1); michael@0: else if (value2.fType == SkOperand2::kString) michael@0: typeOp = (TypeOp) (typeOp + 2); michael@0: SkDynamicMemoryWStream stream; michael@0: SkOperand2::OpType saveType = SkOperand2::kNoType; michael@0: SkBool saveOperand = false; michael@0: if (constantOperands) { michael@0: fActiveStream = &stream; michael@0: saveType = fAccumulatorType; michael@0: saveOperand = fOperandInUse; michael@0: fAccumulatorType = SkOperand2::kNoType; michael@0: fOperandInUse = false; michael@0: } michael@0: if (attributes->fLeftType != SkOperand2::kNoType) { // two operands michael@0: if (value1.fIsWritten == SkScriptValue2::kUnwritten) michael@0: addTokenValue(value1, kAccumulator); michael@0: } michael@0: if (value2.fIsWritten == SkScriptValue2::kUnwritten) michael@0: addTokenValue(value2, rhRegister); michael@0: addToken(typeOp); michael@0: if (constantOperands) { michael@0: addToken(kEnd); michael@0: SkAutoDataUnref data(fStream.copyToData()); michael@0: #ifdef SK_DEBUG michael@0: decompile(data->bytes(), data->size()); michael@0: #endif michael@0: SkScriptRuntime runtime(fCallBackArray); michael@0: runtime.executeTokens((unsigned char*)data->bytes()); michael@0: runtime.getResult(&value1.fOperand); michael@0: if (attributes->fResultIsBoolean == kResultIsBoolean) michael@0: value1.fType = SkOperand2::kS32; michael@0: else if (attributes->fLeftType == SkOperand2::kNoType) // unary operand michael@0: value1.fType = value2.fType; michael@0: fValueStack.push(value1); michael@0: if (value1.fType == SkOperand2::kString) michael@0: runtime.untrack(value1.fOperand.fString); michael@0: else if (value1.fType == SkOperand2::kArray) michael@0: runtime.untrack(value1.fOperand.fArray); michael@0: fActiveStream = &fStream; michael@0: fAccumulatorType = saveType; michael@0: fOperandInUse = saveOperand; michael@0: return true; michael@0: } michael@0: value2.fIsConstant = SkScriptValue2::kVariable; michael@0: fValueStack.push(value2); michael@0: return true; michael@0: } michael@0: michael@0: void SkScriptEngine2::Branch::resolve(SkDynamicMemoryWStream* stream, size_t off) { michael@0: SkASSERT(fDone == kIsNotDone); michael@0: fPrimed = kIsNotPrimed; michael@0: fDone = kIsDone; michael@0: SkASSERT(off > fOffset + sizeof(size_t)); michael@0: size_t offset = off - fOffset - sizeof(offset); michael@0: stream->write(&offset, fOffset, sizeof(offset)); michael@0: } michael@0: michael@0: void SkScriptEngine2::resolveBranch(SkScriptEngine2::Branch& branch) { michael@0: branch.resolve(fActiveStream, getTokenOffset()); michael@0: } michael@0: michael@0: bool SkScriptEngine2::ConvertTo(SkScriptEngine2* engine, SkOperand2::OpType toType, SkScriptValue2* value ) { michael@0: SkASSERT(value); michael@0: SkOperand2::OpType type = value->fType; michael@0: if (type == toType) michael@0: return true; michael@0: SkOperand2& operand = value->fOperand; michael@0: bool success = true; michael@0: switch (toType) { michael@0: case SkOperand2::kS32: michael@0: if (type == SkOperand2::kScalar) michael@0: operand.fS32 = SkScalarFloorToInt(operand.fScalar); michael@0: else { michael@0: SkASSERT(type == SkOperand2::kString); michael@0: success = SkParse::FindS32(operand.fString->c_str(), &operand.fS32) != NULL; michael@0: } michael@0: break; michael@0: case SkOperand2::kScalar: michael@0: if (type == SkOperand2::kS32) michael@0: operand.fScalar = IntToScalar(operand.fS32); michael@0: else { michael@0: SkASSERT(type == SkOperand2::kString); michael@0: success = SkParse::FindScalar(operand.fString->c_str(), &operand.fScalar) != NULL; michael@0: } michael@0: break; michael@0: case SkOperand2::kString: { michael@0: SkString* strPtr = new SkString(); michael@0: SkASSERT(engine); michael@0: engine->track(strPtr); michael@0: if (type == SkOperand2::kS32) michael@0: strPtr->appendS32(operand.fS32); michael@0: else { michael@0: SkASSERT(type == SkOperand2::kScalar); michael@0: strPtr->appendScalar(operand.fScalar); michael@0: } michael@0: operand.fString = strPtr; michael@0: } break; michael@0: case SkOperand2::kArray: { michael@0: SkOpArray* array = new SkOpArray(type); michael@0: *array->append() = operand; michael@0: engine->track(array); michael@0: operand.fArray = array; michael@0: } break; michael@0: default: michael@0: SkASSERT(0); michael@0: } michael@0: value->fType = toType; michael@0: return success; michael@0: } michael@0: michael@0: SkScalar SkScriptEngine2::IntToScalar(int32_t s32) { michael@0: SkScalar scalar; michael@0: if (s32 == (int32_t) SK_NaN32) michael@0: scalar = SK_ScalarNaN; michael@0: else if (SkAbs32(s32) == SK_MaxS32) michael@0: scalar = SkSign32(s32) * SK_ScalarMax; michael@0: else michael@0: scalar = SkIntToScalar(s32); michael@0: return scalar; michael@0: } michael@0: michael@0: bool SkScriptEngine2::ValueToString(const SkScriptValue2& value, SkString* string) { michael@0: switch (value.fType) { michael@0: case SkOperand2::kS32: michael@0: string->reset(); michael@0: string->appendS32(value.fOperand.fS32); michael@0: break; michael@0: case SkOperand2::kScalar: michael@0: string->reset(); michael@0: string->appendScalar(value.fOperand.fScalar); michael@0: break; michael@0: case SkOperand2::kString: michael@0: string->set(*value.fOperand.fString); michael@0: break; michael@0: default: michael@0: SkASSERT(0); michael@0: return false; michael@0: } michael@0: return true; // no error michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: #if defined(SK_SUPPORT_UNITTEST) michael@0: michael@0: #define testInt(expression) { #expression, SkOperand2::kS32, expression, 0, NULL } michael@0: #define testScalar(expression) { #expression, SkOperand2::kScalar, 0, (float) (expression), NULL } michael@0: #define testRemainder(exp1, exp2) { #exp1 "%" #exp2, SkOperand2::kScalar, 0, fmodf((float) exp1, (float) exp2), NULL } michael@0: #define testTrue(expression) { #expression, SkOperand2::kS32, 1, 0, NULL } michael@0: #define testFalse(expression) { #expression, SkOperand2::kS32, 0, 0, NULL } michael@0: michael@0: static const SkScriptNAnswer2 scriptTests[] = { michael@0: testInt(1||(0&&3)), michael@0: testScalar(- -5.5- -1.5), michael@0: testScalar(1.0+5), michael@0: testInt((6+7)*8), michael@0: testInt(3*(4+5)), michael@0: testScalar(1.0+2.0), michael@0: testScalar(3.0-1.0), michael@0: testScalar(6-1.0), michael@0: testScalar(2.5*6.), michael@0: testScalar(0.5*4), michael@0: testScalar(4.5/.5), michael@0: testScalar(9.5/19), michael@0: testRemainder(9.5, 0.5), michael@0: testRemainder(9.,2), michael@0: testRemainder(9,2.5), michael@0: testRemainder(-9,2.5), michael@0: testTrue(-9==-9.0), michael@0: testTrue(-9.==-4.0-5), michael@0: testTrue(-9.*1==-4-5), michael@0: testFalse(-9!=-9.0), michael@0: testFalse(-9.!=-4.0-5), michael@0: testFalse(-9.*1!=-4-5), michael@0: testInt(0x123), michael@0: testInt(0XABC), michael@0: testInt(0xdeadBEEF), michael@0: { "'123'+\"456\"", SkOperand2::kString, 0, 0, "123456" }, michael@0: { "123+\"456\"", SkOperand2::kString, 0, 0, "123456" }, michael@0: { "'123'+456", SkOperand2::kString, 0, 0, "123456" }, michael@0: { "'123'|\"456\"", SkOperand2::kS32, 123|456, 0, NULL }, michael@0: { "123|\"456\"", SkOperand2::kS32, 123|456, 0, NULL }, michael@0: { "'123'|456", SkOperand2::kS32, 123|456, 0, NULL }, michael@0: { "'2'<11", SkOperand2::kS32, 1, 0, NULL }, michael@0: { "2<'11'", SkOperand2::kS32, 1, 0, NULL }, michael@0: { "'2'<'11'", SkOperand2::kS32, 0, 0, NULL }, michael@0: testInt(123), michael@0: testInt(-345), michael@0: testInt(+678), michael@0: testInt(1+2+3), michael@0: testInt(3*4+5), michael@0: testInt(6+7*8), michael@0: testInt(-1-2-8/4), michael@0: testInt(-9%4), michael@0: testInt(9%-4), michael@0: testInt(-9%-4), michael@0: testInt(123|978), michael@0: testInt(123&978), michael@0: testInt(123^978), michael@0: testInt(2<<4), michael@0: testInt(99>>3), michael@0: testInt(~55), michael@0: testInt(~~55), michael@0: testInt(!55), michael@0: testInt(!!55), michael@0: // both int michael@0: testInt(2<2), michael@0: testInt(2<11), michael@0: testInt(20<11), michael@0: testInt(2<=2), michael@0: testInt(2<=11), michael@0: testInt(20<=11), michael@0: testInt(2>2), michael@0: testInt(2>11), michael@0: testInt(20>11), michael@0: testInt(2>=2), michael@0: testInt(2>=11), michael@0: testInt(20>=11), michael@0: testInt(2==2), michael@0: testInt(2==11), michael@0: testInt(20==11), michael@0: testInt(2!=2), michael@0: testInt(2!=11), michael@0: testInt(20!=11), michael@0: // left int, right scalar michael@0: testInt(2<2.), michael@0: testInt(2<11.), michael@0: testInt(20<11.), michael@0: testInt(2<=2.), michael@0: testInt(2<=11.), michael@0: testInt(20<=11.), michael@0: testInt(2>2.), michael@0: testInt(2>11.), michael@0: testInt(20>11.), michael@0: testInt(2>=2.), michael@0: testInt(2>=11.), michael@0: testInt(20>=11.), michael@0: testInt(2==2.), michael@0: testInt(2==11.), michael@0: testInt(20==11.), michael@0: testInt(2!=2.), michael@0: testInt(2!=11.), michael@0: testInt(20!=11.), michael@0: // left scalar, right int michael@0: testInt(2.<2), michael@0: testInt(2.<11), michael@0: testInt(20.<11), michael@0: testInt(2.<=2), michael@0: testInt(2.<=11), michael@0: testInt(20.<=11), michael@0: testInt(2.>2), michael@0: testInt(2.>11), michael@0: testInt(20.>11), michael@0: testInt(2.>=2), michael@0: testInt(2.>=11), michael@0: testInt(20.>=11), michael@0: testInt(2.==2), michael@0: testInt(2.==11), michael@0: testInt(20.==11), michael@0: testInt(2.!=2), michael@0: testInt(2.!=11), michael@0: testInt(20.!=11), michael@0: // both scalar michael@0: testInt(2.<11.), michael@0: testInt(20.<11.), michael@0: testInt(2.<=2.), michael@0: testInt(2.<=11.), michael@0: testInt(20.<=11.), michael@0: testInt(2.>2.), michael@0: testInt(2.>11.), michael@0: testInt(20.>11.), michael@0: testInt(2.>=2.), michael@0: testInt(2.>=11.), michael@0: testInt(20.>=11.), michael@0: testInt(2.==2.), michael@0: testInt(2.==11.), michael@0: testInt(20.==11.), michael@0: testInt(2.!=2.), michael@0: testInt(2.!=11.), michael@0: testInt(20.!=11.), michael@0: // int, string (string is int) michael@0: testFalse(2<'2'), michael@0: testTrue(2<'11'), michael@0: testFalse(20<'11'), michael@0: testTrue(2<='2'), michael@0: testTrue(2<='11'), michael@0: testFalse(20<='11'), michael@0: testFalse(2>'2'), michael@0: testFalse(2>'11'), michael@0: testTrue(20>'11'), michael@0: testTrue(2>='2'), michael@0: testFalse(2>='11'), michael@0: testTrue(20>='11'), michael@0: testTrue(2=='2'), michael@0: testFalse(2=='11'), michael@0: testFalse(2!='2'), michael@0: testTrue(2!='11'), michael@0: // int, string (string is scalar) michael@0: testFalse(2<'2.'), michael@0: testTrue(2<'11.'), michael@0: testFalse(20<'11.'), michael@0: testTrue(2=='2.'), michael@0: testFalse(2=='11.'), michael@0: // scalar, string michael@0: testFalse(2.<'2.'), michael@0: testTrue(2.<'11.'), michael@0: testFalse(20.<'11.'), michael@0: testTrue(2.=='2.'), michael@0: testFalse(2.=='11.'), michael@0: // string, int michael@0: testFalse('2'<2), michael@0: testTrue('2'<11), michael@0: testFalse('20'<11), michael@0: testTrue('2'==2), michael@0: testFalse('2'==11), michael@0: // string, scalar michael@0: testFalse('2'<2.), michael@0: testTrue('2'<11.), michael@0: testFalse('20'<11.), michael@0: testTrue('2'==2.), michael@0: testFalse('2'==11.), michael@0: // string, string michael@0: testFalse('2'<'2'), michael@0: testFalse('2'<'11'), michael@0: testFalse('20'<'11'), michael@0: testTrue('2'=='2'), michael@0: testFalse('2'=='11'), michael@0: // logic michael@0: testInt(1?2:3), michael@0: testInt(0?2:3), michael@0: testInt((1&&2)||3), michael@0: testInt((1&&0)||3), michael@0: testInt((1&&0)||0), michael@0: testInt(1||(0&&3)), michael@0: testInt(0||(0&&3)), michael@0: testInt(0||(1&&3)), michael@0: testInt(0&&1?2:3) michael@0: , { "123.5", SkOperand2::kScalar, 0, SkIntToScalar(123) + SK_Scalar1/2, NULL } michael@0: }; michael@0: michael@0: #define SkScriptNAnswer_testCount SK_ARRAY_COUNT(scriptTests) michael@0: #endif // SK_SUPPORT_UNITTEST michael@0: michael@0: void SkScriptEngine2::UnitTest() { michael@0: #if defined(SK_SUPPORT_UNITTEST) michael@0: ValidateDecompileTable(); michael@0: for (size_t index = 0; index < SkScriptNAnswer_testCount; index++) { michael@0: SkScriptEngine2 engine(scriptTests[index].fType); michael@0: SkScriptValue2 value; michael@0: const char* script = scriptTests[index].fScript; michael@0: const char* scriptPtr = script; michael@0: SkASSERT(engine.evaluateScript(&scriptPtr, &value) == true); michael@0: SkASSERT(value.fType == scriptTests[index].fType); michael@0: SkScalar error; michael@0: switch (value.fType) { michael@0: case SkOperand2::kS32: michael@0: if (value.fOperand.fS32 != scriptTests[index].fIntAnswer) michael@0: SkDEBUGF(("script '%s' == value %d != expected answer %d\n", script, value.fOperand.fS32, scriptTests[index].fIntAnswer)); michael@0: SkASSERT(value.fOperand.fS32 == scriptTests[index].fIntAnswer); michael@0: break; michael@0: case SkOperand2::kScalar: michael@0: error = SkScalarAbs(value.fOperand.fScalar - scriptTests[index].fScalarAnswer); michael@0: if (error >= SK_Scalar1 / 10000) michael@0: SkDEBUGF(("script '%s' == value %g != expected answer %g\n", script, value.fOperand.fScalar / (1.0f * SK_Scalar1), scriptTests[index].fScalarAnswer / (1.0f * SK_Scalar1))); michael@0: SkASSERT(error < SK_Scalar1 / 10000); michael@0: break; michael@0: case SkOperand2::kString: michael@0: SkASSERT(value.fOperand.fString->equals(scriptTests[index].fStringAnswer)); michael@0: break; michael@0: default: michael@0: SkASSERT(0); michael@0: } michael@0: } michael@0: #endif // SK_SUPPORT_UNITTEST michael@0: } michael@0: #endif // SK_DEBUG