I want to provide the methods that ScriptEngine pr...
# getting-started
d
I want to provide the methods that ScriptEngine provides as well as the methods that Invocable provides by delegating to either of those two objects.
d
deinspanjer: Don't you just need "implementation by delegation", i.e.
object JsEngine : ScriptEngine by engine
?
d
Well, I need both the ScriptEngine interface and the Invokable one
In that snippit, where does the engine object get constructed?
I have this code working (I think), but the engine and js objects have to be imported even though they are declared in this object, and that means they have to be public which I’d rather not do.
Copy code
import com.jsonlogic.JsEngine.engine
import com.jsonlogic.JsEngine.js
import jdk.nashorn.api.scripting.NashornScriptEngineFactory
import javax.script.Invocable
import javax.script.ScriptEngine

object JsEngine : ScriptEngine by engine, Invocable by js {
    val engine: ScriptEngine = NashornScriptEngineFactory().getScriptEngine("-strict=true", "--optimistic-types=true", "--language=es6",
    "-timezone=UTC")
    val js: Invocable = engine as Invocable
}
d
Yeah you probably want to have them in a separate object. The ideal way to do this depends, why is JsEngine even a singleton?
d
Cause I don’t want more than one copy of it floating around. It is a global entrypoint to the scripting engine
d
Ok, so... put "js" and "engine" as top level properties (can be private or at least internal) in the same file as the JsEngine object
That should do it then
d
Right. just tried that and the IDE isn’t complaining. 🙂 Thanks!
Is there a difference between private and internal here?
d
private would mean just the file has access, internal means the whole module
d
Oh! I was backward. I thought internal meant just the file. Sheesh
Thank you!