[jsscripting] Fix failure on some platforms & JDKs (#13714)

* [jsscripting] Downgrade GraalVM to fix issue with armv7l & OpenJDK 11.0.16

The community reported several cases where JS Scripting was not working due to some issue with the injection of the global script.
This issue seems to only occur on armv7l (e.g. Raspberry Pi 32bit) and OpenJDK 11.0.16.
Investigation showed that the occurrence of the problem depends on the GraalJS version.

See https://community.openhab.org/t/js-scripting-all-scripts-stop-working-when-upgrading-to-3-4-0-m4/140837.

* [jsscripting] Add logging for injection of JSRuntimeFeatures
* [jsscripting] Lint `@jsscripting-globals.js` with semistandard
* [jsscripting] Remove ICU4J as it moved to `org.graalvm.truffle`

Reference f5661d4655/CHANGELOG.md (version-2200).

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
Florian Hotze 2022-11-14 20:30:44 +01:00 committed by GitHub
parent 40723a80a0
commit 11d3c641e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 177 additions and 178 deletions

View File

@ -22,7 +22,7 @@
!jdk.internal.reflect.*, !jdk.internal.reflect.*,
!jdk.vm.ci.services !jdk.vm.ci.services
</bnd.importpackage> </bnd.importpackage>
<graal.version>22.3.0</graal.version> <graal.version>22.0.0.2</graal.version> <!-- DO NOT UPGRADE: 22.0.0.2 is the latest version working on armv7l / OpenJDK 11.0.16 -->
<asm.version>6.2.1</asm.version> <asm.version>6.2.1</asm.version>
<oh.version>${project.version}</oh.version> <oh.version>${project.version}</oh.version>
<ohjs.version>openhab@2.1.0</ohjs.version> <ohjs.version>openhab@2.1.0</ohjs.version>
@ -135,11 +135,7 @@
<artifactId>js</artifactId> <artifactId>js</artifactId>
<version>${graal.version}</version> <version>${graal.version}</version>
</dependency> </dependency>
<dependency> <!-- com.ibm.icu.icu4j/69.1 is not required on GraalJS >= 22.0.0 as it moved to org.graalvm.truffle -->
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>69.1</version>
</dependency>
<!-- include as version required is older than OH provides --> <!-- include as version required is older than OH provides -->
<dependency> <dependency>

View File

@ -210,7 +210,10 @@ public class OpenhabGraalJSScriptEngine
delegate.getBindings(ScriptContext.ENGINE_SCOPE).put(REQUIRE_WRAPPER_NAME, wrapRequireFn); delegate.getBindings(ScriptContext.ENGINE_SCOPE).put(REQUIRE_WRAPPER_NAME, wrapRequireFn);
// Injections into the JS runtime // Injections into the JS runtime
delegate.put("require", wrapRequireFn.apply((Function<Object[], Object>) delegate.get("require"))); delegate.put("require", wrapRequireFn.apply((Function<Object[], Object>) delegate.get("require")));
jsRuntimeFeatures.getFeatures().forEach((key, obj) -> delegate.put(key, obj)); jsRuntimeFeatures.getFeatures().forEach((key, obj) -> {
LOGGER.debug("Injecting {} into the JS runtime...", key);
delegate.put(key, obj);
});
initialized = true; initialized = true;

View File

@ -4,30 +4,30 @@
'use strict'; 'use strict';
// Append the script file name OR rule UID depending on which is available // Append the script file name OR rule UID depending on which is available
const defaultIdentifier = "org.openhab.automation.script" + (globalThis["javax.script.filename"] ? ".file." + globalThis["javax.script.filename"].replace(/^.*[\\\/]/, '') : globalThis["ruleUID"] ? ".ui." + globalThis["ruleUID"] : ""); const defaultIdentifier = 'org.openhab.automation.script' + (globalThis['javax.script.filename'] ? '.file.' + globalThis['javax.script.filename'].replace(/^.*[\\\/]/, '') : globalThis.ruleUID ? '.ui.' + globalThis.ruleUID : '');
const System = Java.type('java.lang.System'); const System = Java.type('java.lang.System');
const formatRegExp = /%[sdj%]/g; const formatRegExp = /%[sdj%]/g;
// Pass the defaultIdentifier to ThreadsafeTimers to enable naming of scheduled jobs // Pass the defaultIdentifier to ThreadsafeTimers to enable naming of scheduled jobs
ThreadsafeTimers.setIdentifier(defaultIdentifier); ThreadsafeTimers.setIdentifier(defaultIdentifier);
function createLogger(name = defaultIdentifier) { function createLogger (name = defaultIdentifier) {
return Java.type("org.slf4j.LoggerFactory").getLogger(name); return Java.type('org.slf4j.LoggerFactory').getLogger(name);
} }
// User configurable // User configurable
let log = createLogger(); let log = createLogger();
function stringify(value) { function stringify (value) {
try { try {
if (Java.isJavaObject(value)) { if (Java.isJavaObject(value)) {
return value.toString(); return value.toString();
} else { } else {
// special cases // special cases
if (value === undefined) { if (value === undefined) {
return "undefined" return 'undefined';
} }
if (typeof value === 'function') { if (typeof value === 'function') {
return "[Function]" return '[Function]';
} }
if (value instanceof RegExp) { if (value instanceof RegExp) {
return value.toString(); return value.toString();
@ -40,10 +40,10 @@
} }
} }
function format(f) { function format (f) {
if (typeof f !== 'string') { if (typeof f !== 'string') {
var objects = []; const objects = [];
for (var index = 0; index < arguments.length; index++) { for (let index = 0; index < arguments.length; index++) {
objects.push(stringify(arguments[index])); objects.push(stringify(arguments[index]));
} }
return objects.join(' '); return objects.join(' ');
@ -51,10 +51,10 @@
if (arguments.length === 1) return f; if (arguments.length === 1) return f;
var i = 1; let i = 1;
var args = arguments; const args = arguments;
var len = args.length; const len = args.length;
var str = String(f).replace(formatRegExp, function (x) { let str = String(f).replace(formatRegExp, function (x) {
if (x === '%%') return '%'; if (x === '%%') return '%';
if (i >= len) return x; if (i >= len) return x;
switch (x) { switch (x) {
@ -71,7 +71,7 @@
return x; return x;
} }
}); });
for (var x = args[i]; i < len; x = args[++i]) { for (let x = args[i]; i < len; x = args[++i]) {
if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) { if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) {
str += ' ' + x; str += ' ' + x;
} else { } else {
@ -87,7 +87,7 @@
// Polyfills for common NodeJS functions // Polyfills for common NodeJS functions
const console = { const console = {
'assert': function (expression, message) { assert: function (expression, message) {
if (!expression) { if (!expression) {
log.error(message); log.error(message);
} }
@ -165,13 +165,13 @@
// Allow user customizable logging names // Allow user customizable logging names
// Be aware that a log4j2 required a logger defined for the logger name, otherwise messages won't be logged! // Be aware that a log4j2 required a logger defined for the logger name, otherwise messages won't be logged!
set loggerName(name) { set loggerName (name) {
log = createLogger(name); log = createLogger(name);
this._loggerName = name; this._loggerName = name;
ThreadsafeTimers.setIdentifier(name); ThreadsafeTimers.setIdentifier(name);
}, },
get loggerName() { get loggerName () {
return this._loggerName || defaultIdentifier; return this._loggerName || defaultIdentifier;
} }
}; };