Class: Proc

Inherits:
Object show all
Defined in:
lib/ytljit/marshal.rb

Constant Summary collapse

@@iseq_cache =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._allocObject



4
5
6
# File 'lib/ytljit/marshal.rb', line 4

def self._alloc
  Proc.new {}
end

Instance Method Details

#_dump_dataObject



8
9
10
11
12
13
14
15
16
# File 'lib/ytljit/marshal.rb', line 8

def _dump_data
  orgiseq = self.to_iseq
  piseq = @@iseq_cache[orgiseq]
  if !piseq then
    piseq = @@iseq_cache[orgiseq] = patch_iseq(orgiseq.to_a)
  end
    
  [piseq, self.binding.to_a]
end

#_load_data(obj) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ytljit/marshal.rb', line 18

def _load_data(obj)
  iseq, env = obj
  slf = env[0][0]
  $_proc_para = [env, slf]
  prc = lambda {
    $_proc_para[1].instance_eval {
      lambda {|env|
        _lambda_replace
      }
    }.call($_proc_para[0].map {|ele| ele.reverse})
  }
  
  prc2 =  VMLib::InstSeqTree.new(nil, prc.to_iseq.to_a)
  iv = VMLib::InstSeqTree.new(nil, prc2.body[7][3])
  lam = VMLib::InstSeqTree.new(nil, iv.body[5][3])
  lam.body[4][1] = :lambda
  lam.body[4][3] = iseq
  
  prc2.header['type'] = :top
    
  self.copy(ISeq.load(prc2.to_a).eval)
end

#copy(procval) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/ytljit.c', line 196

VALUE
ytl_proc_copy(VALUE self, VALUE procval)
{
    rb_proc_t *src, *dst;
    GetProcPtr(procval, src);
    GetProcPtr(self, dst);

    dst->block = src->block;
    dst->block.proc = procval;
    dst->blockprocval = src->blockprocval;
    dst->envval = src->envval;
    dst->safe_level = src->safe_level;
    dst->is_lambda = src->is_lambda;

    return self;
}

#patch_iseq(iseq, dbase = 0) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ytljit/marshal.rb', line 41

def patch_iseq(iseq, dbase = 0)
  rbody = []
  iseq2 = VMLib::InstSeqTree.new(nil, iseq)
  
  iseq2.body.each do |ele|
    rbody.push ele
    if ele.is_a?(Array) then
      case ele[0]
      when :send
        if ele[3] then
          ele[3] = patch_iseq(VMLib::InstSeqTree.new(iseq, ele[3]), dbase + 1)
        end
                     
      when :getdynamic
        off = ele[1]
        dep = ele[2]
        if dep > dbase then
          rbody.pop
          rbody.push [:getdynamic, 3, 1 + dbase]
          rbody.push [:putobject, dep - 1 - dbase]
          rbody.push [:opt_aref, 0]
          rbody.push [:putobject, off]
          rbody.push [:opt_aref, 0]
        end
        
      when :setdynamic
        off = ele[1]
        dep = ele[2]
        if dep > dbase then
          rbody.pop
          rbody.push [:getdynamic, 3, 1 + dbase]
          rbody.push [:putobject, dep - 1 - dbase]
          rbody.push [:opt_aref, 0]
          rbody.push [:putobject, off]
          rbody.push [:topn, 2]
          rbody.push [:send, :[]=, 2, nil, 0, 0]
          rbody.push [:swap]
          rbody.push [:pop]
        end
        
      when :getlocal
        off = ele[1]
        rbody.pop 
        rbody.push [:getdynamic, 2, 1 + dbase]
        rbody.push [:dup]
        rbody.push [:opt_length]
        rbody.push [:putobject, 1]
        rbody.push [:opt_sub]
        rbody.push [:putobject, dbase]
        rbody.push [:opt_add]
        rbody.push [:opt_aref, 0]
        rbody.push [:putobject, off]
        rbody.push [:opt_aref, 0]
        
      when :setlocal
        off = ele[1]
        rbody.pop 
        rbody.push [:getdynamic, 2, 1 + dbase]
        rbody.push [:dup]
        rbody.push [:opt_length]
        rbody.push [:putobject, 1]
        rbody.push [:opt_sub]
        rbody.push [:putobject, dbase]
        rbody.push [:opt_add]
        rbody.push [:opt_aref, 0]
        rbody.push [:putobject, off]
        rbody.push [:topn, 2]
        rbody.push [:send, :[]=, 2, nil, 0, 0]
        rbody.push [:swap]
        rbody.push [:pop]
      end
    end
  end
  
  iseq2.body = rbody
  iseq2.to_a
end

#to_iseqObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'ext/ytljit.c', line 175

VALUE
ytl_proc_to_iseq(VALUE self)
{
  rb_proc_t *proc;
  rb_iseq_t *iseq;

  GetProcPtr(self, proc);
  iseq = proc->block.iseq;
  if (proc->is_from_method) {
    NODE *node = (NODE *)iseq;
    /* method(:foo).to_proc */
    iseq = rb_method_get_iseq(node->u2.value);
  }
  if (iseq) {
    return iseq->self;
  }
  else {
    return Qnil;
  }
}