lib: update standard library to latest FFI spec
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 31 Aug 2013 12:07:03 +0000 (08:07 -0400)
committerJean Privat <jean@pryen.org>
Mon, 17 Feb 2014 18:56:35 +0000 (13:56 -0500)
At this commit, nitg can be compiled using nitg compiled from Nit in
the previous commit.

Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/standard/file.nit
lib/standard/file_nit.c
lib/standard/file_nit.h
lib/standard/math.nit
lib/standard/posix.nit
lib/standard/stream.nit
lib/standard/stream_nit.c
lib/standard/stream_nit.h
lib/standard/string.nit
lib/standard/time.nit

index 239f785..1e4b345 100644 (file)
@@ -20,6 +20,14 @@ intrude import string
 import string_search
 import time
 
+in "C Header" `{
+       #include <dirent.h>
+       #include <string.h>
+       #include <sys/types.h>
+       #include <sys/stat.h>
+       #include <unistd.h>
+`}
+
 redef class Object
 # Simple I/O
 
@@ -353,7 +361,36 @@ redef class String
        end
 
        # returns files contained within the directory represented by self
-       fun files : Set[ String ] is extern import HashSet, HashSet::add, NativeString::to_s, String::to_cstring, HashSet[String] as( Set[String] ), String as( Object )
+       fun files : Set[ String ] is extern import HashSet[String], HashSet[String].add, NativeString.to_s, String.to_cstring, HashSet[String].as(Set[String]) `{
+               char *dir_path;
+               DIR *dir;
+
+               dir_path = String_to_cstring( recv );
+               if ((dir = opendir(dir_path)) == NULL)
+               {
+                       perror( dir_path );
+                       exit( 1 );
+               }
+               else
+               {
+                       HashSet_of_String results;
+                       String file_name;
+                       struct dirent *de;
+
+                       results = new_HashSet_of_String();
+
+                       while ( ( de = readdir( dir ) ) != NULL )
+                               if ( strcmp( de->d_name, ".." ) != 0 &&
+                                       strcmp( de->d_name, "." ) != 0 )
+                               {
+                                       file_name = NativeString_to_s( strdup( de->d_name ) );
+                                       HashSet_of_String_add( results, file_name );
+                               }
+
+                       closedir( dir );
+                       return HashSet_of_String_as_Set_of_String( results );
+               }
+       `}
 end
 
 redef class NativeString
index ca02ba1..06f5e2e 100644 (file)
 
 #include "file_nit.h"
 
-#ifndef NONITCNI
-/*
-C implementation of file::String::files
-
-Imported methods signatures:
-       HashSet new_HashSet(  ) for hash_collection::HashSet::init
-       void HashSet_add( HashSet recv, Object item ) for hash_collection::HashSet::(abstract_collection::SimpleCollection::add)
-       String NativeString_to_s() for string::NativeString::to_s
-       int HashSet_is_a_Set( HashSet value ) to check if a HashSet[String] is a Set[String]
-       Set HashSet_as_Set( HashSet value ) to cast from HashSet[String] to Set[String]
-*/
-Set String_files___impl( String recv )
-{
-       char *dir_path;
-       DIR *dir;
-
-       dir_path = String_to_cstring( recv );
-       if ((dir = opendir(dir_path)) == NULL)
-       {
-               perror( dir_path );
-               exit( 1 );
-       }
-       else
-       {
-               HashSet results;
-               String file_name;
-               struct dirent *de;
-
-               results = new_HashSet();
-
-               while ( ( de = readdir( dir ) ) != NULL )
-                       if ( strcmp( de->d_name, ".." ) != 0 &&
-                               strcmp( de->d_name, "." ) != 0 )
-                       {
-                               file_name = NativeString_to_s( strdup( de->d_name ) );
-                               HashSet_add( results, String_as_Object( file_name ) );
-                       }
-
-               closedir( dir );
-               return HashSet_as_Set( results );
-       }
-}
-#endif
-
 int string_NativeString_NativeString_file_exists_0(char *f){
        FILE *hdl = fopen(f,"r");
        if(hdl != NULL){
index 6d7967f..1f5b183 100644 (file)
 #include <stdio.h>
 #include <sys/types.h>
 
-#ifndef NONITCNI
-#include <file._nitni.h>
-
-Set String_files___impl( String recv );
-#endif
-
 extern int string_NativeString_NativeString_file_exists_0(char *f);
 extern void *string_NativeString_NativeString_file_stat_0(char *f);
 extern void *file_NativeFile_NativeFile_file_stat_0(FILE *f);
index 269f3bc..5d73c8e 100644 (file)
@@ -38,6 +38,7 @@ redef class Float
        fun acos: Float is extern "kernel_Float_Float_acos_0"
        fun asin: Float is extern "kernel_Float_Float_asin_0"
        fun atan: Float is extern "kernel_Float_Float_atan_0"
+       fun abs: Float `{ return abs(recv); `}
 
        fun pow(e: Float): Float is extern "kernel_Float_Float_pow_1"
        fun log: Float is extern "kernel_Float_Float_log_0"
index 4fd6a03..4ac70a1 100644 (file)
@@ -42,34 +42,34 @@ end
 
 extern class Passwd `{struct passwd*`}
        new from_uid(uid: Int) `{ return getpwuid(uid); `}
