`}
# FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
fun put_char(key: JavaString, value: Char) in "Java" `{
- self.putChar(key, value);
+ self.putChar(key, (char)value);
`}
fun put_short(key: JavaString, value: Int) in "Java" `{
self.putShort(key, (short) value);
char[] java_array = new char[(int)Array_of_Char_length(value)];
for(int i=0; i < java_array.length; ++i)
- java_array[i] = Array_of_Char__index(value, i);
+ java_array[i] = (char)Array_of_Char__index(value, i);
self.putCharArray(key, java_array);
`}
return self.getByte(key, (byte) def_value);
`}
# FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
- fun get_char(key: JavaString): Char in "Java" `{ return self.getChar(key); `}
+ fun get_char(key: JavaString): Char in "Java" `{ return (int)self.getChar(key); `}
# FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
fun get_char_with_def_value(key: JavaString, def_value: Char): Char in "Java" `{
- return self.getChar(key, def_value);
+ return (int)self.getChar(key, (char)def_value);
`}
fun get_short(key: JavaString): Int in "Java" `{ return (short) self.getShort(key); `}
fun get_short_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
if (java_array == null) return nit_array;
for(int i=0; i < java_array.length; ++i)
- Array_of_Char_add(nit_array, java_array[i]);
+ Array_of_Char_add(nit_array, (int)java_array[i]);
return nit_array;
`}
`}
# FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
fun char_extra(name: JavaString, def_value: Char): Char in "Java" `{
- return self.getCharExtra(name, def_value);
+ return (int)self.getCharExtra(name, (char)def_value);
`}
fun char_sequence_array_extra(name: JavaString): Array[String]
import StringCopyArray, StringCopyArray.add, StringCopyArray.collection in "Java" `{
char[] java_array = new char[(int)Array_of_Char_length(value)];
for (int i=0; i < java_array.length; ++i)
- java_array[i] = Array_of_Char__index(value, i);
+ java_array[i] = (char)Array_of_Char__index(value, i);
return self.putExtra(name, java_array);
`}
#include <endian.h>
// Android compatibility
+ #ifndef be32toh
+ #define be32toh(val) betoh32(val)
+ #define le32toh(val) letoh32(val)
+ #endif
+
#ifndef be64toh
#define be64toh(val) betoh64(val)
- #endif
- #ifndef le64toh
#define le64toh(val) letoh64(val)
#endif
`}
redef class NativeString
# Get `self` as a `CppString`
fun to_cpp_string(length: Int): CppString in "C++" `{
- return new std::string(self, length);
+ return new std::string(reinterpret_cast<char*>(self), length);
`}
end
# The first power of `exp` greater or equal to `self`
private fun next_pow(exp: Int): Int
do
- var p = 0
+ var p = 1
while p < self do p = p*exp
return p
end
closed = true
return
end
- var hostname = socket.gethostbyname(host)
- addrin = new NativeSocketAddrIn.with_hostent(hostname, port)
+ var hostname = sys.gethostbyname(host.to_cstring)
+ if hostname.address_is_null then
+ # Error in name lookup
+ var err = sys.h_errno
+ last_error = new IOError(err.to_s)
+
+ closed = true
+ end_reached = true
+
+ return
+ end
+
+ addrin = new NativeSocketAddrIn.with_hostent(hostname, port)
address = addrin.address
init(addrin.port, hostname.h_name)
closed = not internal_connect
end_reached = closed
+ if closed then
+ # Connection failed
+ last_error = new IOError(errno.strerror)
+ end
end
# Creates a client socket, this is meant to be used by accept only
fun descriptor: Int `{ return *self; `}
- fun gethostbyname(n: String): NativeSocketHostent import String.to_cstring `{ return gethostbyname(String_to_cstring(n)); `}
-
fun connect(addrIn: NativeSocketAddrIn): Int `{
return connect(*self, (struct sockaddr*)addrIn, sizeof(*addrIn));
`}
return self | other;
`}
end
+
+redef class Sys
+ # Get network host entry
+ fun gethostbyname(name: NativeString): NativeSocketHostent `{
+ return gethostbyname(name);
+ `}
+
+ # Last error raised by `gethostbyname`
+ fun h_errno: HErrno `{ return h_errno; `}
+end
+
+# Error code of `Sys::h_errno`
+extern class HErrno `{ int `}
+ # The specified host is unknown
+ fun host_not_found: Bool `{ return self == HOST_NOT_FOUND; `}
+
+ # The requested name is valid but does not have an IP address
+ #
+ # Same as `no_data`.
+ fun no_address: Bool `{ return self == NO_ADDRESS; `}
+
+ # The requested name is valid byt does not have an IP address
+ #
+ # Same as `no_address`.
+ fun no_data: Bool `{ return self == NO_DATA; `}
+
+ # A nonrecoverable name server error occurred
+ fun no_recovery: Bool `{ return self == NO_RECOVERY; `}
+
+ # A temporary error occurred on an authoritative name server, try again later
+ fun try_again: Bool `{ return self == TRY_AGAIN; `}
+
+ redef fun to_s
+ do
+ if host_not_found then
+ return "The specified host is unknown"
+ else if no_address then
+ return "The requested name is valid but does not have an IP address"
+ else if no_recovery then
+ return "A nonrecoverable name server error occurred"
+ else if try_again then
+ return "A temporary error occurred on an authoritative name server, try again later"
+ else
+ # This may happen if another call was made to `gethostbyname`
+ # before we fetch the error code.
+ return "Unknown error on `gethostbyname`"
+ end
+ end
+end
self.header.add_decl("#include <string.h>")
self.header.add_decl("#include <sys/types.h>\n")
self.header.add_decl("#include <unistd.h>\n")
+ self.header.add_decl("#include <stdint.h>\n")
self.header.add_decl("#include \"gc_chooser.h\"")
self.header.add_decl("#ifdef ANDROID")
self.header.add_decl(" #include <android/log.h>")
else if mclass.name == "Bool" then
return "short int"
else if mclass.name == "Char" then
- return "char"
+ return "uint32_t"
else if mclass.name == "Float" then
return "double"
else if mclass.name == "Byte" then
return "unsigned char"
else if mclass.name == "NativeString" then
- return "char*"
+ return "unsigned char*"
else if mclass.name == "NativeArray" then
return "val*"
else
v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
return true
else if pname == "ascii" then
- v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
+ v.ret(v.new_expr("(uint32_t){arguments[0]}", ret.as(not null)))
return true
end
else if cname == "Char" then
if pname == "output" then
- v.add("printf(\"%c\", {arguments.first});")
+ v.add("printf(\"%c\", ((unsigned char){arguments.first}));")
return true
else if pname == "object_id" then
v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
return true
else if pname == "ascii" then
- v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
+ v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
return true
end
else if cname == "Byte" then
end
else if cname == "NativeString" then
if pname == "[]" then
- v.ret(v.new_expr("{arguments[0]}[{arguments[1]}]", ret.as(not null)))
+ v.ret(v.new_expr("(uint32_t){arguments[0]}[{arguments[1]}]", ret.as(not null)))
return true
else if pname == "[]=" then
- v.add("{arguments[0]}[{arguments[1]}]={arguments[2]};")
+ v.add("{arguments[0]}[{arguments[1]}]=(unsigned char){arguments[2]};")
return true
else if pname == "copy_to" then
v.add("memmove({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
return true
else if pname == "new" then
- v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
+ v.ret(v.new_expr("(unsigned char*)nit_alloc({arguments[1]})", ret.as(not null)))
return true
end
else if cname == "NativeArray" then
v.ret(v.new_expr("glob_sys", ret.as(not null)))
return true
else if pname == "calloc_string" then
- v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
+ v.ret(v.new_expr("(unsigned char*)nit_alloc({arguments[1]})", ret.as(not null)))
return true
else if pname == "calloc_array" then
v.calloc_array(ret.as(not null), arguments)
ensure_compile_nitni_base(v)
nitni_ccu.header_c_types.add("#include \"{c_name}._ffi.h\"\n")
+ nitni_ccu.header_c_types.add("#include <stdint.h>\n")
nitni_ccu.header_c_types.add """
extern void nitni_global_ref_incr(void*);
extern void nitni_global_ref_decr(void*);
if mtype.name == "Int" then
return self.new_expr("(long)({value})>>2", mtype)
else if mtype.name == "Char" then
- return self.new_expr("(char)((long)({value})>>2)", mtype)
+ return self.new_expr("(uint32_t)((long)({value})>>2)", mtype)
else if mtype.name == "Bool" then
return self.new_expr("(short int)((long)({value})>>2)", mtype)
else
if ftype isa ForeignJavaType then return ftype.java_type.
replace('/', ".").replace('$', ".").replace(' ', "").replace('\n',"")
if mclass.name == "Bool" then return "boolean"
- if mclass.name == "Char" then return "char"
+ if mclass.name == "Char" then return "int"
if mclass.name == "Int" then return "long"
if mclass.name == "Float" then return "double"
if mclass.name == "Byte" then return "byte"
var ftype = mclass.ftype
if ftype isa ForeignJavaType then return "jobject"
if mclass.name == "Bool" then return "jboolean"
- if mclass.name == "Char" then return "jchar"
+ if mclass.name == "Char" then return "jint"
if mclass.name == "Int" then return "jlong"
if mclass.name == "Float" then return "jdouble"
if mclass.name == "Byte" then return "jbyte"
return "L{jni_type};"
end
if mclass.name == "Bool" then return "Z"
- if mclass.name == "Char" then return "C"
+ if mclass.name == "Char" then return "I"
if mclass.name == "Int" then return "J"
if mclass.name == "Float" then return "D"
if mclass.name == "Byte" then return "B"
if ftype isa ForeignJavaType then return "Object"
if mclass.name == "Bool" then return "Boolean"
- if mclass.name == "Char" then return "Char"
+ if mclass.name == "Char" then return "Int"
if mclass.name == "Int" then return "Long"
if mclass.name == "Float" then return "Double"
if mclass.name == "Byte" then return "Byte"
var h_file = "{base_name}.h"
var guard = "{mmodule.c_name.to_upper}_NIT_H"
- write_header_to_file(mmodule, "{compdir}/{h_file}", new Array[String], guard)
+ write_header_to_file(mmodule, "{compdir}/{h_file}", ["<stdint.h>"], guard)
var c_file = "{base_name}.c"
- write_body_to_file(mmodule, "{compdir}/{c_file}", ["<stdlib.h>", "<stdio.h>", "\"{h_file}\""])
+ write_body_to_file(mmodule, "{compdir}/{c_file}", ["<stdlib.h>", "<stdio.h>", "<stdint.h>", "\"{h_file}\""])
files.add( "{compdir}/{c_file}" )
end
do
var name = mclass.name
if name == "Bool" then return "int"
- if name == "Char" then return "char"
+ if name == "Char" then return "uint32_t"
if name == "Float" then return "double"
if name == "Int" then return "long"
if name == "Byte" then return "unsigned char"
- if name == "NativeString" then return "char*"
+ if name == "NativeString" then return "unsigned char*"
if mclass.kind == extern_kind then
var ctype = mclass.ctype
assert ctype != null
redef fun cname_blind do
var name = mclass.name
if name == "Bool" then return "int"
- if name == "Char" then return "char"
+ if name == "Char" then return "uint32_t"
if name == "Float" then return "double"
if name == "Int" then return "long"
if name == "Byte" then return "unsigned char"
- if name == "NativeString" then return "char*"
+ if name == "NativeString" then return "unsigned char*"
if mclass.kind == extern_kind then return "void*"
return "struct nitni_instance *"
end
8:MPropDef
13:MAttributeDef
=properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
----
=to=Entity#0:
=labels=Array(4):
8:MPropDef
13:MAttributeDef
=properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
----
=to=Entity#0:
=labels=Array(4):
8:MPropDef
13:MAttributeDef
=properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
Edge
8:MPropDef
13:MAttributeDef
=properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
----
=to=Entity#0:
=labels=Array(4):
8:MPropDef
13:MAttributeDef
=properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
----
=to=Entity#0:
=labels=Array(4):
8:MPropDef
13:MAttributeDef
=properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
Edge
`}
fun print_a(str: String) import String.to_cstring in "C++" `{
- puts(String_to_cstring(str));
+ puts(reinterpret_cast<char*>(String_to_cstring(str)));
`}
print_a "Hello from `a`."
`}
fun print_b(str: String) import String.to_cstring in "C++" `{
- puts(String_to_cstring(str));
+ puts(reinterpret_cast<char*>(String_to_cstring(str)));
`}
print_a "Hello from `a`."