def monteLessThan(left, right): """ try: return left.op__cmp(right).belowZero() catch _: return right.op__cmp(left).aboveZero() """ try: comparison = left.call(u"op__cmp", [right]) b = comparison.call(u"belowZero", []) return unwrapBool(b) except UserException: comparison = right.call(u"op__cmp", [left]) b = comparison.call(u"aboveZero", []) return unwrapBool(b)
def ask(self, auditor): if not self.active: self.log(u"ask/1: Stolen audition: %s" % pemci, tags=["serious"]) raise userError(u"Audition is out of scope") cached = False self.askedLog.append(auditor) if auditor in self.cache: answer, asked, guards = self.cache[auditor] self.log(u"ask/1: %s: %s (cached)" % (auditor.toString(), boolStr(answer))) for name, value in guards: # We remember what the binding guards for the previous # invocation were. if self.guards[name] != value: # If any of them have changed, we need to re-audit. self.log(u"ask/1: %s: Invalidating" % name) break else: # XXX stopgap: Ignore negative answers in the cache. cached = answer if cached: for a in asked: # We remember the other auditors invoked during this # audition. Let's re-ask them since not all of them may have # cacheable results. self.log(u"ask/1: Reasking %s" % auditor.toString()) answer = self.ask(a) return answer else: # This seems a little convoluted, but the idea is that the logs # are written to during the course of the audition, and then # copied out to the cache afterwards. prevlogs = self.askedLog, self.guardLog self.askedLog = [] self.guardLog = [] try: result = unwrapBool(auditor.call(u"audit", [self])) if self.guardLog is None: self.log(u"ask/1: %s: %s (uncacheable)" % (auditor.toString(), boolStr(result))) else: self.log(u"ask/1: %s: %s" % (auditor.toString(), boolStr(result))) self.cache[auditor] = (result, self.askedLog[:], self.guardLog[:]) return result finally: self.askedLog, self.guardLog = prevlogs self.log(u"ask/1: %s: failure" % auditor.toString()) return False
def recv(self, atom, args): if atom is RESOLVE_1: return wrapBool(self.resolve(args[0])) if atom is RESOLVE_2: return wrapBool(self.resolve(args[0], unwrapBool(args[1]))) if atom is SMASH_1: return wrapBool(self.smash(args[0])) raise Refused(self, atom, args)
def recv(self, atom, args): if atom is COERCE_2: for g in self.subguards: with Ejector() as ej: try: return g.call(u"coerce", [args[0], ej]) except Ejecting as e: if e.ejector is ej: continue throw(args[1], StrObject(u"No subguards matched")) if atom is SUPERSETOF_1: for g in self.subguards: if not unwrapBool(g.call(u"supersetOf", [args[0]])): return wrapBool(False) return wrapBool(True) if atom is _UNCALL_0: from typhon.objects.collections.maps import EMPTY_MAP return ConstList([anyGuard, StrObject(u"get"), ConstList(self.subguards), EMPTY_MAP]) raise Refused(self, atom, args)
def recv(self, atom, args): if atom is COERCE_2: for g in self.subguards: with Ejector() as ej: try: return g.call(u"coerce", [args[0], ej]) except Ejecting as e: if e.ejector is ej: continue throw(args[1], StrObject(u"No subguards matched")) if atom is SUPERSETOF_1: for g in self.subguards: if not unwrapBool(g.call(u"supersetOf", [args[0]])): return wrapBool(False) return wrapBool(True) if atom is _UNCALL_0: from typhon.objects.collections.maps import EMPTY_MAP return ConstList([ anyGuard, StrObject(u"get"), ConstList(self.subguards), EMPTY_MAP ]) raise Refused(self, atom, args)
def runInstruction(self, instruction, pc): index = self.code.index(pc) # jit_debug(self.code.disAt(pc)) if instruction.asInt == ops.DUP.asInt: self.push(self.peek()) return pc + 1 elif instruction.asInt == ops.ROT.asInt: z = self.pop() y = self.pop() x = self.pop() self.push(y) self.push(z) self.push(x) return pc + 1 elif instruction.asInt == ops.POP.asInt: self.pop() return pc + 1 elif instruction.asInt == ops.SWAP.asInt: y = self.pop() x = self.pop() self.push(y) self.push(x) return pc + 1 elif instruction.asInt == ops.ASSIGN_GLOBAL.asInt: value = self.pop() self.env.putValueGlobal(index, value) return pc + 1 elif instruction.asInt == ops.ASSIGN_FRAME.asInt: value = self.pop() self.env.putValueFrame(index, value) return pc + 1 elif instruction.asInt == ops.ASSIGN_LOCAL.asInt: value = self.pop() self.env.putValueLocal(index, value) return pc + 1 elif instruction.asInt == ops.BIND.asInt: binding = self.pop() self.env.createBindingLocal(index, binding) return pc + 1 elif instruction.asInt == ops.BINDFINALSLOT.asInt: guard = self.pop() ej = self.pop() specimen = self.pop() val = guard.call(u"coerce", [specimen, ej]) self.env.createBindingLocal(index, finalBinding(val, guard)) return pc + 1 elif instruction.asInt == ops.BINDVARSLOT.asInt: guard = self.pop() ej = self.pop() specimen = self.pop() val = guard.call(u"coerce", [specimen, ej]) self.env.createBindingLocal(index, varBinding(val, guard)) return pc + 1 elif instruction.asInt == ops.SLOT_GLOBAL.asInt: self.push(self.env.getSlotGlobal(index)) return pc + 1 elif instruction.asInt == ops.SLOT_FRAME.asInt: self.push(self.env.getSlotFrame(index)) return pc + 1 elif instruction.asInt == ops.SLOT_LOCAL.asInt: self.push(self.env.getSlotLocal(index)) return pc + 1 elif instruction.asInt == ops.NOUN_GLOBAL.asInt: self.push(self.env.getValueGlobal(index)) return pc + 1 elif instruction.asInt == ops.NOUN_FRAME.asInt: self.push(self.env.getValueFrame(index)) return pc + 1 elif instruction.asInt == ops.NOUN_LOCAL.asInt: self.push(self.env.getValueLocal(index)) return pc + 1 elif instruction.asInt == ops.BINDING_GLOBAL.asInt: self.push(self.env.getBindingGlobal(index)) return pc + 1 elif instruction.asInt == ops.BINDING_FRAME.asInt: self.push(self.env.getBindingFrame(index)) return pc + 1 elif instruction.asInt == ops.BINDING_LOCAL.asInt: self.push(self.env.getBindingLocal(index)) return pc + 1 elif instruction.asInt == ops.LIST_PATT.asInt: self.listPattern(index) return pc + 1 elif instruction.asInt == ops.LITERAL.asInt: self.push(self.code.literal(index)) return pc + 1 elif instruction.asInt == ops.BINDOBJECT.asInt: self.bindObject(index) return pc + 1 elif instruction.asInt == ops.EJECTOR.asInt: # Look carefully at the order of operations. The handler captures # the depth of the stack, so it's important to create it *before* # pushing the ejector onto the stack. Otherwise, the handler # thinks that the stack started off with an extra level of depth. ej = Ejector() handler = Eject(self, ej, index) self.push(ej) self.env.pushHandler(handler) return pc + 1 elif instruction.asInt == ops.TRY.asInt: self.env.pushHandler(Catch(self, index)) return pc + 1 elif instruction.asInt == ops.UNWIND.asInt: self.env.pushHandler(Unwind(self, index)) return pc + 1 elif instruction.asInt == ops.END_HANDLER.asInt: handler = self.env.popHandler() return handler.drop(self, pc, index) elif instruction.asInt == ops.BRANCH.asInt: cond = unwrapBool(self.pop()) if cond: return pc + 1 else: return index elif instruction.asInt == ops.CALL.asInt: self.call(index, withMap=False) return pc + 1 elif instruction.asInt == ops.CALL_MAP.asInt: self.call(index, withMap=True) return pc + 1 elif instruction.asInt == ops.BUILD_MAP.asInt: self.buildMap(index) return pc + 1 elif instruction.asInt == ops.NAMEDARG_EXTRACT.asInt: k = self.pop() d = unwrapMap(self.pop()) if k not in d: raise userError(u"Named arg %s missing in call" % (k.toString(), )) self.push(d[k]) return pc + 1 elif instruction.asInt == ops.NAMEDARG_EXTRACT_OPTIONAL.asInt: k = self.pop() d = unwrapMap(self.pop()) if k not in d: self.push(NullObject) return pc + 1 self.push(d[k]) return index elif instruction.asInt == ops.JUMP.asInt: return index else: raise RuntimeError("Unknown instruction %s" % instruction.repr.encode("utf-8"))
def _makeSourceSpan(uri, isOneToOne, startLine, startCol, endLine, endCol): return SourceSpan(uri, unwrapBool(isOneToOne), unwrapInt(startLine), unwrapInt(startCol), unwrapInt(endLine), unwrapInt(endCol))
def testUnwrapBoolPromise(self): with scopedVat(testingVat()): p = makeNear(wrapBool(False)) self.assertFalse(unwrapBool(p))
def supersetOf(self, s): if isinstance(s, FinalSlot): return unwrapBool(self.valueGuard.call(u"supersetOf", [s._guard])) return False
def unwrap(self, value): return unwrapBool(value)
def runInstruction(self, instruction, pc): index = self.code.index(pc) # jit_debug(self.code.disAt(pc)) if instruction.asInt == ops.DUP.asInt: self.push(self.peek()) return pc + 1 elif instruction.asInt == ops.ROT.asInt: z = self.pop() y = self.pop() x = self.pop() self.push(y) self.push(z) self.push(x) return pc + 1 elif instruction.asInt == ops.POP.asInt: self.pop() return pc + 1 elif instruction.asInt == ops.SWAP.asInt: y = self.pop() x = self.pop() self.push(y) self.push(x) return pc + 1 elif instruction.asInt == ops.ASSIGN_GLOBAL.asInt: value = self.pop() self.env.putValueGlobal(index, value) return pc + 1 elif instruction.asInt == ops.ASSIGN_FRAME.asInt: value = self.pop() self.env.putValueFrame(index, value) return pc + 1 elif instruction.asInt == ops.ASSIGN_LOCAL.asInt: value = self.pop() self.env.putValueLocal(index, value) return pc + 1 elif instruction.asInt == ops.BIND.asInt: binding = self.pop() self.env.createBindingLocal(index, binding) return pc + 1 elif instruction.asInt == ops.BINDFINALSLOT.asInt: guard = self.pop() ej = self.pop() specimen = self.pop() val = guard.call(u"coerce", [specimen, ej]) self.env.createBindingLocal(index, finalBinding(val, guard)) return pc + 1 elif instruction.asInt == ops.BINDVARSLOT.asInt: guard = self.pop() ej = self.pop() specimen = self.pop() val = guard.call(u"coerce", [specimen, ej]) self.env.createBindingLocal(index, varBinding(val, guard)) return pc + 1 elif instruction.asInt == ops.SLOT_GLOBAL.asInt: self.push(self.env.getSlotGlobal(index)) return pc + 1 elif instruction.asInt == ops.SLOT_FRAME.asInt: self.push(self.env.getSlotFrame(index)) return pc + 1 elif instruction.asInt == ops.SLOT_LOCAL.asInt: self.push(self.env.getSlotLocal(index)) return pc + 1 elif instruction.asInt == ops.NOUN_GLOBAL.asInt: self.push(self.env.getValueGlobal(index)) return pc + 1 elif instruction.asInt == ops.NOUN_FRAME.asInt: self.push(self.env.getValueFrame(index)) return pc + 1 elif instruction.asInt == ops.NOUN_LOCAL.asInt: self.push(self.env.getValueLocal(index)) return pc + 1 elif instruction.asInt == ops.BINDING_GLOBAL.asInt: self.push(self.env.getBindingGlobal(index)) return pc + 1 elif instruction.asInt == ops.BINDING_FRAME.asInt: self.push(self.env.getBindingFrame(index)) return pc + 1 elif instruction.asInt == ops.BINDING_LOCAL.asInt: self.push(self.env.getBindingLocal(index)) return pc + 1 elif instruction.asInt == ops.LIST_PATT.asInt: self.listPattern(index) return pc + 1 elif instruction.asInt == ops.LITERAL.asInt: self.push(self.code.literal(index)) return pc + 1 elif instruction.asInt == ops.BINDOBJECT.asInt: self.bindObject(index) return pc + 1 elif instruction.asInt == ops.EJECTOR.asInt: # Look carefully at the order of operations. The handler captures # the depth of the stack, so it's important to create it *before* # pushing the ejector onto the stack. Otherwise, the handler # thinks that the stack started off with an extra level of depth. ej = Ejector() handler = Eject(self, ej, index) self.push(ej) self.env.pushHandler(handler) return pc + 1 elif instruction.asInt == ops.TRY.asInt: self.env.pushHandler(Catch(self, index)) return pc + 1 elif instruction.asInt == ops.UNWIND.asInt: self.env.pushHandler(Unwind(self, index)) return pc + 1 elif instruction.asInt == ops.END_HANDLER.asInt: handler = self.env.popHandler() return handler.drop(self, pc, index) elif instruction.asInt == ops.BRANCH.asInt: cond = unwrapBool(self.pop()) if cond: return pc + 1 else: return index elif instruction.asInt == ops.CALL.asInt: self.call(index, withMap=False) return pc + 1 elif instruction.asInt == ops.CALL_MAP.asInt: self.call(index, withMap=True) return pc + 1 elif instruction.asInt == ops.BUILD_MAP.asInt: self.buildMap(index) return pc + 1 elif instruction.asInt == ops.NAMEDARG_EXTRACT.asInt: k = self.pop() d = unwrapMap(self.pop()) if k not in d: raise userError(u"Named arg %s missing in call" % (k.toString(),)) self.push(d[k]) return pc + 1 elif instruction.asInt == ops.NAMEDARG_EXTRACT_OPTIONAL.asInt: k = self.pop() d = unwrapMap(self.pop()) if k not in d: self.push(NullObject) return pc + 1 self.push(d[k]) return index elif instruction.asInt == ops.JUMP.asInt: return index else: raise RuntimeError("Unknown instruction %s" % instruction.repr.encode("utf-8"))
def supersetOf(self, guard): for g in self.subguards: if not unwrapBool(g.call(u"supersetOf", [guard])): return False return True