[MLton-commit] r6983

Wesley Terpstra wesley at mlton.org
Tue Nov 11 12:43:15 PST 2008


Update uses of the FFI to have matching symbol scopes in C and SML.
Remove use of the GC_getArrayLength, instead passing the length as an argument.


----------------------------------------------------------------------

U   mlton/trunk/doc/examples/ffi/c_quot.c
U   mlton/trunk/doc/examples/ffi/export.sml
U   mlton/trunk/doc/examples/ffi/ffi-export.c
U   mlton/trunk/doc/examples/ffi/ffi-import.c
U   mlton/trunk/doc/examples/ffi/iimport.sml
U   mlton/trunk/doc/examples/ffi/import.sml
U   mlton/trunk/doc/examples/ffi/import2.sml
U   mlton/trunk/doc/examples/ffi/test_quot.sml

----------------------------------------------------------------------

Modified: mlton/trunk/doc/examples/ffi/c_quot.c
===================================================================
--- mlton/trunk/doc/examples/ffi/c_quot.c	2008-11-11 20:41:42 UTC (rev 6982)
+++ mlton/trunk/doc/examples/ffi/c_quot.c	2008-11-11 20:43:14 UTC (rev 6983)
@@ -1,12 +1,12 @@
 #include "test_quot.h"
 #include <stdio.h>
 
-Int8 c_quot(Int8 x, Int8 y) {
+PRIVATE Int8 c_quot(Int8 x, Int8 y) {
   Int8 z = x / y;
   return z;
 }
 
-void call_sml_quot() {
+PUBLIC void call_sml_quot() {
   Int8 x = -1;
   Int8 y = 10;
   Int8 z = sml_quot(x, y);

Modified: mlton/trunk/doc/examples/ffi/export.sml
===================================================================
--- mlton/trunk/doc/examples/ffi/export.sml	2008-11-11 20:41:42 UTC (rev 6982)
+++ mlton/trunk/doc/examples/ffi/export.sml	2008-11-11 20:43:14 UTC (rev 6983)
@@ -3,31 +3,31 @@
            (print (concat ["i = ", Int.toString i,
                            "  r = ", Real.toString r, "\n"])
             ; #"g"))
-val g = _import "g": unit -> unit;
+val g = _import "g" public: unit -> unit;
 val _ = g ()
 val _ = g ()
 
 val e = _export "f2": (Word8.word -> word array) -> unit;
 val _ = e (fn w =>
            Array.tabulate (10, fn _ => Word.fromLargeWord (Word8.toLargeWord w)))
-val g2 = _import "g2": unit -> word array;
+val g2 = _import "g2" public: unit -> word array;
 val a = g2 ()
 val _ = print (concat ["0wx", Word.toString (Array.sub (a, 0)), "\n"])
 
 val e = _export "f3": (unit -> unit) -> unit;
 val _ = e (fn () => print "hello\n");
-val g3 = _import "g3": unit -> unit;
+val g3 = _import "g3" public: unit -> unit;
 val _ = g3 ()
 
 (* This example demonstrates mutual recursion between C and SML. *)
 val e = _export "f4": (int -> unit) -> unit;
-val g4 = _import "g4": int -> unit;
+val g4 = _import "g4" public: int -> unit;
 val _ = e (fn i => if i = 0 then () else g4 (i - 1))
 val _ = g4 13
 
 val (_, zzzSet) = _symbol "zzz" alloc: (unit -> int) * (int -> unit);
 val () = zzzSet 42
-val g5 = _import "g5": unit -> unit;
+val g5 = _import "g5" public: unit -> unit;
 val _ = g5 ()
 
 val _ = print "success\n"

Modified: mlton/trunk/doc/examples/ffi/ffi-export.c
===================================================================
--- mlton/trunk/doc/examples/ffi/ffi-export.c	2008-11-11 20:41:42 UTC (rev 6982)
+++ mlton/trunk/doc/examples/ffi/ffi-export.c	2008-11-11 20:43:14 UTC (rev 6983)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include "export.h"
 
+/* Functions in C are by default PUBLIC symbols */
 void g () {
         Char8 c;
 

Modified: mlton/trunk/doc/examples/ffi/ffi-import.c
===================================================================
--- mlton/trunk/doc/examples/ffi/ffi-import.c	2008-11-11 20:41:42 UTC (rev 6982)
+++ mlton/trunk/doc/examples/ffi/ffi-import.c	2008-11-11 20:43:14 UTC (rev 6983)
@@ -1,11 +1,11 @@
-#include "platform.h"
+#include "export.h"
 
 Int32 FFI_INT = 13;
 Word32 FFI_WORD = 0xFF;
-Bool FFI_BOOL = TRUE;
+Bool FFI_BOOL = 1;
 Real64 FFI_REAL = 3.14159;
 
-Char8 ffi (Pointer a1, Pointer a2, Pointer a3, Int32 n) {
+Char8 ffi (Pointer a1, Int32 a1len, Pointer a2, Pointer a3, Int32 n) {
         double *ds = (double*)a1;
         int *pi = (int*)a2;
         char *pc = (char*)a3;
@@ -13,7 +13,7 @@
         double sum;
 
         sum = 0.0;
-        for (i = 0; i < GC_getArrayLength (a1); ++i) {
+        for (i = 0; i < a1len; ++i) {
                 sum += ds[i];
                 ds[i] += n;
         }

Modified: mlton/trunk/doc/examples/ffi/iimport.sml
===================================================================
--- mlton/trunk/doc/examples/ffi/iimport.sml	2008-11-11 20:41:42 UTC (rev 6982)
+++ mlton/trunk/doc/examples/ffi/iimport.sml	2008-11-11 20:43:14 UTC (rev 6983)
@@ -18,6 +18,9 @@
       type mode = Word32.word
       type fptr = MLton.Pointer.t
 
+      (* These symbols come from a system libray, so the default import scope
+       * of external is correct.
+       *)
       val dlopen =
          _import "dlopen" : string * mode -> hndl;
       val dlerror =

Modified: mlton/trunk/doc/examples/ffi/import.sml
===================================================================
--- mlton/trunk/doc/examples/ffi/import.sml	2008-11-11 20:41:42 UTC (rev 6982)
+++ mlton/trunk/doc/examples/ffi/import.sml	2008-11-11 20:43:14 UTC (rev 6983)
@@ -1,7 +1,7 @@
 (* main.sml *)
 
 (* Declare ffi to be implemented by calling the C function ffi. *)
-val ffi = _import "ffi": real array * int ref * char ref * int -> char;
+val ffi = _import "ffi" public: real array * int * int ref * char ref * int -> char;
 open Array
 
 val size = 10
@@ -11,9 +11,10 @@
 val n = 17
 
 (* Call the C function *)
-val c = ffi (a, ri, rc, n)
+val c = ffi (a, Array.length a, ri, rc, n)
 
-val (nGet, nSet) = _symbol "FFI_INT": (unit -> int) * (int -> unit);
+(* FFI_INT is declared as public in ffi-import.c *)
+val (nGet, nSet) = _symbol "FFI_INT" public: (unit -> int) * (int -> unit);
 
 val _ = print (concat [Int.toString (nGet ()), "\n"])
 

Modified: mlton/trunk/doc/examples/ffi/import2.sml
===================================================================
--- mlton/trunk/doc/examples/ffi/import2.sml	2008-11-11 20:41:42 UTC (rev 6982)
+++ mlton/trunk/doc/examples/ffi/import2.sml	2008-11-11 20:43:14 UTC (rev 6983)
@@ -1,8 +1,8 @@
 (* main.sml *)
 
 (* Declare ffi to be implemented by calling the C function ffi. *)
-val ffi_addr = _address "ffi" : MLton.Pointer.t;
-val ffi_schema = _import * : MLton.Pointer.t -> real array * int ref * char ref * int -> char;
+val ffi_addr = _address "ffi" public: MLton.Pointer.t;
+val ffi_schema = _import * : MLton.Pointer.t -> real array * int * int ref * char ref * int -> char;
 open Array
 
 val size = 10
@@ -12,20 +12,20 @@
 val n = 17
 
 (* Call the C function *)
-val c = ffi_schema ffi_addr (a, ri, rc, n)
+val c = ffi_schema ffi_addr (a, Array.length a, ri, rc, n)
 
 val _ =
    print (if c = #"c" andalso !ri = 45 andalso !rc = c
              then "success\n"
           else "fail\n")
 
-val n = #1 (_symbol "FFI_INT": (unit -> int) * (int -> unit);) ()
+val n = #1 (_symbol "FFI_INT" public: (unit -> int) * (int -> unit);) ()
 val _ = print (concat [Int.toString n, "\n"])
-val w = #1 (_symbol "FFI_WORD": (unit -> word) * (word -> unit);) ()
+val w = #1 (_symbol "FFI_WORD" public: (unit -> word) * (word -> unit);) ()
 val _ = print (concat [Word.toString w, "\n"])
-val b = #1 (_symbol "FFI_BOOL": (unit -> bool) * (bool -> unit);) ()
+val b = #1 (_symbol "FFI_BOOL" public: (unit -> bool) * (bool -> unit);) ()
 val _ = print (concat [Bool.toString b, "\n"])
-val r = #1 (_symbol "FFI_REAL": (unit -> real) * (real -> unit);) ()
+val r = #1 (_symbol "FFI_REAL" public: (unit -> real) * (real -> unit);) ()
 val _ = print (concat [Real.toString r, "\n"])
 
 signature OPAQUE =
@@ -55,24 +55,24 @@
       val toString = Real.toString
    end
 
-val (n, _) = _symbol "FFI_INT": (unit -> OpaqueInt.t) * (OpaqueInt.t -> unit);
+val (n, _) = _symbol "FFI_INT" public: (unit -> OpaqueInt.t) * (OpaqueInt.t -> unit);
 val _ = print (concat [OpaqueInt.toString (n ()), "\n"])
-val (w, _) = _symbol "FFI_WORD": (unit -> OpaqueWord.t) * (OpaqueWord.t -> unit);
+val (w, _) = _symbol "FFI_WORD" public: (unit -> OpaqueWord.t) * (OpaqueWord.t -> unit);
 val _ = print (concat [OpaqueWord.toString (w ()), "\n"])
-val (b, _) = _symbol "FFI_BOOL": (unit -> OpaqueBool.t) * (OpaqueBool.t -> unit);
+val (b, _) = _symbol "FFI_BOOL" public: (unit -> OpaqueBool.t) * (OpaqueBool.t -> unit);
 val _ = print (concat [OpaqueBool.toString (b ()), "\n"])
-val (r, _) = _symbol "FFI_REAL": (unit -> OpaqueReal.t) * (OpaqueReal.t -> unit);
+val (r, _) = _symbol "FFI_REAL" public: (unit -> OpaqueReal.t) * (OpaqueReal.t -> unit);
 val _ = print (concat [OpaqueReal.toString (r ()), "\n"])
 
-val n_addr = _address "FFI_INT": MLton.Pointer.t;
+val n_addr = _address "FFI_INT" public: MLton.Pointer.t;
 val n = MLton.Pointer.getInt32 (n_addr, 0);
 val _ = print (concat [Int.toString n, "\n"])
-val w_addr = _address "FFI_WORD": MLton.Pointer.t;
+val w_addr = _address "FFI_WORD" public: MLton.Pointer.t;
 val w = MLton.Pointer.getWord32 (w_addr, 0);
 val _ = print (concat [Word.toString w, "\n"])
-val b_addr = _address "FFI_BOOL": MLton.Pointer.t;
+val b_addr = _address "FFI_BOOL" public: MLton.Pointer.t;
 val b = (MLton.Pointer.getInt32 (n_addr, 0)) <> 0
 val _ = print (concat [Bool.toString b, "\n"])
-val r_addr = _address "FFI_REAL": MLton.Pointer.t;
+val r_addr = _address "FFI_REAL" public: MLton.Pointer.t;
 val r = MLton.Pointer.getReal64 (r_addr, 0)
 val _ = print (concat [Real.toString r, "\n"])

Modified: mlton/trunk/doc/examples/ffi/test_quot.sml
===================================================================
--- mlton/trunk/doc/examples/ffi/test_quot.sml	2008-11-11 20:41:42 UTC (rev 6982)
+++ mlton/trunk/doc/examples/ffi/test_quot.sml	2008-11-11 20:43:14 UTC (rev 6983)
@@ -1,10 +1,11 @@
+(* By default _import is external *)
+val c_quot = _import "c_quot" private: Int8.int * Int8.int -> Int8.int;
 
-val c_quot = _import "c_quot": Int8.int * Int8.int -> Int8.int;
-
+(* By default _export is public *)
 val sml_quot = _export "sml_quot": (Int8.int * Int8.int -> Int8.int) -> unit;
 val _ = sml_quot Int8.quot
 
-val call_sml_quot = _import "call_sml_quot": unit -> unit;
+val call_sml_quot = _import "call_sml_quot" public: unit -> unit;
 
 val x : Int8.int = ~1
 val y : Int8.int = 10




More information about the MLton-commit mailing list