Jump to content

Assigning a Record Type Function Result to a Variant


Recommended Posts

This...

procedure PXTeamCreate(aTeam: TTeamStruct; aCallback: TPXMessageCallback);
begin
  var aData: Variant := new JObject;
  aData.team := aTeam.Save;
  PXSend(PXM_TEAMCREATE, aData, aCallback);
end;

Compiles to this:

function PXTeamCreate(aTeam$4, aCallback$4) {
   var aData$5;
   aData$5 = {};
   aData$5.team = {};
   PXSend(122,aData$5,aCallback$4);
};

All I can figure is that TTeamStruct.Save returns a *record* type, and somehow the poor result is being scoped out of existence before it even gets a chance.

 

function TTeamStruct.Save: TTeamXfer;
begin
  Result.TeamID :=  TeamID;
  Result.Name := Name;
  Result.Desc := Desc;
  Result.Web := Web;
  Result.Bonus := Bonus;
  Result.Shots := Shots;
  Result.Splats := Splats;
  Result.BonusCurrent := BonusCurrent;
  Result.ShotsCurrent := ShotsCurrent;
  Result.SplatsCurrent := SplatsCurrent;
end;

  TTeamXfer = record
    TeamID: integer; external 'teamID';
    Name: string; external 'name';
    Desc: string; external 'desc';
    Web: string; external 'web';
    Bonus: integer; external 'bonus';
    Shots: integer; external 'shots';
    Splats: integer; external 'splats';
    BonusCurrent: integer; external 'bonusCurrent';
    ShotsCurrent: integer; external 'shotsCurrent';
    SplatsCurrent: integer; external 'splatsCurrent';
  end;

Here's my current workaround:

procedure PXTeamCreate(aTeam: TTeamStruct; aCallback: TPXMessageCallback);
begin
  var aData: Variant := new JObject;
  var aTeamXfer := aTeam.Save;
  asm
    (@aData).team = @aTeamXfer;
  end;
  PXSend(PXM_TEAMCREATE, aData, aCallback);
end;

 

Link to post
Share on other sites

I'll try to mimic your example...

type
TTeamXfer = record
    TeamID: integer; external 'teamID';
    Name: string; external 'name';
    Desc: string; external 'desc';
    Web: string; external 'web';
    Bonus: integer; external 'bonus';
    Shots: integer; external 'shots';
    Splats: integer; external 'splats';
    BonusCurrent: integer; external 'bonusCurrent';
    ShotsCurrent: integer; external 'shotsCurrent';
    SplatsCurrent: integer; external 'splatsCurrent';
  end;

type
TTeamStruct = class
  public
    r: TTeamXfer;
    function Save(Value: TTeamXfer): TTeamXfer;
end;

function TTeamStruct.Save(Value: TTeamXfer): TTeamXfer;
begin
  Result.TeamID :=  Value.TeamID;
  Result.Name := Value.Name;
  Result.Desc := Value.Desc;
  Result.Web := Value.Web;
  Result.Bonus := Value.Bonus;
  Result.Shots := Value.Shots;
  Result.Splats := Value.Splats;
  Result.BonusCurrent := Value.BonusCurrent;
  Result.ShotsCurrent := Value.ShotsCurrent;
  Result.SplatsCurrent := Value.SplatsCurrent;
end;

type
  TPXMessageCallback = procedure;

procedure test;
begin
  console.log('testing...');
end;

procedure PXSend(v: integer; aData: Variant; aCallback: TPXMessageCallback);
begin
console.log( aData.obj.r );
end;

procedure PXTeamCreate(aTeam: TTeamStruct; aCallback: TPXMessageCallback);
begin
  var aData: Variant := new JObject;
  aData.obj := aTeam;
  aData.team := @aTeam.Save;
  PXSend(200, aData, @aCallback);
end;

 var
   Team: TTeamStruct;
   Team := TTeamStruct.Create;
   Team.r.TeamID := 10;
   Team.r.Name := 'warleyalex';
   PXTeamCreate(Team, @test);

the compiler emittes this:

function PXTeamCreate(aTeam, aCallback) {
   var aData;
   aData = {};
   aData.obj = aTeam;
   aData.team = $Event1(aTeam,TTeamStruct.Save);
   PXSend(200,aData,aCallback);
};

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...