help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] [Q] On Implementing Continuation...


From: Sungjin Chun
Subject: [Help-smalltalk] [Q] On Implementing Continuation...
Date: Tue, 06 Mar 2007 10:54:01 +0900
User-agent: Thunderbird 1.5.0.9 (X11/20070104)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

Attached files are for testing continuation in gst. Contuation is my
trial of porting Squeak version of Seaside's Continuation. It seems that
most parts are very easily 1 to 1 matched between gst and squeak but
swapSender: is not. I've just blindly copy squeak's code to create
swapSender: for gst but this does not work.

Can anyone help me on this?

Thanks in advance.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF7Mm5QqspS1+XJHgRAhFCAKCphxZGdue6vHxfZg6SLrloVg943QCgxAqg
rNUPGAL23//y/fC52m8IAe0=
=H+KJ
-----END PGP SIGNATURE-----
Smalltalk.Object subclass: #Continuation
    instanceVariableNames: 'values'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Seaside-Continuations'!

Continuation comment: nil!

!Continuation class methodsFor: 'instance creation'!

current
    ^ self fromContext: thisContext sender
!

currentDo: aBlock
    ^ aBlock value: (self fromContext: thisContext sender)
!

fromContext: aStack
    ^ self new initializeFromContext: aStack
! !


!Continuation methodsFor: 'private'!

initializeFromContext: aContext
    | valueStream context |
    valueStream _ WriteStream on: (Array new: 20).
    context _ aContext.
    [context notNil] whileTrue: [
        valueStream nextPut: aContext.
        1 to: context class instSize do: [:i | 
            valueStream nextPut: (context instVarAt: i)
        ].
        1 to: context localSize do: [:i |
            valueStream nextPut: (context localAt: i)
        ].
        context _ context sender
    ].
    values _ valueStream contents
!

terminate: aContext
    | context |
    context _ aContext.
    [context notNil] whileTrue: [context _ context swapSender: nil]
! !


!Continuation methodsFor: 'invocation'!

numArgs
    ^ 1
!

value
    self value: nil
!

valueWithArguments: v
    v size == 1 ifFalse: [^ self error: 'continuations can only be resumed with 
only one argument' ].
    self value: v first
!

value: v
    self terminate: thisContext.
    self restoreValues.
    thisContext swapSender: values first.
    ^ v
! !


!Continuation methodsFor: 'resuming'!

restoreValues
    | valueStream context |
    valueStream _ values readStream.
    [valueStream atEnd] whileFalse: [
        context _ valueStream next.
        1 to: context class instSize do: [:i |
            context instVarAt: i put: valueStream next
        ].
        1 to: context localSize do: [:i |
            context localAt: i put: valueStream next
        ]
    ]
! !


!ContextPart methodsFor: 'continuation'!

homeReceiver
    ^ self home receiver
!

localAt: aNumber
    ^ self at: aNumber
!

localAt: aNumber put: anObject
    ^ self at: aNumber put: anObject
!

localSize
    ^ self size
!

swapSender: coroutine
    | oldSender |
    oldSender _ parent.
    parent _ coroutine.
    ^ oldSender
! !

Smalltalk.Object subclass: #TestCont
    instanceVariableNames: 'temp temp2'
    classVariableNames: ''
    poolDictionaries: ''
    category: ''!

TestCont comment: nil!

!TestCont methodsFor: 'testing'!

run
    | x |
    'Now1' printNl.
    temp _ 0.
    'Now2' printNl.
    x _ [temp _ temp + 1. temp2 value].
    'Now3' printNl.
    Continuation currentDo: [:cc | temp2 _ cc. x value].
    'Now4' printNl.
    temp2 _ [].
    'Now5' printNl.
    x value.
    'Now6' printNl.
    temp printNl. "should print 2"
! !

reply via email to

[Prev in Thread] Current Thread [Next in Thread]