class Object
@binding = binding
class << self
attr_accessor :binding
end
end
class Proc
def binding_eval( code)
eval ( code, binding )
end
end
class MyStruct
end
class MyThunkTarget
def work
puts "Hi."
puts foo_obj
end
end
class AssociativeArray < Array
def initialize * args
@collection = [ ]
super * args
end
def insert arg0, arg1=nil
unless arg1.nil ?
@collection << KeyValuePair.new ( arg1, arg0)
else
@collection << arg0
end
end
def << item
insert(item)
end
def [] arg0
result = nil
@collection.each do |it|; result = it.value if it.key == arg0; end
raise IndexError, sprintf("Index '%s' outside array bounds.", arg0) if result.nil?
result
end
def include? arg0
result = false
@collection.each do |it|; result = true if it.key == arg0; end
result
end
end
class KeyValuePair
attr_accessor :key
attr_accessor :value
def initialize arg0, arg1=nil
unless arg0.is_a?(Array) and not arg1.nil?
@key, @value = arg0
else
@key, @value = arg0, arg1
end
end
def == arg0
@key == arg0
end
end
class Thunk
public
def initialize(type_of, *args)
@target, @params = type_of.new, AssociativeArray.new
args.each { |i| @params << KeyValuePair.new(i) }
end
def method_missing(fn, *args)
this = @target.method(fn.to_sym).to_proc
locals = @target.method(fn.to_sym).send(:local_variables)
puts "["
puts locals
puts "]"
puts ""
puts ""
this.call *args
puts ""
rescue NameError => e
name = sprintf( "@%s", e.name ).to_sym
metaclass = class << @target; self; end
raise e unless @params.include? e.name
fieldname_get, fieldname_set = e.name.to_sym, sprintf("%s=", e.name).to_sym
fieldname_attr = sprintf("@%s", e.name).to_sym
metaclass.instance_exec(fieldname_set, fieldname_get, fieldname_attr) do
|set, get, fieldname|
define_method( fieldname_set ) do |value|
instance_variable_set( fieldname, value )
end
define_method( fieldname_get ) do
instance_variable_get( fieldname )
end
end
@target.instance_variable_set( fieldname_attr, @params[e.name] )
retry
end
end
arg0 = "Hello, world!"
arg1 = MyStruct.new
t = Thunk.new ( MyThunkTarget, :foo_obj => arg0, :my_struct => arg1)
t.work
Y2xhc3MgT2JqZWN0CglAYmluZGluZyA9IGJpbmRpbmcKCgljbGFzcyA8PCBzZWxmCgkJYXR0cl9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