[jsscripting] Minor fixes & improvements (#13960)
* [jsscripting] Correct wrong `createScriptEngine` implementation * [jsscripting] Also unlock lock on unexpected exceptions (rethrow them) * [jsscripting] Call super methods from their overrides * [jsscripting] Move superclass call of `beforeInvocation` Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
parent
cd9e1b0590
commit
4d98cca7eb
|
@ -44,9 +44,21 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
|
||||||
private static final String INJECTION_CODE = "Object.assign(this, require('openhab'));";
|
private static final String INJECTION_CODE = "Object.assign(this, require('openhab'));";
|
||||||
private boolean injectionEnabled = true;
|
private boolean injectionEnabled = true;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Whilst we run in parallel with Nashorn, we use a custom mime-type to avoid
|
||||||
|
* disrupting Nashorn scripts. When Nashorn is removed, we take over the standard
|
||||||
|
* JS runtime.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// GraalJSEngineFactory graalJSEngineFactory = new GraalJSEngineFactory();
|
||||||
|
//
|
||||||
|
// scriptTypes.addAll(graalJSEngineFactory.getMimeTypes());
|
||||||
|
// scriptTypes.addAll(graalJSEngineFactory.getExtensions());
|
||||||
|
|
||||||
public static final String MIME_TYPE = "application/javascript;version=ECMAScript-2021";
|
public static final String MIME_TYPE = "application/javascript;version=ECMAScript-2021";
|
||||||
private static final String ALT_MIME_TYPE = "text/javascript;version=ECMAScript-2021";
|
private static final String ALT_MIME_TYPE = "text/javascript;version=ECMAScript-2021";
|
||||||
private static final String ALIAS = "graaljs";
|
private static final String ALIAS = "graaljs";
|
||||||
|
private static final List<String> SCRIPT_TYPES = List.of(MIME_TYPE, ALT_MIME_TYPE, ALIAS);
|
||||||
|
|
||||||
private final JSScriptServiceUtil jsScriptServiceUtil;
|
private final JSScriptServiceUtil jsScriptServiceUtil;
|
||||||
private final JSDependencyTracker jsDependencyTracker;
|
private final JSDependencyTracker jsDependencyTracker;
|
||||||
|
@ -61,19 +73,7 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getScriptTypes() {
|
public List<String> getScriptTypes() {
|
||||||
|
return SCRIPT_TYPES;
|
||||||
/*
|
|
||||||
* Whilst we run in parallel with Nashorn, we use a custom mime-type to avoid
|
|
||||||
* disrupting Nashorn scripts. When Nashorn is removed, we take over the standard
|
|
||||||
* JS runtime.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// GraalJSEngineFactory graalJSEngineFactory = new GraalJSEngineFactory();
|
|
||||||
//
|
|
||||||
// scriptTypes.addAll(graalJSEngineFactory.getMimeTypes());
|
|
||||||
// scriptTypes.addAll(graalJSEngineFactory.getExtensions());
|
|
||||||
|
|
||||||
return List.of(MIME_TYPE, ALT_MIME_TYPE, ALIAS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -83,6 +83,9 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable ScriptEngine createScriptEngine(String scriptType) {
|
public @Nullable ScriptEngine createScriptEngine(String scriptType) {
|
||||||
|
if (!SCRIPT_TYPES.contains(scriptType)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return new DebuggingGraalScriptEngine<>(
|
return new DebuggingGraalScriptEngine<>(
|
||||||
new OpenhabGraalJSScriptEngine(injectionEnabled ? INJECTION_CODE : null, jsScriptServiceUtil));
|
new OpenhabGraalJSScriptEngine(injectionEnabled ? INJECTION_CODE : null, jsScriptServiceUtil));
|
||||||
}
|
}
|
||||||
|
|
|
@ -178,6 +178,8 @@ public class OpenhabGraalJSScriptEngine
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void beforeInvocation() {
|
protected void beforeInvocation() {
|
||||||
|
super.beforeInvocation();
|
||||||
|
|
||||||
lock.lock();
|
lock.lock();
|
||||||
|
|
||||||
if (initialized) {
|
if (initialized) {
|
||||||
|
@ -235,13 +237,13 @@ public class OpenhabGraalJSScriptEngine
|
||||||
@Override
|
@Override
|
||||||
protected Object afterInvocation(Object obj) {
|
protected Object afterInvocation(Object obj) {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
return obj;
|
return super.afterInvocation(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Exception afterThrowsInvocation(Exception e) {
|
protected Exception afterThrowsInvocation(Exception e) {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
return e;
|
return super.afterThrowsInvocation(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -54,6 +54,8 @@ public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoClos
|
||||||
return afterInvocation(super.eval(s, scriptContext));
|
return afterInvocation(super.eval(s, scriptContext));
|
||||||
} catch (ScriptException se) {
|
} catch (ScriptException se) {
|
||||||
throw (ScriptException) afterThrowsInvocation(se);
|
throw (ScriptException) afterThrowsInvocation(se);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,6 +66,8 @@ public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoClos
|
||||||
return afterInvocation(super.eval(reader, scriptContext));
|
return afterInvocation(super.eval(reader, scriptContext));
|
||||||
} catch (ScriptException se) {
|
} catch (ScriptException se) {
|
||||||
throw (ScriptException) afterThrowsInvocation(se);
|
throw (ScriptException) afterThrowsInvocation(se);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,6 +78,8 @@ public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoClos
|
||||||
return afterInvocation(super.eval(s));
|
return afterInvocation(super.eval(s));
|
||||||
} catch (ScriptException se) {
|
} catch (ScriptException se) {
|
||||||
throw (ScriptException) afterThrowsInvocation(se);
|
throw (ScriptException) afterThrowsInvocation(se);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +90,8 @@ public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoClos
|
||||||
return afterInvocation(super.eval(reader));
|
return afterInvocation(super.eval(reader));
|
||||||
} catch (ScriptException se) {
|
} catch (ScriptException se) {
|
||||||
throw (ScriptException) afterThrowsInvocation(se);
|
throw (ScriptException) afterThrowsInvocation(se);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +102,8 @@ public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoClos
|
||||||
return afterInvocation(super.eval(s, bindings));
|
return afterInvocation(super.eval(s, bindings));
|
||||||
} catch (ScriptException se) {
|
} catch (ScriptException se) {
|
||||||
throw (ScriptException) afterThrowsInvocation(se);
|
throw (ScriptException) afterThrowsInvocation(se);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,6 +114,8 @@ public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoClos
|
||||||
return afterInvocation(super.eval(reader, bindings));
|
return afterInvocation(super.eval(reader, bindings));
|
||||||
} catch (ScriptException se) {
|
} catch (ScriptException se) {
|
||||||
throw (ScriptException) afterThrowsInvocation(se);
|
throw (ScriptException) afterThrowsInvocation(se);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue