Skip to content

Initial commit to show approach for arrays native#1050

Open
Ihromant wants to merge 3 commits intokonsoletyper:masterfrom
Ihromant:arrays-js-native
Open

Initial commit to show approach for arrays native#1050
Ihromant wants to merge 3 commits intokonsoletyper:masterfrom
Ihromant:arrays-js-native

Conversation

@Ihromant
Copy link
Contributor

@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?

@konsoletyper
Copy link
Owner

The correct property is:

teavm.tests.wasi=false

import org.teavm.backend.javascript.spi.InjectorContext;
import org.teavm.model.MethodReference;

public class JSArraysGenerator implements Injector {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please point to correct approach example then?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use JSO

}
}

@JSBody(params = "arr", script="arr.sort();")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@konsoletyper Like that?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. And also you don't need all these sortIntJS, sortLongJS, etc. It can be just sortJS with parameter typed overloaded

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then for this case the only possible way is to generate the method. You can't just generate, you should also properly track dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$compare1 property - where did you get this name? Did you try to dig deeper into TeaVM code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$compare1 property - 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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants