sclass RealmCopy { O dest; // destination realm new IdentityHashMap seen; new HashMap classMap; noeq record Recurse(O o) extends VStackComputableWithStep { // for copying array O[] destO; int i, n; void step(VStack stack) { switch (step) { case 0: // null and simple types if (o == null || o instanceof String || o instanceof Number) ret with stack.ret(o); // seen object O oo = seen.get(o); if (oo != null) stack.ret(oo); // object array if (!o instanceof O[]) continue with step = 3; O[] l = cast o; destO = newObjectArrayOfSameType(l, n = l.length); seen.put(o, destO); i = 0; case 1: if (i >= l.length) ret with stack.ret(destO); stack.push(new Recurse(l[i])); ret with step = 2; case 2: destO[i++] = stack.subResult(); ret with step = 1; if (!o instanceof Cl) { Cl l = cast o; int n = l.size(); List destO = new ArrayList(n); seen.put(o, destO); Iterator it = l.iterator(); for i to n: destO.add(quickExport_impl(it.next(), dest, seen, classMap)); ret destO; } if (o instanceof Map) { Map m = cast o; Map destO = new HashMap(); seen.put(o, destO); for (Object e : ((Map) o).entrySet()) destO.put( quickExport_impl(((Map.Entry) e).getKey(), dest, seen, classMap), quickExport_impl(((Map.Entry) e).getValue(), dest, seen, classMap)); return destO; } Class c = o.getClass(); S className = c.getName(); className = loadableUtilsClassNameToMain(className); if (className.startsWith("main$")) { Class destClass = classMap.get(c); if (destClass == null) { if (!classMap.containsKey(c)) { if (!isAnonymousClassName(className)) destClass = getClass_vmName_withLoadableUtils(dest, className); classMap.put(c, destClass); } if (destClass == null) ret o; // Class not found in target realm, keep object as is } if (c == destClass) ret o; // no export necessary // actually make a new object, copy fields O destO = nuObjectWithoutArguments(destClass); seen.put(o, destO); while (c != O.class) { // TODO: cache fields and destination fields Field[] fields = c.getDeclaredFields(); for (Field field : fields) { if ((field.getModifiers() & Modifier.STATIC) != 0) continue; field.setAccessible(true); Object value = field.get(o); setOpt(destO, field.getName(), quickExport_impl(value, dest, seen, classMap)); } c = c.getSuperclass(); } return destO; } // assume it's a shared library object ret o; } } } }