diff --git a/TODO.FACTOR.txt b/TODO.FACTOR.txt index 47916b3e91..9733f8c6e5 100644 --- a/TODO.FACTOR.txt +++ b/TODO.FACTOR.txt @@ -33,6 +33,7 @@ - utf16 string boxing - slot compile problem - nulls at the end of utf16 strings +- x86 register decl + compiler/ffi: diff --git a/library/bootstrap/image.factor b/library/bootstrap/image.factor index b5337eb04f..7f0f21ff48 100644 --- a/library/bootstrap/image.factor +++ b/library/bootstrap/image.factor @@ -253,7 +253,7 @@ M: cons ' ( c -- tagged ) string-type >header emit dup str-length emit dup hashcode emit-fixnum - pack-string + "\0" cat2 pack-string align-here ; M: string ' ( string -- pointer ) diff --git a/native/factor.h b/native/factor.h index c880678c64..08c7259fab 100644 --- a/native/factor.h +++ b/native/factor.h @@ -27,6 +27,9 @@ CELL cs_bot; /* raw pointer to callstack top */ DLLEXPORT CELL cs; +/* TAGGED currently executing quotation */ +CELL callframe; + #include #include #include diff --git a/native/run.h b/native/run.h index 7331bf5f68..1461354704 100644 --- a/native/run.h +++ b/native/run.h @@ -26,9 +26,6 @@ jmp_buf toplevel; sigjmp_buf toplevel; #endif -/* TAGGED currently executing quotation */ -CELL callframe; - /* TAGGED pointer to currently executing word */ CELL executing; diff --git a/native/string.c b/native/string.c index e5a88bd7a5..be8547722b 100644 --- a/native/string.c +++ b/native/string.c @@ -4,7 +4,12 @@ F_STRING* allot_string(CELL capacity) { F_STRING* string = allot_object(STRING_TYPE, - sizeof(F_STRING) + capacity * CHARS); + sizeof(F_STRING) + (capacity + 1) * CHARS); + /* strings are null-terminated in memory, even though they also + have a length field. The null termination allows us to add + the sizeof(F_STRING) to a Factor string to get a C-style + UTF16 string for C library calls. */ + cput(SREF(string,capacity),(uint16_t)'\0'); string->capacity = capacity; return string; } diff --git a/native/string.h b/native/string.h index 1087193118..6f06c7f50e 100644 --- a/native/string.h +++ b/native/string.h @@ -1,11 +1,18 @@ + + typedef struct { CELL header; - /* untagged */ + /* untagged num of chars */ CELL capacity; /* tagged */ CELL hashcode; } F_STRING; +#define SREF(string,index) ((CELL)string + sizeof(F_STRING) + index * CHARS) + +#define SSIZE(pointer) align8(sizeof(F_STRING) + \ + (((F_STRING*)pointer)->capacity + 1) * CHARS) + INLINE F_STRING* untag_string(CELL tagged) { type_check(STRING_TYPE,tagged); @@ -26,11 +33,6 @@ void primitive_memory_to_string(void); DLLEXPORT BYTE* unbox_c_string(void); DLLEXPORT uint16_t* unbox_utf16_string(void); -#define SREF(string,index) ((CELL)string + sizeof(F_STRING) + index * CHARS) - -#define SSIZE(pointer) align8(sizeof(F_STRING) + \ - ((F_STRING*)pointer)->capacity * CHARS) - /* untagged & unchecked */ INLINE CELL string_nth(F_STRING* string, CELL index) {