Skip to content

Commit 0c83abd

Browse files
committed
added simple Lua interpreter with sample init file, plus useful import module
1 parent b89e928 commit 0c83abd

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

assets/import.lua

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
local packages = {}
2+
local append = table.insert
3+
local new = luajava.new
4+
5+
-- SciTE requires this, if you want to see stdout immediately...
6+
7+
io.stdout:setvbuf 'no'
8+
io.stderr:setvbuf 'no'
9+
10+
local function new_tostring (o)
11+
return o:toString()
12+
end
13+
14+
local function call (t,...)
15+
local obj,stat
16+
if select('#',...) == 1 and type(select(1,...))=='table' then
17+
obj = make_array(t,select(1,...))
18+
else
19+
stat,obj = pcall(new,t,...)
20+
if not stat then
21+
print(debug.traceback())
22+
os.exit(1)
23+
end
24+
end
25+
getmetatable(obj).__tostring = new_tostring
26+
return obj
27+
end
28+
29+
local function import_class (classname,packagename)
30+
local res,class = pcall(luajava.bindClass,packagename)
31+
if res then
32+
_G[classname] = class
33+
local mt = getmetatable(class)
34+
mt.__call = call
35+
return class
36+
end
37+
end
38+
39+
local function massage_classname (classname)
40+
if classname:find('_') then
41+
classname = classname:gsub('_','$')
42+
end
43+
return classname
44+
end
45+
46+
local globalMT = {
47+
__index = function(T,classname)
48+
classname = massage_classname(classname)
49+
for i,p in ipairs(packages) do
50+
local class = import_class(classname,p..classname)
51+
if class then return class end
52+
end
53+
print("import cannot find "..classname)
54+
end
55+
}
56+
setmetatable(_G, globalMT)
57+
58+
function import (package)
59+
local i = package:find('%.%*$')
60+
if i then -- a wildcard; put into the package list, including the final '.'
61+
append(packages,package:sub(1,i))
62+
else
63+
local classname = package:match('([%w_]+)$')
64+
if not import_class(classname,package) then
65+
error("cannot find "..package)
66+
end
67+
end
68+
end
69+
70+
append(packages,'')
71+
72+
function proxy (classname,obj)
73+
classname = massage_classname(classname)
74+
-- if the classname contains dots it's assumed to be fully qualified
75+
if classname:find('.',1,true) then
76+
return luajava.createProxy(classname,obj)
77+
end
78+
-- otherwise, it must lie on the package path!
79+
for i,p in ipairs(packages) do
80+
local ok,res = pcall(luajava.createProxy,p..classname, obj)
81+
if ok then return res end
82+
end
83+
error ("cannot find "..classname)
84+
end
85+
86+
87+
function enum(e)
88+
--local e = o:GetEnumerator()
89+
return function()
90+
if e:hasMoreElements() then
91+
return e:nextElement()
92+
end
93+
end
94+
end
95+
96+
function dump (t)
97+
for k,v in pairs(t) do
98+
print(k,v)
99+
end
100+
end
101+
102+
function p (o)
103+
if type(o) == 'userdata' then
104+
local mt = getmetatable(o)
105+
if not mt.__tostring then
106+
return print('java:'..o:toString())
107+
end
108+
end
109+
print(type(o)..':'..tostring(o))
110+
end
111+
112+
import 'java.lang.reflect.Array'
113+
114+
function make_array (Type,list)
115+
local len
116+
local init = type(list)=='table'
117+
if init then
118+
len = #list
119+
else
120+
len = list
121+
end
122+
local arr = Array:newInstance(Type,len)
123+
if init then
124+
for i,v in ipairs(list) do
125+
Array:set(arr,i-1,v)
126+
end
127+
end
128+
return arr
129+
end
130+
131+
132+
import 'java.lang.*'
133+
import 'java.util.*'
134+

init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'import'
2+
context = activity:getApplicationContext()

interp.lua

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'socket'
2+
local c = socket.connect('localhost',3333)
3+
local log = io.open('log.txt','a')
4+
5+
function readfile(file)
6+
local f,err = io.open(file)
7+
if not f then return nil,err end
8+
local contents = f:read '*a':gsub('\n','\001')
9+
f:close()
10+
return contents
11+
end
12+
13+
function eval(line)
14+
c:send(line..'\n')
15+
local res = c:receive()
16+
return res:gsub('\001','\n')
17+
end
18+
19+
local init,err = readfile 'init.lua'
20+
if init then
21+
print 'loading init.lua'
22+
io.write(eval(init)..'\n')
23+
end
24+
25+
io.write '> '
26+
local line = io.read()
27+
28+
while line do
29+
log:write(line,'\n')
30+
local cmd,file = line:match '^%.(.)%s+(.+)$'
31+
if file then
32+
local mod
33+
if cmd == 'm' then
34+
mod = file
35+
file = mod:gsub('%.','/')..'.lua'
36+
end
37+
line,err = readfile(file)
38+
if mod and line then
39+
line = '--mod:'..mod..'\001'..line
40+
end
41+
else
42+
local expr = line:match '^%s*=%s*(.+)$'
43+
if expr then
44+
line = 'print('..expr..')'
45+
end
46+
end
47+
if line then
48+
local res = eval(line)
49+
log:write(res,'\n')
50+
io.write(res)
51+
else
52+
print(err)
53+
end
54+
io.write '> '
55+
line = io.read()
56+
end
57+
log:close()
58+
c:close()

0 commit comments

Comments
 (0)