Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2019-10

another optimization idea

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

warleyalex

I think it is amazing to see new optimizations/fetures introduced to pas2js.

a) for instance, a feature IF-THEN-ELSE - a pascal version of the C ternary
operator ( ? : )
-[ CODE ]--------------------------
var
  msg: String;
  n: Integer;
begin
  msg := 'There are ' + IntToStr(n) + ' user' + if (n>1) then 's' else '';
end;
>>
msg = "There are "+n.toString()+" user"+((n>1)?"s":"");
-------------------------------------
b) Result statement optimization
-[ CODE ]--------------------------
function G: String;
begin
  Result := 'abc';
end;
>>
  this.G = function () {
    var Result = "";
    Result = "abc";
    return Result;
  };
-------------------------------------

c) straightforward object

-[ CODE ]--------------------------
procedure Go;
begin
  console.log('GO!');
end;
var
  obj: TJSObject;
begin
  obj := New(['MyName','Test1','Price',1, 'Func', @Go]);
end;
it generates >>>
  pas.JS.New(["MyName","Test1","Price",1,"Func",$mod.Go]);

but it would be interesting if the output were straightforward like this:

$mod.obj = {"MyName":"Test1","Price":1,"Func":$mod.Go};
-------------------------------------

c) record initialization

-[ CODE ]--------------------------
type
  TShopItem = record
  MyName : string;
  Price : Double;
  Func: TProcedure;
end;
var
  rec: TShopItem = (MyName : 'Pencil'; Price : 15.75);
begin
  console.log(rec);
-------------------------------------
>> the emitted javascript:
  rtl.recNewT($mod,"TShopItem",function () {
    this.$eq = function (b) {
      return true;
    };
    this.$assign = function (s) {
      return this;
    };
  });
  var $mod = this;
  this.rec = pas.Unit1.TShopItem.$clone({MyName: "Pencil", Price: 15.75});
  $mod.$main = function () {
    window.console.log(pas.Unit1.TShopItem.$clone($mod.rec));
  };
*******
Note: I've got an empty object.

d) Another thing, it would be nice if I could initialize the records:

This works for me:
var
  rec: TShopItem = (MyName : 'Pencil'; Price : 15.75; Func: nil);

but I would be handy If I could:

var
  rec: TShopItem = (MyName : 'Pencil'; Price : 15.75; Func: @Go);
of course, we have an expected
---> project1.lpr(80,62) Error: Constant expression expected

e) the record optimization experiment:

-[ CODE ]--------------------------
var
  rec: TShopItem;
begin
  With rec do begin
    MyName := 'Test7';
    Price  := 7;
    Func:= @Go;
  end;
  console.log(rec);
-------------------------------------
>> it generates the expected
-------------------------
  rtl.recNewT($mod,"TShopItem",function () {
    this.MyName = "";
    this.Price = 0.0;
    this.Func = null;
    this.$eq = function (b) {
      return (this.MyName === b.MyName) && (this.Price === b.Price) &&
rtl.eqCallback(this.Func,b.Func);
    };
    this.$assign = function (s) {
      this.MyName = s.MyName;
      this.Price = s.Price;
      this.Func = s.Func;
      return this;
    };
  });
  this.rec = pas.Unit1.TShopItem.$clone({MyName: "Pencil", Price: 15.75,
Func: null});
  $mod.$main = function () {
    var $with1 = $mod.rec;
    $with1.MyName = "Test7";
    $with1.Price = 7;
    $with1.Func = $mod.Go;
    window.console.log(pas.Unit1.TShopItem.$clone($mod.rec));
  };
-------------------------

the crazy experiment here, If I edit the source:

 rtl.recNewT($mod,"TShopItem",function () {
    this.$eq = function (b) {
      //return true;
    };
    this.$assign = function (s) {
      return s;
    };
  });
//
  this.rec = {MyName: "Pencil", Price: 15.75, Func: null};
  $mod.$main = function () {
    var $with1 = $mod.rec;
    $with1.MyName = "Test77";
    $with1.Price = 77;
    $with1.Func = $mod.Go;
    window.console.log(pas.Unit1.TShopItem.$clone($mod.rec));
  };

hum. it works for me with this simple example, but sincerely I don't know if it is correct.

Reply by email

Michael Van Canneyt
to warleyalex
warleyalex wrote
I think it is amazing to see new optimizations/fetures introduced to pas2js. a) for instance, a feature IF-THEN-ELSE - a pascal version of the C ternary operator ( ? : )

As far as I know, ternary syntax is planned for FPC.

Personally I'm very much against the if-then-else construct for this If then else is a statement, not an expression.
Pascal is supposed to be readable, and I think the C syntax is more readable in an expression than a if... then statement. But I will not be the one implementing it so it is not my say.

In each case: as soon as FPC has it, pas2js can have it.

Other optimizations are still planned - there is a whole wiki page with possible optimizations - but an actual planning is not yet made.

Michael.

Reply by email