From a383a00f0e4f4994af57ff90c0411d9ef310a9e9 Mon Sep 17 00:00:00 2001 From: Peter Thomas Date: Fri, 25 Jun 2021 21:47:26 +0530 Subject: [PATCH] one more idea for #1558 that shows promise --- .../intuit/karate/core/ScenarioEngine.java | 24 ++++++++++++------- .../com/intuit/karate/graal/JsExecutable.java | 11 +++------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java b/karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java index 648827fe1..803b4fde8 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java +++ b/karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java @@ -1117,7 +1117,11 @@ private AttachResult recurseAndAttach(String name, Object o, Set seen) { return AttachResult.dirty(value); } } - if (o instanceof JsFunction) { + if (o instanceof Class) { + Class clazz = (Class) o; + Value value = JS.evalForValue("Java.type('" + clazz.getCanonicalName() + "')"); + return AttachResult.dirty(value); + } else if (o instanceof JsFunction) { JsFunction jf = (JsFunction) o; try { return AttachResult.dirty(attachSource(jf.source)); @@ -1169,7 +1173,11 @@ private Object recurseAndAttachAndDeepClone(String name, Object o, Set s return null; } } - if (o instanceof JsFunction) { + if (o instanceof Class) { + Class clazz = (Class) o; + Value value = JS.evalForValue("Java.type('" + clazz.getCanonicalName() + "')"); + return value; + } else if (o instanceof JsFunction) { JsFunction jf = (JsFunction) o; try { return attachSource(jf.source); @@ -1212,20 +1220,20 @@ protected Object recurseAndDetachAndDeepClone(Object o) { private Object recurseAndDetachAndDeepClone(String name, Object o, Set seen) { if (o instanceof Value) { - Value value = (Value) o; + Value value = Value.asValue(o); try { - if (value.canExecute()) { + if (value.canExecute()) { if (value.isMetaObject()) { // js function return new JsFunction(value); } else { // java function return new JsExecutable(value); } - } else { - return value; + } else if (value.isHostObject()) { + return value.asHostObject(); } } catch (Exception e) { - logger.warn("[*** detach deep ***] ignoring non-json value in callonce / callSingle: '{}' - {}", e.getMessage()); - return value; // try our luck + logger.warn("[*** detach deep ***] ignoring non-json value in callonce / callSingle: '{}' - {}", name, e.getMessage()); + return null; } } if (o instanceof List) { diff --git a/karate-core/src/main/java/com/intuit/karate/graal/JsExecutable.java b/karate-core/src/main/java/com/intuit/karate/graal/JsExecutable.java index 11e9b6a86..25fd61181 100644 --- a/karate-core/src/main/java/com/intuit/karate/graal/JsExecutable.java +++ b/karate-core/src/main/java/com/intuit/karate/graal/JsExecutable.java @@ -33,23 +33,18 @@ * @author pthomas3 */ public class JsExecutable implements ProxyExecutable { - + private static final Logger logger = LoggerFactory.getLogger(JsExecutable.class); - private final Value value; + public final Value value; public JsExecutable(Value value) { this.value = value; } - private static final Object LOCK = new Object(); - @Override public Object execute(Value... arguments) { - synchronized (LOCK) { - logger.warn("[*** execute ***] global lock on java function possibly from callonce / callSingle: {}", value); - return value.execute(arguments); - } + return value.execute(arguments); } }