def createWith(self, obj, body, lineno): self.setRequiresActivation() result = Node(Token.BLOCK, lineno) result.addChildToBack(Node(Token.ENTERWITH, obj)) bodyNode = Node(Token.WITH, body, lineno) result.addChildrenToBack(bodyNode) result.addChildToBack(Node(Token.LEAVEWITH)) return result
def createObjectLiteral(self, elems): size = len(elems) / 2 ob = Node(Token.OBJECTLIT) if (size == 0): properties = ScriptRuntime.emptyArgs else: properties = [object() for __idx0 in range(size)] ## for-while i = 0 while (i != size): properties[i] = elems[2 * i] value = elems[2 * i + 1] ob.addChildToBack(value) i += 1 ob.putProp(Node.OBJECT_IDS_PROP, properties) return ob
def createArrayLiteral(self, elems, skipCount): length = len(elems) skipIndexes = None if (skipCount != 0): skipIndexes = [int() for __idx0 in range(skipCount)] array = Node(Token.ARRAYLIT) ## for-while i = 0 j = 0 while (i != length): elem = elems[i] if elem is not None: array.addChildToBack(elem) else: skipIndexes[j] = i j += 1 i += 1 if (skipCount != 0): array.putProp(Node.SKIP_INDEXES_PROP, skipIndexes) return array
def createForIn(self, loop, lhs, obj, body, isForEach): type = lhs.getType() lvalue = None#Node() if (type == Token.VAR): lastChild = lhs.getLastChild() if (lhs.getFirstChild() != lastChild): self.parser.reportError("msg.mult.index") lvalue = Node.newString(Token.NAME, lastChild.getString()) else: lvalue = self.makeReference(lhs) if lvalue is None: self.parser.reportError("msg.bad.for.in.lhs") return obj localBlock = Node(Token.LOCAL_BLOCK) initType = Token.ENUM_INIT_VALUES if isForEach else Token.ENUM_INIT_KEYS init = Node(initType, obj) init.putProp(Node.LOCAL_BLOCK_PROP, localBlock) cond = Node(Token.ENUM_NEXT) cond.putProp(Node.LOCAL_BLOCK_PROP, localBlock) id = Node(Token.ENUM_ID) id.putProp(Node.LOCAL_BLOCK_PROP, localBlock) newBody = Node(Token.BLOCK) assign = self.simpleAssignment(lvalue, id) newBody.addChildToBack(Node(Token.EXPR_VOID, assign)) newBody.addChildToBack(body) loop = self.createWhile(loop, cond, newBody) loop.addChildToFront(init) if (type == Token.VAR): loop.addChildToFront(lhs) localBlock.addChildToBack(loop) return localBlock
def createIf(self, cond, ifTrue, ifFalse, lineno): condStatus = self.isAlwaysDefinedBoolean(cond) if (condStatus == self.ALWAYS_TRUE_BOOLEAN): return ifTrue else: if (condStatus == self.ALWAYS_FALSE_BOOLEAN): if ifFalse is not None: return ifFalse return Node(Token.BLOCK, lineno) result = Node(Token.BLOCK, lineno) ifNotTarget = Node.newTarget() IFNE = Jump(Token.IFNE, cond) IFNE.target = ifNotTarget result.addChildToBack(IFNE) result.addChildrenToBack(ifTrue) if ifFalse is not None: endTarget = Node.newTarget() result.addChildToBack(self.makeJump(Token.GOTO, endTarget)) result.addChildToBack(ifNotTarget) result.addChildrenToBack(ifFalse) result.addChildToBack(endTarget) else: result.addChildToBack(ifNotTarget) return result
def createTryCatchFinally(self, tryBlock, catchBlocks, finallyBlock, lineno): hasFinally = finallyBlock is not None and ( (finallyBlock.getType() != Token.BLOCK) or finallyBlock.hasChildren()) if (tryBlock.getType() == Token.BLOCK) and not tryBlock.hasChildren() and not hasFinally: return tryBlock hasCatch = catchBlocks.hasChildren() if not hasFinally and not hasCatch: return tryBlock handlerBlock = Node(Token.LOCAL_BLOCK) pn = Jump(Token.TRY, tryBlock, lineno) pn.putProp(Node.LOCAL_BLOCK_PROP, handlerBlock) if hasCatch: endCatch = Node.newTarget() pn.addChildToBack(self.makeJump(Token.GOTO, endCatch)) catchTarget = Node.newTarget() pn.target = catchTarget pn.addChildToBack(catchTarget) catchScopeBlock = Node(Token.LOCAL_BLOCK) cb = catchBlocks.getFirstChild() hasDefault = False scopeIndex = 0 while cb is not None: catchLineNo = cb.getLineno() name = cb.getFirstChild() cond = name.getNext() catchStatement = cond.getNext() cb.removeChild(name) cb.removeChild(cond) cb.removeChild(catchStatement) catchStatement.addChildToBack(Node(Token.LEAVEWITH)) catchStatement.addChildToBack(self.makeJump(Token.GOTO, endCatch)) condStmt = None#Node() if (cond.getType() == Token.EMPTY): condStmt = catchStatement hasDefault = True else: condStmt = self.createIf(cond, catchStatement, None, catchLineNo) catchScope = Node(Token.CATCH_SCOPE, name, self.createUseLocal(handlerBlock)) catchScope.putProp(Node.LOCAL_BLOCK_PROP, catchScopeBlock) catchScope.putIntProp(Node.CATCH_SCOPE_PROP, scopeIndex) catchScopeBlock.addChildToBack(catchScope) catchScopeBlock.addChildToBack(self.createWith(self.createUseLocal(catchScopeBlock), condStmt, catchLineNo)) cb = cb.getNext() scopeIndex += 1 pn.addChildToBack(catchScopeBlock) if not hasDefault: rethrow = Node(Token.RETHROW) rethrow.putProp(Node.LOCAL_BLOCK_PROP, handlerBlock) pn.addChildToBack(rethrow) pn.addChildToBack(endCatch) if hasFinally: finallyTarget = Node.newTarget() pn.setFinally(finallyTarget) pn.addChildToBack(self.makeJump(Token.JSR, finallyTarget)) finallyEnd = Node.newTarget() pn.addChildToBack(self.makeJump(Token.GOTO, finallyEnd)) pn.addChildToBack(finallyTarget) fBlock = Node(Token.FINALLY, finallyBlock) fBlock.putProp(Node.LOCAL_BLOCK_PROP, handlerBlock) pn.addChildToBack(fBlock) pn.addChildToBack(finallyEnd) handlerBlock.addChildToBack(pn) return handlerBlock