import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import java.text.SimpleDateFormat;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.nio.file.Files;
import java.nio.file.Path;
class main {
static Object getStandardFunctionHolder(String functionName) {
for (Object o : mcAndUtils())
if (hasMethodNamed(o, functionName))
return o;
return loadFunction_cached(functionName);
}
static List mcAndUtils() {
return llNonNulls(
mc(),
findClass_fullName("loadableUtils.utils"));
}
// This is a bit rough... finds static and non-static methods.
// It's usually just for error messages though, so no worries.
static boolean hasMethodNamed(Object obj, String method) {
if (obj == null) return false;
if (obj instanceof Class)
return hasMethodNamed((Class) obj, method);
return hasMethodNamed(obj.getClass(), method);
}
static boolean hasMethodNamed(Class c, String method) {
while (c != null) {
for (Method m : c.getDeclaredMethods())
if (m.getName().equals(method))
return true;
c = c.getSuperclass();
}
return false;
}
static Class loadFunction_cached(String function) {
return loadFunctions_cached(function);
}
static List llNonNulls(A... a) {
List l = new ArrayList();
for (A x : a) if (x != null) l.add(x);
return l;
}
static Class mc() {
return main.class;
}
static HashMap findClass_fullName_cache = new HashMap();
// returns null on not found
// this is the simple version that is not case-tolerant
static Class findClass_fullName(String name) {
synchronized(findClass_fullName_cache) {
if (findClass_fullName_cache.containsKey(name))
return findClass_fullName_cache.get(name);
Class c;
try {
c = Class.forName(name);
} catch (ClassNotFoundException e) {
c = null;
}
findClass_fullName_cache.put(name, c);
return c;
}
}
static HashMap, Class> loadFunctions_cache = new HashMap();
static Lock loadFunctions_cached_lock = lock();
static boolean loadFunctions_preferDiskCache = false; // Note: This may use outdated versions of standard functions
static Class loadFunctions_cached(String... functions) {
return loadFunctions_cached(asList(functions));
}
static Class loadFunctions_cached(Collection functions) {
TreeSet functionSet = new TreeSet(functions);
Lock __0 = loadFunctions_cached_lock; lock(__0); try {
Class c = loadFunctions_cache.get(functionSet);
if (c != null) return c;
String transpilation = null;
// new mode for slow machines (Raspberry Pi): get server transpilation
if (l(functions) == 1 && isSlowMachine()) {
String snippetID = stdFunctions_cached().get(first(functions));
if (nempty(snippetID)) {
if (!isMD5(getUpToDateSnippetTranspilationMD5(snippetID)))
print("Slow machine mode: " + snippetID + " transpilation not up to date");
else {
transpilation = getServerTranspiledWithoutLibs(snippetID); // TODO: libs?
print("Loaded server transpilation: " + snippetID);
}
}
}
if (transpilation == null) {
String cached = getTranspiledStandardFunctionsIfCached(functionSet);
// TODO: make sure we're using an up-to-date transpiler too
if (cached != null && (loadFunctions_preferDiskCache || standardFunctionsCacheUpToDate(functionSet)))
transpilation = cached;
}
if (transpilation != null) {
// TODO: LIBS
print("Loading " + joinWithPlus(functionSet));
c = veryQuickJava_finish(transpilation, emptyList());
}
if (c == null) c = loadFunctions(functionSet);
loadFunctions_cache.put(functionSet, c);
assertNotNull(loadFunctions_cache.get(functionSet));
return c;
} finally { unlock(__0); } }
static void loadFunctions_cached_clearCache() {
Lock __1 = loadFunctions_cached_lock; lock(__1); try {
loadFunctions_cache.clear();
} finally { unlock(__1); } }
static void _onLoad_loadFunctions_cached() {
ownResource(vmBus_onMessage("refreshedTranspiler", new Runnable() { public void run() { try { loadFunctions_clearCache();
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "loadFunctions_clearCache();"; }}));
}
static void lock(Lock lock) { try {
ping();
if (lock == null) return;
try {
lock.lockInterruptibly();
} catch (InterruptedException e) {
Object reason = vm_threadInterruptionReasonsMap().get(currentThread());
print("Locking interrupted! Reason: " + strOr(reason, "Unknown"));
printStackTrace(e);
rethrow(e);
}
// NO call to ping here! Make sure lock is always released.
} catch (Exception __e) { throw rethrow(__e); } }
static void lock(Lock lock, String msg) {
print("Locking: " + msg);
lock(lock);
}
static void lock(Lock lock, String msg, long timeout) {
print("Locking: " + msg);
lockOrFail(lock, timeout);
}
static ReentrantLock lock() {
return fairLock();
}
// unclear semantics as to whether return null on null
static ArrayList asList(A[] a) {
return a == null ? new ArrayList() : new ArrayList(Arrays.asList(a));
}
static ArrayList asList(int[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (int i : a) l.add(i);
return l;
}
static ArrayList asList(long[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (long i : a) l.add(i);
return l;
}
static ArrayList asList(float[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (float i : a) l.add(i);
return l;
}
static ArrayList asList(double[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (double i : a) l.add(i);
return l;
}
static ArrayList asList(Iterable s) {
if (s instanceof ArrayList) return (ArrayList) s;
ArrayList l = new ArrayList();
if (s != null)
for (A a : s)
l.add(a);
return l;
}
static ArrayList asList(Enumeration e) {
ArrayList l = new ArrayList();
if (e != null)
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static boolean isSlowMachine() {
return eq("1", trimLoadTextFile(javaxDataDir("is-slow-machine.txt")));
}
static Map stdFunctions_cached_map; // name -> snippet ID
static Lock stdFunctions_cached_lock = lock();
static Map stdFunctions_cached() {
Lock __0 = stdFunctions_cached_lock; lock(__0); try {
if (stdFunctions_cached_map == null)
stdFunctions_cached_map = stdFunctions_uncached();
return stdFunctions_cached_map;
} finally { unlock(__0); } }
static synchronized void stdFunctions_clearCache() {
stdFunctions_cached_map = null;
}
static Object first(Object list) {
return first((Iterable) list);
}
static A first(List list) {
return empty(list) ? null : list.get(0);
}
static A first(A[] bla) {
return bla == null || bla.length == 0 ? null : bla[0];
}
static A first(IterableIterator i) {
return first((Iterator) i);
}
static A first(Iterator i) {
return i == null || !i.hasNext() ? null : i.next();
}
static A first(Iterable i) {
if (i == null) return null;
Iterator it = i.iterator();
return it.hasNext() ? it.next() : null;
}
static Character first(String s) { return empty(s) ? null : s.charAt(0); }
static Character first(CharSequence s) { return empty(s) ? null : s.charAt(0); }
static A first(Pair p) {
return p == null ? null : p.a;
}
static Byte first(byte[] l) {
return empty(l) ? null : l[0];
}
static boolean nempty(Collection c) {
return !empty(c);
}
static boolean nempty(CharSequence s) {
return !empty(s);
}
static boolean nempty(Object[] o) { return !empty(o); }
static boolean nempty(byte[] o) { return !empty(o); }
static boolean nempty(int[] o) { return !empty(o); }
static boolean nempty(Map m) {
return !empty(m);
}
static boolean nempty(Iterator i) {
return i != null && i.hasNext();
}
static boolean nempty(Object o) { return !empty(o); }
static boolean isMD5(String s) {
return l(s) == 32 && isLowerHexString(s);
}
static String getUpToDateSnippetTranspilationMD5(String snippetID) { try {
return loadPageSilently(tb_mainServer() + "/tb-int/get-transpilation-md5.php?id=" + psI(snippetID)
+ standardCredentials_noCookies());
} catch (Exception __e) { throw rethrow(__e); } }
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal