Branch: Tag:

2000-08-29

2000-08-29 14:46:36 by Martin Stjernholm <mast@lysator.liu.se>

Fixed describer used in describe_backtrace to handle cyclic
structures. Also fixed a reentrancy problem in it.

Rev: lib/master.pike.in:1.127

1:   /* -*- Pike -*-    * -  * $Id: master.pike.in,v 1.126 2000/08/27 18:29:26 mirar Exp $ +  * $Id: master.pike.in,v 1.127 2000/08/29 14:46:36 mast Exp $    *    * Master-file for Pike.    *
94:   #define UNDEFINED (([])[0])   #define error(X) throw( ({ (X), backtrace()/*[0..sizeof(backtrace())-2]*/ }) )    - string describe_backtrace(array trace); - object low_cast_to_object(string oname, string current_file); -  +    array(string) pike_include_path=({});   array(string) pike_module_path=({});   array(string) pike_program_path=({});
1399:    return master_read_file(f);   }    + class Describer + {    int clipped=0;    int canclip=0; -  +  mapping(mixed:int|string) ident = ([]); +  int identcount = 0;    - // FIXME - string stupid_describe(mixed m, int maxlen) +  void identify_parts (mixed stuff)    { -  string typ; +  if (arrayp (stuff)) { +  if (!ident[stuff]++) +  foreach (stuff, mixed elem) +  identify_parts (elem); +  } +  else if (multisetp (stuff)) { +  if (!ident[stuff]++) +  foreach (indices (stuff), mixed elem) +  identify_parts (elem); +  } +  else if (mappingp (stuff)) { +  if (!ident[stuff]++) +  foreach (indices (stuff), mixed elem) +  identify_parts (elem), identify_parts (stuff[elem]); +  } +  else if (objectp (stuff)) +  ident[stuff]++; +  } +  +  string describe (mixed m, int maxlen) +  { +  if (stringp (ident[m])) return ident[m]; +  else if (intp (ident[m]) && ident[m] > 1) +  ident[m] = "@" + identcount++; +  +  string res, typ;    if (catch (typ=sprintf("%t",m)))    typ = "object"; // Object with a broken _sprintf(), probably.    switch(typ)
1433:    }       case "array": -  if(!sizeof(m)) return "({})"; +  if(!sizeof(m)) res = "({})"; +  else {    if(maxlen<5)    {    clipped++; -  return "array["+sizeof(m)+"]"; +  res = "array["+sizeof(m)+"]";    } -  +  else {    canclip++; -  return "({" + stupid_describe_comma_list(m,maxlen-2) +"})"; +  res = "({" + describe_comma_list(m,maxlen-2) +"})"; +  } +  } +  break;       case "mapping": -  if(!sizeof(m)) return "([])"; -  return "mapping["+sizeof(m)+"]"; +  if(!sizeof(m)) res = "([])"; +  else res = "mapping["+sizeof(m)+"]"; +  break;       case "multiset": -  if(!sizeof(m)) return "(<>)"; -  return "multiset["+sizeof(m)+"]"; +  if(!sizeof(m)) res = "(<>)"; +  else res = "multiset["+sizeof(m)+"]"; +  break;       case "function":    if(string tmp=describe_program(m)) return tmp; -  if(object o=function_object(m)) -  return (describe_object(o)||"")+"->"+function_name(m); +  if(object o=function_object(m)) { +  string s = describe_object(o); +  if (s && s != "object") return s+"->"+function_name(m); +  else return function_name(m); +  }    else {    string tmp;    if (catch (tmp = function_name(m)))
1468:       default:    if (objectp(m)) -  if(string tmp=describe_object(m)) return tmp; +  if(string tmp=describe_object(m)) { +  res = tmp; +  break; +  }    return typ;    } -  +  +  if (stringp (ident[m])) +  return ident[m] + "=" + res; +  return res;    }    - string stupid_describe_comma_list(array x, int maxlen) +  string describe_comma_list(array x, int maxlen)   {    string ret="";   
1496:    {    clipped=0;    canclip=0; -  z[e]=stupid_describe(x[e],len); +  z[e]=describe(x[e],len);    isclipped[e]=clipped;    clippable[e]=canclip;    }
1568:       return ret;   } + }      string describe_object(object o)   {
1611:    string ret;    int backtrace_len=((int)getenv("PIKE_BACKTRACE_LEN")) || bt_max_string_len;    -  +     if(!linewidth)    {    linewidth=99999;
1639:    {    ret+="No backtrace.\n";    }else{ +  Describer desc = Describer(); +  desc->identify_parts (trace); +     for(e = sizeof(trace)-1; e>=0; e--)    {    mixed tmp;
1697:    data ="unknown function";       data+="("+ -  stupid_describe_comma_list(tmp[3..], backtrace_len)+ +  desc->describe_comma_list(tmp[3..], backtrace_len)+    ")";       if(sizeof(pos)+sizeof(data) < linewidth-4)