]> gitweb.factorcode.org Git - factor.git/commitdiff
FFI string encoding conversion
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 20 Apr 2008 10:15:46 +0000 (05:15 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 20 Apr 2008 10:15:46 +0000 (05:15 -0500)
65 files changed:
core/alien/alien.factor
core/alien/arrays/arrays-docs.factor
core/alien/arrays/arrays.factor
core/alien/c-types/c-types-docs.factor
core/alien/c-types/c-types-tests.factor
core/alien/c-types/c-types.factor
core/alien/compiler/compiler-tests.factor
core/alien/compiler/compiler.factor
core/alien/remote-control/remote-control.factor
core/alien/strings/strings-docs.factor [new file with mode: 0644]
core/alien/strings/strings-tests.factor [new file with mode: 0644]
core/alien/strings/strings.factor [new file with mode: 0644]
core/alien/structs/structs-tests.factor
core/alien/structs/structs.factor
core/alien/syntax/syntax.factor
core/bootstrap/primitives.factor
core/classes/tuple/tuple-tests.factor
core/compiler/tests/intrinsics.factor
core/cpu/architecture/architecture.factor
core/generator/fixup/fixup-docs.factor
core/generator/fixup/fixup.factor
core/generator/generator.factor
core/generator/registers/registers.factor
core/generic/standard/standard-tests.factor
core/inference/known-words/known-words.factor
core/io/encodings/encodings-docs.factor
core/io/encodings/utf16/utf16-docs.factor
core/io/encodings/utf16/utf16.factor
core/io/streams/memory/memory.factor [new file with mode: 0644]
core/prettyprint/config/config.factor
core/prettyprint/prettyprint.factor
core/prettyprint/sections/sections.factor
extra/benchmark/binary-trees/binary-trees.factor [new file with mode: 0644]
extra/cocoa/messages/messages.factor
extra/cocoa/subclassing/subclassing.factor
extra/core-foundation/core-foundation.factor
extra/core-foundation/fsevents/fsevents.factor
extra/hardware-info/macosx/macosx.factor
extra/hardware-info/windows/nt/nt.factor
extra/hardware-info/windows/windows.factor
extra/help/handbook/handbook.factor
extra/io/sockets/impl/impl.factor
extra/io/unix/linux/monitors/monitors.factor
extra/io/unix/sockets/sockets.factor
extra/io/windows/nt/files/files.factor
extra/locals/backend/backend.factor
extra/math/ranges/ranges.factor
extra/odbc/odbc.factor
extra/opengl/shaders/shaders.factor
extra/openssl/openssl-tests.factor
extra/openssl/openssl.factor
extra/oracle/oracle.factor
extra/ui/windows/windows.factor
extra/ui/x11/x11.factor
extra/unix/linux/ifreq/ifreq.factor
extra/unix/process/process.factor
extra/webapps/todo/todo-list.xml
extra/windows/ole32/ole32.factor
extra/windows/shell32/shell32.factor
extra/windows/types/types.factor
extra/windows/windows.factor
extra/windows/winsock/winsock.factor
extra/x/x.factor
extra/x11/clipboard/clipboard.factor
extra/x11/xlib/xlib.factor

index f664e1175a0ceed8dea1f2ca5ddf1647ed28ad8c..cc37b85103d2af3cafb1c6ddbc38c59ba99dba78 100755 (executable)
@@ -28,12 +28,6 @@ M: f expired? drop t ;
 : <alien> ( address -- alien )
     f <displaced-alien> { simple-c-ptr } declare ; inline
 
-: alien>native-string ( alien -- string )
-    os windows? [ alien>u16-string ] [ alien>char-string ] if ;
-
-: dll-path ( dll -- string )
-    (dll-path) alien>native-string ;
-
 M: alien equal?
     over alien? [
         2dup [ expired? ] either? [
index f3f27d073930842e66f532096b96131eb8d3ada7..09a09cdc6f97d7136053b2ea2f6dfddbe824d462 100755 (executable)
@@ -18,7 +18,6 @@ ARTICLE: "c-arrays-factor" "Converting C arrays to and from Factor arrays"
 { $subsection >c-ushort-array    }\r
 { $subsection >c-void*-array     }\r
 { $subsection c-bool-array>      }\r
-{ $subsection c-char*-array>     }\r
 { $subsection c-char-array>      }\r
 { $subsection c-double-array>    }\r
 { $subsection c-float-array>     }\r
@@ -30,7 +29,6 @@ ARTICLE: "c-arrays-factor" "Converting C arrays to and from Factor arrays"
 { $subsection c-uint-array>      }\r
 { $subsection c-ulong-array>     }\r
 { $subsection c-ulonglong-array> }\r
-{ $subsection c-ushort*-array>   }\r
 { $subsection c-ushort-array>    }\r
 { $subsection c-void*-array>     } ;\r
 \r
@@ -61,9 +59,7 @@ ARTICLE: "c-arrays-get/set" "Reading and writing elements in C arrays"
 { $subsection double-nth }\r
 { $subsection set-double-nth }\r
 { $subsection void*-nth }\r
-{ $subsection set-void*-nth }\r
-{ $subsection char*-nth }\r
-{ $subsection ushort*-nth } ;\r
+{ $subsection set-void*-nth } ;\r
 \r
 ARTICLE: "c-arrays" "C arrays"\r
 "C arrays are allocated in the same manner as other C data; see " { $link "c-byte-arrays" } " and " { $link "malloc" } "."\r
index 402b01550bb4091a31f8b56cfd028dd1682d72d1..0f756e0ad07eeaeab6eb57d1e0c3d433ab32b670 100644 (file)
@@ -1,8 +1,7 @@
-! Copyright (C) 2007 Slava Pestov.
+! Copyright (C) 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: alien arrays alien.c-types alien.structs
-sequences math kernel generator.registers
-namespaces libc ;
+sequences math kernel namespaces libc cpu.architecture ;
 IN: alien.arrays
 
 UNION: value-type array struct-type ;
@@ -27,7 +26,9 @@ M: array stack-size drop "void*" stack-size ;
 
 M: value-type c-type-reg-class drop int-regs ;
 
-M: value-type c-type-prep drop f ;
+M: value-type c-type-boxer-quot drop f ;
+
+M: value-type c-type-unboxer-quot drop f ;
 
 M: value-type c-type-getter
     drop [ swap <displaced-alien> ] ;
index 8d2b03467b3e0b53395252f75d8abcb6e3a951eb..3cd5afef3368f0dd82edcfe12cdc8e7facda4849 100755 (executable)
@@ -62,28 +62,6 @@ HELP: <c-object>
 
 { <c-object> malloc-object } related-words
 
-HELP: string>char-alien ( string -- array )
-{ $values { "string" string } { "array" byte-array } }
-{ $description "Copies the string to a new byte array, converting it to 8-bit ASCII and adding a trailing null byte." }
-{ $errors "Throws an error if the string contains null characters, or characters beyond the 8-bit range." } ;
-
-{ string>char-alien alien>char-string malloc-char-string } related-words
-
-HELP: alien>char-string ( c-ptr -- string )
-{ $values { "c-ptr" c-ptr } { "string" string } }
-{ $description "Reads a null-terminated 8-bit C string from the specified address." } ;
-
-HELP: string>u16-alien ( string -- array )
-{ $values { "string" string } { "array" byte-array } }
-{ $description "Copies the string to a new byte array in UCS-2 format with a trailing null byte." }
-{ $errors "Throws an error if the string contains null characters." } ;
-
-{ string>u16-alien alien>u16-string malloc-u16-string } related-words
-
-HELP: alien>u16-string ( c-ptr -- string )
-{ $values { "c-ptr" c-ptr } { "string" string } }
-{ $description "Reads a null-terminated UCS-2 string from the specified address." } ;
-
 HELP: memory>byte-array
 { $values { "alien" c-ptr } { "len" "a non-negative integer" } { "byte-array" byte-array } }
 { $description "Reads " { $snippet "len" } " bytes starting from " { $snippet "base" } " and stores them in a new byte array." } ;
@@ -111,18 +89,6 @@ HELP: malloc-byte-array
 { $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
 { $errors "Throws an error if memory allocation fails." } ;
 
-HELP: malloc-char-string
-{ $values { "string" string } { "alien" c-ptr } }
-{ $description "Allocates an unmanaged memory block, and stores a string in 8-bit ASCII encoding with a trailing null byte to the block." }
-{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
-{ $errors "Throws an error if memory allocation fails." } ;
-
-HELP: malloc-u16-string
-{ $values { "string" string } { "alien" c-ptr } }
-{ $description "Allocates an unmanaged memory block, and stores a string in UCS2 encoding with a trailing null character to the block." }
-{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
-{ $errors "Throws an error if memory allocation fails." } ;
-
 HELP: define-nth
 { $values { "name" "a word name" } { "vocab" "a vocabulary name" } }
 { $description "Defines a word " { $snippet { $emphasis "name" } "-nth" } " with stack effect " { $snippet "( n c-ptr -- value )" } " for reading the value with C type " { $snippet "name" } " stored at an alien pointer, displaced by a multiple of the C type's size." }
@@ -202,8 +168,6 @@ $nl
 { $subsection *float }
 { $subsection *double }
 { $subsection *void* }
-{ $subsection *char* }
-{ $subsection *ushort* }
 "Note that while structure and union types do not get these words defined for them, there is no loss of generality since " { $link <void*> } " and " { $link *void* } " may be used." ;
 
 ARTICLE: "c-types-specs" "C type specifiers"
@@ -267,26 +231,6 @@ $nl
 "A wrapper for temporarily allocating a block of memory:"
 { $subsection with-malloc } ;
 
-ARTICLE: "c-strings" "C strings"
-"The C library interface defines two types of C strings:"
-{ $table
-    { "C type" "Notes" }
-    { { $snippet "char*" } "8-bit per character null-terminated ASCII" }
-    { { $snippet "ushort*" } "16-bit per character null-terminated UCS-2" }
-}
-"Passing a Factor string to a C function expecting a C string allocates a " { $link byte-array } " in the Factor heap; the string is then converted to the requested format and a raw pointer is passed to the function. If the conversion fails, for example if the string contains null bytes or characters with values higher than 255, a " { $link c-string-error. } " is thrown."
-"Sometimes a C function has a parameter type of " { $snippet "void*" } ", and various data types, among them strings, can be passed in. In this case, strings are not automatically converted to aliens, and instead you must call one of these words:"
-{ $subsection string>char-alien }
-{ $subsection string>u16-alien }
-{ $subsection malloc-char-string }
-{ $subsection malloc-u16-string }
-"The first two allocate " { $link byte-array } "s, and the latter allocates manually-managed memory which is not moved by the garbage collector and has to be explicitly freed by calling " { $link free } ". See " { $link "byte-arrays-gc" } " for a discussion of the two approaches."
-$nl
-"Finally, a set of words can be used to read and write " { $snippet "char*" } " and " { $snippet "ushort*" } " strings at arbitrary addresses:"
-{ $subsection alien>char-string }
-{ $subsection alien>u16-string }
-"For example, if a C function returns a " { $snippet "char*" } " but stipulates that the caller must deallocate the memory afterward, you must define the function as returning " { $snippet "void*" } ", and call one of the above words before passing the pointer to " { $link free } "." ;
-
 ARTICLE: "c-data" "Passing data between Factor and C"
 "Two defining characteristics of Factor are dynamic typing and automatic memory management, which are somewhat incompatible with the machine-level data model exposed by C. Factor's C library interface defines its own set of C data types, distinct from Factor language types, together with automatic conversion between Factor values and C types. For example, C integer types must be declared and are fixed-width, whereas Factor supports arbitrary-precision integers."
 $nl
index 843b0a826b22696ed0e0c1b43c25419d39516bb3..5f57068bab0d68400e6312e7edfc1cfe64f2bccf 100755 (executable)
@@ -1,30 +1,6 @@
 IN: alien.c-types.tests
 USING: alien alien.syntax alien.c-types kernel tools.test
-sequences system libc ;
-
-[ "\u0000ff" ]
-[ "\u0000ff" string>char-alien alien>char-string ]
-unit-test
-
-[ "hello world" ]
-[ "hello world" string>char-alien alien>char-string ]
-unit-test
-
-[ "hello\u00abcdworld" ]
-[ "hello\u00abcdworld" string>u16-alien alien>u16-string ]
-unit-test
-
-[ t ] [ f expired? ] unit-test
-
-[ "hello world" ] [
-    "hello world" malloc-char-string
-    dup alien>char-string swap free
-] unit-test
-
-[ "hello world" ] [
-    "hello world" malloc-u16-string
-    dup alien>u16-string swap free
-] unit-test
+sequences system libc alien.strings io.encodings.utf8 ;
 
 : foo ( -- n ) "fdafd" f dlsym [ 123 ] unless* ;
 
@@ -67,7 +43,7 @@ TYPEDEF: int* MyIntArray
 
 TYPEDEF: uchar* MyLPBYTE
 
-[ t ] [ "char*" c-type "MyLPBYTE" c-type eq? ] unit-test
+[ t ] [ { "char*" utf8 } c-type "MyLPBYTE" c-type = ] unit-test
 
 [
     0 B{ 1 2 3 4 } <displaced-alien> <void*>
index c97c76069572e13d4a22ba25b9c1e440605eadb1..f67fc78259ff74e23addaee8074183e69f38b69f 100755 (executable)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2004, 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: bit-arrays byte-arrays float-arrays arrays
-generator.registers assocs kernel kernel.private libc math
+assocs kernel kernel.private libc math
 namespaces parser sequences strings words assocs splitting
 math.parser cpu.architecture alien alien.accessors quotations
 layouts system compiler.units io.files io.encodings.binary
@@ -14,7 +14,7 @@ DEFER: *char
 : little-endian? ( -- ? ) 1 <int> *char 1 = ; foldable
 
 TUPLE: c-type
-boxer prep unboxer
+boxer boxer-quot unboxer unboxer-quot
 getter setter
 reg-class size align stack-align? ;
 
@@ -149,23 +149,12 @@ M: float-array byte-length length "double" heap-size * ;
 : malloc-byte-array ( byte-array -- alien )
     dup length dup malloc [ -rot memcpy ] keep ;
 
-: malloc-char-string ( string -- alien )
-    string>char-alien malloc-byte-array ;
-
-: malloc-u16-string ( string -- alien )
-    string>u16-alien malloc-byte-array ;
-
 : memory>byte-array ( alien len -- byte-array )
     dup <byte-array> [ -rot memcpy ] keep ;
 
 : byte-array>memory ( byte-array base -- )
     swap dup length memcpy ;
 
-DEFER: >c-ushort-array
-
-: string>u16-memory ( string base -- )
-    >r >c-ushort-array r> byte-array>memory ;
-
 : (define-nth) ( word type quot -- )
     >r heap-size [ rot * ] swap prefix r> append define-inline ;
 
@@ -378,7 +367,7 @@ M: long-long-type box-return ( type -- )
         "box_float" >>boxer
         "to_float" >>unboxer
         single-float-regs >>reg-class
-        [ >float ] >>prep
+        [ >float ] >>unboxer-quot
     "float" define-primitive-type
 
     <c-type>
@@ -389,30 +378,8 @@ M: long-long-type box-return ( type -- )
         "box_double" >>boxer
         "to_double" >>unboxer
         double-float-regs >>reg-class
-        [ >float ] >>prep
+        [ >float ] >>unboxer-quot
     "double" define-primitive-type
 
-    <c-type>
-        [ alien-cell alien>char-string ] >>getter
-        [ set-alien-cell ] >>setter
-        bootstrap-cell >>size
-        bootstrap-cell >>align
-        "box_char_string" >>boxer
-        "alien_offset" >>unboxer
-        [ string>char-alien ] >>prep
-    "char*" define-primitive-type
-
-    "char*" "uchar*" typedef
-
-    <c-type>
-        [ alien-cell alien>u16-string ] >>getter
-        [ set-alien-cell ] >>setter
-        4 >>size
-        4 >>align
-        "box_u16_string" >>boxer
-        "alien_offset" >>unboxer
-        [ string>u16-alien ] >>prep
-    "ushort*" define-primitive-type
-
     os winnt? cpu x86.64? and "longlong" "long" ? "ptrdiff_t" typedef
 ] with-compilation-unit
index d1a14dd758b2bdaf65a78ef84608b730d351421b..3d0f36e415becc41dc3cadf83e6c33b94a59cf1b 100755 (executable)
@@ -364,6 +364,10 @@ FUNCTION: ulonglong ffi_test_38 ( ulonglong x, ulonglong y ) ;
         + + 1+\r
     ] alien-callback ;\r
 \r
+FUNCTION: void ffi_test_36_point_5 ( ) ;\r
+\r
+[ ] [ ffi_test_36_point_5 ] unit-test\r
+\r
 FUNCTION: int ffi_test_37 ( void* func ) ;\r
 \r
 [ 1 ] [ callback-9 ffi_test_37 ] unit-test\r
index 9bd65aa0bc020b3d25f6505bb22936a40be5640d..3de4c6129121f9675cc15ca4c2bd555c48009a48 100755 (executable)
@@ -3,10 +3,11 @@
 USING: arrays generator generator.registers generator.fixup
 hashtables kernel math namespaces sequences words
 inference.state inference.backend inference.dataflow system
-math.parser classes alien.arrays alien.c-types alien.structs
-alien.syntax cpu.architecture alien inspector quotations assocs
-kernel.private threads continuations.private libc combinators
-compiler.errors continuations layouts accessors ;
+math.parser classes alien.arrays alien.c-types alien.strings
+alien.structs alien.syntax cpu.architecture alien inspector
+quotations assocs kernel.private threads continuations.private
+libc combinators compiler.errors continuations layouts accessors
+;
 IN: alien.compiler
 
 TUPLE: #alien-node < node return parameters abi ;
@@ -20,9 +21,7 @@ TUPLE: #alien-invoke < #alien-node library function ;
 : large-struct? ( ctype -- ? )
     dup c-struct? [
         heap-size struct-small-enough? not
-    ] [
-        drop f
-    ] if ;
+    ] [ drop f ] if ;
 
 : alien-node-parameters* ( node -- seq )
     dup parameters>>
@@ -162,17 +161,16 @@ M: long-long-type flatten-value-type ( type -- )
     dup return>> "void" = 0 1 ?
     swap produce-values ;
 
-: (make-prep-quot) ( parameters -- )
+: (param-prep-quot) ( parameters -- )
     dup empty? [
         drop
     ] [
-        unclip c-type c-type-prep %
-        \ >r , (make-prep-quot) \ r> ,
+        unclip c-type c-type-unboxer-quot %
+        \ >r , (param-prep-quot) \ r> ,
     ] if ;
 
-: make-prep-quot ( node -- quot )
-    parameters>>
-    [ <reversed> (make-prep-quot) ] [ ] make ;
+: param-prep-quot ( node -- quot )
+    parameters>> [ <reversed> (param-prep-quot) ] [ ] make ;
 
 : unbox-parameters ( offset node -- )
     parameters>> [
@@ -200,6 +198,20 @@ M: long-long-type flatten-value-type ( type -- )
 : box-return* ( node -- )
     return>> [ ] [ box-return ] if-void ;
 
+: (return-prep-quot) ( parameters -- )
+    dup empty? [
+        drop
+    ] [
+        unclip c-type c-type-boxer-quot %
+        \ >r , (return-prep-quot) \ r> ,
+    ] if ;
+
+: callback-prep-quot ( node -- quot )
+    parameters>> [ <reversed> (return-prep-quot) ] [ ] make ;
+
+: return-prep-quot ( node -- quot )
+    [ return>> [ ] [ 1array (return-prep-quot) ] if-void ] [ ] make ;
+
 M: alien-invoke-error summary
     drop
     "Words calling ``alien-invoke'' must be compiled with the optimizing compiler." ;
@@ -258,15 +270,15 @@ M: no-such-symbol compiler-error-type
     pop-literal nip >>library
     pop-literal nip >>return
     ! Quotation which coerces parameters to required types
-    dup make-prep-quot recursive-state get infer-quot
+    dup param-prep-quot f infer-quot
     ! Set ABI
-    dup library>>
-    library [ abi>> ] [ "cdecl" ] if*
-    >>abi
+    dup library>> library [ abi>> ] [ "cdecl" ] if* >>abi
     ! Add node to IR
     dup node,
     ! Magic #: consume exactly the number of inputs
-    0 alien-invoke-stack
+    dup 0 alien-invoke-stack
+    ! Quotation which coerces return value to required type
+    return-prep-quot f infer-quot
 ] "infer" set-word-prop
 
 M: #alien-invoke generate-node
@@ -294,11 +306,13 @@ M: alien-indirect-error summary
     pop-parameters >>parameters
     pop-literal nip >>return
     ! Quotation which coerces parameters to required types
-    dup make-prep-quot [ dip ] curry recursive-state get infer-quot
+    dup param-prep-quot [ dip ] curry f infer-quot
     ! Add node to IR
     dup node,
     ! Magic #: consume the function pointer, too
-    1 alien-invoke-stack
+    dup 1 alien-invoke-stack
+    ! Quotation which coerces return value to required type
+    return-prep-quot f infer-quot
 ] "infer" set-word-prop
 
 M: #alien-indirect generate-node
@@ -331,7 +345,7 @@ M: alien-callback-error summary
 
 : callback-bottom ( node -- )
     xt>> [ word-xt drop <alien> ] curry
-    recursive-state get infer-quot ;
+    f infer-quot ;
 
 \ alien-callback [
     4 ensure-values
@@ -371,16 +385,18 @@ TUPLE: callback-context ;
     slip
     wait-to-return ; inline
 
-: prepare-callback-return ( ctype -- quot )
+: callback-return-quot ( ctype -- quot )
     return>> {
         { [ dup "void" = ] [ drop [ ] ] }
         { [ dup large-struct? ] [ heap-size [ memcpy ] curry ] }
-        [ c-type c-type-prep ]
+        [ c-type c-type-unboxer-quot ]
     } cond ;
 
 : wrap-callback-quot ( node -- quot )
     [
-        [ quot>> ] [ prepare-callback-return ] bi append ,
+        [ callback-prep-quot ]
+        [ quot>> ]
+        [ callback-return-quot ] tri 3append ,
         [ callback-context new do-callback ] %
     ] [ ] make ;
 
@@ -405,9 +421,10 @@ TUPLE: callback-context ;
         init-templates
         %prologue-later
         dup alien-stack-frame [
-            dup registers>objects
-            dup wrap-callback-quot %alien-callback
-            %callback-return
+            [ registers>objects ]
+            [ wrap-callback-quot %alien-callback ]
+            [ %callback-return ]
+            tri
         ] with-stack-frame
     ] with-generator ;
 
index b7700c0ff18830264649868552d1d4e041c67c31..1d713f6eddaa59a37aacf96ad7cf369b30b77b39 100755 (executable)
@@ -1,12 +1,12 @@
-! Copyright (C) 2007 Slava Pestov.
+! Copyright (C) 2007, 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.c-types parser threads words kernel.private
-kernel ;
+USING: alien alien.c-types alien.strings parser threads words
+kernel.private kernel io.encodings.utf8 ;
 IN: alien.remote-control
 
 : eval-callback
     "void*" { "char*" } "cdecl"
-    [ eval>string malloc-char-string ] alien-callback ;
+    [ eval>string utf8 malloc-string ] alien-callback ;
 
 : yield-callback
     "void" { } "cdecl" [ yield ] alien-callback ;
diff --git a/core/alien/strings/strings-docs.factor b/core/alien/strings/strings-docs.factor
new file mode 100644 (file)
index 0000000..0dbb4ff
--- /dev/null
@@ -0,0 +1,52 @@
+USING: help.markup help.syntax strings byte-arrays alien libc
+debugger ;
+IN: alien.strings
+
+HELP: string>alien
+{ $values { "string" string } { "encoding" "an encoding descriptor" } { "array" byte-array } }
+{ $description "Encodes a string together with a trailing null code point using the given encoding, and stores the resulting bytes in a freshly-allocated byte array." }
+{ $errors "Throws an error if the string contains null characters, or characters not representable in the given encoding." } ;
+
+{ string>alien alien>string malloc-string } related-words
+
+HELP: alien>string
+{ $values { "c-ptr" c-ptr } { "encoding" "an encoding descriptor" } { "string" string } }
+{ $description "Reads a null-terminated C string from the specified address with the given encoding." } ;
+
+HELP: malloc-string
+{ $values { "string" string } { "encoding" "an encoding descriptor" } { "alien" c-ptr } }
+{ $description "Encodes a string together with a trailing null code point using the given encoding, and stores the resulting bytes in a freshly-allocated unmanaged memory block." }
+{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
+{ $errors "Throws an error if one of the following conditions occurs:"
+    { $list
+        "the string contains null code points"
+        "the string contains characters not representable using the encoding specified"
+        "memory allocation fails"
+    }
+} ;
+
+HELP: string>symbol
+{ $values { "str" string } { "alien" alien } }
+{ $description "Converts the string to a format which is a valid symbol name for the Factor VM's compiled code linker. By performing this conversion ahead of time, the image loader can run without allocating memory."
+$nl
+"On Windows CE, symbols are represented as UCS2 strings, and on all other platforms they are ASCII strings." } ;
+
+HELP: utf16n
+{ $class-description "The encoding descriptor for UTF-16 without a byte order mark in native endian order. This is useful mostly for FFI calls which take input of strings of the type wchar_t*" }
+{ $see-also "encodings-introduction" } ;
+
+ARTICLE: "c-strings" "C strings"
+"C string types are arrays with shape " { $snippet "{ \"char*\" encoding }" } ", where " { $snippet "encoding" } " is an encoding descriptor. The type " { $snippet "\"char*\"" } " is an alias for " { $snippet "{ \"char*\" utf8 }" } ". See " { $link "encodings-descriptors" } " for information about encoding descriptors."
+$nl
+"Passing a Factor string to a C function expecting a C string allocates a " { $link byte-array } " in the Factor heap; the string is then converted to the requested format and a raw pointer is passed to the function. If the conversion fails, for example if the string contains null bytes or characters with values higher than 255, a " { $link c-string-error. } " is thrown."
+$nl
+"Sometimes a C function has a parameter type of " { $snippet "void*" } ", and various data types, among them strings, can be passed in. In this case, strings are not automatically converted to aliens, and instead you must call one of these words:"
+{ $subsection string>alien }
+{ $subsection malloc-string }
+"The first allocates " { $link byte-array } "s, and the latter allocates manually-managed memory which is not moved by the garbage collector and has to be explicitly freed by calling " { $link free } ". See " { $link "byte-arrays-gc" } " for a discussion of the two approaches."
+$nl
+"A word to read strings from arbitrary addresses:"
+{ $subsection alien>string }
+"For example, if a C function returns a " { $snippet "char*" } " but stipulates that the caller must deallocate the memory afterward, you must define the function as returning " { $snippet "void*" } ", and call one of the above words before passing the pointer to " { $link free } "." ;
+
+ABOUT: "c-strings"
diff --git a/core/alien/strings/strings-tests.factor b/core/alien/strings/strings-tests.factor
new file mode 100644 (file)
index 0000000..10cf75f
--- /dev/null
@@ -0,0 +1,28 @@
+USING: alien.strings tools.test kernel libc
+io.encodings.8-bit io.encodings.utf16 io.encodings.ascii alien ;
+IN: alien.strings.tests
+
+[ "\u0000ff" ]
+[ "\u0000ff" latin1 string>alien latin1 alien>string ]
+unit-test
+
+[ "hello world" ]
+[ "hello world" latin1 string>alien latin1 alien>string ]
+unit-test
+
+[ "hello\u00abcdworld" ]
+[ "hello\u00abcdworld" utf16le string>alien utf16le alien>string ]
+unit-test
+
+[ t ] [ f expired? ] unit-test
+
+[ "hello world" ] [
+    "hello world" ascii malloc-string
+    dup ascii alien>string swap free
+] unit-test
+
+[ "hello world" ] [
+    "hello world" utf16n malloc-string
+    dup utf16n alien>string swap free
+] unit-test
+
diff --git a/core/alien/strings/strings.factor b/core/alien/strings/strings.factor
new file mode 100644 (file)
index 0000000..1c15ac8
--- /dev/null
@@ -0,0 +1,108 @@
+! Copyright (C) 2008 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: arrays sequences kernel accessors math alien.accessors
+alien.c-types byte-arrays words io io.encodings
+io.streams.byte-array io.streams.memory io.encodings.utf8
+io.encodings.utf16 system alien strings cpu.architecture ;
+IN: alien.strings
+
+: alien>string ( alien encoding -- string )
+    >r <memory-stream> r> <decoder>
+    "\0" swap stream-read-until drop ;
+
+ERROR: invalid-c-string string ;
+
+: check-string ( string -- )
+    0 over memq? [ invalid-c-string ] [ drop ] if ;
+
+GENERIC# string>alien 1 ( string encoding -- byte-array )
+
+M: alien string>alien drop ;
+
+M: byte-array string>alien drop ;
+
+M: string string>alien
+    over check-string
+    <byte-writer>
+    [ stream-write ]
+    [ 0 swap stream-write1 ]
+    [ stream>> >byte-array ]
+    tri ;
+
+: malloc-string ( string encoding -- alien )
+    string>alien malloc-byte-array ;
+
+PREDICATE: string-type < pair
+    first2 [ "char*" = ] [ word? ] bi* and ;
+
+M: string-type c-type ;
+
+M: string-type heap-size
+    drop "void*" heap-size ;
+
+M: string-type c-type-align
+    drop "void*" c-type-align ;
+
+M: string-type c-type-stack-align?
+    drop "void*" c-type-stack-align? ;
+
+M: string-type unbox-parameter
+    drop "void*" unbox-parameter ;
+
+M: string-type unbox-return
+    drop "void*" unbox-return ;
+
+M: string-type box-parameter
+    drop "void*" box-parameter ;
+
+M: string-type box-return
+    drop "void*" box-return ;
+
+M: string-type stack-size
+    drop "void*" stack-size ;
+
+M: string-type c-type-reg-class
+    drop int-regs ;
+
+M: string-type c-type-boxer
+    drop "void*" c-type-boxer ;
+
+M: string-type c-type-unboxer
+    drop "void*" c-type-unboxer ;
+
+M: string-type c-type-boxer-quot
+    second [ alien>string ] curry [ ] like ;
+
+M: string-type c-type-unboxer-quot
+    second [ string>alien ] curry [ ] like ;
+
+M: string-type c-type-getter
+    drop [ alien-cell ] ;
+
+M: string-type c-type-setter
+    drop [ set-alien-cell ] ;
+
+TUPLE: utf16n ;
+
+! Native-order UTF-16
+
+: utf16n ( -- descriptor )
+    little-endian? utf16le utf16be ? ; foldable
+
+M: utf16n <decoder> drop utf16n <decoder> ;
+
+M: utf16n <encoder> drop utf16n <encoder> ;
+
+: alien>native-string ( alien -- string )
+    os windows? [ utf16n ] [ utf8 ] if alien>string ;
+
+: dll-path ( dll -- string )
+    (dll-path) alien>native-string ;
+
+: string>symbol ( str -- alien )
+    [ os wince? [ utf16n ] [ utf8 ] if string>alien ]
+    over string? [ call ] [ map ] if ;
+
+{ "char*" utf8 } "char*" typedef
+{ "char*" utf16n } "wchar_t*" typedef
+"char*" "uchar*" typedef
index a33a86d4b54fd42e8ec593206c76b465b67c10db..bfdcd31b99ec74cc6c3f1366bae60adb80d8519f 100644 (file)
@@ -1,6 +1,6 @@
 IN: alien.structs.tests
 USING: alien alien.syntax alien.c-types kernel tools.test
-sequences system libc words vocabs namespaces ;
+sequences system libc words vocabs namespaces layouts ;
 
 C-STRUCT: bar
     { "int" "x" }
@@ -9,20 +9,20 @@ C-STRUCT: bar
 [ 36 ] [ "bar" heap-size ] unit-test
 [ t ] [ \ <displaced-alien> "bar" c-type c-type-getter memq? ] unit-test
 
-! This was actually only correct on Windows/x86:
-
-! C-STRUCT: align-test
-!     { "int" "x" }
-!     { "double" "y" } ;
-! 
-! [ 16 ] [ "align-test" heap-size ] unit-test
-! 
-! cell 4 = [
-!     C-STRUCT: one
-!     { "long" "a" } { "double" "b" } { "int" "c" } ;
-! 
-!     [ 24 ] [ "one" heap-size ] unit-test
-] when
+C-STRUCT: align-test
+    { "int" "x" }
+    { "double" "y" } ;
+
+os winnt? cpu x86? and [
+    [ 16 ] [ "align-test" heap-size ] unit-test
+    
+    cell 4 = [
+        C-STRUCT: one
+        { "long" "a" } { "double" "b" } { "int" "c" } ;
+    
+        [ 24 ] [ "one" heap-size ] unit-test
+    ] when
+] when
 
 : MAX_FOOS 30 ;
 
index 6d98d317908436fd309eae7d64227bb1b4612f3b..bc5fa5a3f18248e9eeed6dbfe67b540b9e3922c5 100755 (executable)
@@ -20,14 +20,19 @@ IN: alien.structs
 
 : define-getter ( type spec -- )
     [ set-reader-props ] keep
-    dup slot-spec-reader
-    over slot-spec-type c-getter
+    [ ]
+    [ slot-spec-reader ]
+    [
+        slot-spec-type
+        [ c-getter ] [ c-type c-type-boxer-quot ] bi append
+    ] tri
     define-struct-slot-word ;
 
 : define-setter ( type spec -- )
     [ set-writer-props ] keep
-    dup slot-spec-writer
-    over slot-spec-type c-setter
+    [ ]
+    [ slot-spec-writer ]
+    [ slot-spec-type c-setter ] tri
     define-struct-slot-word ;
 
 : define-field ( type spec -- )
index 67ea30f379f9ee330609df42517a9663cb55eaf5..f0f495cac9cfafd9ee984718b7b5d288fad424ec 100755 (executable)
@@ -1,9 +1,9 @@
 ! Copyright (C) 2005, 2008 Slava Pestov, Alex Chapman.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: arrays alien alien.c-types alien.structs alien.arrays
-kernel math namespaces parser sequences words quotations
-math.parser splitting effects prettyprint prettyprint.sections
-prettyprint.backend assocs combinators ;
+alien.strings kernel math namespaces parser sequences words
+quotations math.parser splitting effects prettyprint
+prettyprint.sections prettyprint.backend assocs combinators ;
 IN: alien.syntax
 
 <PRIVATE
index 061866fe3e30a104c3cd0f73d818387c26ad1cca..dd3a4adf8bedb670e46a44aa93e87135e444e7f3 100755 (executable)
@@ -638,10 +638,6 @@ tuple
     { "set-alien-double" "alien.accessors" }
     { "alien-cell" "alien.accessors" }
     { "set-alien-cell" "alien.accessors" }
-    { "alien>char-string" "alien" }
-    { "string>char-alien" "alien" }
-    { "alien>u16-string" "alien" }
-    { "string>u16-alien" "alien" }
     { "(throw)" "kernel.private" }
     { "alien-address" "alien" }
     { "slot" "slots.private" }
index ce6fd9367c0384535a2dc6ece2f4be658ed9acbf..2932187152d4a3f20536b5ef6bccb3e069738e3e 100755 (executable)
@@ -3,7 +3,8 @@ math.constants parser sequences tools.test words assocs
 namespaces quotations sequences.private classes continuations
 generic.standard effects classes.tuple classes.tuple.private
 arrays vectors strings compiler.units accessors classes.algebra
-calendar prettyprint io.streams.string splitting inspector ;
+calendar prettyprint io.streams.string splitting inspector
+columns ;
 IN: classes.tuple.tests
 
 TUPLE: rect x y w h ;
index fadc57dc8d2e91aca213010c952d158f6531830a..c1ac6b40174c08695706d052c4d0f0c1483e223e 100755 (executable)
@@ -5,7 +5,7 @@ continuations sequences.private hashtables.private byte-arrays
 strings.private system random layouts vectors.private
 sbufs.private strings.private slots.private alien
 alien.accessors alien.c-types alien.syntax namespaces libc
-sequences.private ;
+sequences.private io.encodings.ascii ;
 
 ! Make sure that intrinsic ops compile to correct code.
 [ ] [ 1 [ drop ] compile-call ] unit-test
@@ -364,8 +364,8 @@ cell 8 = [
 [ ] [ "hello world" malloc-char-string "s" set ] unit-test
 
 "s" get [
-    [ "hello world" ] [ "s" get <void*> [ { byte-array } declare *void* ] compile-call alien>char-string ] unit-test
-    [ "hello world" ] [ "s" get <void*> [ { c-ptr } declare *void* ] compile-call alien>char-string ] unit-test
+    [ "hello world" ] [ "s" get <void*> [ { byte-array } declare *void* ] compile-call ascii alien>string ] unit-test
+    [ "hello world" ] [ "s" get <void*> [ { c-ptr } declare *void* ] compile-call ascii alien>string ] unit-test
 
     [ ] [ "s" get free ] unit-test
 ] when
index 4e939bddb856a312128fde16b0c44023cb4b4cf5..338c5341bc51724f5711854d9212c1b0bf0356f7 100755 (executable)
@@ -1,10 +1,17 @@
-! Copyright (C) 2006, 2007 Slava Pestov.
+! Copyright (C) 2006, 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: arrays generic kernel kernel.private math memory
 namespaces sequences layouts system hashtables classes alien
 byte-arrays bit-arrays float-arrays combinators words sets ;
 IN: cpu.architecture
 
+! Register classes
+SINGLETON: int-regs
+SINGLETON: single-float-regs
+SINGLETON: double-float-regs
+UNION: float-regs single-float-regs double-float-regs ;
+UNION: reg-class int-regs float-regs ;
+
 ! A pseudo-register class for parameters spilled on the stack
 SINGLETON: stack-params
 
index 7f4b5026da8cf46a74042a955fd7ff4efd904e2f..f5d530dccbbba9632c13c899f8fed24c3eafe57c 100644 (file)
@@ -13,12 +13,6 @@ HELP: add-literal
 { $values { "obj" object } { "n" integer } }
 { $description "Adds a literal to the " { $link literal-table } ", if it is not already there, and outputs the index of the literal in the table. This literal can then be used as an argument for a " { $link rt-literal } " relocation with " { $link rel-fixup } "." } ;
 
-HELP: string>symbol
-{ $values { "str" string } { "alien" alien } }
-{ $description "Converts the string to a format which is a valid symbol name for the Factor VM's compiled code linker. By performing this conversion ahead of time, the image loader can run without allocating memory."
-$nl
-"On Windows CE, symbols are represented as UCS2 strings, and on all other platforms they are ASCII strings." } ;
-
 HELP: rel-dlsym
 { $values { "name" string } { "dll" "a " { $link dll } " or " { $link f } } { "class" "a relocation class" } }
 { $description "Records that the most recently assembled instruction contains a reference to the " { $snippet "name" } " symbol from " { $snippet "dll" } ". The correct " { $snippet "class" } " to use depends on instruction formats."
index 920690e9d8117a1606e70b909b35cfbf864738e0..ad6cd3051c9f3409ac1d1f27f02c729097bd4700 100755 (executable)
@@ -2,7 +2,7 @@
 ! See http://factorcode.org/license.txt for BSD license.
 USING: arrays generic assocs hashtables
 kernel kernel.private math namespaces sequences words
-quotations strings alien layouts system combinators
+quotations strings alien.strings layouts system combinators
 math.bitfields words.private cpu.architecture ;
 IN: generator.fixup
 
@@ -110,10 +110,6 @@ SYMBOL: literal-table
 
 : add-literal ( obj -- n ) literal-table get push-new* ;
 
-: string>symbol ( str -- alien )
-    [ os wince? [ string>u16-alien ] [ string>char-alien ] if ]
-    over string? [ call ] [ map ] if ;
-
 : add-dlsym-literals ( symbol dll -- )
     >r string>symbol r> 2array literal-table get push-all ;
 
index 390dc28d8e611936b60194dca52f3ce51ee82417..b8de9c35176bb631b3ba2cdaa9e6fb37668c5e37 100755 (executable)
@@ -73,6 +73,7 @@ GENERIC: generate-node ( node -- next )
 : word-dataflow ( word -- effect dataflow )
     [
         dup "no-effect" word-prop [ no-effect ] when
+        dup "no-compile" word-prop [ no-effect ] when
         dup specialized-def over dup 2array 1array infer-quot
         finish-word
     ] with-infer ;
index a3198784eebc84fa0403221eb5eb5cb3b531dbf5..6a1d9ec0f443cf618664e45833e2cd2b0173beff 100755 (executable)
@@ -13,13 +13,6 @@ SYMBOL: +scratch+
 SYMBOL: +clobber+
 SYMBOL: known-tag
 
-! Register classes
-SINGLETON: int-regs
-SINGLETON: single-float-regs
-SINGLETON: double-float-regs
-UNION: float-regs single-float-regs double-float-regs ;
-UNION: reg-class int-regs float-regs ;
-
 <PRIVATE
 
 ! Value protocol
index c31c46f3f7c14fefb350039da31d2721a20b0cd8..1bff9ae15d716260360639e03b8bb4e96b0aa7fe 100644 (file)
@@ -3,7 +3,7 @@ USING: tools.test math math.functions math.constants
 generic.standard strings sequences arrays kernel accessors
 words float-arrays byte-arrays bit-arrays parser namespaces
 quotations inference vectors growable hashtables sbufs
-prettyprint ;
+prettyprint byte-vectors bit-vectors float-vectors ;
 
 GENERIC: lo-tag-test
 
index 2e471420da876f403712a74d93c93a501a5b594c..b68c98d25d5cdf2c34cbfce6befeb4cb1675ea3b 100755 (executable)
@@ -92,6 +92,8 @@ M: object infer-call
     peek-d infer-call
 ] "infer" set-word-prop
 
+\ call t "no-compile" set-word-prop
+
 \ execute [
     1 ensure-values
     pop-literal nip
@@ -471,18 +473,6 @@ set-primitive-effect
 
 \ set-alien-cell { c-ptr c-ptr integer } { } <effect> set-primitive-effect
 
-\ alien>char-string { c-ptr } { string } <effect> set-primitive-effect
-\ alien>char-string make-flushable
-
-\ string>char-alien { string } { byte-array } <effect> set-primitive-effect
-\ string>char-alien make-flushable
-
-\ alien>u16-string { c-ptr } { string } <effect> set-primitive-effect
-\ alien>u16-string make-flushable
-
-\ string>u16-alien { string } { byte-array } <effect> set-primitive-effect
-\ string>u16-alien make-flushable
-
 \ alien-address { alien } { integer } <effect> set-primitive-effect
 \ alien-address make-flushable
 
index bdd9e56d87df19a0bf065c1b6251cab45db1ac90..8a176ce4ec7db6b7a30df6d3b6ce5146e96c2074 100644 (file)
@@ -41,12 +41,13 @@ $low-level-note ;
 
 ARTICLE: "encodings-descriptors" "Encoding descriptors"
 "An encoding descriptor is something which can be used for input or output streams to encode or decode files. It must conform to the " { $link "encodings-protocol" } ". Encodings which you can use are defined in the following vocabularies:"
-{ $vocab-subsection "ASCII" "io.encodings.ascii" }
-{ $vocab-subsection "Binary" "io.encodings.binary" }
+{ $subsection "io.encodings.binary" }
+{ $subsection "io.encodings.utf8" }
+{ $subsection "io.encodings.utf16" }
 { $vocab-subsection "Strict encodings" "io.encodings.strict" }
+"Legacy encodings:"
 { $vocab-subsection "8-bit encodings" "io.encodings.8-bit" }
-{ $vocab-subsection "UTF-8" "io.encodings.utf8" }
-{ $vocab-subsection "UTF-16" "io.encodings.utf16" }
+{ $vocab-subsection "ASCII" "io.encodings.ascii" }
 { $see-also "encodings-introduction" } ;
 
 ARTICLE: "encodings-protocol" "Encoding protocol"
index 1666219db505cf3f979338eb0649871a9417e1f7..f37a9d1d5890ee47f2453ab8a04ff032a3c922c0 100644 (file)
@@ -5,8 +5,7 @@ ARTICLE: "io.encodings.utf16" "UTF-16"
 "The UTF-16 encoding is a variable-width encoding. Unicode code points are encoded as 2 or 4 byte sequences. There are three encoding descriptor classes for working with UTF-16, depending on endianness or the presence of a BOM:"
 { $subsection utf16 }
 { $subsection utf16le }
-{ $subsection utf16be }
-{ $subsection utf16n } ;
+{ $subsection utf16be } ;
 
 ABOUT: "io.encodings.utf16"
 
@@ -22,8 +21,4 @@ HELP: utf16
 { $class-description "The encoding descriptor for UTF-16, that is, UTF-16 with a byte order mark. This is the most useful for general input and output in UTF-16. Streams can be made which read or write wth this encoding." }
 { $see-also "encodings-introduction" } ;
 
-HELP: utf16n
-{ $class-description "The encoding descriptor for UTF-16 without a byte order mark in native endian order. This is useful mostly for FFI calls which take input of strings of the type wchar_t*" }
-{ $see-also "encodings-introduction" } ;
-
-{ utf16 utf16le utf16be utf16n } related-words
+{ utf16 utf16le utf16be } related-words
index 953671d7f426b4ded3fe4048af10ebd41f43ee7a..9093132e5f2dd041b451cf5b9fffbe30294a51e6 100755 (executable)
@@ -1,8 +1,7 @@
 ! Copyright (C) 2006, 2008 Daniel Ehrenberg.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: math kernel sequences sbufs vectors namespaces io.binary
-io.encodings combinators splitting io byte-arrays inspector
-alien.c-types ;
+io.encodings combinators splitting io byte-arrays inspector ;
 IN: io.encodings.utf16
 
 TUPLE: utf16be ;
@@ -11,8 +10,6 @@ TUPLE: utf16le ;
 
 TUPLE: utf16 ;
 
-TUPLE: utf16n ;
-
 <PRIVATE
 
 ! UTF-16BE decoding
@@ -124,13 +121,4 @@ M: utf16 <decoder> ( stream utf16 -- decoder )
 M: utf16 <encoder> ( stream utf16 -- encoder )
     drop bom-le over stream-write utf16le <encoder> ;
 
-! Native-order UTF-16
-
-: utf16n ( -- descriptor )
-    little-endian? utf16le utf16be ? ; foldable
-
-M: utf16n <decoder> drop utf16n <decoder> ;
-
-M: utf16n <encoder> drop utf16n <encoder> ;
-
 PRIVATE>
diff --git a/core/io/streams/memory/memory.factor b/core/io/streams/memory/memory.factor
new file mode 100644 (file)
index 0000000..daadbb0
--- /dev/null
@@ -0,0 +1,13 @@
+! Copyright (C) 2008 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: kernel accessors alien.accessors math io ;
+IN: io.streams.memory
+
+TUPLE: memory-stream alien index ;
+
+: <memory-stream> ( alien -- stream )
+    0 memory-stream boa ;
+
+M: memory-stream stream-read1
+    [ [ alien>> ] [ index>> ] bi alien-unsigned-1 ]
+    [ [ 1+ ] change-index drop ] bi ;
index 1474f51c5316956a462b2d8bce517909e1ce2152..6a649bc5a688b1b9430bfb755a67c4ce5669cf2a 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2003, 2007 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 IN: prettyprint.config
-USING: alien arrays generic assocs io kernel math
+USING: arrays generic assocs io kernel math
 namespaces sequences strings io.styles vectors words
 continuations ;
 
index 525749cfae148e09b3beb7b026d0ccfe934fd3fa..981c8dcfd04447dda4917e7b9c93e09b3e8b3d6a 100755 (executable)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2003, 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 IN: prettyprint
-USING: alien arrays generic generic.standard assocs io kernel
+USING: arrays generic generic.standard assocs io kernel
 math namespaces sequences strings io.styles io.streams.string
 vectors words prettyprint.backend prettyprint.sections
 prettyprint.config sorting splitting math.parser vocabs
index 319e5eab658e93675a231a8bd97e6028b3110b3f..803f6e24599451ae75095f8e3ede4eee5ff43670 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2003, 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien arrays generic hashtables io kernel math assocs
+USING: arrays generic hashtables io kernel math assocs
 namespaces sequences strings io.styles vectors words
 prettyprint.config splitting classes continuations
 io.streams.nested accessors ;
diff --git a/extra/benchmark/binary-trees/binary-trees.factor b/extra/benchmark/binary-trees/binary-trees.factor
new file mode 100644 (file)
index 0000000..e82f9db
--- /dev/null
@@ -0,0 +1,49 @@
+USING: kernel math accessors prettyprint io locals sequences
+math.ranges ;
+IN: benchmark.binary-trees
+
+TUPLE: tree-node item left right ;
+
+C: <tree-node> tree-node
+
+: bottom-up-tree ( item depth -- tree )
+    dup 0 > [
+        1 -
+        [ drop ]
+        [ >r 2 * 1 - r> bottom-up-tree ]
+        [ >r 2 *     r> bottom-up-tree ] 2tri
+    ] [
+        drop f f
+    ] if <tree-node> ;
+
+GENERIC: item-check ( node -- n )
+
+M: tree-node item-check
+    [ item>> ] [ left>> ] [ right>> ] tri [ item-check ] bi@ - + ;
+
+M: f item-check drop 0 ;
+
+: min-depth 4 ; inline
+
+: stretch-tree ( max-depth -- )
+    1 + 0 over bottom-up-tree item-check
+    [ "stretch tree of depth " write pprint ]
+    [ "\t check: " write ] bi* ;
+
+:: long-lived-tree ( max-depth -- )
+    0 max-depth bottom-up-tree
+
+    min-depth max-depth 2 <range> [| depth |
+        max-depth depth - min-depth + 2^ [
+            [1,b] 0 [
+                [ depth ] [ depth neg ] bi
+                [ bottom-up-tree item-check + ] 2bi@
+            ] reduce
+        ]
+        [ 2 * ] bi
+        pprint "\t trees of depth " write depth pprint
+        "\t check: " write .
+    ] each
+
+    "long lived tree of depth " write max-depth pprint
+    "\t check: " write item-check . ;
index ca9509c3ec917bd8ee16939af932d04c2073ce9c..df3f84d45121b203dc013ae1db66f23eff329c02 100755 (executable)
@@ -1,10 +1,10 @@
-! Copyright (C) 2006, 2007 Slava Pestov.
+! Copyright (C) 2006, 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.c-types alien.compiler
+USING: alien alien.c-types alien.strings alien.compiler
 arrays assocs combinators compiler inference.transforms kernel
 math namespaces parser prettyprint prettyprint.sections
 quotations sequences strings words cocoa.runtime io macros
-memoize debugger ;
+memoize debugger io.encodings.ascii ;
 IN: cocoa.messages
 
 : make-sender ( method function -- quot )
@@ -104,7 +104,7 @@ MACRO: (send) ( selector super? -- quot )
 : method-arg-type ( method i -- type )
     f <void*> 0 <int> over
     >r method_getArgumentInfo drop
-    r> *char* ;
+    r> *void* ascii alien>string ;
 
 SYMBOL: objc>alien-types
 
index 48f45f21c0e3ac28a4e9f8b0dad6945d1ecd6931..6b3e1d330ee155b3ecbd0482806d33e90f9577c9 100755 (executable)
@@ -1,14 +1,15 @@
 ! Copyright (C) 2006, 2008 Slava Pestov
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.c-types arrays assocs combinators compiler
-hashtables kernel libc math namespaces parser sequences words
-cocoa.messages cocoa.runtime compiler.units ;
+USING: alien alien.c-types alien.strings arrays assocs
+combinators compiler hashtables kernel libc math namespaces
+parser sequences words cocoa.messages cocoa.runtime
+compiler.units io.encodings.ascii ;
 IN: cocoa.subclassing
 
 : init-method ( method alien -- )
     >r first3 r>
     [ >r execute r> set-objc-method-imp ] keep
-    [ >r malloc-char-string r> set-objc-method-types ] keep
+    [ >r ascii malloc-string r> set-objc-method-types ] keep
     >r sel_registerName r> set-objc-method-name ;
 
 : <empty-method-list> ( n -- alien )
@@ -26,7 +27,7 @@ IN: cocoa.subclassing
 : <objc-class> ( name info -- class )
     "objc-class" malloc-object
     [ set-objc-class-info ] keep
-    [ >r malloc-char-string r> set-objc-class-name ] keep ;
+    [ >r ascii malloc-string r> set-objc-class-name ] keep ;
 
 : <protocol-list> ( name -- protocol-list )
     "objc-protocol-list" malloc-object
index 77ad30ad8ff4acdbc4793d1795f8183c569e3f9b..a4bd24ccca94b602ab43a814d6a32f17fba465e3 100644 (file)
@@ -1,6 +1,7 @@
 ! Copyright (C) 2006, 2008 Slava Pestov
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.c-types alien.syntax kernel math sequences ;
+USING: alien alien.c-types alien.strings alien.syntax kernel
+math sequences io.encodings.utf16 ;
 IN: core-foundation
 
 TYPEDEF: void* CFAllocatorRef
@@ -31,7 +32,7 @@ FUNCTION: CFURLRef CFURLCreateWithString ( CFAllocatorRef allocator, CFStringRef
 
 FUNCTION: CFURLRef CFURLCopyFileSystemPath ( CFURLRef url, int pathStyle ) ;
 
-FUNCTION: CFStringRef CFStringCreateWithCharacters ( CFAllocatorRef allocator, ushort* cStr, CFIndex numChars ) ;
+FUNCTION: CFStringRef CFStringCreateWithCharacters ( CFAllocatorRef allocator, wchar_t* cStr, CFIndex numChars ) ;
 
 FUNCTION: CFIndex CFStringGetLength ( CFStringRef theString ) ;
 
@@ -57,7 +58,7 @@ FUNCTION: void CFRelease ( void* cf ) ;
 : CF>string ( alien -- string )
     dup CFStringGetLength 1+ "ushort" <c-array> [
         >r 0 over CFStringGetLength r> CFStringGetCharacters
-    ] keep alien>u16-string ;
+    ] keep utf16n alien>string ;
 
 : CF>string-array ( alien -- seq )
     CF>array [ CF>string ] map ;
index 3c9dbdbef021928e24871ecd6a6d481b07e06de9..67a4e59d04151ba90e840319da4a156efcbed380 100644 (file)
@@ -1,8 +1,9 @@
 ! Copyright (C) 2008 Slava Pestov
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.c-types alien.syntax kernel math sequences
-namespaces assocs init accessors continuations combinators
-core-foundation core-foundation.run-loop ;
+USING: alien alien.c-types alien.strings alien.syntax kernel
+math sequences namespaces assocs init accessors continuations
+combinators core-foundation core-foundation.run-loop
+io.encodings.utf8 ;
 IN: core-foundation.fsevents
 
 ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
@@ -165,7 +166,7 @@ SYMBOL: event-stream-callbacks
 : >event-triple ( n eventPaths eventFlags eventIds -- triple )
     [
         >r >r >r dup dup
-        r> char*-nth ,
+        r> void*-nth utf8 alien>string ,
         r> int-nth ,
         r> longlong-nth ,
     ] { } make ;
index 91838d2a53f194ca4cf30baed31adcce4b9ae7e8..fe1fd72a21437133628a4f2a55e31eb121bf657d 100644 (file)
@@ -1,6 +1,7 @@
-USING: alien alien.c-types alien.syntax byte-arrays kernel
-namespaces sequences unix hardware-info.backend system
-io.unix.backend ;
+USING: alien alien.c-types alien.strings alien.syntax
+byte-arrays kernel namespaces sequences unix
+hardware-info.backend system io.unix.backend io.encodings.ascii
+;
 IN: hardware-info.macosx
 
 ! See /usr/include/sys/sysctl.h for constants
@@ -19,7 +20,7 @@ FUNCTION: int sysctl ( int* name, uint namelen, void* oldp, size_t* oldlenp, voi
     [ <byte-array> ] [ <uint> ] bi (sysctl-query) ;
 
 : sysctl-query-string ( seq -- n )
-    4096 sysctl-query alien>char-string ;
+    4096 sysctl-query ascii malloc-string ;
 
 : sysctl-query-uint ( seq -- n )
     4 sysctl-query *uint ;
index ba9c1d74b516fe29248f713287073e01e1b64314..2599a33754635672ea80dff94f7e0655dbe88377 100755 (executable)
@@ -1,4 +1,4 @@
-USING: alien alien.c-types
+USING: alien alien.c-types alien.strings
 kernel libc math namespaces hardware-info.backend
 windows windows.advapi32 windows.kernel32 system ;
 IN: hardware-info.windows.nt
@@ -35,12 +35,14 @@ M: winnt total-virtual-mem ( -- n )
 M: winnt available-virtual-mem ( -- n )
     memory-status MEMORYSTATUSEX-ullAvailVirtual ;
 
+: pull-win32-string [ utf16n alien>string ] keep free ;
+
 : computer-name ( -- string )
     MAX_COMPUTERNAME_LENGTH 1+ [ malloc ] keep
     <int> dupd GetComputerName zero? [
         free win32-error f
     ] [
-        [ alien>u16-string ] keep free
+        pull-win32-string
     ] if ;
  
 : username ( -- string )
@@ -48,5 +50,5 @@ M: winnt available-virtual-mem ( -- n )
     <int> dupd GetUserName zero? [
         free win32-error f
     ] [
-        [ alien>u16-string ] keep free
+        pull-win32-string
     ] if ;
index 807fd158baea16682b912f4ece057f579be326e6..10474c09f75e393132072bc0d2015e3eeed9c10e 100755 (executable)
@@ -36,7 +36,7 @@ IN: hardware-info.windows
     os-version OSVERSIONINFO-dwPlatformId ;
 
 : windows-service-pack ( -- string )
-    os-version OSVERSIONINFO-szCSDVersion alien>u16-string ;
+    os-version OSVERSIONINFO-szCSDVersion utf16n alien>string ;
 
 : feature-present? ( n -- ? )
     IsProcessorFeaturePresent zero? not ;
@@ -52,7 +52,7 @@ IN: hardware-info.windows
 
 : get-directory ( word -- str )
     >r MAX_UNICODE_PATH [ <u16-string-object> ] keep dupd r>
-    execute win32-error=0/f alien>u16-string ; inline
+    execute win32-error=0/f utf16n alien>string ; inline
 
 : windows-directory ( -- str )
     \ GetWindowsDirectory get-directory ;
index d5bc1875e44696ddc8a943238d8c7e45bd10d64a..15e3b8be1d48efe2596088bd8d28bf0f7c279464 100755 (executable)
@@ -163,7 +163,7 @@ ARTICLE: "collections" "Collections"
 { $subsection "buffers" } ;
 
 USING: io.sockets io.launcher io.mmap io.monitors
-io.encodings.utf8 io.encodings.binary io.encodings.ascii io.files ;
+io.encodings.utf8 io.encodings.utf16 io.encodings.binary io.encodings.ascii io.files ;
 
 ARTICLE: "encodings-introduction" "An introduction to encodings"
 "In order to express text in terms of binary, some sort of encoding has to be used. In a modern context, this is understood as a two-way mapping between Unicode code points (characters) and some amount of binary. Since English isn't the only language in the world, ASCII is not sufficient as a mapping from binary to Unicode; it can't even express em-dashes or curly quotes. Unicode was designed as a universal character set that could potentially represent everything." $nl
index 498430fdbc84108db12459fed0c91b9be0182f17..2a376e18c2cc7cb25525d1984df53390dd6966c2 100755 (executable)
@@ -1,8 +1,9 @@
-! Copyright (C) 2007 Doug Coleman, Slava Pestov
+! Copyright (C) 2007, 2008 Doug Coleman, Slava Pestov
 ! See http://factorcode.org/license.txt for BSD license.
 USING: arrays byte-arrays io.backend io.binary io.sockets
-kernel math math.parser sequences splitting system
-alien.c-types combinators namespaces alien parser ;
+io.encodings.ascii kernel math math.parser sequences splitting
+system alien.c-types alien.strings alien combinators namespaces
+parser ;
 IN: io.sockets.impl
 
 << {
@@ -130,4 +131,4 @@ M: object resolve-host ( host serv passive? -- seq )
 M: object host-name ( -- name )
     256 <byte-array> dup dup length gethostname
     zero? [ "gethostname failed" throw ] unless
-    alien>char-string ;
+    ascii alien>string ;
index 58c1f0110cd00d1ed57c67bf9438bee6871d2e64..219e0d09a0257cca210f0919c857b5f187a68e5e 100644 (file)
@@ -2,9 +2,10 @@
 ! See http://factorcode.org/license.txt for BSD license.
 USING: kernel io.backend io.monitors io.monitors.recursive
 io.files io.buffers io.monitors io.nonblocking io.timeouts
-io.unix.backend io.unix.select unix.linux.inotify assocs
-namespaces threads continuations init math math.bitfields sets
-alien.c-types alien vocabs.loader accessors system hashtables ;
+io.unix.backend io.unix.select io.encodings.utf8
+unix.linux.inotify assocs namespaces threads continuations init
+math math.bitfields sets alien.strings alien vocabs.loader
+accessors system hashtables ;
 IN: io.unix.linux.monitors
 
 TUPLE: linux-monitor < monitor wd ;
@@ -79,7 +80,7 @@ M: linux-monitor dispose ( monitor -- )
     dup inotify-event-mask ignore-flags? [
         drop f f
     ] [
-        [ inotify-event-name alien>char-string ]
+        [ inotify-event-name utf8 alien>string ]
         [ inotify-event-mask parse-action ] bi
     ] if ;
 
index cecc70fb0825a7d65b90f6214febcfff6958d609..b60cb5760e42a506c23c1be45b500d2cd959c36c 100755 (executable)
@@ -1,13 +1,15 @@
 ! Copyright (C) 2004, 2008 Slava Pestov, Ivan Tikhonov. 
 ! See http://factorcode.org/license.txt for BSD license.
+USING: alien alien.c-types alien.strings generic kernel math
+namespaces threads sequences byte-arrays io.nonblocking
+io.binary io.unix.backend io.streams.duplex io.sockets.impl
+io.backend io.files io.files.private io.encodings.utf8
+math.parser continuations libc combinators system accessors
+qualified unix ;
+
+EXCLUDE: io => read write close ;
+EXCLUDE: io.sockets => accept ;
 
-! We need to fiddle with the exact search order here, since
-! unix::accept shadows streams::accept.
-USING: alien alien.c-types generic io kernel math namespaces
-io.nonblocking parser threads unix sequences
-byte-arrays io.sockets io.binary io.unix.backend
-io.streams.duplex io.sockets.impl math.parser continuations libc
-combinators io.backend io.files io.files.private system accessors ;
 IN: io.unix.sockets
 
 : pending-init-error ( port -- )
@@ -36,7 +38,7 @@ TUPLE: connect-task < output-task ;
     connect-task <io-task> ;
 
 M: connect-task do-io-task
-    io-task-port dup port-handle f 0 write
+    port>> dup handle>> f 0 write
     0 < [ defer-error ] [ drop t ] if ;
 
 : wait-to-connect ( port -- )
@@ -56,8 +58,6 @@ M: unix ((client)) ( addrspec -- client-in client-out )
     ] if ;
 
 ! Server sockets - TCP and Unix domain
-USE: unix
-
 : init-server-socket ( fd -- )
     SOL_SOCKET SO_REUSEADDR sockopt ;
 
@@ -83,8 +83,6 @@ M: accept-task do-io-task
 : wait-to-accept ( server -- )
     [ <accept-task> add-io-task ] with-port-continuation drop ;
 
-USE: io.sockets
-
 : server-fd ( addrspec type -- fd )
     >r dup protocol-family r>  socket-fd
     dup init-server-socket
@@ -187,12 +185,12 @@ M: local protocol-family drop PF_UNIX ;
 M: local sockaddr-type drop "sockaddr-un" c-type ;
 
 M: local make-sockaddr
-    local-path cwd prepend-path
+    path>> (normalize-path)
     dup length 1 + max-un-path > [ "Path too long" throw ] when
     "sockaddr-un" <c-object>
     AF_UNIX over set-sockaddr-un-family
-    dup sockaddr-un-path rot string>char-alien dup length memcpy ;
+    dup sockaddr-un-path rot utf8 string>alien dup length memcpy ;
 
 M: local parse-sockaddr
     drop
-    sockaddr-un-path alien>char-string <local> ;
+    sockaddr-un-path utf8 alien>string <local> ;
index eec473e8403f32c7618e7bc5106446feb6433037..32126443f434b0047e4dd1207603c6e1d23a4251 100755 (executable)
@@ -9,7 +9,7 @@ IN: io.windows.nt.files
 M: winnt cwd
     MAX_UNICODE_PATH dup "ushort" <c-array>
     [ GetCurrentDirectory win32-error=0/f ] keep
-    alien>u16-string ;
+    utf16n alien>string ;
 
 M: winnt cd
     SetCurrentDirectory win32-error=0/f ;
index a51216b079323f42cde09c7caec255efcf57f411..10bed8b5df7b9aacc714c3f2b8c233c4cbd269e6 100644 (file)
@@ -35,3 +35,8 @@ IN: locals.backend
     [ infer-r> ]
     [ { } <effect> infer-shuffle ] bi
 ] "infer" set-word-prop
+
+<<
+{ load-locals get-local drop-locals }
+[ t "no-compile" set-word-prop ] each
+>>
index 81b7f634276fbaf8b89448884dfea37dfa03a8c4..ca69e787264e94634b9a40f3960ced174535bbc9 100755 (executable)
@@ -1,4 +1,5 @@
-USING: kernel layouts math namespaces sequences sequences.private ;
+USING: kernel layouts math namespaces sequences
+sequences.private accessors ;
 IN: math.ranges
 
 TUPLE: range from length step ;
@@ -9,10 +10,10 @@ TUPLE: range from length step ;
     range boa ;
 
 M: range length ( seq -- n )
-    range-length ;
+    length>> ;
 
 M: range nth-unsafe ( n range -- obj )
-    [ range-step * ] keep range-from + ;
+    [ step>> * ] keep from>> + ;
 
 INSTANCE: range immutable-sequence
 
@@ -37,10 +38,10 @@ INSTANCE: range immutable-sequence
 : [0,b) ( b -- range ) 0 swap [a,b) ;
 
 : range-increasing? ( range -- ? )
-    range-step 0 > ;
+    step>> 0 > ;
 
 : range-decreasing? ( range -- ? )
-    range-step 0 < ;
+    step>> 0 < ;
 
 : first-or-peek ( seq head? -- elt )
     [ first ] [ peek ] if ;
@@ -52,7 +53,7 @@ INSTANCE: range immutable-sequence
     dup range-decreasing? first-or-peek ;
 
 : clamp-to-range ( n range -- n )
-    tuck range-min max swap range-max min ;
+    [ min>> max ] [ max>> min ] bi ;
 
 : sequence-index-range  ( seq -- range )
     length [0,b) ;
index 59f5095aad5fe1e13c3cdee328db5e6bc9904a44..0bcd639bc1b96e476fe5d106d2d80ccf0ae6b31a 100644 (file)
@@ -1,10 +1,11 @@
 ! Copyright (C) 2007 Chris Double.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel alien alien.syntax combinators alien.c-types
-       strings sequences namespaces words math threads ;
+USING: kernel alien alien.strings alien.syntax combinators
+alien.c-types strings sequences namespaces words math threads
+io.encodings.ascii ;
 IN: odbc
 
-"odbc" "odbc32.dll" "stdcall" add-library
+<< "odbc" "odbc32.dll" "stdcall" add-library >>
 
 LIBRARY: odbc
 
@@ -150,7 +151,7 @@ FUNCTION: SQLRETURN SQLGetData ( SQLHSTMT statementHandle, SQLUSMALLINT columnNu
   SQL-HANDLE-STMT swap alloc-handle ;
 
 : temp-string ( length -- byte-array length )
-  [ CHAR: \space  <string> string>char-alien ] keep ;
+  [ CHAR: \space  <string> ascii string>alien ] keep ;
 
 : odbc-init ( -- env )
   alloc-env-handle
@@ -192,7 +193,7 @@ C: <column> column
 
 : odbc-describe-column ( statement n -- column )
   dup >r
-  1024 CHAR: \space <string> string>char-alien dup >r
+  1024 CHAR: \space <string> ascii string>alien dup >r
   1024
   0 <short>
   0 <short> dup >r
@@ -204,7 +205,7 @@ C: <column> column
     r> *short
     r> *uint
     r> *short convert-sql-type
-    r> alien>char-string
+    r> ascii alien>string
     r> <column>
   ] [
     r> drop r> drop r> drop r> drop r> drop r> drop
@@ -213,12 +214,12 @@ C: <column> column
 
 : dereference-type-pointer ( byte-array column -- object )
   column-type {
-    { SQL-CHAR [ alien>char-string ] }
-    { SQL-VARCHAR [ alien>char-string ] }
-    { SQL-LONGVARCHAR [ alien>char-string ] }
-    { SQL-WCHAR [ alien>char-string ] }
-    { SQL-WCHARVAR [ alien>char-string ] }
-    { SQL-WLONGCHARVAR [ alien>char-string ] }
+    { SQL-CHAR [ ascii alien>string ] }
+    { SQL-VARCHAR [ ascii alien>string ] }
+    { SQL-LONGVARCHAR [ ascii alien>string ] }
+    { SQL-WCHAR [ ascii alien>string ] }
+    { SQL-WCHARVAR [ ascii alien>string ] }
+    { SQL-WLONGCHARVAR [ ascii alien>string ] }
     { SQL-SMALLINT [ *short ] }
     { SQL-INTEGER [ *long ] }
     { SQL-REAL [ *float ] }
@@ -236,7 +237,7 @@ C: <field> field
 : odbc-get-field ( statement column -- field )
   dup column? [ dupd odbc-describe-column ] unless dup >r column-number
   SQL-C-DEFAULT
-  8192 CHAR: \space <string> string>char-alien dup >r
+  8192 CHAR: \space <string> ascii string>alien dup >r
   8192
   f SQLGetData succeeded? [
     r> r> [ dereference-type-pointer ] keep <field>
index e352eabc10717bb5217b9d86dc7324b3b2c998a1..c05e180c115e889746aa1776a51fa4ccdd22fb17 100755 (executable)
@@ -1,14 +1,12 @@
 ! Copyright (C) 2008 Joe Groff.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: kernel opengl.gl alien.c-types continuations namespaces
-assocs alien libc opengl math sequences combinators
-combinators.lib macros arrays ;
+assocs alien alien.strings libc opengl math sequences combinators
+combinators.lib macros arrays io.encodings.ascii ;
 IN: opengl.shaders
 
 : with-gl-shader-source-ptr ( string quot -- )
-    swap string>char-alien malloc-byte-array [
-        <void*> swap call
-    ] keep free ; inline
+    swap ascii malloc-string [ <void*> swap call ] keep free ; inline
 
 : <gl-shader> ( source kind -- shader )
     glCreateShader dup rot
@@ -47,7 +45,7 @@ IN: opengl.shaders
 : gl-shader-info-log ( shader -- log )
     dup gl-shader-info-log-length dup [
         [ 0 <int> swap glGetShaderInfoLog ] keep
-        alien>char-string
+        ascii alien>string
     ] with-malloc ;
 
 : check-gl-shader ( shader -- shader )
@@ -82,7 +80,7 @@ PREDICATE: fragment-shader < gl-shader (fragment-shader?) ;
 : gl-program-info-log ( program -- log )
     dup gl-program-info-log-length dup [
         [ 0 <int> swap glGetProgramInfoLog ] keep
-        alien>char-string
+        ascii alien>string
     ] with-malloc ;
 
 : check-gl-program ( program -- program )
index c85c0ee21839ca2509fc3dfea73f971fe8442f51..a392589211745552b7ba86efd14274b653f614b1 100755 (executable)
@@ -31,7 +31,7 @@ namespaces math math.parser openssl prettyprint sequences tools.test ;
 ! TODO: debug 'Memory protection fault at address 6c'
 ! get-ctx 1024 "char" malloc-array 1024 0 f password-cb set-default-passwd
 
-[ ] [ get-ctx "password" string>char-alien set-default-passwd-userdata ] unit-test
+[ ] [ get-ctx "password" ascii string>alien set-default-passwd-userdata ] unit-test
 
 ! Enter PEM pass phrase: password
 [ ] [ get-ctx "extra/openssl/test/server.pem" resource-path
index bfa7f3259489f9bfdf1eaf38d9ae523f152e62be..9b237745982451030fea710146fb833282ea1933 100755 (executable)
@@ -3,8 +3,9 @@
 !
 ! Tested with OpenSSL 0.9.8a_0 on Mac OS X 10.4.9 PowerPC
 
-USING: alien alien.c-types assocs kernel libc namespaces
-openssl.libcrypto openssl.libssl sequences ;
+USING: alien alien.c-types alien.strings assocs kernel libc
+namespaces openssl.libcrypto openssl.libssl sequences
+io.encodings.ascii ;
 
 IN: openssl
 
@@ -21,7 +22,7 @@ SYMBOL: rsa
 
 : password-cb ( -- alien )
     "int" { "char*" "int" "int" "void*" } "cdecl"
-    [ 3drop "password" string>char-alien 1023 memcpy
+    [ 3drop "password" ascii string>alien 1023 memcpy
     "password" length ] alien-callback ;
 
 ! =========================================================
index 44b746f8ce792f78c86e7f5dd5bded43e0d3194c..8ef169810af1ca5e35122d8a8f084b1ee27f6ccb 100644 (file)
@@ -4,8 +4,9 @@
 ! Adapted from oci.h and ociap.h
 ! Tested with Oracle version - 10.1.0.3 Instant Client
 
-USING: alien alien.c-types combinators kernel math namespaces oracle.liboci
-prettyprint sequences ;
+USING: alien alien.c-types alien.strings combinators kernel math
+namespaces oracle.liboci prettyprint sequences
+io.encodings.ascii ;
 
 IN: oracle
 
@@ -31,7 +32,7 @@ C: <connection> connection
 : get-oci-error ( object -- * )
     1 f "uint*" <c-object> dup >r 512 "uchar" <c-array> dup >r
     512 OCI_HTYPE_ERROR OCIErrorGet r> r> *uint drop
-    alien>char-string throw ;
+    ascii alien>string throw ;
 
 : check-result ( result -- )
     {
@@ -101,9 +102,9 @@ C: <connection> connection
 
 : oci-log-on ( -- )
     env get err get svc get 
-    con get connection-username dup length swap malloc-char-string swap 
-    con get connection-password dup length swap malloc-char-string swap
-    con get connection-db dup length swap malloc-char-string swap
+    con get connection-username dup length swap ascii malloc-string swap 
+    con get connection-password dup length swap ascii malloc-string swap
+    con get connection-db dup length swap ascii malloc-string swap
     OCILogon check-result ;
 
 ! =========================================================
@@ -118,11 +119,11 @@ C: <connection> connection
     svc get OCI_HTYPE_SVCCTX srv get 0 OCI_ATTR_SERVER err get OCIAttrSet check-result ;
 
 : set-username-attribute ( -- )
-    ses get OCI_HTYPE_SESSION con get connection-username dup length swap malloc-char-string swap 
+    ses get OCI_HTYPE_SESSION con get connection-username dup length swap ascii malloc-string swap 
     OCI_ATTR_USERNAME err get OCIAttrSet check-result ;
 
 : set-password-attribute ( -- )
-    ses get OCI_HTYPE_SESSION con get connection-password dup length swap malloc-char-string swap 
+    ses get OCI_HTYPE_SESSION con get connection-password dup length swap ascii malloc-string swap 
     OCI_ATTR_PASSWORD err get OCIAttrSet check-result ;
 
 : set-attributes ( -- )
@@ -150,7 +151,7 @@ C: <connection> connection
     check-result *void* stm set ;
 
 : prepare-statement ( statement -- )
-    >r stm get err get r> dup length swap malloc-char-string swap
+    >r stm get err get r> dup length swap ascii malloc-string swap
     OCI_NTV_SYNTAX OCI_DEFAULT OCIStmtPrepare check-result ;
 
 : calculate-size ( type -- size )
@@ -222,7 +223,7 @@ C: <connection> connection
 
 : server-version ( -- )
     srv get err get 512 "uchar" malloc-array dup >r 512 OCI_HTYPE_SERVER
-    OCIServerVersion check-result r> alien>char-string . ;
+    OCIServerVersion check-result r> ascii alien>string . ;
 
 ! =========================================================
 ! Public routines
@@ -236,13 +237,13 @@ C: <connection> connection
 
 : fetch-each ( object -- object )
     fetch-statement [
-        buf get alien>char-string res get swap suffix res set
+        buf get ascii alien>string res get swap suffix res set
         fetch-each
     ] [ ] if ;
 
 : run-query ( object -- object )
     execute-statement [
-        buf get alien>char-string res get swap suffix res set
+        buf get ascii alien>string res get swap suffix res set
         fetch-each
     ] [ ] if ;
 
index 6229fc9a6555973b07c8f002c9b4590d41ceb938..e3e1fc51249291df65d9de02cd64fcb05241f2a5 100755 (executable)
@@ -1,13 +1,14 @@
 ! Copyright (C) 2005, 2006 Doug Coleman.
+! Portions copyright (C) 2007, 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.c-types arrays assocs ui ui.gadgets
-ui.backend ui.clipboards ui.gadgets.worlds ui.gestures io kernel
-math math.vectors namespaces prettyprint sequences strings
-vectors words windows.kernel32 windows.gdi32 windows.user32
-windows.opengl32 windows.messages windows.types windows.nt
-windows threads libc combinators continuations command-line
-shuffle opengl ui.render unicode.case ascii math.bitfields
-locals symbols accessors ;
+USING: alien alien.c-types alien.strings arrays assocs ui
+ui.gadgets ui.backend ui.clipboards ui.gadgets.worlds
+ui.gestures io kernel math math.vectors namespaces prettyprint
+sequences strings vectors words windows.kernel32 windows.gdi32
+windows.user32 windows.opengl32 windows.messages windows.types
+windows.nt windows threads libc combinators continuations
+command-line shuffle opengl ui.render unicode.case ascii
+math.bitfields locals symbols accessors ;
 IN: ui.windows
 
 SINGLETON: windows-ui-backend
@@ -36,14 +37,14 @@ SINGLETON: windows-ui-backend
             CF_UNICODETEXT GetClipboardData dup win32-error=0/f
             dup GlobalLock dup win32-error=0/f
             GlobalUnlock win32-error=0/f
-            alien>u16-string
+            utf16n alien>string
         ] if
     ] with-clipboard
     crlf>lf ;
 
 : copy ( str -- )
     lf>crlf [
-        string>u16-alien
+        utf16n string>alien
         EmptyClipboard win32-error=0/f
         GMEM_MOVEABLE over length 1+ GlobalAlloc
             dup win32-error=0/f
@@ -409,7 +410,7 @@ SYMBOL: trace-messages?
         0 over set-WNDCLASSEX-cbClsExtra
         0 over set-WNDCLASSEX-cbWndExtra
         f GetModuleHandle over set-WNDCLASSEX-hInstance
-        f GetModuleHandle "fraptor" string>u16-alien LoadIcon
+        f GetModuleHandle "fraptor" utf16n string>alien LoadIcon
         over set-WNDCLASSEX-hIcon
         f IDC_ARROW LoadCursor over set-WNDCLASSEX-hCursor
 
@@ -447,7 +448,7 @@ SYMBOL: trace-messages?
 : init-win32-ui ( -- )
     V{ } clone nc-buttons set-global
     "MSG" malloc-object msg-obj set-global
-    "Factor-window" malloc-u16-string class-name-ptr set-global
+    "Factor-window" utf16n malloc-string class-name-ptr set-global
     register-wndclassex drop
     GetDoubleClickTime double-click-timeout set-global ;
 
@@ -492,7 +493,7 @@ M: windows-ui-backend raise-window* ( world -- )
 M: windows-ui-backend set-title ( string world -- )
     world-handle
     dup win-title [ free ] when*
-    >r malloc-u16-string r>
+    >r utf16n malloc-string r>
     2dup set-win-title
     win-hWnd WM_SETTEXT 0 roll alien-address SendMessage drop ;
 
index c04427185390bed4441730d029297eb76f9f1afa..0937cd46ed717372df9d7ed0e0c2f97da3207b65 100755 (executable)
@@ -137,8 +137,8 @@ M: world selection-notify-event
     } cond ;
 
 : encode-clipboard ( string type -- bytes )
-    XSelectionRequestEvent-target XA_UTF8_STRING =
-    [ utf8 encode ] [ string>char-alien ] if ;
+    XSelectionRequestEvent-target
+    XA_UTF8_STRING = utf8 ascii ? encode ;
 
 : set-selection-prop ( evt -- )
     dpy get swap
index 31adc5c23767f46069b4da6ef02bc7ab98284313..a6cb90ba063dc4058909a5ebdd22237e77862d98 100755 (executable)
@@ -10,7 +10,7 @@ IN: unix.linux.ifreq
 
 : set-if-addr ( name addr -- )
   "struct-ifreq" <c-object>
-  rot  string>char-alien        over set-struct-ifreq-ifr-ifrn
+  rot  ascii string>alien       over set-struct-ifreq-ifr-ifrn
   swap 0 <inet4> make-sockaddr  over set-struct-ifreq-ifr-ifru
 
   AF_INET SOCK_DGRAM 0 socket SIOCSIFADDR rot ioctl drop ;
@@ -19,7 +19,7 @@ IN: unix.linux.ifreq
 
 : set-if-flags ( name flags -- )
   "struct-ifreq" <c-object>
-  rot  string>char-alien over set-struct-ifreq-ifr-ifrn
+  rot  ascii string>alien over set-struct-ifreq-ifr-ifrn
   swap <short>          over set-struct-ifreq-ifr-ifru
 
   AF_INET SOCK_DGRAM 0 socket SIOCSIFFLAGS rot ioctl drop ;
@@ -28,7 +28,7 @@ IN: unix.linux.ifreq
 
 : set-if-dst-addr ( name addr -- )
   "struct-ifreq" <c-object>
-  rot  string>char-alien        over set-struct-ifreq-ifr-ifrn
+  rot  ascii string>alien       over set-struct-ifreq-ifr-ifrn
   swap 0 <inet4> make-sockaddr  over set-struct-ifreq-ifr-ifru
 
   AF_INET SOCK_DGRAM 0 socket SIOCSIFDSTADDR rot ioctl drop ;
@@ -37,7 +37,7 @@ IN: unix.linux.ifreq
 
 : set-if-brd-addr ( name addr -- )
   "struct-ifreq" <c-object>
-  rot  string>char-alien        over set-struct-ifreq-ifr-ifrn
+  rot  ascii string>alien       over set-struct-ifreq-ifr-ifrn
   swap 0 <inet4> make-sockaddr  over set-struct-ifreq-ifr-ifru
 
   AF_INET SOCK_DGRAM 0 socket SIOCSIFBRDADDR rot ioctl drop ;
@@ -46,7 +46,7 @@ IN: unix.linux.ifreq
 
 : set-if-netmask ( name addr -- )
   "struct-ifreq" <c-object>
-  rot  string>char-alien        over set-struct-ifreq-ifr-ifrn
+  rot  ascii string>alien       over set-struct-ifreq-ifr-ifrn
   swap 0 <inet4> make-sockaddr  over set-struct-ifreq-ifr-ifru
 
   AF_INET SOCK_DGRAM 0 socket SIOCSIFNETMASK rot ioctl drop ;
index ba02f15c7a29e0ca12e645d9967c6773d2bcc6bd..0abefe14f1f8c05169b0f39e9c74f202ddabe080 100755 (executable)
@@ -1,7 +1,6 @@
-USING: kernel alien.c-types sequences math unix
-vectors kernel namespaces continuations
-threads assocs vectors io.unix.backend ;
-
+USING: kernel alien.c-types alien.strings sequences math unix
+vectors kernel namespaces continuations threads assocs vectors
+io.unix.backend io.encodings.utf8 ;
 IN: unix.process
 
 ! Low-level Unix process launching utilities. These are used
@@ -9,16 +8,16 @@ IN: unix.process
 ! io.launcher instead.
 
 : >argv ( seq -- alien )
-    [ malloc-char-string ] map f suffix >c-void*-array ;
+    [ utf8 malloc-string ] map f suffix >c-void*-array ;
 
 : exec ( pathname argv -- int )
-    [ malloc-char-string ] [ >argv ] bi* execv ;
+    [ utf8 malloc-string ] [ >argv ] bi* execv ;
 
 : exec-with-path ( filename argv -- int )
-    [ malloc-char-string ] [ >argv ] bi* execvp ;
+    [ utf8 malloc-string ] [ >argv ] bi* execvp ;
 
 : exec-with-env ( filename argv envp -- int )
-    [ malloc-char-string ] [ >argv ] [ >argv ] tri* execve ;
+    [ utf8 malloc-string ] [ >argv ] [ >argv ] tri* execve ;
 
 : exec-args ( seq -- int )
     [ first ] [ ] bi exec ;
index 056a9c6242c587c40de98bb35545c82a25a241bc..1887fccdc1f3cb7ddbc9f95f9b83d1b2ef7593ce 100644 (file)
@@ -6,7 +6,7 @@
 
        <table class="todo-list">
                <tr><th>Summary</th><th>Priority</th><th>View</th><th>Edit</th></tr>
-               <t:view component="list" />
+               <t:summary component="list" />
        </table>
 
 </t:chloe>
index 44ea853af0c15f0e27229fbc3e2419fca11a0748..dd20f0942d19eaaf65ea1ae1b3beab43d960da10 100644 (file)
@@ -12,8 +12,8 @@ C-STRUCT: GUID
 \r
 TYPEDEF: void* REFGUID\r
 TYPEDEF: void* LPUNKNOWN\r
-TYPEDEF: ushort* LPOLESTR\r
-TYPEDEF: ushort* LPCOLESTR\r
+TYPEDEF: wchar_t* LPOLESTR\r
+TYPEDEF: wchar_t* LPCOLESTR\r
 \r
 TYPEDEF: REFGUID REFIID\r
 TYPEDEF: REFGUID REFCLSID\r
@@ -52,8 +52,8 @@ FUNCTION: HRESULT CLSIDFromString ( LPOLESTR lpsz, REFGUID out_rguid ) ;
     "{01234567-89ab-cdef-0123-456789abcdef}" length ; inline\r
 \r
 : string>guid ( string -- guid )\r
-    string>u16-alien "GUID" <c-object> [ CLSIDFromString ole32-error ] keep ;\r
+    utf16n string>alien "GUID" <c-object> [ CLSIDFromString ole32-error ] keep ;\r
 : guid>string ( guid -- string )\r
     GUID-STRING-LENGTH 1+ [ "ushort" <c-array> ] keep\r
-    [ StringFromGUID2 drop ] { 2 } multikeep alien>u16-string ;\r
+    [ StringFromGUID2 drop ] { 2 } multikeep utf16n alien>string ;\r
 \r
index f938ca15e6410b18dd97a543cdeb3a40e50ac4de..81ecc35b5f122f80e1ed776b0708b05b62730228 100644 (file)
@@ -91,7 +91,7 @@ FUNCTION: HINSTANCE ShellExecuteW ( HWND hwnd, LPCTSTR lpOperation, LPCTSTR lpFi
 : shell32-directory ( n -- str )
     f swap f SHGFP_TYPE_DEFAULT
     MAX_UNICODE_PATH "ushort" <c-array>
-    [ SHGetFolderPath shell32-error ] keep alien>u16-string ;
+    [ SHGetFolderPath shell32-error ] keep utf16n alien>string ;
 
 : desktop ( -- str )
     CSIDL_DESKTOPDIRECTORY shell32-directory ;
index 61b409e8e14af32fc3cf671f99e82b4282ed5e3c..8b4b2d98d29ef300d048a1243001f35743d0473d 100644 (file)
@@ -66,9 +66,8 @@ TYPEDEF: LARGE_INTEGER* PLARGE_INTEGER
 
 TYPEDEF: WCHAR       TCHAR
 TYPEDEF: TCHAR       TBYTE
-! TYPEDEF: uchar*  LPCSTR
-TYPEDEF: ushort*  LPCSTR
-TYPEDEF: ushort*  LPWSTR
+TYPEDEF: wchar_t*  LPCSTR
+TYPEDEF: wchar_t*  LPWSTR
 
 
 
@@ -126,10 +125,10 @@ TYPEDEF: WCHAR*              LPCWSTR
 ! TYPEDEF: WCHAR*              LPWSTR
 
 TYPEDEF: WCHAR*               LPSTR
-TYPEDEF: ushort* LPCTSTR
-TYPEDEF: ushort* LPWTSTR
+TYPEDEF: wchar_t* LPCTSTR
+TYPEDEF: wchar_t* LPWTSTR
 
-TYPEDEF: ushort*       LPTSTR
+TYPEDEF: wchar_t*       LPTSTR
 TYPEDEF: LPCSTR      PCTSTR
 TYPEDEF: LPSTR       PTSTR
 
index 600c0a4039c4a3cb10109f223a78538ea9e97ad0..0e555ed7e99a59723e7da0f8131662825fc8af7d 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2005, 2006 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.syntax alien.c-types arrays combinators
-kernel math namespaces parser prettyprint sequences
+USING: alien alien.syntax alien.c-types alien.strings arrays
+combinators kernel math namespaces parser prettyprint sequences
 windows.errors windows.types windows.kernel32 words ;
 IN: windows
 
@@ -14,7 +14,7 @@ FUNCTION: void* error_message ( DWORD id ) ;
 
 : (win32-error-string) ( n -- string )
     error_message
-    dup alien>u16-string
+    dup utf16n alien>string
     swap LocalFree drop ;
 
 : win32-error-string ( -- str )
index cc19cdc2a3a401c083cd6f8e07b7be0d6c080ec9..b9f8739a193b717d85ee0bc850351be338adb115 100644 (file)
@@ -397,7 +397,7 @@ FUNCTION: void GetAcceptExSockaddrs ( void* a, int b, int c, int d, void* e, voi
 : (winsock-error-string) ( n -- str )
     ! #! WSAStartup returns the error code 'n' directly
     dup winsock-expected-error?
-    [ drop f ] [ error_message alien>u16-string ] if ;
+    [ drop f ] [ error_message utf16n alien>string ] if ;
 
 : winsock-error-string ( -- string/f )
     WSAGetLastError (winsock-error-string) ;
index 63d90f58dbca6e36f73112bd52776c8fcb49a14c..aeb6af3ee623cf8ed3ca0eb6e6c12a4ee70828cd 100644 (file)
@@ -1,7 +1,8 @@
 
-USING: kernel io alien alien.c-types namespaces threads
+USING: kernel io alien alien.c-types alien.strings namespaces threads
        arrays sequences assocs math vars combinators.lib
-       x11.constants x11.events x11.xlib mortar slot-accessors geom.rect ;
+       x11.constants x11.events x11.xlib mortar slot-accessors geom.rect
+       io.encodings.ascii ;
 
 IN: x
 
@@ -29,7 +30,7 @@ define-independent-class
 
 <display> "create" !( name <display> -- display ) [
   new-empty swap >>name
-  dup $name dup [ string>char-alien ] [ ] if XOpenDisplay
+  dup $name dup [ ascii string>alien ] [ ] if XOpenDisplay
   dup [ >>ptr ] [ "XOpenDisplay error" throw ] if
   dup $ptr XDefaultScreen >>default-screen
   dup $ptr XDefaultRootWindow dupd <window> new >>default-root
@@ -433,7 +434,7 @@ add-method
 
 <window> "fetch-name" !( window -- name-or-f )
   [ <- raw f <void*> dup >r   XFetchName drop   r>
-    dup *void* alien-address 0 = [ drop f ] [ *char* ] if ]
+    dup *void* [ drop f ] [ *void* ascii alien>string ] if ]
 add-method
 
 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
index a63a3903a11b41afe24cf084174a1df666ced5b6..9e1e0ef92021c149d717b7fab8793e0f74812ead 100755 (executable)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2006, 2007 Slava Pestov
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.c-types alien.syntax arrays kernel math
-namespaces sequences io.encodings.string io.encodings.utf8 x11.xlib
-x11.constants ;
+USING: alien alien.c-types alien.strings alien.syntax arrays
+kernel math namespaces sequences io.encodings.string
+io.encodings.utf8 io.encodings.ascii x11.xlib x11.constants ;
 IN: x11.clipboard
 
 ! This code was based on by McCLIM's Backends/CLX/port.lisp
@@ -25,7 +25,7 @@ TUPLE: x-clipboard atom contents ;
     CurrentTime XConvertSelection drop ;
 
 : snarf-property ( prop-return -- string )
-    dup *void* [ *char* ] [ drop f ] if ;
+    dup *void* [ *void* ascii alien>string ] [ drop f ] if ;
 
 : window-property ( win prop delete? -- string )
     >r dpy get -rot 0 -1 r> AnyPropertyType
index 752c6c442eb98a754a5e540d31b85d674d28ea9c..154bf4d6ffe196dfb8d67017f1db2cb4b37a8d62 100755 (executable)
@@ -11,8 +11,9 @@
 ! modify, just find the function or data structure in the manual
 ! and note the section.
 
-USING: kernel arrays alien alien.c-types alien.syntax
-math math.bitfields words sequences namespaces continuations ;
+USING: kernel arrays alien alien.c-types alien.strings
+alien.syntax math math.bitfields words sequences namespaces
+continuations io.encodings.ascii ;
 IN: x11.xlib
 
 LIBRARY: xlib
@@ -1372,7 +1373,7 @@ SYMBOL: root
 
 : initialize-x ( display-string -- )
     init-locale
-    dup [ string>char-alien ] when
+    dup [ ascii string>alien ] when
     XOpenDisplay check-display dpy set-global
     dpy get XDefaultScreen scr set-global
     dpy get scr get XRootWindow root set-global ;