Project

General

Profile

Feature #6251 ยป magic-comments-for-compile-options.diff

shugo (Shugo Maeda), 04/03/2012 06:20 PM

View differences:

include/ruby/intern.h
647 647
#define rb_argv rb_get_argv()
648 648
RUBY_EXTERN VALUE rb_argv0;
649 649
VALUE rb_get_argv(void);
650
void *rb_load_file(const char*);
650
void *rb_load_file(VALUE, const char*);
651 651
void ruby_script(const char*);
652 652
void ruby_prog_init(void);
653 653
void ruby_set_argv(int, char**);
internal.h
114 114
/* iseq.c */
115 115
VALUE rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE filepath, VALUE line, VALUE opt);
116 116
VALUE rb_iseq_clone(VALUE iseqval, VALUE newcbase);
117
void rb_compile_option_set(VALUE opt);
117 118

  
118 119
/* load.c */
119 120
VALUE rb_get_load_path(void);
......
144 145
/* parse.y */
145 146
VALUE rb_parser_get_yydebug(VALUE);
146 147
VALUE rb_parser_set_yydebug(VALUE, VALUE);
148
VALUE rb_parser_compile_option(VALUE);
147 149
int rb_is_const_name(VALUE name);
148 150
int rb_is_class_name(VALUE name);
149 151
int rb_is_global_name(VALUE name);
iseq.c
383 383
}
384 384

  
385 385
VALUE
386
rb_iseq_new_top(NODE *node, VALUE name, VALUE filename, VALUE filepath, VALUE parent)
386
rb_iseq_new_top(NODE *node, VALUE name, VALUE filename, VALUE filepath,
387
		VALUE parent, VALUE compile_option)
387 388
{
388
    return rb_iseq_new_with_opt(node, name, filename, filepath, INT2FIX(0), parent, ISEQ_TYPE_TOP,
389
				&COMPILE_OPTION_DEFAULT);
389
    rb_compile_option_t option;
390

  
391
    make_compile_option(&option, compile_option);
392
    return rb_iseq_new_with_opt(node, name, filename, filepath, INT2FIX(0),
393
				parent, ISEQ_TYPE_TOP, &option);
390 394
}
391 395

  
392 396
VALUE
......
625 629
    return opt;
626 630
}
627 631

  
632
void
633
rb_compile_option_set(VALUE opt)
634
{
635
    iseq_s_compile_option_set(Qnil, opt);
636
}
637

  
628 638
static VALUE
629 639
iseq_s_compile_option_get(VALUE self)
630 640
{
load.c
300 300
    state = EXEC_TAG();
301 301
    if (state == 0) {
302 302
	NODE *node;
303
	VALUE iseq;
303
	VALUE parser, iseq;
304 304

  
305 305
	th->mild_compile_error++;
306
	node = (NODE *)rb_load_file(RSTRING_PTR(fname));
306
	parser = rb_parser_new();
307
	node = (NODE *)rb_load_file(parser, RSTRING_PTR(fname));
307 308
	loaded = TRUE;
308
	iseq = rb_iseq_new_top(node, rb_str_new2("<top (required)>"), fname, rb_realpath_internal(Qnil, fname, 1), Qfalse);
309
	iseq = rb_iseq_new_top(node, rb_str_new2("<top (required)>"),
310
			       fname, rb_realpath_internal(Qnil, fname, 1),
311
			       Qfalse, rb_parser_compile_option(parser));
309 312
	th->mild_compile_error--;
310 313
	rb_iseq_eval(iseq);
311 314
    }
parse.y
238 238
    int parser_ruby_sourceline;	/* current line no. */
239 239
    rb_encoding *enc;
240 240
    rb_encoding *utf8;
241
    VALUE compile_option;
241 242

  
242 243
    int parser_yydebug;
243 244

  
......
6518 6519
    rb_compile_warning(ruby_sourcefile, ruby_sourceline, "invalid value for %s: %s", name, val);
6519 6520
}
6520 6521

  
6522
static void
6523
parser_set_compile_option(struct parser_params *parser,
6524
			  const char *name, const char *val)
6525
{
6526
    switch (*val) {
6527
      case 't': case 'T':
6528
	if (strcasecmp(val, "true") == 0) {
6529
	    rb_hash_aset(parser->compile_option, ID2SYM(rb_intern(name)),
6530
			 Qtrue);
6531
	    return;
6532
	}
6533
	break;
6534
      case 'f': case 'F':
6535
	if (strcasecmp(val, "false") == 0) {
6536
	    rb_hash_aset(parser->compile_option, ID2SYM(rb_intern(name)),
6537
			 Qfalse);
6538
	    return;
6539
	}
6540
	break;
6541
    }
6542
    rb_compile_warning(ruby_sourcefile, ruby_sourceline,
6543
		       "invalid value for %s: %s", name, val);
6544
}
6545

  
6546
static void
6547
parser_set_compile_option_num(struct parser_params *parser,
6548
			      const char *name, const char *val)
6549
{
6550
    rb_hash_aset(parser->compile_option, ID2SYM(rb_intern(name)),
6551
		 rb_cstr_to_inum(val, 10, 0));
6552
}
6553

  
6521 6554
struct magic_comment {
6522 6555
    const char *name;
6523 6556
    rb_magic_comment_setter_t func;
......
6528 6561
    {"coding", magic_comment_encoding, parser_encode_length},
6529 6562
    {"encoding", magic_comment_encoding, parser_encode_length},
6530 6563
    {"warn_indent", parser_set_token_info},
6564
    {"inline_const_cache", parser_set_compile_option},
6565
    {"peephole_optimization", parser_set_compile_option},
6566
    {"tailcall_optimization", parser_set_compile_option},
6567
    {"specialized_instruction", parser_set_compile_option},
6568
    {"operands_unification", parser_set_compile_option},
6569
    {"instructions_unification", parser_set_compile_option},
6570
    {"stack_caching", parser_set_compile_option},
6571
    {"trace_instruction", parser_set_compile_option},
6572
    {"debug_level", parser_set_compile_option_num},
6531 6573
};
6532 6574
#endif
6533 6575

  
......
10407 10449
    parser->parser_lvtbl = 0;
10408 10450
    parser->parser_ruby__end__seen = 0;
10409 10451
    parser->parser_ruby_sourcefile = 0;
10452
    parser->compile_option = rb_hash_new();
10410 10453
#ifndef RIPPER
10411 10454
    parser->is_ripper = 0;
10412 10455
    parser->parser_eval_tree_begin = 0;
......
10441 10484
    rb_gc_mark(p->parser_lex_input);
10442 10485
    rb_gc_mark(p->parser_lex_lastline);
10443 10486
    rb_gc_mark(p->parser_lex_nextline);
10487
    rb_gc_mark(p->compile_option);
10444 10488
#ifndef RIPPER
10445 10489
    rb_gc_mark((VALUE)p->parser_eval_tree_begin) ;
10446 10490
    rb_gc_mark((VALUE)p->parser_eval_tree) ;
......
10600 10644
    return flag;
10601 10645
}
10602 10646

  
10647
VALUE
10648
rb_parser_compile_option(VALUE self)
10649
{
10650
    struct parser_params *parser;
10651

  
10652
    TypedData_Get_Struct(self, struct parser_params, &parser_data_type,
10653
			 parser);
10654
    return parser->compile_option;
10655
}
10656

  
10603 10657
#ifdef YYMALLOC
10604 10658
#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
10605 10659
#define NEWHEAP() rb_node_newnode(NODE_ALLOCA, 0, (VALUE)parser->heap, 0)
ruby.c
1405 1405
	    tree = load_file(parser, opt->script_name, 1, opt);
1406 1406
	});
1407 1407
    }
1408
    rb_compile_option_set(rb_parser_compile_option(parser));
1408 1409
    rb_progname = opt->script_name;
1409 1410
    rb_vm_set_progname(rb_progname);
1410 1411
    if (opt->dump & DUMP_BIT(yydebug)) return Qtrue;
......
1677 1678
}
1678 1679

  
1679 1680
void *
1680
rb_load_file(const char *fname)
1681
rb_load_file(VALUE parser, const char *fname)
1681 1682
{
1682 1683
    struct cmdline_options opt;
1683 1684
    VALUE fname_v = rb_str_new_cstr(fname);
1684 1685

  
1685
    return load_file(rb_parser_new(), fname_v, 0, cmdline_options_init(&opt));
1686
    return load_file(parser, fname_v, 0, cmdline_options_init(&opt));
1686 1687
}
1687 1688

  
1688 1689
static void
vm_core.h
501 501
#pragma GCC visibility push(default)
502 502
#endif
503 503
VALUE rb_iseq_new(NODE*, VALUE, VALUE, VALUE, VALUE, enum iseq_type);
504
VALUE rb_iseq_new_top(NODE *node, VALUE name, VALUE filename, VALUE filepath, VALUE parent);
504
VALUE rb_iseq_new_top(NODE *node, VALUE name, VALUE filename, VALUE filepath, VALUE parent, VALUE compile_option);
505 505
VALUE rb_iseq_new_main(NODE *node, VALUE filename, VALUE filepath);
506 506
VALUE rb_iseq_new_with_bopt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE, enum iseq_type, VALUE);
507 507
VALUE rb_iseq_new_with_opt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE, enum iseq_type, const rb_compile_option_t*);