def unwrapInt(o): from typhon.objects.refs import resolution i = resolution(o) if isinstance(i, IntObject): return i.getInt() if isinstance(i, BigInt): try: return i.bi.toint() except OverflowError: raise WrongType(u"Integer was too large") raise WrongType(u"Not an integer!")
def call(self, target, verb, args, namedArgs): """ Pass a message to an object. """ if not isinstance(namedArgs, ConstMap): raise WrongType(u"namedArgs must be a ConstMap") return target.call(verb, args, namedArgs)
def promoteToBigInt(o): from typhon.objects.refs import resolution i = resolution(o) if isinstance(i, BigInt): return i.bi if isinstance(i, IntObject): return rbigint.fromint(i.getInt()) raise WrongType(u"Not promotable to big integer!")
def unwrapMap(o): from typhon.objects.refs import resolution m = resolution(o) if isinstance(m, ConstMap): return m.objectMap if isinstance(m, FlexMap): return m.objectMap raise WrongType(u"Not a map!")
def unwrapBool(o): from typhon.objects.refs import resolution b = resolution(o) if b is TrueObject: return True if b is FalseObject: return False raise WrongType(u"Not a boolean!")
def unwrapSet(o): from typhon.objects.refs import resolution m = resolution(o) if isinstance(m, ConstSet): return m.objectSet if isinstance(m, FlexSet): return m.objectSet raise WrongType(u"Not a set!")
def promoteToDouble(o): from typhon.objects.refs import resolution n = resolution(o) if isinstance(n, IntObject): return float(n.getInt()) if isinstance(n, DoubleObject): return n.getDouble() if isinstance(n, BigInt): return n.bi.tofloat() raise WrongType(u"Failed to promote to double")
def recv(self, atom, args): if atom is ASBYTES_0: return BytesObject(self.publicKey) if atom is PAIRWITH_1: secret = args[0] if not isinstance(secret, SecretKey): raise WrongType(u"Not a secret key!") return KeyPair(self.publicKey, secret.secretKey) raise Refused(self, atom, args)
def sendOnly(self, target, verb, args, namedArgs): """ Send a message to an object. The message will be delivered on some subsequent turn. """ namedArgs = resolution(namedArgs) if not isinstance(namedArgs, ConstMap): raise WrongType(u"namedArgs must be a ConstMap") # Signed, sealed, delivered, I'm yours. sendAtom = getAtom(verb, len(args)) vat = currentVat.get() vat.sendOnly(target, sendAtom, args, namedArgs)
def callWithMessage(self, target, message): """ Pass a message of `[verb :Str, args :List, namedArgs :Map]` to an object. """ if len(message) != 3: raise userError( u"callWithPair/2 requires a [verb, args, namedArgs] triple") verb = unwrapStr(message[0]) args = unwrapList(message[1]) namedArgs = resolution(message[2]) if not isinstance(namedArgs, ConstMap): raise WrongType(u"namedArgs must be a ConstMap") return target.call(verb, args, namedArgs)
def run(self, o): if o is NullObject: return Null if isinstance(o, IntObject): return Int(promoteToBigInt(o)) if isinstance(o, BigInt): try: return Int(o.bi) except OverflowError: pass if isinstance(o, StrObject): return Str(o.getString()) if isinstance(o, DoubleObject): return Double(o.getDouble()) if isinstance(o, CharObject): return Char(o.getChar()) raise WrongType(u"Not a literal")
def recv(self, atom, args): if atom is ASBYTES_0: # XXX should figure this out log.log(["sodium"], u"asBytes/0: Revealing secret key") return BytesObject(self.secretKey) if atom is PAIRWITH_1: public = args[0] if not isinstance(public, PublicKey): raise WrongType(u"Not a public key!") return KeyPair(public.publicKey, self.secretKey) if atom is PUBLICKEY_0: publicKey = rsodium.regenerateKey(self.secretKey) return PublicKey(publicKey) raise Refused(self, atom, args)
def unwrapBytes(o): from typhon.objects.refs import resolution s = resolution(o) if isinstance(s, BytesObject): return s.getBytes() # XXX temporary only: Permit lists of ints. # from typhon.objects.collections.lists import ConstList, unwrapList # if isinstance(s, ConstList): # buf = "" # l = unwrapList(s) # for obj in l: # if isinstance(obj, IntObject): # buf += chr(obj._i) # else: # raise WrongType(u"Not a byte!") # return buf raise WrongType(u"Not a bytestring!")
def send(self, target, verb, args, namedArgs): """ Send a message to an object, returning a promise for the message delivery. The promise will be fulfilled after successful delivery, or smashed upon error. The message will be delivered on some subsequent turn. """ namedArgs = resolution(namedArgs) if not isinstance(namedArgs, ConstMap): raise WrongType(u"namedArgs must be a ConstMap") # Signed, sealed, delivered, I'm yours. sendAtom = getAtom(verb, len(args)) vat = currentVat.get() return vat.send(target, sendAtom, args, namedArgs)
def add(self, right): if right is NullObject: return self if isinstance(right, StaticScope): rightNamesRead = [ name for name in right.read if name not in self.defs + self.vars ] rightNamesSet = [ name for name in right.set if name not in self.vars ] for name in rightNamesSet: if name in self.defs: raise userError(u"Can't assign to final noun %s" % name) return StaticScope(orList(self.read, rightNamesRead), orList(self.set, rightNamesSet), orList(self.defs, right.defs), orList(self.vars, right.vars), self.meta or right.meta) else: raise WrongType(u"Not a static scope")
def recv(self, atom, args): if atom is CALLWITHPAIR_2 or atom is CALLWITHPAIR_3: target = args[0] pair = unwrapList(args[1]) if len(pair) not in (2, 3): raise userError(u"callWithPair/2 requires a pair!") if len(pair) == 3: namedArgs = pair[2] else: namedArgs = EMPTY_MAP sendVerb = unwrapStr(pair[0]) sendArgs = unwrapList(pair[1]) rv = target.call(sendVerb, sendArgs, namedArgs) if rv is None: print "callWithPair/2: Returned None:", \ target.__class__.__name__, sendVerb.encode("utf-8") raise RuntimeError("Implementation error") return rv if atom is CALL_3 or atom is CALL_4: target = args[0] sendVerb = unwrapStr(args[1]) sendArgs = unwrapList(args[2]) if len(args) == 3: namedArgs = EMPTY_MAP else: namedArgs = args[3] rv = target.call(sendVerb, sendArgs, namedArgs) if rv is None: print "call/3: Returned None:", target.__class__.__name__, \ sendVerb.encode("utf-8") raise RuntimeError("Implementation error") return rv if atom is SENDONLY_3: target = args[0] sendVerb = unwrapStr(args[1]) sendArgs = unwrapList(args[2]) # Signed, sealed, delivered, I'm yours. sendAtom = getAtom(sendVerb, len(sendArgs)) vat = currentVat.get() vat.sendOnly(target, sendAtom, sendArgs, EMPTY_MAP) return NullObject if atom is SEND_3: target = args[0] sendVerb = unwrapStr(args[1]) sendArgs = unwrapList(args[2]) # Signed, sealed, delivered, I'm yours. sendAtom = getAtom(sendVerb, len(sendArgs)) vat = currentVat.get() return vat.send(target, sendAtom, sendArgs, EMPTY_MAP) if atom is CALLWITHMESSAGE_2: target = args[0] msg = unwrapList(args[1]) if len(msg) != 3: raise userError( u"callWithPair/2 requires a [verb, args, namedArgs] triple" ) sendVerb = unwrapStr(msg[0]) sendArgs = unwrapList(msg[1]) sendNamedArgs = resolution(msg[2]) if not isinstance(sendNamedArgs, ConstMap): raise WrongType(u"namedArgs must be a ConstMap") rv = target.call(sendVerb, sendArgs, sendNamedArgs) if rv is None: print "callWithPair/2: Returned None:", \ target.__class__.__name__, sendVerb.encode("utf-8") raise RuntimeError("Implementation error") return rv if atom is CALL_4: target = args[0] sendVerb = unwrapStr(args[1]) sendArgs = unwrapList(args[2]) sendNamedArgs = resolution(args[3]) if not isinstance(sendNamedArgs, ConstMap): raise WrongType(u"namedArgs must be a ConstMap") rv = target.call(sendVerb, sendArgs, sendNamedArgs) if rv is None: print "call/3: Returned None:", target.__class__.__name__, \ sendVerb.encode("utf-8") raise RuntimeError("Implementation error") return rv if atom is SENDONLY_4: target = args[0] sendVerb = unwrapStr(args[1]) sendArgs = unwrapList(args[2]) sendNamedArgs = resolution(args[3]) if not isinstance(sendNamedArgs, ConstMap): raise WrongType(u"namedArgs must be a ConstMap") # Signed, sealed, delivered, I'm yours. sendAtom = getAtom(sendVerb, len(sendArgs)) vat = currentVat.get() return vat.sendOnly(target, sendAtom, sendArgs, sendNamedArgs) if atom is SEND_4: target = args[0] sendVerb = unwrapStr(args[1]) sendArgs = unwrapList(args[2]) sendNamedArgs = resolution(args[3]) if not isinstance(sendNamedArgs, ConstMap): raise WrongType(u"namedArgs must be a ConstMap") # Signed, sealed, delivered, I'm yours. sendAtom = getAtom(sendVerb, len(sendArgs)) vat = currentVat.get() return vat.send(target, sendAtom, sendArgs, sendNamedArgs) if atom is TOQUOTE_1: return StrObject(args[0].toQuote()) if atom is TOSTRING_1: return StrObject(toString(args[0])) raise Refused(self, atom, args)
def pairWith(self, secret): if not isinstance(secret, SecretKey): raise WrongType(u"Not a secret key!") return KeyPair(self.publicKey, secret.secretKey)
def unwrapDouble(o): from typhon.objects.refs import resolution d = resolution(o) if isinstance(d, DoubleObject): return d.getDouble() raise WrongType(u"Not a double!")
def unwrapChar(o): from typhon.objects.refs import resolution c = resolution(o) if isinstance(c, CharObject): return c.getChar() raise WrongType(u"Not a char!")
def unwrapBytes(o): from typhon.objects.refs import resolution s = resolution(o) if isinstance(s, BytesObject): return s.getBytes() raise WrongType(u"Not a bytestring!")
def contains(self, needle): if isinstance(needle, IntObject): return chr(needle._i) in self._bs if isinstance(needle, BytesObject): return needle._bs in self._bs raise WrongType(u"Not an int or bytestring!")
def add(self, other): if isinstance(other, BytesObject): return self._bs + other._bs if isinstance(other, IntObject): return self._bs + str(chr(other._i)) raise WrongType(u"Not an int or bytestring!")
def unwrapStr(o): from typhon.objects.refs import resolution s = resolution(o) if isinstance(s, StrObject): return s.getString() raise WrongType(u"Not a string!")
def contains(self, needle): if isinstance(needle, CharObject): return needle._c in self._s if isinstance(needle, StrObject): return needle._s in self._s raise WrongType(u"Not a string or char!")
def add(self, other): if isinstance(other, StrObject): return self._s + other._s if isinstance(other, CharObject): return self._s + unicode(other._c) raise WrongType(u"Not a string or char!")
def pairWith(self, public): if not isinstance(public, PublicKey): raise WrongType(u"Not a public key!") return KeyPair(public.publicKey, self.secretKey)
def unwrapBigInt(o): from typhon.objects.refs import resolution bi = resolution(o) if isinstance(bi, BigInt): return bi.bi raise WrongType(u"Not a big integer!")