def loop(iterable, consumer): """ Perform an iterative loop. """ # If the consumer is *not* a ScriptObject, then damn them to the slow # path. In order for the consumer to not be ScriptObject, though, the # compiler and optimizer must have decided that an object could be # directly passed to _loop(), which is currently impossible to do without # manual effort. It's really not a common pathway at all. if not isinstance(consumer, ScriptObject): return slowLoop(iterable, consumer) # Rarer path: If the consumer doesn't actually have RUN_2, then they're # not going to be JIT'd. Again, the compiler and optimizer won't ever do # this to us; it has to be intentional. code = consumer.codeScript.strategy.lookupMethod(RUN_2) if code is None: return slowLoop(iterable, consumer) iterator = iterable.call(u"_makeIterator", []) ej = Ejector() try: while True: # JIT merge point. loopDriver.jit_merge_point(code=code, consumer=consumer, ejector=ej, iterator=iterator) globals = promote(consumer.globals) if isinstance(consumer, BusyObject): machine = SmallCaps(code, consumer.closure, globals) else: machine = SmallCaps(code, None, globals) values = unwrapList(iterator.call(u"next", [ej])) # Push the arguments onto the stack, backwards. values.reverse() for arg in values: machine.push(arg) machine.push(NullObject) machine.push(EMPTY_MAP) machine.run() except Ejecting as e: if e.ejector is not ej: raise finally: ej.disable() return NullObject
def evaluateWithTraces(code, scope): try: machine = SmallCaps.withDictScope(code, scope) machine.run() return machine.pop() except UserException as ue: debug_print("Caught exception:", ue.formatError()) return None
def evaluateRaise(codes, scope): """ Like evaluateTerms, but does not catch exceptions. """ env = None result = NullObject for code in codes: machine = SmallCaps.withDictScope(code, scope) machine.run() result = machine.pop() env = machine.env return result, env
def runMatcher(self, code, message, ej): machine = SmallCaps(code, self.closure, self.globals) machine.push(message) machine.push(ej) machine.run() return machine.pop()
def runMethod(self, method, args, namedArgs): machine = SmallCaps(method, self.closure, self.globals) # print "--- Running", self.displayName, atom, args # Push the arguments onto the stack, backwards. machine.push(namedArgs) for arg in reversed(args): machine.push(arg) machine.push(NullObject) machine.push(namedArgs) machine.run() return machine.pop()
def runMatcher(self, code, message, ej): machine = SmallCaps(code, None, promote(self.globals)) machine.push(message) machine.push(ej) machine.run() return machine.pop()