[Pas2js] another optimization idea

warleyalex warleyalex at yahoo.com.br
Sat Oct 5 01:03:07 CEST 2019


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. 



--
Sent from: http://pas2js.38893.n8.nabble.com/


More information about the Pas2js mailing list