Skip to content

Commit

Permalink
[jsscripting] Library and Doc updates (openhab#11800)
Browse files Browse the repository at this point in the history
* [jsscripting] Library and Doc updates

This updates the documentation, bumps the NPM version library, and implements conversion of JS date types (js-joda) to Java types

Signed-off-by: Dan Cunningham <[email protected]>
Signed-off-by: Nick Waterton <[email protected]>
  • Loading branch information
digitaldan authored and NickWaterton committed Dec 30, 2021
1 parent cd50330 commit b32d740
Show file tree
Hide file tree
Showing 7 changed files with 512 additions and 131 deletions.
618 changes: 489 additions & 129 deletions bundles/org.openhab.automation.jsscripting/README.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion bundles/org.openhab.automation.jsscripting/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<graal.version>21.3.0</graal.version>
<asm.version>6.2.1</asm.version>
<oh.version>${project.version}</oh.version>
<ohjs.version>[email protected].0</ohjs.version>
<ohjs.version>[email protected].1</ohjs.version>
</properties>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
Expand All @@ -38,6 +40,8 @@
import org.eclipse.jdt.annotation.Nullable;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.Value;
import org.openhab.automation.jsscripting.internal.fs.DelegatingFileSystem;
import org.openhab.automation.jsscripting.internal.fs.PrefixedSeekableByteChannel;
import org.openhab.automation.jsscripting.internal.fs.ReadOnlySeekableByteArrayChannel;
Expand Down Expand Up @@ -77,10 +81,27 @@ public class OpenhabGraalJSScriptEngine extends InvocationInterceptingScriptEngi
public OpenhabGraalJSScriptEngine(@Nullable String injectionCode) {
super(null); // delegate depends on fields not yet initialised, so we cannot set it immediately
this.globalScript = GLOBAL_REQUIRE + (injectionCode != null ? injectionCode : "");

// Custom translate JS Objects - > Java Objects
HostAccess hostAccess = HostAccess.newBuilder(HostAccess.ALL)
// Translate JS-Joda ZonedDateTime to java.time.ZonedDateTime
.targetTypeMapping(Value.class, ZonedDateTime.class, (v) -> v.hasMember("withFixedOffsetZone"), v -> {
return ZonedDateTime
.parse(v.invokeMember("withFixedOffsetZone").invokeMember("toString").asString());
}, HostAccess.TargetMappingPrecedence.LOW)

// Translate JS-Joda Duration to java.time.Duration
.targetTypeMapping(Value.class, Duration.class,
// picking two members to check as Duration has many common function names
(v) -> v.hasMember("minusDuration") && v.hasMember("toNanos"), v -> {
return Duration.ofNanos(v.invokeMember("toNanos").asLong());
}, HostAccess.TargetMappingPrecedence.LOW)
.build();

delegate = GraalJSScriptEngine.create(
Engine.newBuilder().allowExperimentalOptions(true).option("engine.WarnInterpreterOnly", "false")
.build(),
Context.newBuilder("js").allowExperimentalOptions(true).allowAllAccess(true)
Context.newBuilder("js").allowExperimentalOptions(true).allowAllAccess(true).allowHostAccess(hostAccess)
.option("js.commonjs-require-cwd", JSDependencyTracker.LIB_PATH)
.option("js.nashorn-compat", "true") // to ease migration
.option("js.ecmascript-version", "2021") // nashorn compat will enforce es5 compatibility, we
Expand Down

0 comments on commit b32d740

Please sign in to comment.