From a85d0b6129e21219b62deda25f524418fa739a22 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Fri, 9 Jun 2023 15:13:33 +0300 Subject: [PATCH 1/4] fix_bug_switch: add validation to tbel --- .../script/api/tbel/DefaultTbelInvokeService.java | 6 ++++++ ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java index f98e33b615..5497e6cbbc 100644 --- a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java +++ b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java @@ -25,6 +25,7 @@ import com.google.common.util.concurrent.MoreExecutors; import lombok.Getter; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.mvel2.CompileException; import org.mvel2.ExecutionContext; import org.mvel2.MVEL; import org.mvel2.ParserContext; @@ -65,6 +66,8 @@ public class DefaultTbelInvokeService extends AbstractScriptInvokeService implem protected final Map scriptIdToHash = new ConcurrentHashMap<>(); protected final Map scriptMap = new ConcurrentHashMap<>(); + private final String tbelSwitch = "switch"; + private final String tbelSwitchErrorMsg = "TBEL does not support the 'switch'."; protected Cache compiledScriptsCache; private SandboxedParserConfiguration parserConfig; @@ -181,6 +184,9 @@ public class DefaultTbelInvokeService extends AbstractScriptInvokeService implem } return scriptId; } catch (Exception e) { + if (((CompileException) e).getExpr() != null && new String(((CompileException) e).getExpr()).contains(tbelSwitch)) { + e = new CompileException(tbelSwitchErrorMsg, ((CompileException) e).getExpr(), ((CompileException) e).getCursor(), e.getCause()); + } throw new TbScriptException(scriptId, TbScriptException.ErrorCode.COMPILATION, scriptBody, e); } }); diff --git a/ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js b/ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js index bc22e806c3..13ec1d821f 100644 --- a/ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js +++ b/ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js @@ -5229,6 +5229,10 @@ var JSHINT = (function() { var a = [], p; while (!state.tokens.next.reach && state.tokens.next.id !== "(end)") { + if (state.tokens.next.value === "switch") { + warning("E067", state.tokens.next, "switch"); + break; + } if (state.tokens.next.id === ";") { p = peek(); @@ -9215,7 +9219,7 @@ var JSHINT = (function() { statements(0); } - if (state.tokens.next.id !== "(end)") { + if (state.tokens.next.id !== "(end)"&& state.tokens.next.value !== "switch") { quit("E041", state.tokens.curr); } @@ -11266,7 +11270,8 @@ var errors = { E064: "Super call may only be used within class method bodies.", E065: "Functions defined outside of strict mode with non-simple parameter lists may not " + "enable strict mode.", - E066: "Asynchronous iteration is only available with for-of loops." + E066: "Asynchronous iteration is only available with for-of loops.", + E067: "Expected without the 'switch' statement. TBEL does not support the 'switch' statement." }; var warnings = { @@ -11364,7 +11369,6 @@ var warnings = { W086: "Expected a 'break' statement before '{a}'.", W087: "Forgotten 'debugger' statement?", W088: "Creating global 'for' variable. Should be 'for (var {a} ...'.", - // W288: "The syntax of function '{a}' is specific to TBEL, and is not supported by JS executor.", W089: "The body of a for in should be wrapped in an if statement to filter " + "unwanted properties from the prototype.", W090: "'{a}' is not a statement label.", From f534e2db6407ab949ffc013cd97abeb72cd75902 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Mon, 12 Jun 2023 13:05:10 +0300 Subject: [PATCH 2/4] fix_bug_switch: add validation to tbel mew msg --- ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js b/ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js index 13ec1d821f..3a4b3d90b8 100644 --- a/ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js +++ b/ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js @@ -11271,7 +11271,7 @@ var errors = { E065: "Functions defined outside of strict mode with non-simple parameter lists may not " + "enable strict mode.", E066: "Asynchronous iteration is only available with for-of loops.", - E067: "Expected without the 'switch' statement. TBEL does not support the 'switch' statement." + E067: "Expected an 'if/else' and instead saw 'switch'. TBEL does not support the 'switch' statement." }; var warnings = { From 7f0c5f219b5bf7e91a09acd724c3659c736d9f42 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Mon, 12 Jun 2023 19:00:59 +0300 Subject: [PATCH 3/4] fix_bug_switch: add CompileException --- .../script/api/tbel/DefaultTbelInvokeService.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java index 5497e6cbbc..0e56612c21 100644 --- a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java +++ b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java @@ -183,11 +183,13 @@ public class DefaultTbelInvokeService extends AbstractScriptInvokeService implem lock.unlock(); } return scriptId; - } catch (Exception e) { - if (((CompileException) e).getExpr() != null && new String(((CompileException) e).getExpr()).contains(tbelSwitch)) { - e = new CompileException(tbelSwitchErrorMsg, ((CompileException) e).getExpr(), ((CompileException) e).getCursor(), e.getCause()); + } catch (CompileException ce) { + if ( ce.getExpr() != null && new String(ce.getExpr()).contains(tbelSwitch)) { + ce = new CompileException(tbelSwitchErrorMsg, ce.getExpr(), ce.getCursor(), ce.getCause()); } - throw new TbScriptException(scriptId, TbScriptException.ErrorCode.COMPILATION, scriptBody, e); + throw new TbScriptException(scriptId, TbScriptException.ErrorCode.COMPILATION, scriptBody, ce); + } catch (Exception e) { + throw new RuntimeException(e); } }); } From 6d69c9b2138cdad0b767d63c6cce858957368e22 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Tue, 13 Jun 2023 11:39:48 +0300 Subject: [PATCH 4/4] fix_bug_switch: add CompileException2 --- .../thingsboard/script/api/tbel/DefaultTbelInvokeService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java index 0e56612c21..bbf441a659 100644 --- a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java +++ b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java @@ -189,7 +189,7 @@ public class DefaultTbelInvokeService extends AbstractScriptInvokeService implem } throw new TbScriptException(scriptId, TbScriptException.ErrorCode.COMPILATION, scriptBody, ce); } catch (Exception e) { - throw new RuntimeException(e); + throw new TbScriptException(scriptId, TbScriptException.ErrorCode.COMPILATION, scriptBody, e); } }); }