Skip to content

Commit

Permalink
improve log message on attach / detach failures #1558
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Jun 21, 2021
1 parent 96eb351 commit d6fbcc3
Showing 1 changed file with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ private void attachVariables(Set<Object> seen) {
try {
vars.put(k, new Variable(o));
} catch (Exception e) {
logger.debug("[*** attach variables ***] failed to attach graal value: {} - {}", k, e.getMessage());
logger.debug("[*** attach variables ***] ignoring non-json value: '{}' - {}", k, e.getMessage());
}
}
JS.put(k, o);
Expand All @@ -1066,7 +1066,7 @@ protected Map<String, Variable> detachVariables() {
Set<Object> seen = Collections.newSetFromMap(new IdentityHashMap());
Map<String, Variable> detached = new HashMap(vars.size());
vars.forEach((k, v) -> {
Object o = recurseAndDetachAndDeepClone(v.getValue(), seen);
Object o = recurseAndDetachAndDeepClone(k, v.getValue(), seen);
detached.put(k, new Variable(o));
});
return detached;
Expand All @@ -1080,19 +1080,15 @@ protected void recurseAndAttach(String name, Object o) {

private Object recurseAndAttach(String name, Object o, Set<Object> seen) {
if (o instanceof Value) {
Value value = Value.asValue(o);
try {
if (value.canExecute()) {
if (value.isMetaObject()) { // js function
return attach(value);
} else { // java function
return value;
}
} else { // anything else, including java-type references
Value value = Value.asValue(o);
if (value.canExecute() && value.isMetaObject()) { // js function
return attach(value);
} else { // anything else, including java functions and java-type references
return value;
}
} catch (Exception e) {
logger.warn("[*** attach ***] failed to attach js value: {} - {}", name, e.getMessage());
logger.warn("[*** attach ***] ignoring non-json value: '{}' - {}", name, e.getMessage());
return null;
}
} else if (o instanceof JsFunction) {
Expand Down Expand Up @@ -1130,15 +1126,15 @@ private Object recurseAndAttach(String name, Object o, Set<Object> seen) {
// called only by result processing of callonce / callSingle
protected Object recurseAndAttachAndDeepClone(Object o) {
Set<Object> seen = Collections.newSetFromMap(new IdentityHashMap());
return recurseAndAttachAndDeepClone(o, seen);
return recurseAndAttachAndDeepClone("", o, seen);
}

private Object recurseAndAttachAndDeepClone(Object o, Set<Object> seen) {
private Object recurseAndAttachAndDeepClone(String name, Object o, Set<Object> seen) {
if (o instanceof Value) {
try {
return Value.asValue(o);
} catch (Exception e) {
logger.warn("[*** attach deep ***] failed to attach graal value: {}", e.getMessage());
logger.warn("[*** attach deep ***] ignoring non-json value: '{}' - {}", name, e.getMessage());
return null;
}
}
Expand All @@ -1148,8 +1144,12 @@ private Object recurseAndAttachAndDeepClone(Object o, Set<Object> seen) {
} else if (o instanceof List) {
if (seen.add(o)) {
List list = (List) o;
List copy = new ArrayList(list.size());
list.forEach(v -> copy.add(recurseAndAttachAndDeepClone(v, seen)));
int count = list.size();
List copy = new ArrayList(count);
for (int i = 0; i < count; i++) {
Object child = list.get(i);
copy.add(recurseAndAttachAndDeepClone(name + "[" + i + "]", child, seen));
}
return copy;
} else {
return o;
Expand All @@ -1158,7 +1158,7 @@ private Object recurseAndAttachAndDeepClone(Object o, Set<Object> seen) {
if (seen.add(o)) {
Map<String, Object> map = (Map) o;
Map<String, Object> copy = new LinkedHashMap(map.size());
map.forEach((k, v) -> copy.put(k, recurseAndAttachAndDeepClone(v, seen)));
map.forEach((k, v) -> copy.put(k, recurseAndAttachAndDeepClone(name + "." + k, v, seen)));
return copy;
} else {
return o;
Expand All @@ -1171,10 +1171,10 @@ private Object recurseAndAttachAndDeepClone(Object o, Set<Object> seen) {
// only for callonce and callSingle
protected Object recurseAndDetachAndDeepClone(Object o) {
Set<Object> seen = Collections.newSetFromMap(new IdentityHashMap());
return recurseAndDetachAndDeepClone(o, seen);
return recurseAndDetachAndDeepClone("", o, seen);
}

private Object recurseAndDetachAndDeepClone(Object o, Set<Object> seen) {
private Object recurseAndDetachAndDeepClone(String name, Object o, Set<Object> seen) {
if (o instanceof Value) {
Value value = (Value) o;
try {
Expand All @@ -1189,15 +1189,19 @@ private Object recurseAndDetachAndDeepClone(Object o, Set<Object> seen) {
o = JsValue.toJava(value);
}
} catch (Exception e) {
logger.warn("[*** detach deep ***] unsupported value in callonce / callSingle: {}", e.getMessage());
logger.warn("[*** detach deep ***] ignoring non-json value in callonce / callSingle: '{}' - {}", e.getMessage());
return null;
}
}
if (o instanceof List) {
if (seen.add(o)) {
List list = (List) o;
int count = list.size();
List copy = new ArrayList(list.size());
list.forEach(v -> copy.add(recurseAndDetachAndDeepClone(v, seen)));
for (int i = 0; i < count; i++) {
Object child = list.get(i);
copy.add(recurseAndDetachAndDeepClone(name + "[" + i + "]", child, seen));
}
return copy;
} else {
return o;
Expand All @@ -1206,7 +1210,7 @@ private Object recurseAndDetachAndDeepClone(Object o, Set<Object> seen) {
if (seen.add(o)) {
Map<String, Object> map = (Map) o;
Map<String, Object> copy = new LinkedHashMap(map.size());
map.forEach((k, v) -> copy.put(k, recurseAndDetachAndDeepClone(v, seen)));
map.forEach((k, v) -> copy.put(k, recurseAndDetachAndDeepClone(name + "." + k, v, seen)));
return copy;
} else {
return o;
Expand Down Expand Up @@ -1301,7 +1305,7 @@ public void setVariables(Map<String, Object> map) {
try {
setVariable(k, v);
} catch (Exception e) {
logger.warn("[*** set variables ***] skipping invalid graal value: {} - {}", k, e.getMessage());
logger.warn("[*** set variables ***] ignoring non-json value: {} - {}", k, e.getMessage());
}
});
}
Expand Down

0 comments on commit d6fbcc3

Please sign in to comment.