def testRefEqualitySettled(self): with scopedVat(testingVat()): first, r = makePromise() r.resolve(IntObject(42)) second, r = makePromise() r.resolve(IntObject(42)) self.assertEqual(optSame(first, second), EQUAL)
def connectStreamCB(connect, status): status = intmask(status) stream = connect.c_handle try: vat, resolvers = ruv.unstashStream(stream) sourceResolver, sinkResolver = unwrapList(resolvers) assert isinstance(sourceResolver, LocalResolver) assert isinstance(sinkResolver, LocalResolver) with scopedVat(vat): if status >= 0: debug_print("Made connection!") wrappedStream = ruv.wrapStream(stream, 2) sourceResolver.resolve(StreamSource(wrappedStream, vat)) sinkResolver.resolve(StreamSink(wrappedStream, vat)) else: error = "Connection failed: " + ruv.formatError(status) debug_print(error) sourceResolver.smash(StrObject(error.decode("utf-8"))) sinkResolver.smash(StrObject(error.decode("utf-8"))) # Done with stream. ruv.closeAndFree(stream) except: if not we_are_translated(): raise
def testSwitchableBecomesResolved(self): with scopedVat(testingVat()): p, r = makePromise() self.assertFalse(isResolved(p)) r.resolve(IntObject(42)) self.assertTrue(isResolved(p))
def runUntilDone(vatManager, uv_loop, recorder): # This may take a while. anyVatHasTurns = vatManager.anyVatHasTurns() while anyVatHasTurns or ruv.loopAlive(uv_loop): for vat in vatManager.vats: if vat.hasTurns(): with scopedVat(vat) as vat: with recorder.context("Time spent in vats"): vat.takeSomeTurns() if ruv.loopAlive(uv_loop): with recorder.context("Time spent in I/O"): ruv.cleanup() try: if anyVatHasTurns: # More work to be done, so don't block. ruv.run(uv_loop, ruv.RUN_NOWAIT) else: # No more work to be done, so blocking is fine. ruv.run(uv_loop, ruv.RUN_ONCE) except CompilerFailed as cf: debug_print("Caught fatal exception while reacting:", cf.formatError()) raise except UserException as ue: debug_print("Caught exception while reacting:", ue.formatError()) anyVatHasTurns = vatManager.anyVatHasTurns()
def connectCB(connect, status): status = intmask(status) stream = connect.c_handle try: vat, resolvers = ruv.unstashStream(stream) fountResolver, drainResolver = unwrapList(resolvers) assert isinstance(fountResolver, LocalResolver) assert isinstance(drainResolver, LocalResolver) with scopedVat(vat): if status >= 0: debug_print("Made connection!") fountResolver.resolve(StreamFount(stream, vat)) drainResolver.resolve(StreamDrain(stream, vat)) else: error = "Connection failed: " + ruv.formatError(status) debug_print(error) fountResolver.smash(StrObject(error.decode("utf-8"))) drainResolver.smash(StrObject(error.decode("utf-8"))) # Done with stream. ruv.closeAndFree(stream) except: if not we_are_translated(): raise
def run(self, state, k): from typhon.objects.collections.maps import EMPTY_MAP r, sink = self.target._nextSink() with scopedVat(self.target._vat): p = self.target._vat.send(sink, self.verb, self.args, EMPTY_MAP) r.resolve(p) if k: k.do(state, Ok(None))
def run(self, state, k): from typhon.objects.collections.maps import EMPTY_MAP for r, sink in self.target._queue: r.resolve(NullObject) with scopedVat(self.target._vat): self.target._vat.send(sink, self.verb, self.args, EMPTY_MAP) if k: k.do(state, Ok(None))
def exited(self, exit_status, term_signal): if self.pid == self.EMPTY_PID: self.retrievePID() self.exit_and_signal = (intmask(exit_status), intmask(term_signal)) toResolve, self.resolvers = self.resolvers, [] with scopedVat(self.vat): for resolver in toResolve: self.resolveWaiter(resolver)
def testSwitchableChains(self): with scopedVat(testingVat()): p, r = makePromise() p2, r2 = makePromise() r.resolve(p2) self.assertFalse(isResolved(p)) r2.resolve(IntObject(42)) self.assertTrue(isResolved(p))
def gaiCB(gai, status, ai): status = intmask(status) vat, resolver = ruv.unstashGAI(gai) with scopedVat(vat): assert isinstance(resolver, LocalResolver), "implementation error" if status < 0: msg = ruv.formatError(status).decode("utf-8") resolver.smash(StrObject(u"libuv error: %s" % msg)) else: gaiList = walkAI(ai) resolver.resolve(ConstList(gaiList[:])) ruv.freeAddrInfo(ai) ruv.free(gai)
def readFileCB(fs): size = intmask(fs.c_result) with ruv.unstashingFS(fs) as (vat, source): assert isinstance(source, FileSource) with scopedVat(vat): if size > 0: data = charpsize2str(source._buf.c_base, size) source.deliver(data) elif size < 0: msg = ruv.formatError(size).decode("utf-8") source.abort(u"libuv error: %s" % msg) else: # EOF. source.complete()
def closeSetContentsCB(fs): try: vat, sc = ruv.unstashFS(fs) # Need to scope vat here. with scopedVat(vat): assert isinstance(sc, SetContents) size = intmask(fs.c_result) if size < 0: msg = ruv.formatError(size).decode("utf-8") sc.fail(u"libuv error: %s" % msg) else: # Success. sc.rename() except: print "Exception in closeSetContentsCB"
def readFileCB(fs): size = intmask(fs.c_result) vat, source = ruv.unstashFS(fs) assert isinstance(source, FileSource) with scopedVat(vat): if size > 0: data = charpsize2str(source._buf.c_base, size) source.deliver(data) elif size < 0: msg = ruv.formatError(size).decode("utf-8") source.abort(u"libuv error: %s" % msg) else: # EOF. source.complete() ruv.fsDiscard(fs)
def openDrainCB(fs): # As above. try: fd = intmask(fs.c_result) vat, r = ruv.unstashFS(fs) assert isinstance(r, LocalResolver) with scopedVat(vat): if fd < 0: msg = ruv.formatError(fd).decode("utf-8") r.smash(StrObject(u"Couldn't open file drain: %s" % msg)) # Done with fs. ruv.fsDiscard(fs) else: r.resolve(FileDrain(fs, fd, vat)) except: print "Exception in openDrainCB"
def openFountCB(fs): # Does *not* run user-level code. The scoped vat is only for promise # resolution. try: fd = intmask(fs.c_result) vat, r = ruv.unstashFS(fs) assert isinstance(r, LocalResolver) with scopedVat(vat): if fd < 0: msg = ruv.formatError(fd).decode("utf-8") r.smash(StrObject(u"Couldn't open file fount: %s" % msg)) # Done with fs. ruv.fsDiscard(fs) else: r.resolve(FileFount(fs, fd, vat)) except: print "Exception in openFountCB"
def openGetContentsCB(fs): try: fd = intmask(fs.c_result) vat, r = ruv.unstashFS(fs) assert isinstance(r, LocalResolver) with scopedVat(vat): if fd < 0: msg = ruv.formatError(fd).decode("utf-8") r.smash(StrObject(u"Couldn't open file fount: %s" % msg)) # Done with fs. ruv.fsDiscard(fs) else: # Strategy: Read and use the callback to queue additional reads # until done. This call is known to its caller to be expensive, so # there's not much point in trying to be clever about things yet. gc = GetContents(vat, fs, fd, r) gc.queueRead() except: print "Exception in openGetContentsCB"
def readStreamCB(stream, status, buf): status = intmask(status) # We only restash in the success case, not the error cases. vat, source = ruv.unstashStream(stream) assert isinstance(source, StreamSource), "Implementation error" # Don't read any more. We'll call .readStart() when we're interested in # reading again. ruv.readStop(stream) with scopedVat(vat): if status > 0: # Restash required. ruv.stashStream(stream, (vat, source)) data = charpsize2str(buf.c_base, status) source.deliver(data) elif status == -4095: # EOF. source.complete() else: msg = ruv.formatError(status).decode("utf-8") source.abort(u"libuv error: %s" % msg)
def readCB(stream, status, buf): status = intmask(status) try: # We only restash in the success case, not the error cases. vat, fount = ruv.unstashStream(stream) assert isinstance(fount, StreamFount), "Implementation error" with scopedVat(vat): if status > 0: # Restash required. ruv.stashStream(stream, (vat, fount)) data = charpsize2str(buf.c_base, status) fount.receive(data) elif status == 0: # EOF. fount.stop(u"End of stream") else: msg = ruv.formatError(status).decode("utf-8") fount.abort(u"libuv error: %s" % msg) except: if not we_are_translated(): raise
def testUnwrapMapPromise(self): with scopedVat(testingVat()): p = makeNear(ConstMap({})) self.assertEqual(unwrapMap(p).items(), [])
def testUnwrapListPromise(self): with scopedVat(testingVat()): p = makeNear(ConstList([])) self.assertEqual(unwrapList(p), [])
def testPromiseResolved(self): with scopedVat(testingVat()): p, r = makePromise() r.resolve(IntObject(42)) self.assertTrue(isSettled(p))
def testRefEquality(self): with scopedVat(testingVat()): first, r = makePromise() second, r = makePromise() self.assertEqual(optSame(first, second), NOTYET)
def abort(self, reason): if self.fount is not None: with scopedVat(self.vat): from typhon.objects.collections.maps import EMPTY_MAP self.vat.sendOnly(self.fount, ABORTFLOW_0, [], EMPTY_MAP) self.closing()
def runTyphon(argv): # Start metrics. recorder = globalRecorder() recorder.start() # Initialize libsodium. if rsodium.init() < 0: print "Couldn't initialize libsodium!" return 1 config = Configuration(argv) if config.verbose: enableDebugPrint() config.enableLogging() if len(config.argv) < 2: print "No file provided?" return 1 # Pass user configuration to the JIT. set_user_param(None, config.jit) # Intialize our loop. uv_loop = ruv.alloc_loop() # Usurp SIGPIPE, as libuv does not handle it. rsignal.pypysig_ignore(rsignal.SIGPIPE) # Initialize our first vat. It shall be immortal. vatManager = VatManager() vat = Vat(vatManager, uv_loop, checkpoints=-1) vatManager.vats.append(vat) # Update loop timing information. Until the loop really gets going, we # have to do this ourselves in order to get the timing correct for early # timers. ruv.update_time(uv_loop) try: with scopedVat(vat) as vat: prelude = loadPrelude(config, recorder, vat) except LoadFailed as lf: print lf return 1 except CompilerFailed as cf: debug_print("Caught exception while importing prelude:", cf.formatError()) return 1 except UserException as ue: debug_print("Caught exception while importing prelude:", ue.formatError()) return 1 registerGlobals(prelude) scope = safeScope() scope.update(prelude) ss = scope.copy() reflectedSS = monteMap() for k, b in ss.iteritems(): reflectedSS[StrObject(u"&&" + k)] = b ss[u"safeScope"] = finalBinding(ConstMap(reflectedSS), deepFrozenGuard) reflectedSS[StrObject(u"&&safeScope")] = ss[u"safeScope"] scope[u"safeScope"] = ss[u"safeScope"] scope.update(unsafeScope(config)) # The initial vat is included as `currentVat` to the first level of # loading and such. scope[u"currentVat"] = finalBinding(vat, anyGuard) reflectedUnsafeScope = monteMap() unsafeScopeDict = {} for k, b in scope.iteritems(): reflectedUnsafeScope[StrObject(u"&&" + k)] = b unsafeScopeDict[k] = b rus = finalBinding(ConstMap(reflectedUnsafeScope), anyGuard) reflectedUnsafeScope[StrObject(u"&&unsafeScope")] = rus unsafeScopeDict[u"unsafeScope"] = rus try: module = obtainModule([""], recorder, config.argv[1]) except LoadFailed as lf: print lf return 1 if config.loadOnly: # We are finished. return 0 if not config.benchmark: benchmarkSettings.disable() with profiling("vmprof.log", config.profile): # Update loop timing information. ruv.update_time(uv_loop) debug_print("Taking initial turn in script...") result = NullObject try: with recorder.context("Time spent in vats"): with scopedVat(vat): result = module.eval(unsafeScopeDict)[0] if result is None: return 1 except UserException as ue: debug_print("Caught exception while taking initial turn:", ue.formatError()) return 1 # Exit status code. exitStatus = 0 # Update loop timing information. ruv.update_time(uv_loop) try: runUntilDone(vatManager, uv_loop, recorder) rv = resolution(result) if result is not None else NullObject if isinstance(rv, IntObject): exitStatus = rv.getInt() except SystemExit: pass # Huh, apparently this doesn't work. Wonder why/why not. # exitStatus = se.code finally: recorder.stop() recorder.printResults() # Clean up and exit. cleanUpEverything() return exitStatus
def testUnwrapListPromise(self): with scopedVat(testingVat()): p = makeNear(wrapList([])) self.assertEqual(unwrapList(p), [])
def runTyphon(argv): recorder = Recorder() recorder.start() # Initialize libsodium. if rsodium.init() < 0: print "Couldn't initialize libsodium!" return 1 config = Configuration(argv) if config.verbose: enableDebugPrint() config.enableLogging() if len(config.argv) < 2: print "No file provided?" return 1 # Pass user configuration to the JIT. set_user_param(None, config.jit) # Intialize our loop. uv_loop = ruv.alloc_loop() # Usurp SIGPIPE, as libuv does not handle it. rsignal.pypysig_ignore(rsignal.SIGPIPE) # Initialize our first vat. It shall be immortal. vatManager = VatManager() vat = Vat(vatManager, uv_loop, checkpoints=-1) vatManager.vats.append(vat) # Update loop timing information. Until the loop really gets going, we # have to do this ourselves in order to get the timing correct for early # timers. ruv.update_time(uv_loop) try: with scopedVat(vat) as vat: prelude = loadPrelude(config, recorder, vat) except LoadFailed as lf: print lf return 1 registerGlobals(prelude) scope = safeScope() scope.update(prelude) ss = scope.copy() reflectedSS = monteMap() for k, b in ss.iteritems(): reflectedSS[StrObject(u"&&" + k)] = b ss[u"safeScope"] = finalBinding(ConstMap(reflectedSS), deepFrozenGuard) reflectedSS[StrObject(u"&&safeScope")] = ss[u"safeScope"] scope[u"safeScope"] = ss[u"safeScope"] scope.update(unsafeScope(config)) reflectedUnsafeScope = monteMap() unsafeScopeDict = {} for k, b in scope.iteritems(): reflectedUnsafeScope[StrObject(u"&&" + k)] = b unsafeScopeDict[k] = b rus = finalBinding(ConstMap(reflectedUnsafeScope), anyGuard) reflectedUnsafeScope[StrObject(u"&&unsafeScope")] = rus unsafeScopeDict[u"unsafeScope"] = rus try: code = obtainModule([""], config.argv[1], recorder) except LoadFailed as lf: print lf return 1 if config.loadOnly: # We are finished. return 0 if not config.benchmark: benchmarkSettings.disable() with profiling("vmprof.log", config.profile): # Update loop timing information. ruv.update_time(uv_loop) debug_print("Taking initial turn in script...") result = NullObject with recorder.context("Time spent in vats"): with scopedVat(vat): result = evaluateTerms([code], unsafeScopeDict) if result is None: return 1 # Exit status code. exitStatus = 0 # Update loop timing information. ruv.update_time(uv_loop) try: runUntilDone(vatManager, uv_loop, recorder) rv = resolution(result) if result is not None else NullObject if isinstance(rv, IntObject): exitStatus = rv.getInt() except SystemExit: pass # Huh, apparently this doesn't work. Wonder why/why not. # exitStatus = se.code finally: recorder.stop() recorder.printResults() # Clean up and exit. cleanUpEverything() return exitStatus
def testPromise(self): with scopedVat(testingVat()): p, r = makePromise() self.assertFalse(isSettled(p))
def testRefEqualityReflexive(self): with scopedVat(testingVat()): p, r = makePromise() self.assertEqual(optSame(p, p), EQUAL)
def testUnwrapIntPromise(self): with scopedVat(testingVat()): p = makeNear(IntObject(42)) self.assertEqual(unwrapInt(p), 42)
def testPromise(self): with scopedVat(testingVat()): p, r = makePromise() self.assertFalse(p.isSettled())
def testResolveNear(self): with scopedVat(testingVat()): p = makeNear(wrapBool(False)) self.assertFalse(resolution(p).isTrue())
def testUnwrapBoolPromise(self): with scopedVat(testingVat()): p = makeNear(wrapBool(False)) self.assertFalse(unwrapBool(p))
def testPromoteToDoublePromise(self): with scopedVat(testingVat()): p = makeNear(DoubleObject(4.2)) self.assertAlmostEqual(promoteToDouble(p), 4.2)
def testPromiseResolved(self): with scopedVat(testingVat()): p, r = makePromise() r.resolve(IntObject(42)) self.assertTrue(p.isSettled())