|
| 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 | + |
0 commit comments