Is it possible to evaluate a non `const` property ...
# arrow-meta
a
Is it possible to evaluate a non
const
property of a companion object at compile time?
r
you mean with type refinements? It should be possible at the moment to evaluate anything that comes from the standard lib or it’s in the compiler classpath depending on how it’s instantiated
a
Something like this:
The compiler plugin needs to access the value of the
constraints
property during code generation. Where should I look?
r
it’s done differently in meta to avoid refering to properties or outher members
πŸ‘ 1
The signature of validate includes the <receiver>.() -> ensuring this function is pure so it can validate any String at compile time but also be used at runtime as validation api that coherces to null, validated or either
πŸ‘Œ 1
Does Constrains come from your own plugin or is this a third party plugin?
a
It is a part of my library, but a different module/artifact than the plugin. Though that might change in the future.
πŸ‘ 1
@raulraja So I've been trying for the past two days, but I can't seem to get
eval
to work: When I try this:
Copy code
val script = "println(\"X\")"
try {
    eval(script)
} catch (e: ScriptException) {
    messageCollector?.report(
        CompilerMessageSeverity.ERROR,
        "$e",
         null
    )
}
It always fails:
Copy code
javax.script.ScriptException: error: cannot access script base class 'org.jetbrains.kotlin.script.jsr223.KotlinStandardJsr223ScriptTemplate'. Check your module classpath for missing or conflicting dependencies
I tried adding different dependencies to the plugin, but none seem to work:
Copy code
implementation(kotlin("scripting-jsr223"))
implementation(kotlin("script-util"))
implementation(kotlin("script-runtime"))
r
I believe there is also a manifest file for the service you need to tell it which script engine factory you are using local or daemon based
πŸ‘ 1
Alternatively you can instantiate then but afaik the deps look good
KotlinJsr223JvmLocalScriptEngineFactory().scriptEngine
πŸ‘ 1
Something like that I believe, sorry on my phone or I'd provide a link or example more elaborate
✌️ 1
βœ”οΈ 1
If you have a small reproducible project I can take a look later today
a
@raulraja The manifest file was missing, but adding it didn't solve the problem. Instantiating the engine myself yielded the same result as well. I've prepared a small project, you can find it here: https://github.com/AhmedMourad0/compiler_plugin_eval_srp Thank you for spending your time to help me with this, I really appreciate it!
r
Hi @Ahmed Mourad, trying the project now. is this what you get?
Copy code
e: javax.script.ScriptException: error: cannot access script base class 'org.jetbrains.kotlin.script.jsr223.KotlinStandardJsr223ScriptTemplate'. Check your module classpath for missing or conflicting dependencies
βœ”οΈ 1
I’m guessing you are missing the script util. If I add this I get another error:
Copy code
implementation("org.jetbrains.kotlin:kotlin-script-util:$kotlinVersion") {
        exclude("org.jetbrains.kotlin", "kotlin-stdlib")
        exclude("org.jetbrains.kotlin", "kotlin-compiler")
        exclude("org.jetbrains.kotlin", "kotlin-compiler-embeddable")
    }
At this point I think @Rachel can help us better to get that project configured as a plugin that would depend on meta and that it has the dependency to actually evaluate with the JSR Factory like refined types have in meta.
πŸ™Œ 1
✌️ 1
βœ”οΈ 1
a
The above error is what I was getting, however, adding the script utils dependency doesn't change it for me.
r
Addind the dep explicitly as
kotlinCompilerClass
shows this regardless of exclusion:
Copy code
Could not find or load main class org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
I recall we did not bundle the script engine in the shadow if I’m not mistaken because the IDEA plugin requires a different factory
a
After adding the dep as
kotlinCompilerClass
I get the:
Copy code
Could not find or load main class org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
error regardless of whether I call
eval
or not.
r
Hi @Ahmed Mourad πŸ™Œ When reviewing your case I realized that the shadow plugin was including undesired content in the jar. The new compiler plugin should be Arrow Meta Compiler Plugin + new meta plugin. I've just updated the hello world example to follow that premise.
πŸ™Œ 1
a
@Rachel Hey! I removed the shadow jar following the samples, it shows this error now:
Copy code
java.lang.NoClassDefFoundError: org/jetbrains/kotlin/script/jsr223/KotlinJsr223JvmLocalScriptEngineFactory
I tried adding these deps:
Copy code
implementation(kotlin("scripting-jsr223"))
implementation(kotlin("script-runtime"))
implementation("org.jetbrains.kotlin:kotlin-script-util:$kotlinVersion") {
    exclude("org.jetbrains.kotlin", "kotlin-stdlib")
    exclude("org.jetbrains.kotlin", "kotlin-compiler")
    exclude("org.jetbrains.kotlin", "kotlin-compiler-embeddable")
}
But it made no difference. These are the changes from the previous version: https://github.com/AhmedMourad0/compiler_plugin_eval_srp/commit/dba18c44e0771c011ec2b40ece2d2df546b596bd
r
Great @Ahmed Mourad, the error makes sense now
implementation
adds the dependencies in
compileClasspath
instead of
kotlinCompilerClasspath
Please, add these dependencies:
Copy code
kotlinCompilerClasspath("org.jetbrains.kotlin:kotlin-script-util:1.3.61") {
        exclude("org.jetbrains.kotlin", "kotlin-stdlib")
        exclude("org.jetbrains.kotlin", "kotlin-compiler")
        exclude("org.jetbrains.kotlin", "kotlin-compiler-embeddable")
    }
    kotlinCompilerClasspath("org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.61")
    kotlinCompilerClasspath("org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.3.61")
It doesn't raise an error for me now
Please, let us know if it also works for you
Thanks!!
a
@Rachel Unfortunately, that didn't fix it: https://github.com/AhmedMourad0/compiler_plugin_eval_srp/commit/b3a1ede95ffb7440a745d5b7d226d92f6ff92e4c It still produces the same error.
r
Sorry I didn't specify the place
Please, add them in the
sample
module instead of
plugin
module
Did it work now @Ahmed Mourad?
a
@Rachel Yup, the error is now gone! I don't think the analysis handler works now though, shouldn't this show me an error with "X" as the message? https://github.com/AhmedMourad0/compiler_plugin_eval_srp/blob/a1361e9b1fa0fe06c7e9d50ade9d181cd5dda3f7/plugin/src/main/kotlin/dev/ahmedmourad/eval/EvalExtensions.kt
πŸ‘ 1
πŸŽ‰ 1
No errors are shown at all so I believe the
analysisCompleted
block is never executed.
r
If you place something like:
Copy code
messageCollector?.report(CompilerMessageSeverity.WARNING, eval("2 + 3").toString(), null)
βœ”οΈ 1
in the
try
section, you'll see something like:
Copy code
> Task :sample:compileKotlin
w: 5
βœ”οΈ 1
So the
analysisCompleted
block is executed during compilation
βœ”οΈ 1
a
@Rachel Yup, you're right! Thanks a lot!
πŸ™Œ 2