Class: Proc

Inherits:
Object show all
Defined in:
(unknown)

Direct Known Subclasses

UnboundProc

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(str) ⇒ UnboundProc

Load a Proc from a String. When it is loaded, it will be an UnboundProc, until it is bound to a Binding with UnboundProc#bind.

Returns:



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'ext/internal/proc/proc.c', line 200

static VALUE proc_load(VALUE klass, VALUE str)
{
#ifdef RUBY_VM
  VALUE iseq = marshal_load(str);
  return create_proc(rb_cUnboundProc, Qnil, iseq_check(iseq));
#else
  VALUE arr = marshal_load(str);
  NODE * body, * var;

  if(   rb_safe_level() >= 4
     || (rb_safe_level() >= 1 && OBJ_TAINTED(str)))
  {
    /* no playing with knives in the sandbox */
    rb_raise(rb_eSecurityError, "Insecure: can't load proc");
  }

  Check_Type(arr, T_ARRAY);
  body = unwrap_node(RARRAY_PTR(arr)[0]);
  var = unwrap_node(RARRAY_PTR(arr)[1]);
  return create_proc(rb_cUnboundProc, Qnil, body, var);
#endif
}

Instance Method Details

#push(anotherProc) ⇒ self

Append the body of anotherProc onto proc.

Returns:

  • (self)


251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'ext/internal/proc/proc.c', line 251

static VALUE proc_push(VALUE self, VALUE other)
{
#ifdef RUBY_VM
  rb_raise(rb_eRuntimeError, "Proc#push not implemented yet for YARV");
#else
  struct BLOCK * b1;
  struct BLOCK * b2;
  Data_Get_Struct(self, struct BLOCK, b1);
  Data_Get_Struct(other, struct BLOCK, b2);
  b1->body = NEW_NODE(NODE_BLOCK, b1->body, 0, b2->body);
  return self;
#endif
}

#dump(limit) ⇒ String

Dump a Proc to a String.

Returns:

  • (String)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'ext/internal/proc/proc.c', line 125

static VALUE proc_dump(VALUE self, VALUE limit)
{
  if(rb_safe_level() >= 4)
  {
    /* no access to potentially sensitive data from the sandbox */
    rb_raise(rb_eSecurityError, "Insecure: can't dump proc");
  }

  {
#ifdef RUBY_VM
    rb_proc_t * p;
    VALUE iseq, str;
    rb_iseq_t * iseqdat;
    GetProcPtr(self, p);
    iseq = p->block.iseq->self;
    iseqdat = iseq_check(iseq);
    iseqdat->type = ISEQ_TYPE_TOP; /* TODO: is this right? */
    str = marshal_dump(iseq, limit);
    return str;
#else
    struct BLOCK * b;
    VALUE body, var, arr;

    Data_Get_Struct(self, struct BLOCK, b);
    body = wrap_node(b->body);
    var = wrap_node(b->var);
    arr = rb_assoc_new(body, var);
    return marshal_dump(arr, limit);
#endif
  }
}

#bodyNode

Returns the Proc’s body Node.

On YARV, this will return the instruction sequence for the proc’s block.

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'ext/internal/proc/proc.c', line 44

static VALUE proc_body(VALUE self)
{
#ifdef RUBY_VM
  rb_proc_t * p;
  GetProcPtr(self, p);
  return p->block.iseq->self;
#else
  struct BLOCK * b;
  if(rb_safe_level() >= 4)
  {
    /* no access to potentially sensitive data from the sandbox */
    rb_raise(rb_eSecurityError, "Insecure: can't get proc body");
  }
  Data_Get_Struct(self, struct BLOCK, b);
  return wrap_node(b->body);
#endif
}

#push(anotherProc) ⇒ self

Append the body of anotherProc onto proc.

Returns:

  • (self)


251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'ext/internal/proc/proc.c', line 251

static VALUE proc_push(VALUE self, VALUE other)
{
#ifdef RUBY_VM
  rb_raise(rb_eRuntimeError, "Proc#push not implemented yet for YARV");
#else
  struct BLOCK * b1;
  struct BLOCK * b2;
  Data_Get_Struct(self, struct BLOCK, b1);
  Data_Get_Struct(other, struct BLOCK, b2);
  b1->body = NEW_NODE(NODE_BLOCK, b1->body, 0, b2->body);
  return self;
#endif
}

#unbindUnboundProc

Create an UnboundProc from a Proc.

Returns:



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'ext/internal/proc/proc.c', line 229

static VALUE proc_unbind(VALUE self)
{
#ifdef RUBY_VM
  rb_proc_t * p;
  GetProcPtr(self, p);
  return create_proc(rb_cUnboundProc, Qnil, p->block.iseq);
#else
  struct BLOCK * b;
  Data_Get_Struct(self, struct BLOCK, b);
  /* no need for a security check to unbind a proc -- though without the
   * ability to bind, this doesn't seem very useful.
   */
  return create_proc(rb_cUnboundProc, Qnil, b->body, b->var);
#endif
}

#varNode

Returns the Proc’s argument Node.

This method is undefined on YARV.

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/internal/proc/proc.c', line 71

static VALUE proc_var(VALUE self)
{
  struct BLOCK * b;
  if(rb_safe_level() >= 4)
  {
    /* no access to potentially sensitive data from the sandbox */
    rb_raise(rb_eSecurityError, "Insecure: can't get proc var");
  }
  Data_Get_Struct(self, struct BLOCK, b);
  if(b->var == (NODE*)1)
  {
    /* no parameter || */
    return INT2NUM(1);
  }
  else if(b->var == (NODE*)2)
  {
    /* also no params, but I'm not sure how this one gets generated */
    return INT2NUM(2);
  }
  else
  {
    return wrap_node(b->var);
  }
}