]> gitweb.factorcode.org Git - factor.git/commitdiff
string>memory and memory>string primitives
authorSlava Pestov <slava@factorcode.org>
Mon, 20 Dec 2004 02:07:17 +0000 (02:07 +0000)
committerSlava Pestov <slava@factorcode.org>
Mon, 20 Dec 2004 02:07:17 +0000 (02:07 +0000)
factor/jedit/WordListDialog.java
factor/parser/Traits.java
library/bootstrap/primitives.factor
library/primitives.factor
library/tools/debugger.factor
native/error.h
native/primitives.c
native/primitives.h
native/string.c
native/string.h

index 7846378ce29ccb60f0dd03c7a4ec9bb31f8a55b2..7b14f9494d71bf31857ff06083d72052706a93c2 100644 (file)
@@ -90,8 +90,10 @@ public abstract class WordListDialog extends EnhancedDialog
 
                try
                {
-                       preview.setText(FactorPlugin.evalInWire(
-                               FactorPlugin.factorWord(word) + " see").trim());
+                       String text = FactorPlugin.evalInWire(
+                               FactorPlugin.factorWord(word) + " see").trim();
+                       preview.setText(text);
+                       preview.setCaretPosition(text.length());
                }
                catch(Exception e)
                {
index ae9aa7ea007a78ed9b7e785723a1ca1802242fa1..31f9512f50da2ea1acaffb00d2adf5241c44dcde 100644 (file)
@@ -42,6 +42,9 @@ public class Traits extends FactorParsingDefinition
                throws Exception
        {
                FactorWord w = reader.nextWord(true);
+               if(w == null)
+                       return;
+
                w.def = new FactorTraitsDefinition(w);
                reader.intern("<" + w.name + ">",true);
                reader.intern(w.name + "?",true);
index 5c1d1a400fa58fe7adb49be520f971e1da36387d..6d64f4bc7f6e1af38d7259e2e422a00be633e9a7 100644 (file)
@@ -237,6 +237,8 @@ vocabularies get [
     [ "alien" | "set-alien-1" ]
     [ "kernel" | "heap-stats" ]
     [ "errors" | "throw" ]
+    [ "kernel-internals" | "string>memory" ]
+    [ "kernel-internals" | "memory>string" ]
 ] [
     unswons create swap succ [ f define ] keep
 ] each drop
index a4838243068246bf66ef6f77141eba2f7e59b193..7f312ed683c966533461483d0ac7247ee81d56eb 100644 (file)
@@ -31,6 +31,7 @@ USE: errors
 USE: files
 USE: io-internals
 USE: kernel
+USE: kernel-internals
 USE: lists
 USE: math
 USE: math-internals
@@ -227,6 +228,8 @@ USE: words
     [ set-alien-1            " n alien off -- "                   [ 3 | 0 ] ]
     [ heap-stats             " -- instances bytes "               [ 0 | 2 ] ]
     [ throw                  " error -- "                         [ 1 | 0 ] ]
+    [ string>memory          " str address -- "                   [ 2 | 0 ] ]
+    [ memory>string          " address length -- str "            [ 2 | 1 ] ]
 ] [
     uncons dupd uncons car ( word word stack-effect infer-effect )
     >r "stack-effect" set-word-property r>
index 66846fc2aee40bf6ae7eca4464f5c698c3696af5..6e64233da01bb4cd1b2134ff4038cecfd98683a7 100644 (file)
@@ -78,9 +78,6 @@ USE: math
 : negative-array-size-error ( obj -- )
     "Cannot allocate array with negative size " write . ;
 
-: bad-primitive-error ( obj -- )
-    "Bad primitive number: " write . ;
-
 : c-string-error ( obj -- )
     "Cannot convert to C string: " write . ;
 
@@ -106,7 +103,6 @@ USE: math
         float-format-error
         signal-error
         negative-array-size-error
-        bad-primitive-error
         c-string-error
         ffi-disabled-error
         ffi-error
index 5f7a2994687b18c21be698b6cc2172d765b8505a..8b65edfd433a08f0045145b5e6e6bc110fa18a57 100644 (file)
@@ -9,11 +9,10 @@
 #define ERROR_FLOAT_FORMAT (8<<3)
 #define ERROR_SIGNAL (9<<3)
 #define ERROR_NEGATIVE_ARRAY_SIZE (10<<3)
-#define ERROR_BAD_PRIMITIVE (11<<3)
-#define ERROR_C_STRING (12<<3)
-#define ERROR_FFI_DISABLED (13<<3)
-#define ERROR_FFI (14<<3)
-#define ERROR_CLOSED (15<<3)
+#define ERROR_C_STRING (11<<3)
+#define ERROR_FFI_DISABLED (12<<3)
+#define ERROR_FFI (13<<3)
+#define ERROR_CLOSED (14<<3)
 
 /* When throw_error throws an error, it sets this global and
 longjmps back to the top-level. */
index 9ae07001d4c895dc6dd8bd8e6261d7065c22778a..7de79c4c1e7770308e2e7272246860eaaa55ef63 100644 (file)
@@ -189,13 +189,15 @@ XT primitives[] = {
        primitive_alien_1,
        primitive_set_alien_1,
        primitive_heap_stats,
-       primitive_throw
+       primitive_throw,
+       primitive_string_to_memory,
+       primitive_memory_to_string
 };
 
 CELL primitive_to_xt(CELL primitive)
 {
        if(primitive < 0 || primitive >= PRIMITIVE_COUNT)
-               general_error(ERROR_BAD_PRIMITIVE,tag_fixnum(primitive));
-
-       return (CELL)primitives[primitive];
+               return (CELL)undefined;
+       else
+               return (CELL)primitives[primitive];
 }
index 735d6b8ac11f64493ae14dac4c78698f60904e25..c41f8b479683b3598b4042b856963ede5a3bc84d 100644 (file)
@@ -1,4 +1,4 @@
 extern XT primitives[];
-#define PRIMITIVE_COUNT 189
+#define PRIMITIVE_COUNT 191
 
 CELL primitive_to_xt(CELL primitive);
index 1f387737fcad77b89450678dd00e7f312f07a5d4..9504db93effcf621c3ea636eb2ff441d80de4a10 100644 (file)
@@ -59,17 +59,15 @@ F_STRING* grow_string(F_STRING* string, F_FIXNUM capacity, uint16_t fill)
        return new_string;
 }
 
-/* untagged */
-F_STRING* from_c_string(const BYTE* c_string)
+INLINE F_STRING* memory_to_string(const BYTE* string, CELL length)
 {
-       CELL length = strlen(c_string);
        F_STRING* s = allot_string(length);
        CELL i;
 
        for(i = 0; i < length; i++)
        {
-               cput(SREF(s,i),*c_string);
-               c_string++;
+               cput(SREF(s,i),*string);
+               string++;
        }
 
        rehash_string(s);
@@ -77,6 +75,19 @@ F_STRING* from_c_string(const BYTE* c_string)
        return s;
 }
 
+void primitive_memory_to_string(void)
+{
+       CELL length = unbox_cell();
+       BYTE* string = (BYTE*)unbox_cell();
+       dpush(tag_object(memory_to_string(string,length)));
+}
+
+/* untagged */
+F_STRING* from_c_string(const BYTE* c_string)
+{
+       return memory_to_string(c_string,strlen(c_string));
+}
+
 /* FFI calls this */
 void box_c_string(const BYTE* c_string)
 {
@@ -98,19 +109,27 @@ BYTE* to_c_string(F_STRING* s)
        return to_c_string_unchecked(s);
 }
 
+INLINE void string_to_memory(F_STRING* s, BYTE* string)
+{
+       CELL i;
+       for(i = 0; i < s->capacity; i++)
+               string[i] = string_nth(s,i);
+}
+
+void primitive_string_to_memory(void)
+{
+       F_STRING* str = untag_string(dpop());
+       BYTE* address = (BYTE*)unbox_cell();
+       string_to_memory(str,address);
+}
+
 /* untagged */
 BYTE* to_c_string_unchecked(F_STRING* s)
 {
        F_STRING* _c_str = allot_string(s->capacity / CHARS + 1);
-       CELL i;
-
        BYTE* c_str = (BYTE*)(_c_str + 1);
-       
-       for(i = 0; i < s->capacity; i++)
-               c_str[i] = string_nth(s,i);
-
+       string_to_memory(s,c_str);
        c_str[s->capacity] = '\0';
-
        return c_str;
 }
 
index 90e6f2dd8dc043fbc8bca9f63618e35eb3ac84de..4e48f20c65566de4cbc1987ccf016b7d05cf7971 100644 (file)
@@ -19,8 +19,10 @@ void rehash_string(F_STRING* str);
 F_STRING* grow_string(F_STRING* string, F_FIXNUM capacity, uint16_t fill);
 BYTE* to_c_string(F_STRING* s);
 BYTE* to_c_string_unchecked(F_STRING* s);
+void primitive_string_to_memory(void);
 DLLEXPORT void box_c_string(const BYTE* c_string);
 F_STRING* from_c_string(const BYTE* c_string);
+void primitive_memory_to_string(void);
 DLLEXPORT BYTE* unbox_c_string(void);
 
 #define SREF(string,index) ((CELL)string + sizeof(F_STRING) + index * CHARS)