import semantize
private import parser::tables
import mixin
+import primitive_types
redef class ToolContext
# --discover-call-trace
end
else if cname == "NativeFile" then
if pname == "native_stdout" then
- var instance = new PrimitiveInstance[OStream](mpropdef.mclassdef.mclass.mclass_type, sys.stdout)
+ var inst = new PrimitiveNativeFile.native_stdout
+ var instance = new PrimitiveInstance[PrimitiveNativeFile](mpropdef.mclassdef.mclass.mclass_type, inst)
v.init_instance_primitive(instance)
return instance
else if pname == "native_stdin" then
- var instance = new PrimitiveInstance[IStream](mpropdef.mclassdef.mclass.mclass_type, sys.stdin)
+ var inst = new PrimitiveNativeFile.native_stdin
+ var instance = new PrimitiveInstance[PrimitiveNativeFile](mpropdef.mclassdef.mclass.mclass_type, inst)
v.init_instance_primitive(instance)
return instance
else if pname == "native_stderr" then
- var instance = new PrimitiveInstance[OStream](mpropdef.mclassdef.mclass.mclass_type, sys.stderr)
+ var inst = new PrimitiveNativeFile.native_stderr
+ var instance = new PrimitiveInstance[PrimitiveNativeFile](mpropdef.mclassdef.mclass.mclass_type, inst)
v.init_instance_primitive(instance)
return instance
else if pname == "io_open_read" then
var a1 = args[1].val.as(Buffer)
- var instance = new PrimitiveInstance[IStream](mpropdef.mclassdef.mclass.mclass_type, new IFStream.open(a1.to_s))
+ var inst = new PrimitiveNativeFile.io_open_read(a1.to_s)
+ var instance = new PrimitiveInstance[PrimitiveNativeFile](mpropdef.mclassdef.mclass.mclass_type, inst)
v.init_instance_primitive(instance)
return instance
else if pname == "io_open_write" then
var a1 = args[1].val.as(Buffer)
- var instance = new PrimitiveInstance[OStream](mpropdef.mclassdef.mclass.mclass_type, new OFStream.open(a1.to_s))
+ var inst = new PrimitiveNativeFile.io_open_write(a1.to_s)
+ var instance = new PrimitiveInstance[PrimitiveNativeFile](mpropdef.mclassdef.mclass.mclass_type, inst)
v.init_instance_primitive(instance)
return instance
end
var recvval = args.first.val
if pname == "io_write" then
var a1 = args[1].val.as(Buffer)
- recvval.as(OStream).write(a1.substring(0, args[2].to_i).to_s)
- return args[2]
+ return v.int_instance(recvval.as(PrimitiveNativeFile).io_write(a1.to_cstring, args[2].to_i))
else if pname == "io_read" then
- var str = recvval.as(IStream).read(args[2].to_i)
var a1 = args[1].val.as(Buffer)
- new FlatBuffer.from(str).copy(0, str.length, a1.as(FlatBuffer), 0)
- return v.int_instance(str.length)
+ var ns = new NativeString(a1.length)
+ var len = recvval.as(PrimitiveNativeFile).io_read(ns, args[2].to_i)
+ a1.clear
+ a1.append(ns.to_s_with_length(len))
+ return v.int_instance(len)
+ else if pname == "flush" then
+ recvval.as(PrimitiveNativeFile).flush
+ return null
else if pname == "io_close" then
- recvval.as(IOS).close
- return v.int_instance(0)
- else if pname == "address_is_null" then
- return v.false_instance
+ return v.int_instance(recvval.as(PrimitiveNativeFile).io_close)
end
else if pname == "calloc_array" then
var recvtype = args.first.mtype.as(MClassType)
else if pname == "errno" then
return v.int_instance(sys.errno)
else if pname == "address_is_null" then
+ var recv = args[0]
+ if recv isa PrimitiveInstance[PrimitiveNativeFile] then
+ return v.bool_instance(recv.val.address_is_null)
+ end
return v.false_instance
end
return v.error_instance
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# This file is free software, which comes along with NIT. This software is
+# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. You can modify it is you want, provided this header
+# is kept unaltered, and a notification of the changes is added.
+# You are allowed to redistribute it and sell it, alone or is a part of
+# another product.
+
+# Encapsulates all primitive data types of nit
+#
+# Ensures that the use in the interpreter is independant of the
+# underlying implementation and that the services are semantically correct.
+module primitive_types
+
+intrude import standard::file
+
+# Wrapper for `NativeFile`
+class PrimitiveNativeFile
+
+ var file: FStream
+
+ init native_stdin do
+ file = new IFStream.from_fd(0)
+ end
+
+ init native_stdout do
+ file = new OFStream.from_fd(1)
+ end
+
+ init native_stderr do
+ file = new OFStream.from_fd(2)
+ end
+
+ init io_open_read(path: String) do
+ file = new IFStream.open(path.to_s)
+ end
+
+ init io_open_write(path: String) do
+ file = new OFStream.open(path.to_s)
+ end
+
+ fun address_is_null: Bool do return file._file.address_is_null
+
+ fun io_read(buf: NativeString, len: Int): Int do return file._file.io_read(buf, len)
+
+ fun io_write(buf: NativeString, len: Int): Int do return file._file.io_write(buf, len)
+
+ fun io_close: Int do return file._file.io_close
+
+ fun file_stat: FileStat do return file._file.file_stat
+
+ fun fileno: Int do return file._file.fileno
+
+ fun flush: Int do return file._file.flush
+end