From: Alexis Laferrière Date: Sat, 31 Aug 2013 12:07:03 +0000 (-0400) Subject: lib: update standard library to latest FFI spec X-Git-Tag: v0.6.4~25^2~13 X-Git-Url: http://nitlanguage.org?hp=ecaafae6d5bf6c404244a76ff97492bd9a4c9cf5 lib: update standard library to latest FFI spec At this commit, nitg can be compiled using nitg compiled from Nit in the previous commit. Signed-off-by: Alexis Laferrière --- diff --git a/lib/standard/file.nit b/lib/standard/file.nit index 239f785..1e4b345 100644 --- a/lib/standard/file.nit +++ b/lib/standard/file.nit @@ -20,6 +20,14 @@ intrude import string import string_search import time +in "C Header" `{ + #include + #include + #include + #include + #include +`} + 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 diff --git a/lib/standard/file_nit.c b/lib/standard/file_nit.c index ca02ba1..06f5e2e 100644 --- a/lib/standard/file_nit.c +++ b/lib/standard/file_nit.c @@ -24,50 +24,6 @@ #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){ diff --git a/lib/standard/file_nit.h b/lib/standard/file_nit.h index 6d7967f..1f5b183 100644 --- a/lib/standard/file_nit.h +++ b/lib/standard/file_nit.h @@ -20,12 +20,6 @@ #include #include -#ifndef NONITCNI -#include - -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); diff --git a/lib/standard/math.nit b/lib/standard/math.nit index 269f3bc..5d73c8e 100644 --- a/lib/standard/math.nit +++ b/lib/standard/math.nit @@ -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" diff --git a/lib/standard/posix.nit b/lib/standard/posix.nit index 4fd6a03..4ac70a1 100644 --- a/lib/standard/posix.nit +++ b/lib/standard/posix.nit @@ -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; `} diff --git a/lib/standard/stream.nit b/lib/standard/stream.nit index c63e38a..5dbe9d8 100644 --- a/lib/standard/stream.nit +++ b/lib/standard/stream.nit @@ -15,6 +15,13 @@ module stream import string +in "C" `{ + #include + #include + #include + #include +`} + # 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 0 ) { + /* analyse results */ + for ( i=0; i -#include - 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 0 ) { - /* analyse results */ - for ( i=0; i -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))) diff --git a/lib/standard/string.nit b/lib/standard/string.nit index 4326d04..5066f9b 100644 --- a/lib/standard/string.nit +++ b/lib/standard/string.nit @@ -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; diff --git a/lib/standard/time.nit b/lib/standard/time.nit index 97a4afd..11ba1fe 100644 --- a/lib/standard/time.nit +++ b/lib/standard/time.nit @@ -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;