@@ -285,19 +285,27 @@ VAL MKSTR(VM* vm, const char* str) {
285 Closure* cl = allocate(sizeof(Closure) + // Type) + sizeof(char*) +
285 Closure* cl = allocate(sizeof(Closure) + // Type) + sizeof(char*) +
286 sizeof(char)*len, 0);
286 sizeof(char)*len, 0);
287 SETTY(cl, CT_STRING);
287 SETTY(cl, CT_STRING);
288− cl -> info.str = (char*)cl + sizeof(Closure);
288+ cl->info.str.str = (char*)cl + sizeof(Closure);
289 if (str == NULL) {
289 if (str == NULL) {
290− cl->info.str = NULL;
290+ cl->info.str.str = NULL;
292− strcpy(cl -> info.str, str);
292+ strcpy(cl->info.str.str, str);
294+ cl->info.str.len = len > 0 ? len : 0;
294 return cl;
295 return cl;
297char* GETSTROFF(VAL stroff) {
298char* GETSTROFF(VAL stroff) {
298 // Assume STROFF
299 // Assume STROFF
299 StrOffset* root = stroff->info.str_offset;
300 StrOffset* root = stroff->info.str_offset;
300− return (root->str->info.str + root->offset);
301+ return (root->str->info.str.str + root->offset);
304+size_t GETSTROFFLEN(VAL stroff) {
306+ // we're working in char* here so no worries about utf8 char length
307+ StrOffset* root = stroff->info.str_offset;
308+ return (root->str->info.str.len - root->offset);
303VAL MKCDATA(VM* vm, CHeapItem * item) {
311VAL MKCDATA(VM* vm, CHeapItem * item) {
@@ -342,12 +350,26 @@ VAL MKFLOATc(VM* vm, double val) {
344VAL MKSTRc(VM* vm, char* str) {
352VAL MKSTRc(VM* vm, char* str) {
353+ int len = strlen(str);
355+ Closure* cl = allocate(sizeof(Closure) + // Type) + sizeof(char*) +
356+ sizeof(char)*len+1, 1);
357+ SETTY(cl, CT_STRING);
358+ cl -> info.str.str = (char*)cl + sizeof(Closure);
360+ strcpy(cl->info.str.str, str);
361+ cl->info.str.len = len;
365+VAL MKSTRclen(VM* vm, char* str, int len) {
345 Closure* cl = allocate(sizeof(Closure) + // Type) + sizeof(char*) +
366 Closure* cl = allocate(sizeof(Closure) + // Type) + sizeof(char*) +
346− sizeof(char)*strlen(str)+1, 1);
367+ sizeof(char)*len+1, 1);
347 SETTY(cl, CT_STRING);
368 SETTY(cl, CT_STRING);
348− cl -> info.str = (char*)cl + sizeof(Closure);
369+ cl -> info.str.str = (char*)cl + sizeof(Closure);
350− strcpy(cl -> info.str, str);
371+ strcpy(cl->info.str.str, str);
372+ cl->info.str.len = len;
351 return cl;
373 return cl;
@@ -397,6 +419,13 @@ VAL MKB64(VM* vm, uint64_t bits64) {
397 return cl;
419 return cl;
422+void idris_trace(VM* vm, const char* func, int line) {
423+ printf("At %s:%d\n", func, line);
400void dumpStack(VM* vm) {
429void dumpStack(VM* vm) {
401 int i = 0;
430 int i = 0;
402 VAL* root;
431 VAL* root;
@@ -404,7 +433,8 @@ void dumpStack(VM* vm) {
404 for (root = vm->valstack; root < vm->valstack_top; ++root, ++i) {
433 for (root = vm->valstack; root < vm->valstack_top; ++root, ++i) {
405 printf("%d: ", i);
434 printf("%d: ", i);
406 dumpVal(*root);
435 dumpVal(*root);
407− if (*root >= (VAL)(vm->heap.heap) && *root < (VAL)(vm->heap.end)) { printf("OK"); }
436+ if (*root >= (VAL)(vm->heap.heap) && *root < (VAL)(vm->heap.end)) { printf(" OK"); }
437+ if (root == vm->valstack_base) { printf(" <--- base"); }
408 printf("\n");
438 printf("\n");
410 printf("RET: ");
440 printf("RET: ");
@@ -428,7 +458,7 @@ void dumpVal(VAL v) {
428 printf("] ");
458 printf("] ");
430 case CT_STRING:
460 case CT_STRING:
431− printf("STR[%s]", v->info.str);
461+ printf("STR[%s]", v->info.str.str);
433 case CT_FWD:
463 case CT_FWD:
434 printf("CT_FWD ");
464 printf("CT_FWD ");
@@ -490,8 +520,9 @@ VAL idris_castIntStr(VM* vm, VAL i) {
490 int x = (int) GETINT(i);
520 int x = (int) GETINT(i);
491 Closure* cl = allocate(sizeof(Closure) + sizeof(char)*16, 0);
521 Closure* cl = allocate(sizeof(Closure) + sizeof(char)*16, 0);
492 SETTY(cl, CT_STRING);
522 SETTY(cl, CT_STRING);
493− cl -> info.str = (char*)cl + sizeof(Closure);
523+ cl->info.str.str = (char*)cl + sizeof(Closure);
494− sprintf(cl -> info.str, "%d", x);
524+ sprintf(cl->info.str.str, "%d", x);
525+ cl->info.str.len = strlen(cl->info.str.str);
495 return cl;
526 return cl;
@@ -503,32 +534,33 @@ VAL idris_castBitsStr(VM* vm, VAL i) {
503 case CT_BITS8:
534 case CT_BITS8:
504 // max length 8 bit unsigned int str 3 chars (256)
535 // max length 8 bit unsigned int str 3 chars (256)
505 cl = allocate(sizeof(Closure) + sizeof(char)*4, 0);
536 cl = allocate(sizeof(Closure) + sizeof(char)*4, 0);
506− cl->info.str = (char*)cl + sizeof(Closure);
537+ cl->info.str.str = (char*)cl + sizeof(Closure);
507− sprintf(cl->info.str, "%" PRIu8, (uint8_t)i->info.bits8);
538+ sprintf(cl->info.str.str, "%" PRIu8, (uint8_t)i->info.bits8);
509 case CT_BITS16:
540 case CT_BITS16:
510 // max length 16 bit unsigned int str 5 chars (65,535)
541 // max length 16 bit unsigned int str 5 chars (65,535)
511 cl = allocate(sizeof(Closure) + sizeof(char)*6, 0);
542 cl = allocate(sizeof(Closure) + sizeof(char)*6, 0);
512− cl->info.str = (char*)cl + sizeof(Closure);
543+ cl->info.str.str = (char*)cl + sizeof(Closure);
513− sprintf(cl->info.str, "%" PRIu16, (uint16_t)i->info.bits16);
544+ sprintf(cl->info.str.str, "%" PRIu16, (uint16_t)i->info.bits16);
515 case CT_BITS32:
546 case CT_BITS32:
516 // max length 32 bit unsigned int str 10 chars (4,294,967,295)
547 // max length 32 bit unsigned int str 10 chars (4,294,967,295)
517 cl = allocate(sizeof(Closure) + sizeof(char)*11, 0);
548 cl = allocate(sizeof(Closure) + sizeof(char)*11, 0);
518− cl->info.str = (char*)cl + sizeof(Closure);
549+ cl->info.str.str = (char*)cl + sizeof(Closure);
519− sprintf(cl->info.str, "%" PRIu32, (uint32_t)i->info.bits32);
550+ sprintf(cl->info.str.str, "%" PRIu32, (uint32_t)i->info.bits32);
521 case CT_BITS64:
552 case CT_BITS64:
522 // max length 64 bit unsigned int str 20 chars (18,446,744,073,709,551,615)
553 // max length 64 bit unsigned int str 20 chars (18,446,744,073,709,551,615)
523 cl = allocate(sizeof(Closure) + sizeof(char)*21, 0);
554 cl = allocate(sizeof(Closure) + sizeof(char)*21, 0);
524− cl->info.str = (char*)cl + sizeof(Closure);
555+ cl->info.str.str = (char*)cl + sizeof(Closure);
525− sprintf(cl->info.str, "%" PRIu64, (uint64_t)i->info.bits64);
556+ sprintf(cl->info.str.str, "%" PRIu64, (uint64_t)i->info.bits64);
528 fprintf(stderr, "Fatal Error: ClosureType %d, not an integer type", ty);
559 fprintf(stderr, "Fatal Error: ClosureType %d, not an integer type", ty);
529 exit(EXIT_FAILURE);
560 exit(EXIT_FAILURE);
563+ cl->info.str.len = strlen(cl->info.str.str);
532 SETTY(cl, CT_STRING);
564 SETTY(cl, CT_STRING);
533 return cl;
565 return cl;
@@ -545,8 +577,10 @@ VAL idris_castStrInt(VM* vm, VAL i) {
545VAL idris_castFloatStr(VM* vm, VAL i) {
577VAL idris_castFloatStr(VM* vm, VAL i) {
546 Closure* cl = allocate(sizeof(Closure) + sizeof(char)*32, 0);
578 Closure* cl = allocate(sizeof(Closure) + sizeof(char)*32, 0);
547 SETTY(cl, CT_STRING);
579 SETTY(cl, CT_STRING);
548− cl -> info.str = (char*)cl + sizeof(Closure);
580+ cl->info.str.str = (char*)cl + sizeof(Closure);
549− snprintf(cl -> info.str, 32, "%.16g", GETFLOAT(i));
581+ snprintf(cl->info.str.str, 32, "%.16g", GETFLOAT(i));
583+ cl->info.str.len = strlen(cl->info.str.str);
550 return cl;
584 return cl;
@@ -559,11 +593,13 @@ VAL idris_concat(VM* vm, VAL l, VAL r) {
559 char *ls = GETSTR(l);
593 char *ls = GETSTR(l);
560 // dumpVal(l);
594 // dumpVal(l);
561 // printf("\n");
595 // printf("\n");
562− Closure* cl = allocate(sizeof(Closure) + strlen(ls) + strlen(rs) + 1, 0);
596+ Closure* cl = allocate(sizeof(Closure) + GETSTRLEN(l) + GETSTRLEN(r) + 1, 0);
563 SETTY(cl, CT_STRING);
597 SETTY(cl, CT_STRING);
564− cl -> info.str = (char*)cl + sizeof(Closure);
598+ cl->info.str.str = (char*)cl + sizeof(Closure);
565− strcpy(cl -> info.str, ls);
599+ strcpy(cl->info.str.str, ls);
566− strcat(cl -> info.str, rs);
600+ strcat(cl->info.str.str, rs);
602+ cl->info.str.len = GETSTRLEN(l) + GETSTRLEN(r);
567 return cl;
603 return cl;
@@ -670,21 +706,26 @@ VAL idris_strTail(VM* vm, VAL str) {
670VAL idris_strCons(VM* vm, VAL x, VAL xs) {
706VAL idris_strCons(VM* vm, VAL x, VAL xs) {
671 char *xstr = GETSTR(xs);
707 char *xstr = GETSTR(xs);
672 int xval = GETINT(x);
708 int xval = GETINT(x);
709+ int xlen = GETSTRLEN(xs);
673 if ((xval & 0x80) == 0) { // ASCII char
711 if ((xval & 0x80) == 0) { // ASCII char
674 Closure* cl = allocate(sizeof(Closure) +
712 Closure* cl = allocate(sizeof(Closure) +
675− strlen(xstr) + 2, 0);
713+ xlen + 2, 0);
676 SETTY(cl, CT_STRING);
714 SETTY(cl, CT_STRING);
677− cl -> info.str = (char*)cl + sizeof(Closure);
715+ cl->info.str.str = (char*)cl + sizeof(Closure);
678− cl -> info.str[0] = (char)(GETINT(x));
716+ cl->info.str.str[0] = (char)(GETINT(x));
679− strcpy(cl -> info.str+1, xstr);
717+ strcpy(cl->info.str.str+1, xstr);
718+ cl->info.str.len = xlen + 1;
680 return cl;
719 return cl;
682 char *init = idris_utf8_fromChar(xval);
721 char *init = idris_utf8_fromChar(xval);
722+ int newlen = strlen(init) + xlen;
683− Closure* cl = allocate(sizeof(Closure) + strlen(init) + strlen(xstr) + 1, 0);
723+ Closure* cl = allocate(sizeof(Closure) + newlen + 1, 0);
684 SETTY(cl, CT_STRING);
724 SETTY(cl, CT_STRING);
685− cl -> info.str = (char*)cl + sizeof(Closure);
725+ cl->info.str.str = (char*)cl + sizeof(Closure);
686− strcpy(cl -> info.str, init);
726+ strcpy(cl->info.str.str, init);
687− strcat(cl -> info.str, xstr);
727+ strcat(cl->info.str.str, xstr);
728+ cl->info.str.len = newlen;
688 free(init);
729 free(init);
689 return cl;
730 return cl;
@@ -701,7 +742,7 @@ VAL idris_substr(VM* vm, VAL offset, VAL length, VAL str) {
701 char* str_val = GETSTR(str);
742 char* str_val = GETSTR(str);
703 // If the substring is a suffix, use idris_strShift to avoid reallocating
744 // If the substring is a suffix, use idris_strShift to avoid reallocating
704− if (offset_val + length_val >= strlen(str_val)) {
745+ if (offset_val + length_val >= GETSTRLEN(str)) {
705 return idris_strShift(vm, str, offset_val);
746 return idris_strShift(vm, str, offset_val);
@@ -709,20 +750,23 @@ VAL idris_substr(VM* vm, VAL offset, VAL length, VAL str) {
709 char *end = idris_utf8_advance(start, length_val);
750 char *end = idris_utf8_advance(start, length_val);
710 Closure* newstr = allocate(sizeof(Closure) + (end - start) +1, 0);
751 Closure* newstr = allocate(sizeof(Closure) + (end - start) +1, 0);
711 SETTY(newstr, CT_STRING);
752 SETTY(newstr, CT_STRING);
712− newstr -> info.str = (char*)newstr + sizeof(Closure);
753+ newstr->info.str.str = (char*)newstr + sizeof(Closure);
713− memcpy(newstr -> info.str, start, end - start);
754+ memcpy(newstr->info.str.str, start, end - start);
714− *(newstr -> info.str + (end - start) + 1) = '\0';
755+ *(newstr->info.str.str + (end - start) + 1) = '\0';
756+ newstr->info.str.len = strlen(newstr->info.str.str);
715 return newstr;
757 return newstr;
719VAL idris_strRev(VM* vm, VAL str) {
761VAL idris_strRev(VM* vm, VAL str) {
720 char *xstr = GETSTR(str);
762 char *xstr = GETSTR(str);
763+ int xlen = GETSTRLEN(str);
721− Closure* cl = allocate(sizeof(Closure) +
765+ Closure* cl = allocate(sizeof(Closure) + xlen + 1, 0);
722− strlen(xstr) + 1, 0);
723 SETTY(cl, CT_STRING);
766 SETTY(cl, CT_STRING);
724− cl->info.str = (char*)cl + sizeof(Closure);
767+ cl->info.str.str = (char*)cl + sizeof(Closure);
768+ cl->info.str.len = xlen;
725− idris_utf8_rev(xstr, cl->info.str);
769+ idris_utf8_rev(xstr, cl->info.str.str);
726 return cl;
770 return cl;
@@ -832,7 +876,7 @@ VAL doCopyTo(VM* vm, VAL x) {
832 cl = MKFLOATc(vm, x->info.f);
876 cl = MKFLOATc(vm, x->info.f);
834 case CT_STRING:
878 case CT_STRING:
835− cl = MKSTRc(vm, x->info.str);
879+ cl = MKSTRc(vm, x->info.str.str);
837 case CT_BIGINT:
881 case CT_BIGINT:
838 cl = MKBIGMc(vm, x->info.ptr);
882 cl = MKBIGMc(vm, x->info.ptr);