Closures in DWScript / OP4JS
Closures, also known as “anonymous methods” in Delphi, are now supported by DWScript for the JavaScript CodeGen, with the same syntax as in Delphi:
DelphiTools, SamplingProfiler and General Delphi News
Closures, also known as “anonymous methods” in Delphi, are now supported by DWScript for the JavaScript CodeGen, with the same syntax as in Delphi:
Great SOPA blackout page from Wikipedia, being one the top worldwide site, this should generate more awareness than any other!
I usually avoid posting non-technical stuff, let SOPA be the exception that confirms the rule! As for a description of the problems of SOPA, see:
Jon Lennart Aasenden just posted on G+ several links to new “How do I” entries in the OP4JS Documentation:
You may also want to read Primoz Gabrijelcic’s article “First Steps with Smart Mobile Studio“.
The DelphiTools.info website has just been relocated, it also transitioned from a Linux host to a Windows one.
There were a few glitches with WordPress during the transfer, but they should hopefully have been solved, if not, feel free to post about them here!
Just committed to the SynEdit SVN a few enhancements:
edit 27/12: committed another optimization, AFAICT, when working on large files, SynEdit is now faster than the Delphi IDE code editor and *way* faster than Scintilla/notepad++ 5.9.6.2 (with and without a syntax highlighter like DWS’s)
We’ve now sent “Smart Mobile Studio” Alpha version to 50 testers.
Did you miss the beta invite?
Visit www.SmartMobileStudio.com to participate.
SmartMobileStudio leverages DWScript’s JavaScript CodeGen.
My first test app with the alpha was a clock, check it in your iOS or Android Browser or in the Android market. Source is included in the alpha. Will be beautified it later on 🙂
I’ve been playing on another one, head over to YouTube to see a small video, you can also get the apk (47 kB), but beware it’s basic, ugly, and definitely early alpha, but it’s coded in Pascal!
Below is a snippet of the source code (using DWS inline implementations for illustration and compactness purposes, most of OP4JS is written in the more classic interface/implementation style), it’s a snip of the root class of the mini-engine of the game (yes, virtual methods are supported):
type
TEntity = class
X, Y : Float;
function Progress : Boolean; virtual;
begin
// does nothing by default
end;
constructor Create(aX, aY : Float);
begin
X := aX;
Y := aY;
end;
function Dist2(entity : TEntity) : Float;
begin
Result := Sqr(X-entity.X)+Sqr(Y-entity.Y);
end;
end;
A 64bit XorShift is now used to generate random numbers in DWScript, and there is a now a separate random number generator per-execution, which is auto-randomized when an execution is created.
Previously, the RTL random generator was used, this was “okay” when you had only one script using random numbers at a time, but multiple scripts running at the same time would interfere (Randomize calls would affect each others f.i.), and Random isn’t really thread-safe.
Performance fo XorShift is roughly comparable to the Delphi RTL’s linear congruential generator, but with much better statistical random properties and a very long period, without the overhead of a Mersenne Twister. For those interested in the mathematical details, see “XorShift RNGs” paper by G. Marsagalia.
As an illustration of the improved random properties, consider filling a bitmap with “random” RGB colors for each pixel:
var x, y : Integer;
for x := 0 to bmp.Width-1 do
for y := 0 to bmp.Height-1 do
bmp.Pixel[x, y] := RandomInt($1000000);
Using the Delphi built-in Random, you’ll get something like the image below (generated at 512×512, then halved and downgraded to 4bpp for web consumption)
Oooh… the horizontal scratch lines! Not so random after all… I don’t know if the Delphi LCG is as biased as RANDU, but visibly, it is probably not something you want to rely upon too much.
And now, the same but with the XorShift implementation now used in DWS:
The XorShift implementation is very simple, fast, and doesn’t require much memory: a single 64bit value is enough to get good random, use two if you want longer periods that won’t have a chance to loop before the universe ends.
Last but not least, 64bit XorShift may be fast in 32bit binaries, but it practically walks on water in 64bit binaries 😉
Here is a quick summary of recent additions to DWScript:
*: FWIW since the old benchmark, compile and execution performance almost tripled and memory requirements were cut by approx 30%. At the same time the language became quite a bit richer.