Skip to content

Commit

Permalink
* parse.y (rb_compose_ivar2): function to create a new ivar2
Browse files Browse the repository at this point in the history
  symbol from a symbol and a class.  back-ported from matzruby.

* parse.y (rb_decompose_ivar2): reverse function of
  rb_compose_ivar2(). 

* marshal.c (w_symbol): support class local instance variables.

* marshal.c (r_object0): ditto.

* compile.c (defined_expr): ditto.

* compile.c (iseq_compile_each): ditto.

* insns.def: add two new instructions: getinstancevariable2 and
  setinstancevariable2.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11630 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Feb 4, 2007
1 parent 71364da commit 72f2d2a
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 27 deletions.
19 changes: 19 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ Sun Feb 4 02:22:59 2007 Akinori MUSHA <[email protected]>
* lib/cgi.rb (CGI::QueryExtension::read_multipart): Remove a debug
print.

Sat Feb 3 23:51:58 2007 Yukihiro Matsumoto <[email protected]>

* parse.y (rb_compose_ivar2): function to create a new ivar2
symbol from a symbol and a class. back-ported from matzruby.

* parse.y (rb_decompose_ivar2): reverse function of
rb_compose_ivar2().

* marshal.c (w_symbol): support class local instance variables.

* marshal.c (r_object0): ditto.

* compile.c (defined_expr): ditto.

* compile.c (iseq_compile_each): ditto.

* insns.def: add two new instructions: getinstancevariable2 and
setinstancevariable2.

Sat Feb 3 23:21:13 2007 Yukihiro Matsumoto <[email protected]>

* insns.def (setclassvariable): remove unnecessary operand.
Expand Down
22 changes: 22 additions & 0 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,11 @@ defined_expr(yarv_iseq_t *iseq, LINK_ANCHOR *ret,
ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_IVAR),
ID2SYM(node->nd_vid), needstr);
return 1;
case NODE_IVAR2:
ADD_INSN(ret, nd_line(node), putnil);
ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_IVAR2),
ID2SYM(node->nd_vid), needstr);
return 1;

case NODE_GVAR:
ADD_INSN(ret, nd_line(node), putnil);
Expand Down Expand Up @@ -3327,6 +3332,15 @@ iseq_compile_each(yarv_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
ID2SYM(node->nd_vid));
break;
}
case NODE_IASGN2:{
COMPILE(ret, "lvalue", node->nd_value);
if (!poped) {
ADD_INSN(ret, nd_line(node), dup);
}
ADD_INSN1(ret, nd_line(node), setinstancevariable2,
ID2SYM(node->nd_vid));
break;
}
case NODE_CDECL:{
COMPILE(ret, "lvalue", node->nd_value);

Expand Down Expand Up @@ -3903,6 +3917,14 @@ iseq_compile_each(yarv_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
}
break;
}
case NODE_IVAR2:{
debugi("nd_vid", node->nd_vid);
if (!poped) {
ADD_INSN1(ret, nd_line(node), getinstancevariable2,
ID2SYM(node->nd_vid));
}
break;
}
case NODE_CONST:{
debugi("nd_vid", node->nd_vid);

Expand Down
2 changes: 2 additions & 0 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ gc_mark_children(VALUE ptr, int lev)
case NODE_DASGN:
case NODE_DASGN_CURR:
case NODE_IASGN:
case NODE_IASGN2:
case NODE_CVASGN:
case NODE_COLON3:
case NODE_OPT_N:
Expand Down Expand Up @@ -935,6 +936,7 @@ gc_mark_children(VALUE ptr, int lev)
case NODE_LVAR:
case NODE_DVAR:
case NODE_IVAR:
case NODE_IVAR2:
case NODE_CVAR:
case NODE_NTH_REF:
case NODE_BACK_REF:
Expand Down
38 changes: 38 additions & 0 deletions insns.def
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ getinstancevariable
val = rb_ivar_get(GET_SELF(), id);
}

/**
@c variable
@e get class local instance variable id of obj.
@j obj のクラスローカルインスタンス変数 id を得る。
*/
DEFINE_INSN
getinstancevariable2
(ID id)
()
(VALUE val)
{
/* need to cache composed id */
id = rb_compose_ivar2(id, eval_get_cvar_base(th, GET_ISEQ()));
val = rb_ivar_get(GET_SELF(), id);
}

/**
@c variable
@e set instance variable id of obj as val.
Expand All @@ -200,6 +216,22 @@ setinstancevariable
rb_ivar_set(GET_SELF(), id, val);
}

/**
@c variable
@e set class local instance variable id of obj as val.
@j obj のクラスローカルインスタンス変数を val にする。
*/
DEFINE_INSN
setinstancevariable2
(ID id)
(VALUE val)
()
{
/* need to cache composed id */
id = rb_compose_ivar2(id, eval_get_cvar_base(th, GET_ISEQ()));
rb_ivar_set(GET_SELF(), id, val);
}

/**
@c variable
@e get class variable id of klass as val.
Expand Down Expand Up @@ -901,6 +933,12 @@ defined
expr_type = "instance-variable";
}
break;
case DEFINED_IVAR2:
klass = get_cref(GET_ISEQ(), GET_LFP())->nd_clss;
if (rb_ivar_defined(GET_SELF(), rb_compose_ivar2(SYM2ID(obj), klass))) {
expr_type = "class local instance-variable";
}
break;
case DEFINED_GVAR:
if (rb_gvar_defined((struct global_entry *)(obj & ~1))) {
expr_type = "global-variable";
Expand Down
3 changes: 3 additions & 0 deletions intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ ID rb_id_attrset(ID);
void rb_gc_mark_parser(void);
int rb_is_const_id(ID);
int rb_is_instance_id(ID);
int rb_is_instance2_id(ID);
int rb_is_class_id(ID);
int rb_is_local_id(ID);
int rb_is_junk_id(ID);
Expand All @@ -409,6 +410,8 @@ void rb_backref_set(VALUE);
VALUE rb_lastline_get(void);
void rb_lastline_set(VALUE);
VALUE rb_sym_all_symbols(void);
ID rb_compose_ivar2(ID, VALUE);
ID rb_decompose_ivar2(ID, VALUE*);
/* process.c */
struct rb_exec_arg {
int argc;
Expand Down
4 changes: 4 additions & 0 deletions iseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,8 @@ node_name(int node)
return "NODE_GASGN";
case NODE_IASGN:
return "NODE_IASGN";
case NODE_IASGN2:
return "NODE_IASGN2";
case NODE_CDECL:
return "NODE_CDECL";
case NODE_CVASGN:
Expand Down Expand Up @@ -905,6 +907,8 @@ node_name(int node)
return "NODE_GVAR";
case NODE_IVAR:
return "NODE_IVAR";
case NODE_IVAR2:
return "NODE_IVAR2";
case NODE_CONST:
return "NODE_CONST";
case NODE_CVAR:
Expand Down
66 changes: 56 additions & 10 deletions marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ shortlen(long len, BDIGIT *ds)
#endif

#define MARSHAL_MAJOR 4
#define MARSHAL_MINOR 8
#define MARSHAL_MINOR 9

#define TYPE_NIL '0'
#define TYPE_TRUE 'T'
Expand All @@ -73,6 +73,7 @@ shortlen(long len, BDIGIT *ds)
#define TYPE_MODULE 'm'

#define TYPE_SYMBOL ':'
#define TYPE_SYMBOL2 ','
#define TYPE_SYMLINK ';'

#define TYPE_IVAR 'I'
Expand Down Expand Up @@ -304,16 +305,30 @@ w_float(double d, struct dump_arg *arg)
static void
w_symbol(ID id, struct dump_arg *arg)
{
const char *sym = rb_id2name(id);
const char *sym;
st_data_t num;

if (st_lookup(arg->symbols, id, &num)) {
w_byte(TYPE_SYMLINK, arg);
w_long((long)num, arg);
}
else {
w_byte(TYPE_SYMBOL, arg);
w_bytes(sym, strlen(sym), arg);
if (rb_is_instance2_id(id)) {
VALUE klass;
volatile VALUE path;

id = rb_decompose_ivar2(id, &klass);
path = class2path(klass);
w_byte(TYPE_SYMBOL2, arg);
sym = rb_id2name(id);
w_bytes(sym, strlen(sym), arg);
w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
}
else {
sym = rb_id2name(id);
w_byte(TYPE_SYMBOL, arg);
w_bytes(sym, strlen(sym), arg);
}
st_add_direct(arg->symbols, id, arg->symbols->num_entries);
}
}
Expand Down Expand Up @@ -360,12 +375,14 @@ w_extended(VALUE klass, struct dump_arg *arg, int check)
static void
w_class(char type, VALUE obj, struct dump_arg *arg, int check)
{
volatile VALUE p;
char *path;

VALUE klass = CLASS_OF(obj);
w_extended(klass, arg, check);
w_byte(type, arg);
path = RSTRING_PTR(class2path(rb_class_real(klass)));
p = class2path(rb_class_real(klass));
path = RSTRING_PTR(p);
w_unique(path, arg);
}

Expand Down Expand Up @@ -490,7 +507,7 @@ w_object(VALUE obj, struct dump_arg *arg, int limit)
}
w_byte(TYPE_CLASS, arg);
{
VALUE path = class2path(obj);
volatile VALUE path = class2path(obj);
w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
}
break;
Expand Down Expand Up @@ -738,7 +755,9 @@ struct load_arg {
int taint;
};

