Using Lightweight Applications to Inspect the JavaScript Output

A lightweight Smart Mobile Project containing only the code of the .dpr file provides JavaScript output code that is easy to find in the small HTML file produced. This example shows how some very basic Smart Pascal code is transpiled to JavaScript. For this example we made selections using the Project Options dialogue to enable translation:
  • without code packing (Code Generation tab);
  • without code obfuscation (Code Generation tab);
  • without use of main body to see output free from $Application() (Code Generation tab);
  • JavaScript embedded into HTML file (Linker tab);
  • without a theme (Linker tab).

See below the Smart Pascal source code, the emitted JavaScript and a copy of the output to the HTML document. You can easily examine the output of language features such as RandomInt, Date and the use of records to see how Smart Mobile Studio deals with them.

Smart Pascal Code

uses W3C.HTML5;

procedure writeln(s: string);
begin
  Document.Write(s + '<br>');
end;

// Constants and Variables;
const MyConst = 42;
var MyChar: String;
var CurrentNum := 3;
var Maximum := 2;
var Rainfall: array [1 .. 12] of integer;
var FirstQuarter := ['January', 'February', 'March'];

// Assignment
MyChar := 'A';
Rainfall[1] := 73;
// Output
writeln('<b>Output to document</b>');
writeln('MyChar: ' + MyChar);
writeln('MyConst: ' + inttostr(MyConst));
writeln('Rainfall (mm) in ' +  FirstQuarter[0] + ': ' + inttostr(Rainfall[1]));

// Selection
writeln('<b>Selection</b>');
if (CurrentNum > Maximum) then
  begin
    Maximum := CurrentNum;
    writeln('Maximum increased');
  end
else
  writeln('Maximum unchanged');

case Maximum of
  2: writeln('Maximum still ' + intToStr(Maximum));
  3: writeln('Maximum now ' + intToStr(Maximum));
end;

// Iteration
writeln('<b>Iteration</b>');
for var i := 1 to 3 do
  writeln('For loop iteration ' + IntToStr(i));

var j := 1;
repeat
  writeln('Repeat loop iteration ' + IntToStr(j));
  inc(j);
until j = 4;

var k := 1;
while k < 4 do
  begin
    writeln('While loop iteration ' + IntToStr(k));
    inc(k);
  end;

// String manipulation
writeln('<b>String Manipulation</b>');
var intP := pos('gram', 'programming');  // intP becomes 4
var strL := leftStr('programming', 7);   // strL becomes 'program'
var strM := midStr('programming', 5, 3); // strM becomes 'ram'
var strR := rightStr('programming', 4);  // strR becomes 'ming'
var intL := length('programming');       // intL becomes 11

writeln('IntP: ' + intToStr(intP) + ', strL: ' + strL + ', strM: ' + strM +
        ', strR: ' + strR + ', IntL: ' + intToStr(intL));    

JavaScript Output

The bootstap function of the JavaScript script in the HTML document contains the following code.
function RightStr(s,n) { return s.substr(s.length-n) }
function writeln(s) {
   document.write(s+"<br>");
};
var MyChar = "";
var CurrentNum = 0,
   Maximum = 0,
   Rainfall = [0,0,0,0,0,0,0,0,0,0,0,0],
   FirstQuarter = ["","",""],
   i = 0;
var j = 0,
   k = 0,
   intP = 0,
   strL = "",
   strM = "",
   strR = "",
   intL = 0;
CurrentNum = 3;
Maximum = 2;
FirstQuarter = ["January", "February", "March"];
MyChar = "A";
Rainfall[(1)-1] = 73;
writeln("<b>Output to document<\/b>");
writeln("MyChar: "+MyChar);
writeln("MyConst: "+(42).toString());
writeln("Rainfall (mm) in "+FirstQuarter[0]+": "+(Rainfall[(1)-1]).toString());
writeln("<b>Selection<\/b>");
if (CurrentNum>Maximum) {
   Maximum = CurrentNum;
   writeln("Maximum increased");
} else {
   writeln("Maximum unchanged");
}
switch (Maximum) {
   case 2 :
      writeln("Maximum still "+Maximum.toString());
      break;
   case 3 :
      writeln("Maximum now "+Maximum.toString());
      break;
}
writeln("<b>Iteration<\/b>");
for(i=1;i<=3;i++) {
   writeln("For loop iteration "+i.toString());
}
j = 1;
do {
   writeln("Repeat loop iteration "+j.toString());
   ++j;
} while (!(j==4));
k = 1;
while (k<4) {
   writeln("While loop iteration "+k.toString());
   ++k;
}
writeln("<b>String Manipulation<\/b>");
intP = ("programming".indexOf("gram")+1);
strL = ("programming").substr(0,7);
strM = "programming".substr(4,3);
strR = RightStr("programming",4);
intL = "programming".length;
writeln("IntP: "+intP.toString()+", strL: "+strL+", strM: "+strM+", strR: "+strR+", IntL: "+intL.toString());

 }

Output to Document

A copy of the output to the HTML document follows.
Output to document
MyChar: A
MyConst: 42
Rainfall (mm) in January: 73
Selection
Maximum increased
Maximum now 3
Iteration
For loop iteration 1
For loop iteration 2
For loop iteration 3
Repeat loop iteration 1
Repeat loop iteration 2
Repeat loop iteration 3
While loop iteration 1
While loop iteration 2
While loop iteration 3
String Manipulation
IntP: 4, strL: program, strM: ram, strR: ming, IntL: 11
Programming - a skill for life!

Smart Pascal applications giving small HTML files