Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
add partial continuation stuff for maglev, for later use
Browse files Browse the repository at this point in the history
  • Loading branch information
timfel committed Oct 24, 2012
1 parent 42426be commit 800947c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/maglev/reflection/core_ext/exception.rb
@@ -0,0 +1,3 @@
class Exception
primitive '__resume', 'resume:'
end
21 changes: 21 additions & 0 deletions lib/maglev/reflection/core_ext/proc.rb
@@ -0,0 +1,21 @@
require File.expand_path("../method.rb", __FILE__)
require File.expand_path("../thread.rb", __FILE__)

class ExecBlock
primitive 'shift', 'shift'
primitive 'reset', 'reset'
end

class Proc
def __shift
@_st_block.shift
end

def __reset
@_st_block.reset
end
end

class Thread
primitive 'call', 'call:'
end
50 changes: 50 additions & 0 deletions lib/maglev/reflection/support/partial_continuation.rb
@@ -0,0 +1,50 @@
class PartialContinuation
attr_accessor :markerBlock, :partial

def self.currentDomarkerBlock(block, markerBlock)
if (marker = markerBlock.call).nil?
raise RuntimeError, 'Marker not found when capturing partial continuation.'
end
return block.call to_offset_markerBlock(marker, 1, markerBlock)
end

def self.to_offset_markerBlock(method, offset, &block)
idx = find_frame_for(method)
partial = Thread.__partialContinuationFromLevel_to(3 + offset, idx)
new.tap do |o|
o.partial = partial
o.markerBlock = block
end
end

def self.find_frame_for(method)
index = 1
while frame = Thread.current.__frame_contents_at(index) do
if frame[0] == method
return level
else
level += 1
end
end
nil
end

def call(arg = nil)
marker = markerBlock.call
if marker.nil?
marker = Thread.current.__frame_contents_at(2).first
frameIndex = 2
else
frameIndex = self.class.find_frame_for marker
end
Thread.current.__installPartialContinuation_atLevel_value partial, frameIndex, arg
end

alias [] call
end

def test_callcc
PartialContinuation.currentDomarkerBlock
currentDo: aBlock
markerBlock: [ self callbackMarker ]
end

0 comments on commit 800947c

Please sign in to comment.