static VALUE r_entry(VALUE v, struct load_arg *arg);
static VALUE r_object(struct load_arg *arg);
static VALUE path2class(const char *path);

static int
r_byte(struct load_arg *arg)
Expand Down Expand Up @@ -855,10 +874,24 @@ r_symlink(struct load_arg *arg)
static ID
r_symreal(struct load_arg *arg)
{
ID id;
volatile VALUE s = r_bytes(arg);
ID id = rb_intern(RSTRING_PTR(s));

st_insert(arg->symbols, arg->symbols->num_entries, id);

return id;
}

static ID
r_symivar2(struct load_arg *arg)
{
volatile VALUE s = r_bytes(arg);
ID id = rb_intern(RSTRING_PTR(s));
VALUE klass;

id = rb_intern(RSTRING_PTR(s));
s = r_bytes(arg);
klass = r_entry(path2class(RSTRING_PTR(s)), arg);
id = rb_compose_ivar2(id, klass);
st_insert(arg->symbols, arg->symbols->num_entries, id);

return id;
Expand All @@ -867,10 +900,19 @@ r_symreal(struct load_arg *arg)
static ID
r_symbol(struct load_arg *arg)
{
if (r_byte(arg) == TYPE_SYMLINK) {
int type;

switch ((type = r_byte(arg))) {
case TYPE_SYMBOL:
return r_symreal(arg);
case TYPE_SYMBOL2:
return r_symivar2(arg);
case TYPE_SYMLINK:
return r_symlink(arg);
default:
rb_raise(rb_eArgError, "dump format error(0x%x)", type);
break;
}
return r_symreal(arg);
}

static const char*
Expand Down Expand Up @@ -1274,6 +1316,10 @@ r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
v = ID2SYM(r_symreal(arg));
break;

case TYPE_SYMBOL2:
v = ID2SYM(r_symivar2(arg));
break;

case TYPE_SYMLINK:
v = ID2SYM(r_symlink(arg));
break;
Expand Down
4 changes: 4 additions & 0 deletions node.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ enum node_type {
NODE_DASGN_CURR,
NODE_GASGN,
NODE_IASGN,
NODE_IASGN2,
NODE_CDECL,
NODE_CVASGN,
NODE_CVDECL,
Expand All @@ -70,6 +71,7 @@ enum node_type {
NODE_DVAR,
NODE_GVAR,
NODE_IVAR,
NODE_IVAR2,
NODE_CONST,
NODE_CVAR,
NODE_NTH_REF,
Expand Down Expand Up @@ -282,6 +284,7 @@ typedef struct RNode {
#define NEW_DASGN(v,val) NEW_NODE(NODE_DASGN,v,val,0)
#define NEW_DASGN_CURR(v,val) NEW_NODE(NODE_DASGN_CURR,v,val,0)
#define NEW_IASGN(v,val) NEW_NODE(NODE_IASGN,v,val,0)
#define NEW_IASGN2(v,val) NEW_NODE(NODE_IASGN2,v,val,0)
#define NEW_CDECL(v,val,path) NEW_NODE(NODE_CDECL,v,val,path)
#define NEW_CVASGN(v,val) NEW_NODE(NODE_CVASGN,v,val,0)
#define NEW_CVDECL(v,val) NEW_NODE(NODE_CVDECL,v,val,0)
Expand All @@ -294,6 +297,7 @@ typedef struct RNode {
#define NEW_LVAR(v) NEW_NODE(NODE_LVAR,v,0,local_cnt(v))
#define NEW_DVAR(v) NEW_NODE(NODE_DVAR,v,0,0)
#define NEW_IVAR(v) NEW_NODE(NODE_IVAR,v,0,0)
#define NEW_IVAR2(v) NEW_NODE(NODE_IVAR2,v,0,0)
#define NEW_CONST(v) NEW_NODE(NODE_CONST,v,0,0)
#define NEW_CVAR(v) NEW_NODE(NODE_CVAR,v,0,0)
#define NEW_NTH_REF(n) NEW_NODE(NODE_NTH_REF,0,n,local_cnt('~'))
Expand Down
Loading

0 comments on commit 72f2d2a

Please sign in to comment.