Discussion:
[fpc-devel] mantis 0038496 custom variants and documentation
Marco van de Voort via fpc-devel
2021-05-29 22:53:42 UTC
Permalink
Before the 3.2.2 release I looked into mantis 0038496 and now I come
back to it.

I noticed that custom variants are completely undocumented, is this know
(IOW should I file a bug?). What I wanted to look up are the rules for
implementing binaryop, since that is where the problem is.

The bug is for the following code:

{$ifdef fpc}
 {$mode delphi}
{$else}
 {$apptype console}
{$endif}

uses variants,fmtbcd;
var
  fBCD1: TBcd;
  V1, V2, V3:Variant;
  L1: Integer;
begin
  L1:=123;
  fBCD1:=1234.345;

  V1:=123;
  V2:=VarFmtBCDCreate(fBCD1);
  V3:=V1 + V2;
  writeln(v3);
  readln;
end.

The addition finally ends up in  TFMTBcdFactory.BinaryOp which is
declared as follows

procedure TFMTBcdFactory.BinaryOp(var Left: TVarData; const Right:
TVarData; const Operation: TVarOp);

i.e.  not a 3 operand action, but a two operand.  This probably means
that in the case of v3:=v1+v2 this is encoded as v1:=v1,v2,add,
modifying V1.

The fmtbcd binaryop code however says that the left type (integer)  is
not either double or a a FMTBCD custom variant and thus throws an exception.

I think it is allowed to change the result type of the variant passed in
binaryop(), but am not 100% sure, and also not sure if there are other
things to keep an eye for (e.g. finalization of the existing value).

Does anybody know open source custom variant examples other than fmtbcd ?






_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.
Michael Van Canneyt via fpc-devel
2021-05-30 08:43:51 UTC
Permalink
Post by Marco van de Voort via fpc-devel
Before the 3.2.2 release I looked into mantis 0038496 and now I come
back to it.
I noticed that custom variants are completely undocumented, is this know
(IOW should I file a bug?).
Please do. I am aware of this issue, but it always falls between the other
issues. If you happen to have some sample programs (however small) I can
use to document this, please attach them to the issue.

I know that Synopse makes heavy use of the customvariant (I believe their
JSON document it a customvariant), maybe it can be a source of inspiration.

Michael.
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.f
Don Alfredo via fpc-devel
2021-05-30 09:39:06 UTC
Permalink
https://synopse.info/forum/viewtopic.php?id=5894

Op zo 30 mei 2021 10:44 schreef Michael Van Canneyt via fpc-devel <
Post by Michael Van Canneyt via fpc-devel
Post by Marco van de Voort via fpc-devel
Before the 3.2.2 release I looked into mantis 0038496 and now I come
back to it.
I noticed that custom variants are completely undocumented, is this know
(IOW should I file a bug?).
Please do. I am aware of this issue, but it always falls between the other
issues. If you happen to have some sample programs (however small) I can
use to document this, please attach them to the issue.
I know that Synopse makes heavy use of the customvariant (I believe their
JSON document it a customvariant), maybe it can be a source of inspiration.
Michael.
_______________________________________________
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Ondrej Kelle via fpc-devel
2021-05-30 10:10:13 UTC
Permalink
chakracore-delphi (experimental/variants branch) also has a custom variant
(descendant of TInvokeableVariantType) to wrap javascript values/objects so
you can access properties and call methods on javascript objects at runtime
(similar to how you can use ActiveX/Automation objects through Variants
when not compiling against a type library).

https://github.com/tondrej/chakracore-delphi
Blog post:
https://tondrej.blogspot.com/2020/10/variant-support-in-chakracore-delphi.html

Cheers,
Ondrej

On Sun, 30 May 2021 at 11:39, Don Alfredo via fpc-devel <
Post by Don Alfredo via fpc-devel
https://synopse.info/forum/viewtopic.php?id=5894
Op zo 30 mei 2021 10:44 schreef Michael Van Canneyt via fpc-devel <
Post by Michael Van Canneyt via fpc-devel
Post by Marco van de Voort via fpc-devel
Before the 3.2.2 release I looked into mantis 0038496 and now I come
back to it.
I noticed that custom variants are completely undocumented, is this
know
Post by Marco van de Voort via fpc-devel
(IOW should I file a bug?).
Please do. I am aware of this issue, but it always falls between the other
issues. If you happen to have some sample programs (however small) I can
use to document this, please attach them to the issue.
I know that Synopse makes heavy use of the customvariant (I believe their
JSON document it a customvariant), maybe it can be a source of inspiration.
Michael.
_______________________________________________
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
_______________________________________________
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Sven Barth via fpc-devel
2021-05-30 11:04:36 UTC
Permalink
Post by Don Alfredo via fpc-devel
https://synopse.info/forum/viewtopic.php?id=5894
<https://synopse.info/forum/viewtopic.php?id=5894>
I'm already working on a fix for this, cause I had seen that thread a
few days ago already. It would have been nice however if you had found
this out during the RC phase which had lasted long enough.

Also why is looking through the code of TSynInvokeableVariantType the
first time I hear about the swapped order of parameters? Did you report
this as a bug? And if not, why not? This could have been fixed with
3.2.2 as well! (And I'm indeed working on fixing it)

Also: no top posting, please.

Regards,
Sven
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.fre
Sven Barth via fpc-devel
2021-05-30 11:02:19 UTC
Permalink
Post by Marco van de Voort via fpc-devel
Before the 3.2.2 release I looked into mantis 0038496 and now I come
back to it.
I noticed that custom variants are completely undocumented, is this
know (IOW should I file a bug?). What I wanted to look up are the
rules for implementing binaryop, since that is where the problem is.
{$ifdef fpc}
 {$mode delphi}
{$else}
 {$apptype console}
{$endif}
uses variants,fmtbcd;
var
  fBCD1: TBcd;
  V1, V2, V3:Variant;
  L1: Integer;
begin
  L1:=123;
  fBCD1:=1234.345;
  V1:=123;
  V2:=VarFmtBCDCreate(fBCD1);
  V3:=V1 + V2;
  writeln(v3);
  readln;
end.
The addition finally ends up in  TFMTBcdFactory.BinaryOp which is
declared as follows
TVarData; const Operation: TVarOp);
i.e.  not a 3 operand action, but a two operand.  This probably means
that in the case of v3:=v1+v2 this is encoded as v1:=v1,v2,add,
modifying V1.
The fmtbcd binaryop code however says that the left type (integer)  is
not either double or a a FMTBCD custom variant and thus throws an exception.
I think it is allowed to change the result type of the variant passed
in binaryop(), but am not 100% sure, and also not sure if there are
other things to keep an eye for (e.g. finalization of the existing
value).
Does anybody know open source custom variant examples other than fmtbcd ?
The only one I'm aware of that makes use of BinaryOp is Python4Delphi (
https://github.com/pyscripter/python4delphi/blob/master/Source/VarPyth.pas
). mORMot derives its TSynInvokeableVariantType and thus its TDocVariant
from TInvokableVariantType, but does not override the operator methods.

Regards,
Sven
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/c
Marco van de Voort via fpc-devel
2021-05-30 13:04:56 UTC
Permalink
Post by Sven Barth via fpc-devel
Post by Marco van de Voort via fpc-devel
I think it is allowed to change the result type of the variant passed
in binaryop(), but am not 100% sure, and also not sure if there are
other things to keep an eye for (e.g. finalization of the existing
value).
Does anybody know open source custom variant examples other than fmtbcd ?
The only one I'm aware of that makes use of BinaryOp is Python4Delphi (
https://github.com/pyscripter/python4delphi/blob/master/Source/VarPyth.pas
). mORMot derives its TSynInvokeableVariantType and thus its
TDocVariant from TInvokableVariantType, but does not override the
operator methods.
I think I saw this one before in March. I also remember the conclusion:
this should be fixed in LefPromotion(), but FPC doesn't implement
calling that method :-)
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.f

Loading...