Branch: Tag:

2019-07-31

2019-07-31 07:55:20 by Henrik Grubbström (Grubba) <grubba@grubba.org>

encode_value: Support a String.Buffer for debug output.

Giving a String.Buffer object as the third argument to
encode_value{,_canonic}() now causes debug level 1
(ie disassembly-style) to be written to the buffer.

Also makes the debug argument part of the public type.

2181:   {    ONERROR tmp;    struct encode_data d, *data; + #ifdef ENCODE_DEBUG    struct string_builder debug_buf; -  + #endif    int i;    data=&d;   
2193: Inside #if defined(ENCODE_DEBUG)
   * It's intentionally not part of the function    * prototype, to keep the argument position free for    * other uses in the future. */ -  BIT_VOID | BIT_INT, +  BIT_VOID | BIT_INT | BIT_OBJECT,   #endif    0);   
2207: Inside #if defined(ENCODE_DEBUG)
  #ifdef ENCODE_DEBUG    data->debug_buf = NULL;    data->debug_pos = 0; -  data->debug = args > 2 ? Pike_sp[2-args].u.integer : 0; -  data->depth = -2; -  if (data->debug) { +  data->debug = 0; +  if (args > 2) { +  if (TYPEOF(Pike_sp[2-args]) == PIKE_T_OBJECT) { +  struct object *sb = Pike_sp[2-args].u.object; +  if ((data->debug_buf = string_builder_from_string_buffer(sb))) { +  data->debug = 1; +  } +  } else { +  if ((data->debug = Pike_sp[2-args].u.integer)) {    init_string_builder(&debug_buf, 0);    data->debug_buf = &debug_buf;    } -  +  } +  } +  data->depth = -2;   #endif       if(args > 1 && TYPEOF(Pike_sp[1-args]) == T_OBJECT)
2263:    free_mapping(data->encoded);    free_array (data->delayed);    -  EDB(1, { + #ifdef ENCODE_DEBUG +  if (data->debug_buf == &debug_buf) {    write_and_reset_string_builder(2, data->debug_buf);    free_string_builder(data->debug_buf); -  }); +  } + #endif       pop_n_elems(args);    push_string(buffer_finish_pike_string(&data->buf));
2294:   {    ONERROR tmp;    struct encode_data d, *data; + #ifdef ENCODE_DEBUG +  struct string_builder debug_buf; + #endif    int i;    data=&d;   
2305: Inside #if defined(ENCODE_DEBUG)
   * It's intentionally not part of the function    * prototype, to keep the argument position free for    * other uses in the future. */ -  BIT_VOID | BIT_INT, +  BIT_VOID | BIT_INT | BIT_OBJECT,   #endif    0);   
2316:    SET_SVAL(data->counter, T_INT, NUMBER_NUMBER, integer, COUNTER_START);      #ifdef ENCODE_DEBUG -  data->debug = args > 2 ? Pike_sp[2-args].u.integer : 0; +  data->debug_buf = NULL; +  data->debug_pos = 0; +  data->debug = 0; +  if (args > 2) { +  if (TYPEOF(Pike_sp[2-args]) == PIKE_T_OBJECT) { +  struct object *sb = Pike_sp[2-args].u.object; +  if ((data->debug_buf = string_builder_from_string_buffer(sb))) { +  data->debug = 1; +  } +  } else { +  if ((data->debug = Pike_sp[2-args].u.integer)) { +  init_string_builder(&debug_buf, 0); +  data->debug_buf = &debug_buf; +  } +  } +  }    data->depth = -2;   #endif   
2334:    SET_ONERROR(tmp, free_encode_data, data);    addstr("\266ke0", 4);    +  EDB(1, { +  string_builder_append_disassembly(data->debug_buf, +  data->debug_pos, +  ((char *)data->buf.end) - +  (data->buf.length - data->debug_pos), +  data->buf.dst, +  ".format 0", +  NULL, +  "Encoding #0."); +  data->debug_pos = data->buf.length - +  (((char *)data->buf.end) - ((char *)data->buf.dst)); +  }); +     encode_value2(Pike_sp-args, data, 1);    -  for (i = 0; i < data->delayed->size; i++) +  for (i = 0; i < data->delayed->size; i++) { +  EDB(1, { +  string_builder_sprintf(data->debug_buf, +  "0x%016x # Delayed item #%d\n", +  data->debug_pos, i); +  });    encode_value2 (ITEM(data->delayed) + i, data, 2); -  +  }       UNSET_ONERROR(tmp);    -  +  EDB(1, { +  ENCODE_FLUSH(); +  }); +     if (data->codec) free_object (data->codec);    free_mapping(data->encoded);    free_array (data->delayed);    -  + #ifdef ENCODE_DEBUG +  if (data->debug_buf == &debug_buf) { +  write_and_reset_string_builder(2, data->debug_buf); +  free_string_builder(data->debug_buf); +  } + #endif +     pop_n_elems(args);    push_string(buffer_finish_pike_string(&data->buf));   }