B4J Question B4JS, ABMaterial, BANano, and alternatives

RobertK

Member
Licensed User
Longtime User
Hello @Erel and all the group. It's good to be back in the Forum after an extended absence.

I am currently looking for a simple lightweight solution to convert B4J Subs to JavaScript functions, without the need for integrated web development or UI design tools. Consider the following trivial example and please excuse any syntax errors.

B4X:
Sub Pythagoras(a As Double, b As Double) As Double
    Dim c As Double
    c = Sqrt((a*a) + (b*b))
    Return c   
End Sub

JavaScript:
Function Pythagoras(a, b) {
let c = Math.sqrt((a*a)+(b*b));
return c;
}

There is a quite a bit of information, both on and off the Forum, about B4JS, ABMaterial, and BANano, dating from approximately 2018 to 2021. Among the sources I have consulted are:


I see that @alwaysbusy and others have invested considerable effort in this project and, at first glance, it seems like a comprehensive solution capable of much more than I need at this stage. I am certain it includes the capability to do what I am looking for but, unfortunately, I have not yet been able to locate it. Perhaps someone familiar with the library could help guide me to the right place in the documentation or provide a quick walk-through.

Another approach might be to try to filter and modify the contents of the .java file generated by the B4J compiler. In the case of the above example, it looks like:

Java:
public static double  _pythagoras(double _a,double _b) throws Exception{
double _c = 0;
 //BA.debugLineNum = 23;BA.debugLine="Sub Pythagoras(a As Double, b As Double) As Double";
 //BA.debugLineNum = 25;BA.debugLine="Dim c As Double";
_c = 0;
 //BA.debugLineNum = 27;BA.debugLine="c = Sqrt((a*a) + (b*b))";
_c = anywheresoftware.b4a.keywords.Common.Sqrt((_a*_a)+(_b*_b));
 //BA.debugLineNum = 29;BA.debugLine="Return c";
if (true) return _c;
 //BA.debugLineNum = 31;BA.debugLine="End Sub";
return 0;
}

Is there any documentation for this file? And even if there is, this approach feels too much like reverse-engineering instead of a simple solution to a simple problem.

Have I overlooked something? Any advice would be greatly appreciated. Many Thanks!
 

MichalK73

Well-Known Member
Licensed User
Longtime User
Try using an AI like Copilot. Copilot understands the structure of the Basic language well and converts to JavaScript well.
1770791262391.png
 
Upvote 0

RobertK

Member
Licensed User
Longtime User
Thanks for the helpful suggestion. I would like to give it a try. Which Copilot are you referring to? There are many of them. Is it this one?


Or do you have something else in mind? Please provide more details and a link.
 
Upvote 0

aminoacid

Well-Known Member
Licensed User
Longtime User
Thanks for the helpful suggestion. I would like to give it a try. Which Copilot are you referring to? There are many of them. Is it this one?


Or do you have something else in mind? Please provide more details and a link.

Actually ,I've done it successfully with ChatGPT also. Just tell it what you want to do in plain english and paste the B4J sub. See below:

1770925087077.png
 
Upvote 0
Top