!752 sS suckerID = "#1004698"; static interface Algorithm { S predict(int max); void feed(S s); } // simple test algorithm sS lua = [[ function p(m, s) if s ~= nil then c = s:sub(s:len()) end return c end ]]; static O sucker; p { wakeSucker(suckerID, "L " + trim(lua), new LuaAlgo(trim(lua))); fixContextClassLoader(); luaCompile(true); wakeSucker(suckerID, "L " + trim(lua), new LuaAlgo(trim(lua))); cleanUp(sucker); } static void wakeSucker(S suckerID, S desc, Algorithm algo) { ping(); if (sucker == null) { print("Waking..."); sucker = run(suckerID); } print("Feeding..."); Class intrface = getClass(sucker, "main$Algorithm"); assertNotNull("main$Algorithm", intrface); O proxy = proxy(intrface, algo); O result = callOpt(sucker, "suck", desc, proxy); print("Suck Result: " + struct(result)); } static class LuaAlgo implements Algorithm { LuaValue predict, feed; S feeding; *(S code) { Sandbox s = luaSandbox(); evalLua(s, code); //print("KEYS: " + structure(s.keys())); predict = s.get("predict"); if (predict.isnil()) predict = s.get("p"); assertFalse(predict.isnil()); } public S predict(int max) { LuaValue s = predict.call(Lua.value(max), feeding != null ? Lua.value(feeding) : Lua.NIL); feeding = null; ret s.isnil() ? "" : s.tojstring(); } public void feed(S s) { feeding = s; } } static Algorithm makeAlgo(S desc) { assertStartsWith(desc, "L "); ret new LuaAlgo(desc.substring(2)); }