sclass RealmCopy { O dest; // destination realm new IdentityHashMap seen; new HashMap classMap; noeq record CopyObject(O o) extends VStackComputableWithStep { void step(VStack stack) { // 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); dispatchByType(stack, o); } } void dispatchByType(VStack stack, O o) { if (o cast O[]) ret with stack.replace(new CopyObjectArray(o)); // default: keep object stack.ret(o); } noeq record CopyObjectArray(O[] o) extends VStackComputableWithStep { O[] out; int i; void step(VStack stack) { if (step == 0) { out = newObjectArrayOfSameType(o); seen.put(o, out); ++step; } if (stack.hasSubResult()) out[i++] = stack.subResult(); if (i >= o.length) ret with stack.ret(out); stack.call(new CopyObject(o[i])); } } noeq record CopyCollection(Cl o) extends VStackComputableWithStep { Cl out; Iterator it; void step(VStack stack) { if (step == 0) { out = similarEmptyCollection(o); seen.put(o, out); ++step; } if (stack.hasSubResult()) out.add(stack.subResult()); if (!it.hasNext()) ret with stack.ret(out); stack.call(new CopyObject(it.next())); } } noeq record CopyMap(Map o) extends VStackComputableWithStep { Map out; Iterator it; Map.Entry entry; O copiedKey; void step(VStack stack) { if (step == 0) { out = similarEmptyMap(o); seen.put(o, out); ++step; } if (stack.hasSubResult()) { if (step == 1) { // got the copied key copiedKey = stack.subResult(); // now copy the value stack.call(new CopyObject(entry.getValue())); ret with ++step; } else { // got the copied value - can put into map out.put(copiedKey, stack.subResult()); step = 1; } } if (!it.hasNext()) ret with stack.ret(out); entry = it.next(); stack.call(new CopyObject(entry.getKey())); } } /* 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; } } }*/ }