Source

lang-io / io / coroutinemodel.py

import greenlet

from io.model import W_Object
from pypy.rlib.rcoroutine import make_coroutine_classes
d = make_coroutine_classes(W_Object)

Coroutine = d['Coroutine']
AbstractThunk = d['AbstractThunk']
BaseCoState = d['BaseCoState']


class W_Coroutine(Coroutine):
    def __init__(self, space, state, protos):
        Coroutine.__init__(self, state)

        W_Object.__init__(self, space, protos)

    def clone(self):
        return W_Coroutine(self.space, self.costate, [self])

    @staticmethod
    def w_getcurrent(space):
        return W_Coroutine._get_state(space).current

    @staticmethod
    def w_getmain(space):
        return W_Coroutine._get_state(space).main
    @staticmethod
    def _get_state(space):
        # XXX: Need a propper caching machinery
        if space._coroutine_state is None:
            space._coroutine_state = AppCoState(space)
            space._coroutine_state.post_install()
        return space._coroutine_state

    def run(self, space, w_receiver, w_context):
        if self.thunk is None:
            t = IoThunk(space, self.slots['runMessage'], w_receiver, w_context)
            self.bind(t)
            p = self.lookup('parentCoroutine')
            if not space.isnil(p):
                self.parent = p
        self.switch()


class AppCoState(BaseCoState):
    def __init__(self, space):
        BaseCoState.__init__(self)
        self.space = space

    def post_install(self):
        self.main = W_Coroutine(self.space, self, [self.space.w_object])
        self.current = self.main
        # self.current.slots['parentCoroutine'] = self.main

class IoThunk(AbstractThunk):
    def __init__(self, space, w_message, w_receiver, w_context):
        self.space = space
        self.w_message = w_message
        self.w_receiver = w_receiver
        self.w_context = w_context

    def call(self):
        t = self.w_message.eval(self.space, self.w_receiver, self.w_context)
        self.w_receiver.slots['result'] = t
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.