Skip to content
This repository
Newer
Older
100644 777 lines (685 sloc) 18.986 kb
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
1 /**********************************************************************
2
3 vm_eval.c -
4
5 $Author: nobu $
6 created at: Sat May 24 16:02:32 JST 2008
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12 **********************************************************************/
13
14 #include "ruby/ruby.h"
15 #include "ruby/node.h"
16 #include "ruby/st.h"
cb654164 » Laurent Sansonetti
2009-05-23 the great schism, part I
17 #include "vm.h"
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
18 #include "objc.h"
19 #include "id.h"
20
21 #include "vm_method.c"
22
23 static inline VALUE
f357c641 » Laurent Sansonetti
2009-05-11 a new implementation for storing active blocks in the VM which fixes …
24 rb_call(VALUE recv, ID mid, int argc, const VALUE *argv, int scope,
25 bool pass_current_block)
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
26 {
27 SEL sel;
28 if (mid == ID_ALLOCATOR) {
29 sel = selAlloc;
30 }
31 else {
32 const char *midstr = rb_id2name(mid);
33 if (argc > 0 && midstr[strlen(midstr) - 1] != ':') {
34 char buf[100];
35 snprintf(buf, sizeof buf, "%s:", midstr);
36 sel = sel_registerName(buf);
37 }
38 else {
39 sel = sel_registerName(midstr);
40 }
41 }
f357c641 » Laurent Sansonetti
2009-05-11 a new implementation for storing active blocks in the VM which fixes …
42
43 void *cache = rb_vm_get_call_cache(sel);
44 rb_vm_block_t *block = pass_current_block ? rb_vm_current_block() : NULL;
45 return rb_vm_call_with_cache2(cache, block, recv, CLASS_OF(recv),
46 sel, argc, argv);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
47 }
48
49 /*
50 * call-seq:
51 * obj.method_missing(symbol [, *args] ) => result
52 *
53 * Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
54 * <i>symbol</i> is the symbol for the method called, and <i>args</i>
55 * are any arguments that were passed to it. By default, the interpreter
56 * raises an error when this method is called. However, it is possible
57 * to override the method to provide more dynamic behavior.
58 * If it is decided that a particular method should not be handled, then
59 * <i>super</i> should be called, so that ancestors can pick up the
60 * missing method.
61 * The example below creates
62 * a class <code>Roman</code>, which responds to methods with names
63 * consisting of roman numerals, returning the corresponding integer
64 * values.
65 *
66 * class Roman
67 * def romanToInt(str)
68 * # ...
69 * end
70 * def method_missing(methId)
71 * str = methId.id2name
72 * romanToInt(str)
73 * end
74 * end
75 *
76 * r = Roman.new
77 * r.iv #=> 4
78 * r.xxiii #=> 23
79 * r.mm #=> 2000
80 */
81
82 static VALUE
83 rb_method_missing(VALUE obj, SEL sel, int argc, const VALUE *argv)
84 {
85 return rb_vm_method_missing(obj, argc, argv);
86 }
87
88 VALUE
89 rb_apply(VALUE recv, ID mid, VALUE args)
90 {
91 int argc;
92 VALUE *argv;
93
94 argc = RARRAY_LEN(args); /* Assigns LONG, but argc is INT */
95 argv = ALLOCA_N(VALUE, argc);
96 MEMCPY(argv, RARRAY_PTR(args), VALUE, argc);
f357c641 » Laurent Sansonetti
2009-05-11 a new implementation for storing active blocks in the VM which fixes …
97 return rb_call(recv, mid, argc, argv, CALL_FCALL, false);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
98 }
99
100 VALUE
101 rb_funcall(VALUE recv, ID mid, int n, ...)
102 {
103 VALUE *argv;
104 va_list ar;
105 va_start(ar, n);
106
107 if (n > 0) {
108 long i;
109
110 argv = ALLOCA_N(VALUE, n);
111
112 for (i = 0; i < n; i++) {
113 argv[i] = va_arg(ar, VALUE);
114 }
115 va_end(ar);
116 }
117 else {
118 argv = 0;
119 }
f357c641 » Laurent Sansonetti
2009-05-11 a new implementation for storing active blocks in the VM which fixes …
120 return rb_call(recv, mid, n, argv, CALL_FCALL, false);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
121 }
122
123 VALUE
124 rb_funcall2(VALUE recv, ID mid, int argc, const VALUE *argv)
125 {
f357c641 » Laurent Sansonetti
2009-05-11 a new implementation for storing active blocks in the VM which fixes …
126 return rb_call(recv, mid, argc, argv, CALL_FCALL, false);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
127 }
128
129 VALUE
130 rb_funcall3(VALUE recv, ID mid, int argc, const VALUE *argv)
131 {
f357c641 » Laurent Sansonetti
2009-05-11 a new implementation for storing active blocks in the VM which fixes …
132 return rb_call(recv, mid, argc, argv, CALL_PUBLIC, false);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
133 }
134
135 static VALUE
136 send_internal(int argc, VALUE *argv, VALUE recv, int scope)
137 {
138 VALUE vid;
139
140 if (argc == 0) {
141 rb_raise(rb_eArgError, "no method name given");
142 }
143
144 vid = *argv++; argc--;
f357c641 » Laurent Sansonetti
2009-05-11 a new implementation for storing active blocks in the VM which fixes …
145 return rb_call(recv, rb_to_id(vid), argc, argv, scope, true);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
146 }
147
148 /*
149 * call-seq:
150 * obj.send(symbol [, args...]) => obj
151 * obj.__send__(symbol [, args...]) => obj
152 *
153 * Invokes the method identified by _symbol_, passing it any
154 * arguments specified. You can use <code>__send__</code> if the name
155 * +send+ clashes with an existing method in _obj_.
156 *
157 * class Klass
158 * def hello(*args)
159 * "Hello " + args.join(' ')
160 * end
161 * end
162 * k = Klass.new
163 * k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
164 */
165
166 static VALUE
167 rb_f_send(VALUE recv, SEL sel, int argc, VALUE *argv)
168 {
169 return send_internal(argc, argv, recv, NOEX_NOSUPER | NOEX_PRIVATE);
170 }
171
172 /*
173 * call-seq:
174 * obj.public_send(symbol [, args...]) => obj
175 *
176 * Invokes the method identified by _symbol_, passing it any
177 * arguments specified. Unlike send, public_send calls public
178 * methods only.
179 *
180 * 1.public_send(:puts, "hello") # causes NoMethodError
181 */
182
183 static VALUE
184 rb_f_public_send(VALUE recv, SEL sel, int argc, VALUE *argv)
185 {
186 return send_internal(argc, argv, recv, NOEX_PUBLIC);
187 }
188
189 /* yield */
190
191 VALUE
192 rb_yield_values(int n, ...)
193 {
194 if (n == 0) {
dabee306 » Laurent Sansonetti
2009-11-04 rb_yield is now inlined + improved GC_WB to not emit a write barrier …
195 return rb_vm_yield(0, 0);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
196 }
197 else {
198 int i;
199 VALUE *argv;
200 va_list args;
201 argv = ALLOCA_N(VALUE, n);
202
203 va_start(args, n);
204 for (i=0; i<n; i++) {
205 argv[i] = va_arg(args, VALUE);
206 }
207 va_end(args);
208
dabee306 » Laurent Sansonetti
2009-11-04 rb_yield is now inlined + improved GC_WB to not emit a write barrier …
209 return rb_vm_yield(n, argv);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
210 }
211 }
212
213 VALUE
214 rb_yield_splat(VALUE values)
215 {
216 VALUE tmp = rb_check_array_type(values);
217 if (NIL_P(tmp)) {
218 rb_raise(rb_eArgError, "not an array");
219 }
dabee306 » Laurent Sansonetti
2009-11-04 rb_yield is now inlined + improved GC_WB to not emit a write barrier …
220 return rb_vm_yield(RARRAY_LEN(tmp), RARRAY_PTR(tmp));
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
221 }
222
223 static VALUE
224 loop_i(void)
225 {
10ef9b1d » Laurent Sansonetti
2009-08-27 testing thread cancelability is all loops wasn't a bright idea
226 int count = 0;
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
227 for (;;) {
228 rb_yield(Qundef);
229 RETURN_IF_BROKEN();
a2e495be » vincentisambart
2009-08-28 fixed some of Laurent's strange code ;)
230 if (++count >= 100) {
10ef9b1d » Laurent Sansonetti
2009-08-27 testing thread cancelability is all loops wasn't a bright idea
231 TEST_THREAD_CANCEL();
232 count = 0;
233 }
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
234 }
235 return Qnil;
236 }
237
238 /*
239 * call-seq:
240 * loop {|| block }
241 *
242 * Repeatedly executes the block.
243 *
244 * loop do
245 * print "Input: "
246 * line = gets
247 * break if !line or line =~ /^qQ/
248 * # ...
249 * end
250 *
251 * StopIteration raised in the block breaks the loop.
252 */
253
254 static VALUE
cbe906ec » Laurent Sansonetti
2009-08-26 introduce rb_objc_define_module_function() which mimics the ruby spec
255 rb_f_loop(VALUE rcv, SEL sel)
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
256 {
cbe906ec » Laurent Sansonetti
2009-08-26 introduce rb_objc_define_module_function() which mimics the ruby spec
257 RETURN_ENUMERATOR(rcv, 0, 0);
5c8cd3fe » Laurent Sansonetti
2009-04-03 fixed the return value of #loop (in case of break)
258 return rb_rescue2(loop_i, (VALUE)0, 0, 0, rb_eStopIteration, (VALUE)0);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
259 }
260
261 VALUE
8a233296 » Laurent Sansonetti
2009-03-22 do not stack blocks since this doesn't go well with the ruby semantics
262 rb_objc_block_call(VALUE obj, SEL sel, void *cache, int argc, VALUE *argv,
38a26b8f » Laurent Sansonetti
2009-03-20 implemented NODE_IFUNC-type blocks
263 VALUE (*bl_proc) (ANYARGS), VALUE data2)
264 {
2c54dbd2 » Laurent Sansonetti
2009-07-01 AOT compiler: added support for blocks
265 rb_vm_block_t *b = rb_vm_create_block((IMP)bl_proc, obj, data2);
8a233296 » Laurent Sansonetti
2009-03-22 do not stack blocks since this doesn't go well with the ruby semantics
266 if (cache == NULL) {
267 cache = rb_vm_get_call_cache(sel);
268 }
f357c641 » Laurent Sansonetti
2009-05-11 a new implementation for storing active blocks in the VM which fixes …
269 return rb_vm_call_with_cache2(cache, b, obj, 0, sel, argc, argv);
38a26b8f » Laurent Sansonetti
2009-03-20 implemented NODE_IFUNC-type blocks
270 }
271
272 VALUE
273 rb_block_call(VALUE obj, ID mid, int argc, VALUE *argv,
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
274 VALUE (*bl_proc) (ANYARGS), VALUE data2)
275 {
38a26b8f » Laurent Sansonetti
2009-03-20 implemented NODE_IFUNC-type blocks
276 SEL sel;
277 if (argc == 0) {
278 sel = sel_registerName(rb_id2name(mid));
279 }
280 else {
281 char buf[100];
282 snprintf(buf, sizeof buf, "%s:", rb_id2name(mid));
283 sel = sel_registerName(buf);
284 }
8a233296 » Laurent Sansonetti
2009-03-22 do not stack blocks since this doesn't go well with the ruby semantics
285 return rb_objc_block_call(obj, sel, NULL, argc, argv, bl_proc, data2);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
286 }
287
288 VALUE
289 rb_each(VALUE obj)
290 {
f357c641 » Laurent Sansonetti
2009-05-11 a new implementation for storing active blocks in the VM which fixes …
291 return rb_call(obj, idEach, 0, 0, CALL_FCALL, false);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
292 }
293
cf73725a » Laurent Sansonetti
2010-01-17 experimental debugger
294 VALUE
295 rb_vm_eval_string(VALUE self, VALUE klass, VALUE src, rb_vm_binding_t *binding,
296 const char *file, const int line)
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
297 {
40717caf » Laurent Sansonetti
2009-03-24 do not print syntax error messages while parsing code within eval
298 bool old_parse_in_eval = rb_vm_parse_in_eval();
299 rb_vm_set_parse_in_eval(true);
cf73725a » Laurent Sansonetti
2010-01-17 experimental debugger
300 if (binding != NULL) {
084a52a0 » Laurent Sansonetti
2009-04-03 a faster implementation of dvars + fixed eval bugs + preliminary impl…
301 // Binding must be added because the parser needs it.
cf73725a » Laurent Sansonetti
2010-01-17 experimental debugger
302 rb_vm_add_binding(binding);
084a52a0 » Laurent Sansonetti
2009-04-03 a faster implementation of dvars + fixed eval bugs + preliminary impl…
303 }
304
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
305 NODE *node = rb_compile_string(file, src, line);
084a52a0 » Laurent Sansonetti
2009-04-03 a faster implementation of dvars + fixed eval bugs + preliminary impl…
306
cf73725a » Laurent Sansonetti
2010-01-17 experimental debugger
307 if (binding != NULL) {
084a52a0 » Laurent Sansonetti
2009-04-03 a faster implementation of dvars + fixed eval bugs + preliminary impl…
308 // We remove the binding now but we still pass it to the VM, which
309 // will use it for compilation.
310 rb_vm_pop_binding();
311 }
40717caf » Laurent Sansonetti
2009-03-24 do not print syntax error messages while parsing code within eval
312 rb_vm_set_parse_in_eval(old_parse_in_eval);
084a52a0 » Laurent Sansonetti
2009-04-03 a faster implementation of dvars + fixed eval bugs + preliminary impl…
313
22ef83cd » Laurent Sansonetti
2009-03-21 let's raise SyntaxError exceptions when there is a parsing error inst…
314 if (node == NULL) {
40717caf » Laurent Sansonetti
2009-03-24 do not print syntax error messages while parsing code within eval
315 VALUE exc = rb_vm_current_exception();
316 if (exc != Qnil) {
7c968bd0 » Laurent Sansonetti
2009-05-12 fixing more exceptions bugs
317 rb_vm_raise_current_exception();
40717caf » Laurent Sansonetti
2009-03-24 do not print syntax error messages while parsing code within eval
318 }
319 else {
320 rb_raise(rb_eSyntaxError, "compile error");
321 }
22ef83cd » Laurent Sansonetti
2009-03-21 let's raise SyntaxError exceptions when there is a parsing error inst…
322 }
084a52a0 » Laurent Sansonetti
2009-04-03 a faster implementation of dvars + fixed eval bugs + preliminary impl…
323
cf73725a » Laurent Sansonetti
2010-01-17 experimental debugger
324 return rb_vm_run_under(klass, self, file, node, binding, true);
325 }
326
327 static VALUE
328 eval_string(VALUE self, VALUE klass, VALUE src, VALUE scope, const char *file,
329 const int line)
330 {
331 rb_vm_binding_t *binding = NULL;
332 if (scope != Qnil) {
333 if (!rb_obj_is_kind_of(scope, rb_cBinding)) {
334 rb_raise(rb_eTypeError, "wrong argument type %s (expected Binding)",
335 rb_obj_classname(scope));
336 }
337 binding = (rb_vm_binding_t *)DATA_PTR(scope);
338 }
339 return rb_vm_eval_string(self, klass, src, binding, file, line);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
340 }
341
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
342 static VALUE
343 specific_eval(int argc, VALUE *argv, VALUE klass, VALUE self)
344 {
90a3eb64 » Laurent Sansonetti
2009-08-25 implemented module_function as a scope + fixed a couple of eval-relat…
345 VALUE retval;
346
347 // XXX: not exception-safe
348 const long old_version = RCLASS_VERSION(klass);
349
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
350 if (rb_block_given_p()) {
351 if (argc > 0) {
90a3eb64 » Laurent Sansonetti
2009-08-25 implemented module_function as a scope + fixed a couple of eval-relat…
352 rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)",
353 argc);
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
354 }
90a3eb64 » Laurent Sansonetti
2009-08-25 implemented module_function as a scope + fixed a couple of eval-relat…
355 rb_vm_set_current_scope(klass, SCOPE_PUBLIC);
356 retval = rb_vm_yield_under(klass, self, 0, NULL);
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
357 }
358 else {
4fcff256 » Laurent Sansonetti
2009-03-23 implemented #instance_eval with a string + disabled the inline #eval …
359 const char *file = "(eval)";
360 int line = 1;
361
362 if (argc == 0) {
363 rb_raise(rb_eArgError, "block not supplied");
364 }
365 if (rb_safe_level() >= 4) {
366 StringValue(argv[0]);
367 }
368 else {
369 SafeStringValue(argv[0]);
370 }
371 if (argc > 3) {
372 const char *name = rb_id2name(rb_frame_callee());
373 rb_raise(rb_eArgError,
374 "wrong number of arguments: %s(src) or %s{..}",
375 name, name);
376 }
377 if (argc > 2) {
378 line = NUM2INT(argv[2]);
379 }
380 if (argc > 1) {
381 file = StringValuePtr(argv[1]);
382 }
90a3eb64 » Laurent Sansonetti
2009-08-25 implemented module_function as a scope + fixed a couple of eval-relat…
383 rb_vm_set_current_scope(klass, SCOPE_PUBLIC);
384 retval = eval_string(self, klass, argv[0], Qnil, file, line);
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
385 }
90a3eb64 » Laurent Sansonetti
2009-08-25 implemented module_function as a scope + fixed a couple of eval-relat…
386
387 RCLASS_SET_VERSION(klass, old_version);
388
389 return retval;
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
390 }
391
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
392 /*
393 * call-seq:
394 * eval(string [, binding [, filename [,lineno]]]) => obj
395 *
396 * Evaluates the Ruby expression(s) in <em>string</em>. If
397 * <em>binding</em> is given, the evaluation is performed in its
398 * context. The binding may be a <code>Binding</code> object or a
399 * <code>Proc</code> object. If the optional <em>filename</em> and
400 * <em>lineno</em> parameters are present, they will be used when
401 * reporting syntax errors.
402 *
403 * def getBinding(str)
404 * return binding
405 * end
406 * str = "hello"
407 * eval "str + ' Fred'" #=> "hello Fred"
408 * eval "str + ' Fred'", getBinding("bye") #=> "bye Fred"
409 */
410
411 VALUE
412 rb_f_eval(VALUE self, SEL sel, int argc, VALUE *argv)
413 {
414 VALUE src, scope, vfile, vline;
415 const char *file = "(eval)";
416 int line = 1;
417
418 rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
419 if (rb_safe_level() >= 4) {
420 StringValue(src);
421 if (!NIL_P(scope) && !OBJ_TAINTED(scope)) {
422 rb_raise(rb_eSecurityError,
423 "Insecure: can't modify trusted binding");
424 }
425 }
426 else {
427 SafeStringValue(src);
428 }
429 if (argc >= 3) {
430 StringValue(vfile);
431 }
432 if (argc >= 4) {
433 line = NUM2INT(vline);
434 }
435 if (!NIL_P(vfile)) {
436 file = RSTRING_PTR(vfile);
437 }
90a3eb64 » Laurent Sansonetti
2009-08-25 implemented module_function as a scope + fixed a couple of eval-relat…
438 VALUE klass;
439 switch (TYPE(self)) {
440 case T_CLASS:
441 case T_MODULE:
442 klass = self;
443 break;
444 default:
445 klass = 0;
446 break;
447 }
448 return eval_string(self, klass, src, scope, file, line);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
449 }
450
451 VALUE
452 rb_eval_string(const char *str)
453 {
084a52a0 » Laurent Sansonetti
2009-04-03 a faster implementation of dvars + fixed eval bugs + preliminary impl…
454 return eval_string(rb_vm_top_self(), 0, rb_str_new2(str), Qnil, "(eval)", 1);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
455 }
456
457 VALUE
458 rb_eval_cmd(VALUE cmd, VALUE arg, int level)
459 {
460 VALUE val = Qnil; /* OK */
461 volatile int safe = rb_safe_level();
462
463 if (OBJ_TAINTED(cmd)) {
464 level = 4;
465 }
466
467 if (TYPE(cmd) != T_STRING) {
468 rb_set_safe_level_force(level);
469 val = rb_funcall2(cmd, rb_intern("call"), RARRAY_LEN(arg),
470 RARRAY_PTR(arg));
471 rb_set_safe_level_force(safe);
472 return val;
473 }
474
4fcff256 » Laurent Sansonetti
2009-03-23 implemented #instance_eval with a string + disabled the inline #eval …
475 val = eval_string(0, rb_vm_top_self(), cmd, Qnil, 0, 0);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
476 rb_set_safe_level_force(safe);
477 return val;
478 }
479
480 /*
481 * call-seq:
482 * obj.instance_eval(string [, filename [, lineno]] ) => obj
483 * obj.instance_eval {| | block } => obj
484 *
485 * Evaluates a string containing Ruby source code, or the given block,
486 * within the context of the receiver (_obj_). In order to set the
487 * context, the variable +self+ is set to _obj_ while
488 * the code is executing, giving the code access to _obj_'s
489 * instance variables. In the version of <code>instance_eval</code>
490 * that takes a +String+, the optional second and third
491 * parameters supply a filename and starting line number that are used
492 * when reporting compilation errors.
493 *
494 * class KlassWithSecret
495 * def initialize
496 * @secret = 99
497 * end
498 * end
499 * k = KlassWithSecret.new
500 * k.instance_eval { @secret } #=> 99
501 */
502
503 static VALUE
504 rb_obj_instance_eval(VALUE self, SEL sel, int argc, VALUE *argv)
505 {
506 VALUE klass;
507
508 if (SPECIAL_CONST_P(self)) {
de8126fc » Laurent Sansonetti
2009-06-18 appropriately toggle the VM current class when #module_eval/#class_ev…
509 klass = 0;
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
510 }
511 else {
9590d405 » Laurent Sansonetti
2009-08-22 fixing and adding several missing ruby method features
512 klass = rb_singleton_class(self);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
513 }
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
514 return specific_eval(argc, argv, klass, self);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
515 }
516
517 /*
518 * call-seq:
519 * obj.instance_exec(arg...) {|var...| block } => obj
520 *
521 * Executes the given block within the context of the receiver
522 * (_obj_). In order to set the context, the variable +self+ is set
523 * to _obj_ while the code is executing, giving the code access to
524 * _obj_'s instance variables. Arguments are passed as block parameters.
525 *
526 * class KlassWithSecret
527 * def initialize
528 * @secret = 99
529 * end
530 * end
531 * k = KlassWithSecret.new
532 * k.instance_exec(5) {|x| @secret+x } #=> 104
533 */
534
535 static VALUE
536 rb_obj_instance_exec(VALUE self, SEL sel, int argc, VALUE *argv)
537 {
538 VALUE klass;
539
540 if (SPECIAL_CONST_P(self)) {
ae48953d » Laurent Sansonetti
2009-03-24 fixed singleton class definition, when passing a non-Array object as …
541 klass = 0;
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
542 }
543 else {
544 klass = rb_singleton_class(self);
545 }
12a1d67f » Laurent Sansonetti
2009-08-25 fixing bugs in instance_eval & friends + fixed some specs so that the…
546 if (!rb_block_given_p()) {
547 rb_raise(rb_eLocalJumpError, "no block given");
548 }
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
549 return rb_vm_yield_under(klass, self, argc, argv);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
550 }
551
552 /*
553 * call-seq:
554 * mod.class_eval(string [, filename [, lineno]]) => obj
555 * mod.module_eval {|| block } => obj
556 *
557 * Evaluates the string or block in the context of _mod_. This can
558 * be used to add methods to a class. <code>module_eval</code> returns
559 * the result of evaluating its argument. The optional _filename_
560 * and _lineno_ parameters set the text for error messages.
561 *
562 * class Thing
563 * end
564 * a = %q{def hello() "Hello there!" end}
565 * Thing.module_eval(a)
566 * puts Thing.new.hello()
567 * Thing.module_eval("invalid code", "dummy", 123)
568 *
569 * <em>produces:</em>
570 *
571 * Hello there!
572 * dummy:123:in `module_eval': undefined local variable
573 * or method `code' for Thing:Class
574 */
575
576 VALUE
577 rb_mod_module_eval(VALUE mod, SEL sel, int argc, VALUE *argv)
578 {
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
579 return specific_eval(argc, argv, mod, mod);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
580 }
581
582 /*
583 * call-seq:
584 * mod.module_exec(arg...) {|var...| block } => obj
585 * mod.class_exec(arg...) {|var...| block } => obj
586 *
587 * Evaluates the given block in the context of the class/module.
588 * The method defined in the block will belong to the receiver.
589 *
590 * class Thing
591 * end
592 * Thing.class_exec{
593 * def hello() "Hello there!" end
594 * }
595 * puts Thing.new.hello()
596 *
597 * <em>produces:</em>
598 *
599 * Hello there!
600 */
601
602 VALUE
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
603 rb_mod_module_exec(VALUE mod, SEL sel, int argc, VALUE *argv)
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
604 {
1827cf43 » Laurent Sansonetti
2009-03-21 experimental implementation of instance_eval/instance_exec/module_eva…
605 return rb_vm_yield_under(mod, mod, argc, argv);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
606 }
607
608 /*
609 * call-seq:
610 * throw(symbol [, obj])
611 *
612 * Transfers control to the end of the active +catch+ block
613 * waiting for _symbol_. Raises +NameError+ if there
614 * is no +catch+ block for the symbol. The optional second
615 * parameter supplies a return value for the +catch+ block,
616 * which otherwise defaults to +nil+. For examples, see
617 * <code>Kernel::catch</code>.
618 */
619
620 static VALUE
621 rb_f_throw(VALUE rcv, SEL sel, int argc, VALUE *argv)
622 {
755b4642 » Laurent Sansonetti
2009-04-06 implemented catch/throw
623 VALUE tag, value;
624
625 rb_scan_args(argc, argv, "11", &tag, &value);
626
627 return rb_vm_throw(tag, value);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
628 }
629
630 void
631 rb_throw(const char *tag, VALUE val)
632 {
633 VALUE argv[2];
634
635 argv[0] = ID2SYM(rb_intern(tag));
636 argv[1] = val;
637 rb_f_throw(Qnil, 0, 2, argv);
638 }
639
640 void
641 rb_throw_obj(VALUE tag, VALUE val)
642 {
643 VALUE argv[2];
644
645 argv[0] = tag;
646 argv[1] = val;
647 rb_f_throw(Qnil, 0, 2, argv);
648 }
649
650 /*
651 * call-seq:
652 * catch(symbol) {| | block } > obj
653 *
654 * +catch+ executes its block. If a +throw+ is
655 * executed, Ruby searches up its stack for a +catch+ block
656 * with a tag corresponding to the +throw+'s
657 * _symbol_. If found, that block is terminated, and
658 * +catch+ returns the value given to +throw+. If
659 * +throw+ is not called, the block terminates normally, and
660 * the value of +catch+ is the value of the last expression
661 * evaluated. +catch+ expressions may be nested, and the
662 * +throw+ call need not be in lexical scope.
663 *
664 * def routine(n)
665 * puts n
666 * throw :done if n <= 0
667 * routine(n-1)
668 * end
669 *
670 *
671 * catch(:done) { routine(3) }
672 *
673 * <em>produces:</em>
674 *
675 * 3
676 * 2
677 * 1
678 * 0
679 */
680
681 static VALUE
755b4642 » Laurent Sansonetti
2009-04-06 implemented catch/throw
682 rb_f_catch(VALUE rcv, SEL sel, VALUE tag)
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
683 {
755b4642 » Laurent Sansonetti
2009-04-06 implemented catch/throw
684 return rb_vm_catch(tag);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
685 }
686
687 /*
688 * call-seq:
689 * caller(start=1) => array
690 *
691 * Returns the current execution stack---an array containing strings in
692 * the form ``<em>file:line</em>'' or ``<em>file:line: in
693 * `method'</em>''. The optional _start_ parameter
694 * determines the number of initial stack entries to omit from the
695 * result.
696 *
697 * def a(skip)
698 * caller(skip)
699 * end
700 * def b(skip)
701 * a(skip)
702 * end
703 * def c(skip)
704 * b(skip)
705 * end
706 * c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10"]
707 * c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11"]
708 * c(2) #=> ["prog:8:in `c'", "prog:12"]
709 * c(3) #=> ["prog:13"]
710 */
711
712 static VALUE
713 rb_f_caller(VALUE klass, SEL sel, int argc, VALUE *argv)
714 {
715 VALUE level;
716 int lev;
717
718 rb_scan_args(argc, argv, "01", &level);
719
720 if (NIL_P(level)) {
721 lev = 1;
722 }
723 else {
724 lev = NUM2INT(level);
725 }
726
727 if (lev < 0) {
728 rb_raise(rb_eArgError, "negative level (%d)", lev);
729 }
730
731 return rb_vm_backtrace(lev);
732 }
733
734 void
735 rb_backtrace(void)
736 {
737 long i, count;
738 VALUE ary;
739
740 ary = rb_vm_backtrace(-1);
741 for (i = 0, count = RARRAY_LEN(ary); i < count; i++) {
742 printf("\tfrom %s\n", RSTRING_PTR(RARRAY_AT(ary, i)));
743 }
744 }
745
746 VALUE
747 rb_make_backtrace(void)
748 {
749 return rb_vm_backtrace(-1);
750 }
751
752 void
753 Init_vm_eval(void)
754 {
cbe906ec » Laurent Sansonetti
2009-08-26 introduce rb_objc_define_module_function() which mimics the ruby spec
755 rb_objc_define_module_function(rb_mKernel, "catch", rb_f_catch, 1);
756 rb_objc_define_module_function(rb_mKernel, "throw", rb_f_throw, -1);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
757
cbe906ec » Laurent Sansonetti
2009-08-26 introduce rb_objc_define_module_function() which mimics the ruby spec
758 rb_objc_define_module_function(rb_mKernel, "loop", rb_f_loop, 0);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
759
760 rb_objc_define_method(rb_cNSObject, "instance_eval", rb_obj_instance_eval, -1);
761 rb_objc_define_method(rb_cNSObject, "instance_exec", rb_obj_instance_exec, -1);
762 rb_objc_define_private_method(rb_cNSObject, "method_missing", rb_method_missing, -1);
763 rb_objc_define_method(rb_cNSObject, "__send__", rb_f_send, -1);
764
765 rb_objc_define_method(rb_cBasicObject, "instance_eval", rb_obj_instance_eval, -1);
766 rb_objc_define_method(rb_cBasicObject, "instance_exec", rb_obj_instance_exec, -1);
767 rb_objc_define_private_method(rb_cBasicObject, "method_missing", rb_method_missing, -1);
768 rb_objc_define_method(rb_cBasicObject, "__send__", rb_f_send, -1);
769
770 rb_objc_define_method(rb_mKernel, "send", rb_f_send, -1);
771 rb_objc_define_method(rb_mKernel, "public_send", rb_f_public_send, -1);
772
773 rb_objc_define_method(rb_cModule, "module_exec", rb_mod_module_exec, -1);
774 rb_objc_define_method(rb_cModule, "class_exec", rb_mod_module_exec, -1);
775
cbe906ec » Laurent Sansonetti
2009-08-26 introduce rb_objc_define_module_function() which mimics the ruby spec
776 rb_objc_define_module_function(rb_mKernel, "caller", rb_f_caller, -1);
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
777 }
778
Something went wrong with that request. Please try again.