[pypy-svn] r66660 - in pypy/branch/io-lang/pypy/lang/io: . test

david at codespeak.net david at codespeak.net
Wed Jul 29 11:10:35 CEST 2009


Author: david
Date: Wed Jul 29 11:10:34 2009
New Revision: 66660

Added:
   pypy/branch/io-lang/pypy/lang/io/coroutine.py
   pypy/branch/io-lang/pypy/lang/io/coroutinemodel.py
   pypy/branch/io-lang/pypy/lang/io/test/test_coro.py
Modified:
   pypy/branch/io-lang/pypy/lang/io/objspace.py
Log:
start working on coroutine implementation

Added: pypy/branch/io-lang/pypy/lang/io/coroutine.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/coroutine.py	Wed Jul 29 11:10:34 2009
@@ -0,0 +1,12 @@
+from pypy.lang.io.register import register_method
+from pypy.lang.io.model import W_Object
+from pypy.lang.io.coroutinemodel import W_Coroutine
+
+
+ at register_method('Coroutine', 'currentCoroutine')
+def coroutine_get_current(space, w_target, w_message, w_context):
+    return W_Coroutine.w_getcurrent(space)
+    
+ at register_method('Coroutine', 'isCurrent')
+def coroutine_is_current(space, w_target, w_message, w_context):
+    return space.newbool(w_target is W_Coroutine.w_getcurrent(space))
\ No newline at end of file

Added: pypy/branch/io-lang/pypy/lang/io/coroutinemodel.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/coroutinemodel.py	Wed Jul 29 11:10:34 2009
@@ -0,0 +1,49 @@
+from pypy.lang.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.state, [self])
+    
+    @staticmethod
+    def w_getcurrent(space):
+        return W_Coroutine._get_state(space).current
+
+    @staticmethod
+    def _get_state(space):
+        # XXX: Need a propper caching machinery
+        if not hasattr(space, '_coroutine_state'):
+            space._coroutine_state = AppCoState(space)
+            space._coroutine_state.post_install()
+        return space._coroutine_state
+
+class AppCoState(BaseCoState):
+    def __init__(self, space):
+        BaseCoState.__init__(self)
+        self.w_tempval = space.w_nil
+        self.space = space
+        
+    def post_install(self):
+        self.current = self.main = W_Coroutine(self.space, self, [self.space.w_object])
+        # self.main.subctx.framestack = None    # wack
+
+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):
+        self.w_message.eval(self.space, self.w_receiver, self.w_context)
\ No newline at end of file

Modified: pypy/branch/io-lang/pypy/lang/io/objspace.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/objspace.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/objspace.py	Wed Jul 29 11:10:34 2009
@@ -1,5 +1,6 @@
 from pypy.rlib.objectmodel import instantiate
 from pypy.lang.io.model import W_Number, W_Object, W_CFunction, W_Block, W_Message, W_List, W_Map, W_ImmutableSequence
+from pypy.lang.io.coroutinemodel import W_Coroutine
 from pypy.lang.io.register import cfunction_definitions
 
 import pypy.lang.io.number
@@ -9,6 +10,7 @@
 import pypy.lang.io.call
 import pypy.lang.io.message
 import pypy.lang.io.map
+import pypy.lang.io.coroutine
 
 class ObjSpace(object):
     """docstring for ObjSpace"""
@@ -25,7 +27,7 @@
         self.w_list = W_List(self, [self.w_object])
         self.w_call = W_Object(self, [self.w_object])
         self.w_map = W_Map(self, [self.w_object])
-        
+        self.w_coroutine = W_Coroutine.w_getcurrent(self)
         # flow control objects
         self.w_normal = W_Object(self, [self.w_object])
         self.w_break = W_Object(self, [self.w_object])
@@ -56,9 +58,13 @@
         self.init_w_call()
          
         self.init_w_map()
+
+        self.init_w_coroutine()
         
         self.init_w_flow_objects()
         
+
+        
     def init_w_map(self):
         for key, function in cfunction_definitions['Map'].items():
             self.w_map.slots[key] = W_CFunction(self, function)
@@ -91,6 +97,7 @@
         self.w_core.slots['Call'] = self.w_call
         self.w_core.slots['Map'] = self.w_map
         self.w_core.slots['Number'] = self.w_number
+        self.w_core.slots['Coroutine'] = self.w_coroutine
 
     def init_w_number(self):
         self.w_number = instantiate(W_Number)
@@ -140,11 +147,14 @@
         self.w_core.slots['Eol'].slots['type'] = W_ImmutableSequence(self, 'Eol')
         
         
+    def init_w_coroutine(self):
+        for key, function in cfunction_definitions['Coroutine'].items():
+            self.w_coroutine.slots[key] = W_CFunction(self, function)
+        
     def break_status(self, result):
         self.stop_status = self.w_break
         self.w_return_value = result
         
-        
     def normal_status(self):
         self.stop_status = self.w_normal
         self.w_return_value = self.w_nil
@@ -162,3 +172,7 @@
         self.stop_status = self.w_return
         self.w_return_value = result
         
+    def newbool(self, value):
+        if value:
+            return self.w_true
+        return self.w_false
\ No newline at end of file

Added: pypy/branch/io-lang/pypy/lang/io/test/test_coro.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_coro.py	Wed Jul 29 11:10:34 2009
@@ -0,0 +1,20 @@
+from pypy.lang.io.parserhack import parse, interpret
+from pypy.lang.io.model import W_Object
+import py
+
+def test_isCurrent():
+    inp = """Coroutine currentCoroutine isCurrent"""
+    result, space = interpret(inp)
+    assert result is space.w_true
+    
+def test_coro_resume():
+    py.test.skip()
+    inp = """
+    b := message(currentCoro setResult(23); currentCoro parentCoroutine resume; "bye" print)
+    a := Coroutine currentCoroutine clone
+    a setRunMessage(b) run
+    a result
+    """
+    res,space = interpret(inp)
+    assert res.value == 23
+    



More information about the Pypy-commit mailing list