New Recompile Feature in NICE Libraries

by Erick Engelke
September 26, 2024

NOTE: this post is superceded by my post 46 which shows a better solution!

The following is only applicable to clients of my Nice Library. Find out more at Try Nice Library.

For some time I’ve wanted to Elevate to add a few features which are present in several competing Pascal-to-JavaScript compilers.

Since they have not yet agreed to the additions, I’ve made a 'recompile' feature which can add such functionality at run-time. It sort of recompiles source code in place.

This is still in its infancy, I just finished the basic code before writing this and it’s a bit awkward. But it shows the potentional which I hope to generalize to handling more function.

The new commands it adds are:

  • nicedebugger() - which invokes the browser’s debugger at the current line of JavaScript

  • nicejavascript() which adds the following text as JavaScript. It is different than CreateObject() which calls Eval(), because that cannot access local variables, whereas mine can.

  • nicecomment() which adds comments to the resulting javascript.

Here is a sample function using these features, copied from my Nice samples program where you will find the source code.

function myfunction( s : string ):string;
begin
   // call Browser's debugger
   nicedebugger();

   // typical EWB line
   s := 'some random text';

   // insert a comment into javascript
   nicecomment('Next line is inserted assembler');

   // execute javascript
   nicejavascript('s = "totally new text, old text was: " + s;');

   // back to ewb
   showmessage( s );
   result := s;
end;

To see the EWB compiled JavaScript, either open the resulting .js file in your browser, or just call my NiceCompilerGetSourceCode( myfunction );

function niceformrecompile_myfunction_1(s)
{
   var $r;
   nicedebugger();
   s = "some random text";
   nicecomment("Next line is inserted assembler");
   nicejavascript("s = \"totally new text, old text was: \" + s;");
   webforms_showmessage(s);
   $r = s;
   return $r;
}

Notice that EWB treats these are normal functions, so far so good.

Then NiceCompilerRecompile( myfunction ) recompiles the function, resulting in the following valid JavaScript.

function niceformrecompile_myfunction_1(s)
{
   var $r;
debugger;
   s = "some random text";
// Next line is inserted assembler
s = "totally new text, old text was: " + s;
   webforms_showmessage(s);
   $r = s;
   return $r;
}

As the Nice samples program shows, executing this in a capable browser (like Chrome) will debug the program, and the result is what would be expected.

The samples program also shows a Method being recompiled, it’s the second line of examples under Recompile.

There are some shortcomings in this early development release - mostly requiring some weird casting because of challenges with the EWB compiler in its current release with respect to variant usage.

If Elevate implements something similar, I will obviously gladly retire this feature.

Erick