static hlt_string __exception_render(const hlt_exception* e, hlt_execution_context* ctx) { hlt_exception* excpt = 0; if ( ! e ) return hlt_string_from_asciiz("(Null)", &excpt, ctx); hlt_string s = hlt_string_from_asciiz(e->type->name, &excpt, ctx); if ( e->arg ) { hlt_string arg = hlt_object_to_string(e->type->argtype, e->arg, 0, &excpt, ctx); s = hlt_string_concat(s, hlt_string_from_asciiz(" with argument '", &excpt, ctx), &excpt, ctx); s = hlt_string_concat(s, arg, &excpt, ctx); s = hlt_string_concat(s, hlt_string_from_asciiz("'", &excpt, ctx), &excpt, ctx); } if ( e->vid != HLT_VID_MAIN ) { char buffer[1024]; snprintf(buffer, sizeof(buffer), " in virtual thread %" PRId64, e->vid); s = hlt_string_concat(s, hlt_string_from_asciiz(buffer, &excpt, ctx), &excpt, ctx); } if ( e->location ) { char buffer[1024]; snprintf(buffer, sizeof(buffer), " (from %s)", e->location); s = hlt_string_concat(s, hlt_string_from_asciiz(buffer, &excpt, ctx), &excpt, ctx); } return s; }
static hlt_string _tuple_to_string(const hlt_type_info* type, void* obj, __hlt_pointer_stack* seen, hlt_exception** excpt, hlt_execution_context* ctx) { void* tuple = obj; hlt_string s = hlt_string_from_asciiz("(", excpt, ctx); for ( int i = 0; i < hlt_tuple_length(type, excpt, ctx); i++ ) { void* ev = hlt_tuple_get(type, tuple, i, excpt, ctx); hlt_tuple_element et = hlt_tuple_get_type(type, i, excpt, ctx); if ( i > 0 ) s = hlt_string_concat_asciiz(s, ", ", excpt, ctx); if ( et.name && *et.name ) { hlt_string n = hlt_string_from_asciiz(et.name, excpt, ctx); s = hlt_string_concat(s, n, excpt, ctx); s = hlt_string_concat_asciiz(s, "=", excpt, ctx); } hlt_string es = _object_to_string(et.type, ev, seen, excpt, ctx); s = hlt_string_concat(s, es, excpt, ctx); } s = hlt_string_concat_asciiz(s, ")", excpt, ctx); return s; }
hlt_string hlt_regexp_to_string(const hlt_type_info* type, const void* obj, int32_t options, __hlt_pointer_stack* seen, hlt_exception** excpt, hlt_execution_context* ctx) { const hlt_regexp* re = *((const hlt_regexp**)obj); if ( ! re ) return hlt_string_from_asciiz("(Null)", excpt, ctx); if ( ! re->num ) return hlt_string_from_asciiz("<no pattern>", excpt, ctx); if ( re->num == 1 ) return re->patterns[0]; hlt_string s = hlt_string_from_asciiz("", excpt, ctx); hlt_string pipe = hlt_string_from_asciiz(" | ", excpt, ctx);; hlt_string slash = hlt_string_from_asciiz("/", excpt, ctx);; for ( int32_t idx = 0; idx < re->num; idx++ ) { if ( idx > 0 ) s = hlt_string_concat(s, pipe, excpt, ctx); s = hlt_string_concat(s, slash, excpt, ctx); s = hlt_string_concat(s, re->patterns[idx], excpt, ctx); s = hlt_string_concat(s, slash, excpt, ctx); } return s; }
static hlt_string _map_to_string(const hlt_type_info* type, void* obj, __hlt_pointer_stack* seen, hlt_exception** excpt, hlt_execution_context* ctx) { hlt_map* map = *(hlt_map **)obj; const hlt_type_info* ktype = hlt_map_key_type(type, excpt, ctx); const hlt_type_info* vtype = hlt_map_value_type(type, excpt, ctx); hlt_iterator_map i = hlt_map_begin(map, excpt, ctx); hlt_iterator_map end = hlt_map_end(excpt, ctx); hlt_string sep = hlt_string_from_asciiz(": ", excpt, ctx); hlt_string s = hlt_string_from_asciiz("{", excpt, ctx); int first = 1; while ( ! hlt_iterator_map_eq(i, end, excpt, ctx) ) { void* kp = hlt_iterator_map_deref_key(i, excpt, ctx); void* vp = hlt_iterator_map_deref_value(i, excpt, ctx); hlt_string ks = _object_to_string(ktype, kp, seen, excpt, ctx); hlt_string vs = _object_to_string(vtype, vp, seen, excpt, ctx); if ( ! first ) s = hlt_string_concat_asciiz(s, ", ", excpt, ctx); s = hlt_string_concat(s, ks, excpt, ctx); s = hlt_string_concat(s, sep, excpt, ctx); s = hlt_string_concat(s, vs, excpt, ctx); i = hlt_iterator_map_incr(i, excpt, ctx); first = 0; } s = hlt_string_concat_asciiz(s, "}", excpt, ctx); return s; }
static hlt_string _list_to_string(const hlt_type_info* type, void* obj, __hlt_pointer_stack* seen, hlt_exception** excpt, hlt_execution_context* ctx) { hlt_list* list = *(hlt_list **)obj; const hlt_type_info* etype = hlt_list_element_type(type, excpt, ctx); hlt_iterator_list i = hlt_list_begin(list, excpt, ctx); hlt_iterator_list end = hlt_list_end(list, excpt, ctx); hlt_string s = hlt_string_from_asciiz("[", excpt, ctx); int first = 1; while ( ! hlt_iterator_list_eq(i, end, excpt, ctx) ) { void* ep = hlt_iterator_list_deref(i, excpt, ctx); hlt_string es = _object_to_string(etype, ep, seen, excpt, ctx); if ( ! first ) s = hlt_string_concat_asciiz(s, ", ", excpt, ctx); s = hlt_string_concat(s, es, excpt, ctx); i = hlt_iterator_list_incr(i, excpt, ctx); first = 0; } s = hlt_string_concat_asciiz(s, "]", excpt, ctx); return s; }
static hlt_string _embedded_to_string(const hlt_type_info* type, void* obj, __hlt_pointer_stack* seen, hlt_exception** excpt, hlt_execution_context* ctx) { hlt_string s = hlt_object_to_string(type, obj, 0, excpt, ctx); hlt_string prefix = hlt_string_from_asciiz("object(", excpt, ctx); s = hlt_string_concat(prefix, s, excpt, ctx); s = hlt_string_concat_asciiz(s, ")", excpt, ctx); return s; }
static hlt_string _string_to_string(const hlt_type_info* type, void* obj, __hlt_pointer_stack* seen, hlt_exception** excpt, hlt_execution_context* ctx) { #if 1 hlt_string s = *(hlt_string *)obj; #else // TODO: We should differentiate if we are just printing out a string, in // which case we don't want quotes. Or if we are nested inside another // objects, in which we do. hlt_string prefix = hlt_string_from_asciiz("\"", excpt, ctx); s = hlt_string_concat(prefix, s, excpt, ctx); s = hlt_string_concat_asciiz(s, "\"", excpt, ctx); #endif return s; }
hlt_string hilti_fmt(hlt_string fmt, const hlt_type_info* type, void* tuple, hlt_exception** excpt, hlt_execution_context* ctx) // { assert(type->type == HLT_TYPE_TUPLE); int8_t buffer[BufferSize]; hlt_string result; const int8_t* p = fmt->bytes; hlt_string dst = 0; int64_t bpos = 0; int type_param = 0; hlt_string_size i = 0; while ( i < fmt->len ) { if ( p[i] == '%' ) { if ( ++i == fmt->len ) { hlt_set_exception(excpt, &hlt_exception_wrong_arguments, 0, ctx); return 0; } // Control character. if ( p[i] != '%' ) { _do_fmt(fmt, type, tuple, &type_param, &i, buffer, &bpos, &dst, excpt, ctx); if ( *excpt ) return 0; continue; } // Fall-through with quoted '%'. } _add_char(p[i++], buffer, &bpos, &dst, excpt, ctx); if ( *excpt ) return 0; } result = hlt_string_from_data(buffer, bpos, excpt, ctx); if ( *excpt ) return 0; if ( dst ) result = hlt_string_concat(dst, result, excpt, ctx); return result; }
static hlt_string _unit_to_string(const hlt_type_info* type, void* obj, __hlt_pointer_stack* seen, hlt_exception** excpt, hlt_execution_context* ctx) { void* unit = *(void**)obj; if ( ! unit ) return hlt_string_from_asciiz("(Null)", excpt, ctx); binpac_unit_cookie cookie = 0; binpac_unit_item item; int first = 1; hlt_string v; hlt_string s = hlt_string_from_asciiz("<", excpt, ctx); while ( (cookie = binpac_unit_iterate(&item, type, unit, 0, cookie, excpt, ctx)) ) { if ( ! item.value ) // Don't print fields that aren't set. continue; if ( ! first ) s = hlt_string_concat_asciiz(s, ", ", excpt, ctx); if ( item.name ) s = hlt_string_concat_asciiz(s, item.name, excpt, ctx); else s = hlt_string_concat_asciiz(s, "<\?\?\?>", excpt, ctx); s = hlt_string_concat_asciiz(s, "=", excpt, ctx); v = item.value ? _object_to_string(item.type, item.value, seen, excpt, ctx) : hlt_string_from_asciiz("(not set)", excpt, ctx); s = hlt_string_concat(s, v, excpt, ctx); first = 0; } s = hlt_string_concat_asciiz(s, ">", excpt, ctx); return s; }
static void _add_char(int8_t c, int8_t* buffer, hlt_string_size* bpos, hlt_string* dst, hlt_exception** excpt, hlt_execution_context* ctx) { // Adds one character 'c' to the string we're building. If there's space // in the buffer, it's added there and 'dst' is returned unmodified. If // not, the current buffer is added to 'dst', the buffer is cleared and // the character is inserted into it. if ( *bpos < BufferSize - 1 ) // We have space. buffer[(*bpos)++] = c; else { // Copy buffer to destination string and start over. hlt_string new_dst = hlt_string_from_data(buffer, *bpos, excpt, ctx); if ( *excpt ) return; *dst = hlt_string_concat(*dst, new_dst, excpt, ctx); buffer[0] = c; *bpos = 1; } }