Skip to content

Kotlin Scripting support #379

Description

@aartiPl

All the issues connected with the implementation of the Kotlin Scripting engine in KScript.

Activity

  1. aartiPl commented on Sep 14, 2022

    @aartiPl
    CollaboratorAuthor

    @ligee, @ileasile - as I have mentioned in #Kotlin/KEEP, I have an initial implementation of support for Kotlin Scripting in kscript. Those files are mostly a copy of Kotlin Scripting files - I have divided the evaluation into 2 phases: compilation and execution because I want to get compilation messages first, and then the output of the application, not everything at the end.

    The problems which I have encountered:

    1. Compiler sees the incorrect path of the root file, and I don't know how to modify this path programmatically.
      Please look at KscriptHost.kt 'compile' function. I am setting the path of the file explicitly in line 57. But during compilation, the path of the root file is always resolved to the current directory.
      See the below screenshot for details:
      obraz

    (As you can see, I am starting the script from home dir; internally, kscript is resolving script dir correctly, but I don't know how to pass it to the compiler, and as a result, the path is resolved incorrectly)

    1. Is it necessary to provide jar cache through system properties?

    2. How to get all compiled file names and paths (needed for creating the IntelliJ project)?

    3. How to get all dependencies (needed for creating of IntelliJ project)?

    4. How to implement URL resolving with caching files locally?

    As there is lacking documentation, it is difficult to determine what's wrong with my implementation. Can you help with the above problems (the first one is very practical as I am currently on it; for others, it would be nice to get some hints, at least)?

  2. aartiPl commented on Sep 15, 2022

    @aartiPl
    CollaboratorAuthor

    I have renamed the branch to kscript_4.5 - it's too early to release Kotlin Scripting in the next version.
    https://github.com/kscripting/kscript/tree/kscript_4.5

  3. ligee commented on Sep 19, 2022

    @ligee

    @aartiPl, it is great to see your efforts to implement kscript on top of the scripting API.
    Let's have a look at your questions:

    1. if you're talking about scriptFileLocation - this variable is set by the compiler, and used inside the evaluator for script to be able to access its own path (e.g. by default via the __FILE__ variable).
      If you need to set the script's "virtual path" for diagnostics and maybe imports, you need to implement your own variant of the SourceCode rather than use toScriptSource function, or alternatively use FileScriptSource and pass a correct file to it along with the text.

    2. What do you mean by jar cache, and where do you provide it via the system properties?

    3. The scripts are by default do not compile into files, they are kept in memory. If you need them as class files, you need to use pseudo-evaluation into a cache. You can find an example of this functiunality around usages of this class - https://github.com/JetBrains/kotlin/blob/3e19e9d190f800da551650cdcbbdbb9f7c845f07/libraries/scripting/jvm-host-test/test/kotlin/script/experimental/jvmhost/test/CachingTest.kt#L342
      (BTW, quite likely you can use the same approach for generating the whole IntelliJ project.)

    4. All dependencies are stored in the dependencies property of the compilation configuration, and you can get the resulting configuration from the compiled script and it will include also the dependencies resolved from maven and other sources, if applicable.

    5. I guess you mean some variant of the @Import handling. In the main-kts, it is implemented here - https://github.com/JetBrains/kotlin/blob/ca2f37f6ebe9dd7349cc4cab1e1ab1ddfdb5976e/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/scriptDef.kt#L176
      and you can adapt this code for your case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions