annotate src/luan/modules/Io.luan @ 1204:5d0cbd908582

remove Io socket support add SSL for Rpc
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Mar 2018 19:48:15 -0700
parents e15a41a8b4b2
children 623dfe0e2e73
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
321
7f7708e8fdd4 remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 320
diff changeset
1 java()
7f7708e8fdd4 remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 320
diff changeset
2 local IoLuan = require "java:luan.modules.IoLuan"
7f7708e8fdd4 remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 320
diff changeset
3 local System = require "java:java.lang.System"
297
899253043270 remove PackageLuan.load_lib()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
4
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
5 local Io = {}
503
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 435
diff changeset
6
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
7 Io.ip = IoLuan.ip
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
8 Io.my_ips = IoLuan.my_ips
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
9 Io.read_console_line = IoLuan.read_console_line
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
10 Io.schemes = IoLuan.newSchemes()
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
11 Io.uri = IoLuan.uri
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
12 Io.stdin = IoLuan.defaultStdin.table()
1204
5d0cbd908582 remove Io socket support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1192
diff changeset
13 --Io.socket_server = IoLuan.socket_server
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
14 Io.stdout = IoLuan.textWriter(System.out)
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
15 Io.stderr = IoLuan.textWriter(System.err)
1134
e54ae41e9501 cleaner security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1088
diff changeset
16 Io.unrestricted = IoLuan.unrestricted
298
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
17
753
5e3970ccd86a improve password handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 746
diff changeset
18 -- used by http and rpc
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
19 Io.password = "password"
298
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
20
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 680
diff changeset
21 local Luan = require "luan:Luan.luan"
554
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
22 local error = Luan.error
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
23 local to_string = Luan.to_string or error()
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
24 local type = Luan.type or error()
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
25 local try = Luan.try or error()
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
26 local ipairs = Luan.ipairs or error()
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
27 local pairs = Luan.pairs or error()
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
28 local values = Luan.values or error()
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
29 local load = Luan.load or error()
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 680
diff changeset
30 local Table = require "luan:Table.luan"
554
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
31 local unpack = Table.unpack or error()
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 680
diff changeset
32 local String = require "luan:String.luan"
554
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
33 local encode = String.encode or error()
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
34 local matches = String.matches or error()
539
473e456444ff Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents: 503
diff changeset
35
320
fed1893821bf remove global namespace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 319
diff changeset
36
680
ecd436959855 improve template statements
Franklin Schmidt <fschmidt@gmail.com>
parents: 629
diff changeset
37 -- do not change
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
38 function Io.template_write(...)
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
39 return Io.stdout.write(...)
680
ecd436959855 improve template statements
Franklin Schmidt <fschmidt@gmail.com>
parents: 629
diff changeset
40 end
ecd436959855 improve template statements
Franklin Schmidt <fschmidt@gmail.com>
parents: 629
diff changeset
41
ecd436959855 improve template statements
Franklin Schmidt <fschmidt@gmail.com>
parents: 629
diff changeset
42
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
43 function Io.print_to(out,...)
298
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
44 local list = {}
539
473e456444ff Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents: 503
diff changeset
45 for v in values(...) do
298
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
46 list[#list+1] = to_string(v)
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
47 list[#list+1] = '\t'
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
48 end
554
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
49 if #list > 0 then
298
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
50 list[#list] = '\n'
539
473e456444ff Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents: 503
diff changeset
51 out.write( unpack(list) )
298
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
52 end
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
53 end
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
54
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
55 function Io.print(...)
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
56 Io.print_to(Io.stdout,...)
298
2ce23c7e2342 remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 297
diff changeset
57 end
392
8668f3799ad9 add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents: 321
diff changeset
58
8668f3799ad9 add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents: 321
diff changeset
59
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
60 function Io.output_to(out,fn,...)
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
61 local old_out = Io.stdout
589
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
62 return try( {
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
63 function(...)
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
64 Io.stdout = out
589
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
65 fn(...)
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
66 end;
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
67 finally = function()
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
68 Io.stdout = old_out
589
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
69 end;
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
70 }, ... )
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
71 end
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
72
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
73 local uri = Io.uri -- make local
589
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
74
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
75 function Io.output_of(fn,...)
589
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
76 local string_uri = uri "string:"
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
77 local out = string_uri.text_writer()
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
78 Io.output_to(out,fn,...)
589
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
79 out.close()
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
80 return string_uri.read_text()
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
81 end
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
82
97c8ae330efe add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents: 554
diff changeset
83
554
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
84 -- debug
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
85
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
86 function Io.debug(prompt)
554
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
87 prompt = prompt or "luan_debug> "
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
88 local function console()
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
89 return Io.read_console_line(prompt)
554
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
90 end
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
91 local env = {}
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
92 for line in console do
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
93 try {
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
94 function()
694
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
95 local fn
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
96 try {
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
97 function()
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
98 fn = load("return "..line,"stdin",env)
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
99 end
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
100 catch = function(e)
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
101 fn = load(line,"stdin",env)
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
102 end
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
103 }
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
104 Io.print( fn() )
694
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
105 end
554
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
106 catch = function(e)
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
107 Io.print(e)
694
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
108 end
554
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
109 }
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
110 end
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
111 end
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
112
18504c41b0be move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents: 539
diff changeset
113
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
114 return Io