A new version 2014.10.15 of the DWS Web Server is available.
This version contains a variety of improvements and fixes, along with a new sample for JS libraries.
Here is the download link for the installer:
DWSWebServer 2014.10.15 (1514 kB)
And here is the list of changes:
- added support for .p2js libraries and marking functions with “export”
- fixed a couple of problematic samples
- updated icon
Files with a “.p2js” extensions are considered to be Pascal libraries that have to be compiled to JavaScript libraries, so for instance “IncrementCounter.p2js”
library IncrementCounter; var doc external "document", win external "window" : Variant; var counter : Integer; procedure IncrementCounter; export; // Export this function in global JS context begin var countDiv := doc.getElementById('counter'); countDiv.innerHTML := IntToStr(counter); Inc(counter); win.setTimeout(@IncrementCounter, 1000); end;
will be compiled to
(function(){ function IncrementCounter() { var countDiv; countDiv = document.getElementById("counter"); countDiv.innerHTML = counter.toString(); ++counter; window.setTimeout(IncrementCounter,1000); }; window.IncrementCounter = IncrementCounter; var counter = 0; })();
By default the global JS context is assumed to be the “window” object, the “export” makes the function visible outside of the library’s private scope.
The resulting library can be used directly in an HTML page
<html><body> <h3 style="color:blue">Hello IncrementCounter!</h3> <div id="counter">???</div> <script src="js/IncrementCounter.p2js"></script> <script> IncrementCounter(); // we call it here! </script> </body></html>
Note that you can also “manually” export by assigning to (a field of) an external variable, the equivalent of the previous export clause would be some code like
var win external "window" : Variant; ... win.IncrementCounter := @IncrementCounter;
that you would place in an initialization section or setup function.
One thing to keep in mind is that exported functions have a well-defined name (here it’s “IncrementCounter”), while Codegen’ed functions have a contextual name, which can change if you have obfuscation enabled (in Smart MS) or simply have multiple IncrementCounter functions in different units, the Codegen’d would then name them like “IncrementCounter$1”, “IncrementCounter$2” or with some short pseudo-random alpha-numeric gibberish in the case of obfuscation.