Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ void modified(Map<String, ?> config) {
wrapperEnabled ? "Enabled" : "Disabled");
}
if (oldEventConversionEnabled != eventConversionEnabled) {
if (eventConversionEnabled && (!isInjectionEnabledForUiBasedScript() || !wrapperEnabled)) {
if (eventConversionEnabled && !isInjectionEnabledForUiBasedScript()) {
logger.warn(
"Enabled event conversion for UI-based scripts, but auto-injection or wrapper is disabled. Event conversion will not work.");
"Enabled event conversion for UI-based scripts, but auto-injection is disabled. Event conversion will not work.");
}
if (!eventConversionEnabled) {
logger.info(
Expand All @@ -99,7 +99,7 @@ void modified(Map<String, ?> config) {
* @param config configuration parameters to apply to JavaScript
*/
private void update(Map<String, ?> config) {
logger.trace("JavaScript Script Engine Configuration: {}", config);
logger.debug("JavaScript Script Engine Configuration: {}", config);

injectionEnabled = ConfigParser.valueAsOrElse(config.get(CFG_INJECTION_ENABLED), Integer.class,
INJECTION_ENABLED_FOR_UI_BASED_SCRIPTS_ONLY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public class OpenhabGraalJSScriptEngine
}
}
private static final String OPENHAB_JS_INJECTION_CODE = "Object.assign(this, require('openhab'));";
private static final String EVENT_CONVERSION_CODE = "const event = (typeof this.rules?._getTriggeredData === 'function') ? rules._getTriggeredData(ctx, true) : this.event";
private static final String EVENT_CONVERSION_CODE = "this.event = (typeof this.rules?._getTriggeredData === 'function') ? rules._getTriggeredData(ctx, true) : this.event";

private static final String REQUIRE_WRAPPER_NAME = "__wraprequire__";
/** Shared Polyglot {@link Engine} across all instances of {@link OpenhabGraalJSScriptEngine} */
Expand Down Expand Up @@ -156,7 +156,7 @@ public class OpenhabGraalJSScriptEngine
*/
public OpenhabGraalJSScriptEngine(GraalJSScriptEngineConfiguration configuration,
JSScriptServiceUtil jsScriptServiceUtil, JSDependencyTracker jsDependencyTracker) {
super(null); // delegate depends on fields not yet initialised, so we cannot set it immediately
super(null); // delegate depends on fields not yet initialized, so we cannot set it immediately
this.configuration = configuration;
this.jsRuntimeFeatures = jsScriptServiceUtil.getJSRuntimeFeatures(lock);

Expand Down Expand Up @@ -328,18 +328,20 @@ protected void beforeInvocation() {

@Override
protected String onScript(String script) {
if (isUiBasedScript() && configuration.isWrapperEnabled()) {
logger.debug("Wrapping script for engine '{}' ...", engineIdentifier);

String eventConversionScript = "";
if (configuration.isEventConversionEnabled()) {
eventConversionScript = EVENT_CONVERSION_CODE + System.lineSeparator();
}
if (!isUiBasedScript()) {
return super.onScript(script);
}

return "(function() {" + System.lineSeparator() + eventConversionScript + script + System.lineSeparator()
+ "})()";
String newScript = script;
if (configuration.isEventConversionEnabled()) {
logger.debug("Injecting event conversion code into script for engine '{}'.", engineIdentifier);
newScript = EVENT_CONVERSION_CODE + System.lineSeparator() + newScript;
}
if (configuration.isWrapperEnabled()) {
logger.debug("Wrapping script for engine '{}' ...", engineIdentifier);
newScript = "(function() {" + System.lineSeparator() + newScript + System.lineSeparator() + "})()";
}
return super.onScript(script);
return super.onScript(newScript);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import javax.script.ScriptException;

/**
* Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException} being
* thrown.
* Delegate allowing AOP-style interception of calls, either before invocation, after invocation, upon a
* {@link ScriptException} being thrown, or on receiving the script code.
*
* @param <T> The delegate class
* @author Jonathan Gilbert - Initial contribution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* LifecycleTracker implementation
*
* <p>
* We can't use core's lifecycle tracker for JS Scripting, because its dispose hooks are called after the engine has
* We can't use core's lifecycle tracker for JS Scripting because its disposal hooks are called after the engine has
* been closed (which will not work).
*
* @author Florian Hotze - Initial contribution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<description><![CDATA[
Converting the event data from Java to JavaScript types in UI-based scripts allows working with event data in a native JS way without special handling for Java types.<br>
With this option enabled, the event data available in UI-based scripts is all JS types and the same as in file-based scripts.<br>
Please note that this option <strong>requires both auto-injection & wrapper enabled</strong> and only applies to UI-based scripts and does not affect file-based scripts.
Please note that this option <strong>requires auto-injection enabled at least for UI-based scripts</strong>.
]]></description>
<default>true</default>
<advanced>true</advanced>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ addon.jsscripting.description = This adds a JS (ECMAScript-2024) script engine.
automation.config.jsscripting.dependencyTrackingEnabled.label = Enable Dependency Tracking
automation.config.jsscripting.dependencyTrackingEnabled.description = Dependency tracking allows your scripts to automatically reload when one of its dependencies is updated. You may want to disable dependency tracking if you plan on editing or updating a shared library, but don't want all your scripts to reload until you can test it. Please note that changing this setting only applies to scripts loaded after the change.
automation.config.jsscripting.eventConversionEnabled.label = Convert Event from Java to JavaScript type in UI-based scripts
automation.config.jsscripting.eventConversionEnabled.description = Converting the event data from Java to JavaScript types in UI-based scripts allows working with event data in a native JS way without special handling for Java types.<br> With this option enabled, the event data available in UI-based scripts is all JS types and the same as in file-based scripts.<br> Please note that this option <strong>requires both auto-injection & wrapper enabled</strong> and only applies to UI-based scripts and does not affect file-based scripts.
automation.config.jsscripting.eventConversionEnabled.description = Converting the event data from Java to JavaScript types in UI-based scripts allows working with event data in a native JS way without special handling for Java types.<br> With this option enabled, the event data available in UI-based scripts is all JS types and the same as in file-based scripts.<br> Please note that this option <strong>requires auto-injection enabled at least for UI-based scripts</strong>.
automation.config.jsscripting.group.environment.label = JavaScript Environment
automation.config.jsscripting.group.environment.description = This group defines JavaScript's environment.
automation.config.jsscripting.group.system.label = System Behaviour
Expand Down