<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>Hi Ethan,<br>
to show the contents of a method body, it is recommended to use
the Op::toText method. This converts the op in a representation
that can be more easily understood by us humans.</p>
<p>I suggest you look there, and see if that helps understanding
better how things are connected (it sure helped me a lot).</p>
<p>Cheers<br>
Maurizio<br>
</p>
<p><br>
</p>
<div class="moz-cite-prefix">On 31/01/2024 15:43, Ethan McCue wrote:<br>
</div>
<blockquote type="cite" cite="mid:CA+NR86jo_UV45Su4HELLXW9xYu4P5+MTxN5eoT38jZ505tQkBw@mail.gmail.com">
<div dir="ltr">Hi all,<br>
<br>
I understand this is extremely early, but I started trying to
experiment with the prototype API yesterday. I feel like there
are some core concepts that I am not understanding.<br>
<br>
I was trying to make a basic Java -> JS compiler. The idea is
that if you stick to a set of special classes and initialize
them with constants, there would be a mechanical translation to
equivalent JS.<br>
<br>
<font face="monospace"> final class OnlyAvailableInBrowser
extends RuntimeException {<br>
}</font><br>
<br>
<font face="monospace"> public final class JsConsole {<br>
private JsConsole() {}<br>
<br>
public static void log(JsAny o) {<br>
throw new </font><span style="font-family:monospace">OnlyAvailableInBrowser();</span><font face="monospace"><br>
}<br>
}<br>
<br>
public sealed abstract class JsAny permits JsString {<br>
}<br>
<br>
public final class JsString extends JsAny {<br>
private JsString() {}<br>
<br>
public static JsString of(String s) {<br>
throw new </font><span style="font-family:monospace">OnlyAvailableInBrowser();</span><br>
<font face="monospace"> }</font><br>
<font face="monospace"> }</font><br>
<br>
<font face="arial, sans-serif">So if there is a method like this<br>
<br>
</font><font face="monospace"> @CodeReflection<br>
public static void f() {<br>
var s = JsString.of("abc");<br>
JsConsole.log(s);<br>
}<br>
</font><font face="arial, sans-serif"><br>
Then I want to get at the fact that the string constant "abc"
is passed to JsString#of, that the result of that gets
assigned to a variable, and that that variable gets passed to
JsConsole#log<br>
<br>
Looking at the model and the body of the model doesn't give me
many clues<br>
<br>
</font><font face="monospace"> public static void
main(String[] args) throws NoSuchMethodException {<br>
var model = Main.class.getMethod("f")<br>
.getCodeModel()<br>
.orElseThrow();<br>
<br>
System.out.println(model);<br>
System.out.println(model.body());</font><font face="arial, sans-serif"><br>
</font>
<div><font face="monospace"><br>
</font></div>
<div><font face="monospace">java.lang.reflect.code.op.CoreOps$FuncOp@63440df3<br>
java.lang.reflect.code.Body@6121c9d6</font><br>
</div>
<div><font face="monospace"><br>
</font></div>
<div><font face="arial, sans-serif">Looking at the operations in
the body gets me closer<br>
<br>
</font> <font face="monospace">model.body().entryBlock().ops().forEach(System.out::println);</font><br>
<br>
<font face="monospace">java.lang.reflect.code.op.CoreOps$ConstantOp@1060b431<br>
java.lang.reflect.code.op.CoreOps$InvokeOp@612679d6<br>
java.lang.reflect.code.op.CoreOps$VarOp@11758f2a<br>
java.lang.reflect.code.op.CoreOps$VarAccessOp$VarLoadOp@e720b71<br>
java.lang.reflect.code.op.CoreOps$InvokeOp@1b26f7b2<br>
java.lang.reflect.code.op.CoreOps$ReturnOp@491cc5c9</font><br>
</div>
<div><font face="monospace"><br>
</font></div>
<div><font face="arial, sans-serif">But it's unclear, to me, how
to tie the invoke op to the constant op. The only unique
method on InvokeOp is invokeDescriptor which doesn't seem
right.</font><br>
<br>
<font face="monospace">.operands() </font><font face="arial, sans-serif">seems appropriate, but that has a
list of </font>[java.lang.reflect.code.Op$Result@6a1aab78].
Looking at that class I see an op method<br>
<br>
<font face="monospace"> var invokeOp =
(CoreOps.InvokeOp) model<br>
.body()<br>
.entryBlock()<br>
.ops()<br>
.get(1);<br>
<br>
System.out.println(((Op.Result)
invokeOp.operands().get(0)).op());<br>
</font><br>
Which does get me a constant op, but I don't quite understand
how to relate that constant op to the op in the list of ops.
Or, put another way, I don't understand how to traverse the
model properly.<br>
<br>
</div>
</div>
</blockquote>
</body>
</html>