-       new from_name(name: String) import String::to_cstring `{ return getpwnam( String_to_cstring(name) ); `}
+       new from_name(name: String) import String.to_cstring `{ return getpwnam( String_to_cstring(name) ); `}
 
-       fun name: String import NativeString::to_s `{ return NativeString_to_s(recv->pw_name); `}
-       fun passwd: String import NativeString::to_s `{ return NativeString_to_s(recv->pw_passwd); `}
+       fun name: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_name); `}
+       fun passwd: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_passwd); `}
        fun uid: Int `{ return recv->pw_uid; `}
        fun gid: Int `{ return recv->pw_gid; `}
-       fun gecos: String import NativeString::to_s `{ return NativeString_to_s(recv->pw_gecos); `}
-       fun dir: String import NativeString::to_s `{ return NativeString_to_s(recv->pw_dir); `}
-       fun shell: String import NativeString::to_s `{ return NativeString_to_s(recv->pw_shell); `}
+       fun gecos: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_gecos); `}
+       fun dir: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_dir); `}
+       fun shell: String import NativeString.to_s `{ return NativeString_to_s(recv->pw_shell); `}
 end
 
 extern class Group `{struct group*`}
        new from_gid(gid: Int) `{ return getgrgid(gid); `}
-       new from_name(name: String) import String::to_cstring `{ return getgrnam( String_to_cstring(name) ); `}
+       new from_name(name: String) import String.to_cstring `{ return getgrnam( String_to_cstring(name) ); `}
 
-       fun name: String import NativeString::to_s `{ return NativeString_to_s(recv->gr_name); `}
-       fun passwd: String import NativeString::to_s `{ return NativeString_to_s(recv->gr_passwd); `}
+       fun name: String import NativeString.to_s `{ return NativeString_to_s(recv->gr_name); `}
+       fun passwd: String import NativeString.to_s `{ return NativeString_to_s(recv->gr_passwd); `}
        fun gid: Int `{ return recv->gr_gid; `}
-       fun mem: Array[String] import Array, Array::add, NativeString::to_s, String as (nullable Object) `{
+       fun mem: Array[String] import Array[String], Array[String].add, NativeString.to_s `{
                char **mem;
                int m;
-               Array ret;
+               Array_of_String ret;
 
                mem = recv->gr_mem;
-               ret = new_Array();
+               ret = new_Array_of_String();
 
                for (m = 0; mem[m] != NULL; m++)
-                       Array_add(ret, String_as_nullable_Object( NativeString_to_s(mem[m]) ));
+                       Array_of_String_add(ret, NativeString_to_s(mem[m]));
 
                return ret;
        `}
index c63e38a..5dbe9d8 100644 (file)
@@ -15,6 +15,13 @@ module stream
 
 import string
 
+in "C" `{
+       #include <unistd.h>
+       #include <poll.h>
+       #include <errno.h>
+       #include <string.h>
+`}
+
 # Abstract stream class
 interface IOS
        # close the stream
@@ -295,7 +302,57 @@ redef interface Object
                end
        end
 
-       private fun intern_poll( in_fds : Array[Int], out_fds : Array[Int] ) : nullable Int is extern import Array::length, Array::[], nullable Object as ( Int ), Int as nullable
+       private fun intern_poll(in_fds: Array[Int], out_fds: Array[Int]) : nullable Int is extern import Array[Int].length, Array[Int].[], Int.as(nullable Int) `{
+               int in_len, out_len, total_len;
+               struct pollfd *c_fds;
+               sigset_t sigmask;
+               int i;
+               int first_polled_fd = -1;
+               int result;
+
+               in_len = Array_of_Int_length( in_fds );
+               out_len = Array_of_Int_length( out_fds );
+               total_len = in_len + out_len;
+               c_fds = malloc( sizeof(struct pollfd) * total_len );
+
+               /* input streams */
+               for ( i=0; i<in_len; i ++ ) {
+                       int fd;
+                       fd = Array_of_Int__index( in_fds, i );
+
+                       c_fds[i].fd = fd;
+                       c_fds[i].events = POLLIN;
+               }
+
+               /* output streams */
+               for ( i=0; i<out_len; i ++ ) {
+                       int fd;
+                       fd = Array_of_Int__index( out_fds, i );
+
+                       c_fds[i].fd = fd;
+                       c_fds[i].events = POLLOUT;
+               }
+
+               /* poll all fds, unlimited timeout */
+               result = poll( c_fds, total_len, -1 );
+
+               if ( result > 0 ) {
+                       /* analyse results */
+                       for ( i=0; i<total_len; i++ )
+                               if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
+                                        c_fds[i].revents & POLLHUP ) /* closed */
+                               {
+                                       first_polled_fd = c_fds[i].fd;
+                                       break;
+                               }
+
+                       return Int_as_nullable( first_polled_fd );
+               }
+               else if ( result < 0 )
+                       fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
+
+               return null_Int();
+       `}
 end
 
 # Stream to a String. Mainly used for compatibility with OStream type and tests.
index de71a41..14bfece 100644 (file)
 
 #include "stream_nit.h"
 
-#include <poll.h>
-#include <errno.h>
-
 int stream_FDStream_FDStream_native_read_char_1(void *s, int fd) {
        int result;
        char buf;
        ssize_t r = read(fd, &buf, 1);
-       if (r == 0) 
+       if (r == 0)
                result = -1;
-       else 
+       else
                result = buf;
        return result;
 }
-#ifndef NONITCNI
-
-/*
-C implementation of stream::Object::intern_poll
-
-Imported methods signatures:
-       bigint Array_length( Array recv ) for array::AbstractArrayRead::(abstract_collection::Collection::length)
-       nullable_Object Array__index( Array recv, bigint index ) for array::Array::(abstract_collection::SequenceRead::[])
-       int nullable_Object_is_a_bigint( nullable_Object value ) to check if a nullable Object is a Int
-       bigint nullable_Object_as_bigint( nullable_Object value ) to cast from nullable Object to Int
-       int Int_is_null( nullable_Int value ) to check if a nullable Int is a Int
-       bigint bigint_as_not_null( nullable_Int value ) to cast from nullable Int to Int
-*/
-nullable_Int Object_intern_poll___impl( Object recv, Array in_fds, Array out_fds ) {
-       int in_len, out_len, total_len;
-       struct pollfd *c_fds;
-       sigset_t sigmask;
-       int i;
-       nullable_Object tmp_nit_obj;
-       int first_polled_fd = -1;
-       int result;
-
-       in_len = Array_length( in_fds );
-       out_len = Array_length( out_fds );
-       total_len = in_len + out_len;
-       c_fds = malloc( sizeof(struct pollfd) * total_len );
-
-       /* input streams */
-       for ( i=0; i<in_len; i ++ ) {
-               int fd;
-               tmp_nit_obj = Array__index( in_fds, i );
-               fd = nullable_Object_as_Int( tmp_nit_obj );
-
-               c_fds[i].fd = fd;
-               c_fds[i].events = POLLIN;
-       }
-
-       /* output streams */
-       for ( i=0; i<out_len; i ++ ) {
-               int fd;
-               tmp_nit_obj = Array__index( out_fds, i );
-               fd = nullable_Object_as_Int( tmp_nit_obj );
-
-               c_fds[i].fd = fd;
-               c_fds[i].events = POLLOUT;
-       }
-
-       /* poll all fds, unlimited timeout */
-       result = poll( c_fds, total_len, -1 );
-
-       if ( result > 0 ) {
-               /* analyse results */
-               for ( i=0; i<total_len; i++ )
-                       if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
-                                c_fds[i].revents & POLLHUP ) /* closed */
-                       {
-                               first_polled_fd = c_fds[i].fd;
-                               break;
-                       }
-
-               return Int_as_nullable( first_polled_fd );
-       }
-       else if ( result < 0 )
-               fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
-
-       return null_Int();
-}
-#endif
index b28e56a..3a4d51e 100644 (file)
 
 int stream_FDStream_FDStream_native_read_char_1(void *s, int fd);
 
-#ifndef NONITCNI
-#include <stream._nitni.h>
-nullable_Int Object_intern_poll___impl( Object recv, Array in_fds, Array out_fds );
-#endif
-
 #define stream_FDStream_FDStream_native_close_1(self, p0) (close(p0))
 #define stream_FDStream_FDStream_native_read_3(s, i, b, l) read((i), ((b)), ((l)))
 #define stream_FDStream_FDStream_native_write_3(s, i, b, l) write((i), ((b)), ((l)))
index 4326d04..5066f9b 100644 (file)
@@ -943,7 +943,7 @@ redef class Float
                end
        end
 
-       fun to_precision_native(nb: Int): String import NativeString::to_s `{
+       fun to_precision_native(nb: Int): String import NativeString.to_s `{
                int size;
                char *str;
 
index 97a4afd..11ba1fe 100644 (file)
@@ -36,7 +36,7 @@ extern class TimeT `{time_t`}
 
        fun update `{ time(&recv); `}
 
-       fun ctime: String import NativeString::to_s_with_copy `{
+       fun ctime: String import NativeString.to_s_with_copy `{
                return NativeString_to_s_with_copy( ctime(&recv) );
        `}
 
@@ -85,10 +85,10 @@ extern class Tm `{struct tm *`}
        fun yday: Int `{ return recv->tm_yday; `}
        fun is_dst: Bool `{ return recv->tm_isdst; `}
 
-       fun asctime: String import NativeString::to_s_with_copy `{
+       fun asctime: String import NativeString.to_s_with_copy `{
                return NativeString_to_s_with_copy( asctime(recv) );
        `}
-       fun strftime(format: String): String import String::to_cstring, NativeString::to_s `{
+       fun strftime(format: String): String import String.to_cstring, NativeString.to_s `{
                char* buf, *c_format;
                size_t res;