Initial commit to show approach for arrays native#1050
Initial commit to show approach for arrays native#1050Ihromant wants to merge 3 commits intokonsoletyper:masterfrom
Conversation
|
The correct property is: |
| import org.teavm.backend.javascript.spi.InjectorContext; | ||
| import org.teavm.model.MethodReference; | ||
|
|
||
| public class JSArraysGenerator implements Injector { |
There was a problem hiding this comment.
I don't think that using generators here is an optimal approach. This may be trivial for sort and fill, but sort with comparison function and copyOfRange would be more problematic.
There was a problem hiding this comment.
Could you please point to correct approach example then?
| } | ||
| } | ||
|
|
||
| @JSBody(params = "arr", script="arr.sort();") |
There was a problem hiding this comment.
Yes. And also you don't need all these sortIntJS, sortLongJS, etc. It can be just sortJS with parameter typed overloaded
There was a problem hiding this comment.
Then there is a problem.
@JSBody(params = {"arr", "cmp"}, script="arr.sort(cmp);")
private static native void sortJS(@JSByRef Object/*[]*/ arr, TComparator<Object> cmp);can't be compiled because
ERROR: Method java.util.Arrays.sortJS(Ljava/lang/Object;Ljava/util/Comparator;)V is not a proper native JavaScript method declaration: its 1th parameter is declared as JSByRef, which has incompatible type
There was a problem hiding this comment.
Then for this case the only possible way is to generate the method. You can't just generate, you should also properly track dependencies.
There was a problem hiding this comment.
I tried to understand how pass comparator to native method. Closest example that I found was SystemNativeGenerator, but it doesn't handle functions. When I try to get JS function from wrapper accessing it via $compare1 property I get ClassCastException. Obviously I'm doing it wrong, but what is the correct way?
There was a problem hiding this comment.
$compare1 property - where did you get this name? Did you try to dig deeper into TeaVM code?
There was a problem hiding this comment.
$compare1property - where did you get this name?
I get this variable name when debugged in JS where compare function is hidden. Obviously it is not correct (even if it helped with normal code, I suppose it would be broken in minified code).
Did you try to dig deeper into TeaVM code?
I tried to find some example in existing generators/injectors but didn't manage to find any example where function is transformed from Java to JS. Documentation suggests to work with @JSFunctor when working with @JSBody, but it didn't help either. So this is my level of digging.
There was a problem hiding this comment.
You are using org.teavm.backend.javascript.templating.JavaScriptTemplateFactory. Did you try to look into it? Did you find references to org.teavm.backend.javascript.templating.TemplatingAstWriter from there? Did you try to understand the code and see how other generators using templates?
There was a problem hiding this comment.
TemplatingAstWriter is used inside JavaScriptTemplate. Now after you pointed on it specifically I can see it.
I didn't look exactly at that class, but will take a look. Still, the closest example that I've seen is JSNativeGenerator, but it does the opposite from what I understand: allows to call JS functions from Java. If some reverse example was present, it would be great.
@konsoletyper this is initial approach for arrays native. Here is only one method in order to review early and not fix 20 similar lines later. Please review whether it is correct because It could be wrong after recent changes in JS interop and instanceof.
One more question:
I added line teavm.junit.wasi=false to gradle.properties, but it didn't block wasi tests from execution. Were there some changes and how run tests for specific platrofm using properties?