Mercurial Hosting > luan
comparison src/luan/modules/Io.luan @ 1088:bae2d0c2576c
change module naming convention
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 26 Dec 2016 22:29:36 -0700 |
parents | 1a68fc55a80c |
children | e54ae41e9501 |
comparison
equal
deleted
inserted
replaced
1087:4aab4dd3ac9c | 1088:bae2d0c2576c |
---|---|
1 java() | 1 java() |
2 local IoLuan = require "java:luan.modules.IoLuan" | 2 local IoLuan = require "java:luan.modules.IoLuan" |
3 local System = require "java:java.lang.System" | 3 local System = require "java:java.lang.System" |
4 | 4 |
5 local M = {} | 5 local Io = {} |
6 | 6 |
7 M.ip = IoLuan.ip | 7 Io.ip = IoLuan.ip |
8 M.my_ips = IoLuan.my_ips | 8 Io.my_ips = IoLuan.my_ips |
9 M.read_console_line = IoLuan.read_console_line | 9 Io.read_console_line = IoLuan.read_console_line |
10 M.schemes = IoLuan.newSchemes() | 10 Io.schemes = IoLuan.newSchemes() |
11 M.uri = IoLuan.uri | 11 Io.uri = IoLuan.uri |
12 M.stdin = IoLuan.defaultStdin.table() | 12 Io.stdin = IoLuan.defaultStdin.table() |
13 M.socket_server = IoLuan.socket_server | 13 Io.socket_server = IoLuan.socket_server |
14 M.stdout = IoLuan.textWriter(System.out) | 14 Io.stdout = IoLuan.textWriter(System.out) |
15 M.stderr = IoLuan.textWriter(System.err) | 15 Io.stderr = IoLuan.textWriter(System.err) |
16 | 16 |
17 -- used by http and rpc | 17 -- used by http and rpc |
18 M.password = "password" | 18 Io.password = "password" |
19 | 19 |
20 local Luan = require "luan:Luan.luan" | 20 local Luan = require "luan:Luan.luan" |
21 local error = Luan.error | 21 local error = Luan.error |
22 local to_string = Luan.to_string or error() | 22 local to_string = Luan.to_string or error() |
23 local type = Luan.type or error() | 23 local type = Luan.type or error() |
32 local encode = String.encode or error() | 32 local encode = String.encode or error() |
33 local matches = String.matches or error() | 33 local matches = String.matches or error() |
34 | 34 |
35 | 35 |
36 -- do not change | 36 -- do not change |
37 function M.template_write(...) | 37 function Io.template_write(...) |
38 return M.stdout.write(...) | 38 return Io.stdout.write(...) |
39 end | 39 end |
40 | 40 |
41 | 41 |
42 function M.print_to(out,...) | 42 function Io.print_to(out,...) |
43 local list = {} | 43 local list = {} |
44 for v in values(...) do | 44 for v in values(...) do |
45 list[#list+1] = to_string(v) | 45 list[#list+1] = to_string(v) |
46 list[#list+1] = '\t' | 46 list[#list+1] = '\t' |
47 end | 47 end |
49 list[#list] = '\n' | 49 list[#list] = '\n' |
50 out.write( unpack(list) ) | 50 out.write( unpack(list) ) |
51 end | 51 end |
52 end | 52 end |
53 | 53 |
54 function M.print(...) | 54 function Io.print(...) |
55 M.print_to(M.stdout,...) | 55 Io.print_to(Io.stdout,...) |
56 end | 56 end |
57 | 57 |
58 | 58 |
59 function M.output_to(out,fn,...) | 59 function Io.output_to(out,fn,...) |
60 local old_out = M.stdout | 60 local old_out = Io.stdout |
61 return try( { | 61 return try( { |
62 function(...) | 62 function(...) |
63 M.stdout = out | 63 Io.stdout = out |
64 fn(...) | 64 fn(...) |
65 end; | 65 end; |
66 finally = function() | 66 finally = function() |
67 M.stdout = old_out | 67 Io.stdout = old_out |
68 end; | 68 end; |
69 }, ... ) | 69 }, ... ) |
70 end | 70 end |
71 | 71 |
72 local uri = M.uri -- make local | 72 local uri = Io.uri -- make local |
73 | 73 |
74 function M.output_of(fn,...) | 74 function Io.output_of(fn,...) |
75 local string_uri = uri "string:" | 75 local string_uri = uri "string:" |
76 local out = string_uri.text_writer() | 76 local out = string_uri.text_writer() |
77 M.output_to(out,fn,...) | 77 Io.output_to(out,fn,...) |
78 out.close() | 78 out.close() |
79 return string_uri.read_text() | 79 return string_uri.read_text() |
80 end | 80 end |
81 | 81 |
82 | 82 |
124 strict and error("can't repr type '"..tp.."' of "..obj) | 124 strict and error("can't repr type '"..tp.."' of "..obj) |
125 out.write( "<", obj, ">" ) | 125 out.write( "<", obj, ">" ) |
126 end | 126 end |
127 end | 127 end |
128 | 128 |
129 function M.repr(obj,strict) | 129 function Io.repr(obj,strict) |
130 local string_uri = uri "string:" | 130 local string_uri = uri "string:" |
131 local out = string_uri.text_writer() | 131 local out = string_uri.text_writer() |
132 do_repr(out,obj,strict or false,{}) | 132 do_repr(out,obj,strict or false,{}) |
133 out.close() | 133 out.close() |
134 return string_uri.read_text() | 134 return string_uri.read_text() |
136 | 136 |
137 | 137 |
138 -- useful for SimplyHTML responsiveness | 138 -- useful for SimplyHTML responsiveness |
139 | 139 |
140 local NO = {} | 140 local NO = {} |
141 M.NO = NO | 141 Io.NO = NO |
142 | 142 |
143 function M.dont_write_when_no(write_fn) | 143 function Io.dont_write_when_no(write_fn) |
144 return function(...) | 144 return function(...) |
145 for v in values(...) do | 145 for v in values(...) do |
146 if v == NO then | 146 if v == NO then |
147 return | 147 return |
148 end | 148 end |
152 end | 152 end |
153 | 153 |
154 | 154 |
155 -- debug | 155 -- debug |
156 | 156 |
157 function M.debug(prompt) | 157 function Io.debug(prompt) |
158 prompt = prompt or "luan_debug> " | 158 prompt = prompt or "luan_debug> " |
159 local function console() | 159 local function console() |
160 return M.read_console_line(prompt) | 160 return Io.read_console_line(prompt) |
161 end | 161 end |
162 local env = {} | 162 local env = {} |
163 for line in console do | 163 for line in console do |
164 try { | 164 try { |
165 function() | 165 function() |
170 end | 170 end |
171 catch = function(e) | 171 catch = function(e) |
172 fn = load(line,"stdin",env) | 172 fn = load(line,"stdin",env) |
173 end | 173 end |
174 } | 174 } |
175 M.print( fn() ) | 175 Io.print( fn() ) |
176 end | 176 end |
177 catch = function(e) | 177 catch = function(e) |
178 M.print(e) | 178 Io.print(e) |
179 end | 179 end |
180 } | 180 } |
181 end | 181 end |
182 end | 182 end |
183 | 183 |
184 | 184 |
185 return M | 185 return Io |