summaryrefslogtreecommitdiff
path: root/tests/webtbs/tw17904.pas
blob: 06a01e7b2b496f05d8c0cce6c843160f100d9714 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

{$ifdef fpc}{$mode objfpc}{$h+}{$endif}
{$apptype console}

uses Variants, SysUtils;

type
  TTest = class(TCustomVariantType)
    procedure Clear(var V: TVarData); override;
    procedure Copy(var Dest: TVarData; const Source: TVarData; const Indirect: Boolean); override;
    procedure DispInvoke(Dest: PVarData; const Source: TVarData; CallDesc: PCallDesc; Params: Pointer); override;
  end;

procedure TTest.Clear(var V: TVarData);
begin
end;

procedure TTest.Copy(var Dest: TVarData; const Source: TVarData; const Indirect: Boolean);
begin
end;

procedure TTest.DispInvoke(Dest: PVarData; const Source: TVarData; CallDesc: PCallDesc; Params: Pointer);
var
  tmp: Word;
begin
  if (CallDesc^.ArgCount =2) and Assigned(Dest) then
  begin
    //writeln(HexStr(PPointer(Params)^), ' ', HexStr(PPointer(Params)[1]));
    WordRec(tmp).Lo := CallDesc^.ArgTypes[0];
    WordRec(tmp).Hi := CallDesc^.ArgTypes[1];
    // !! FPC passes args right-to-left, Delphi does same left-to-right
    // Moreover, IDispatch needs args right-to-left, and Variant Dispatch needs left-to-right. Nice, huh?
    {$ifdef fpc}
    tmp := Swap(tmp);
    {$endif}
    Variant(Dest^) := tmp;
  end;  
end;

type
  TTestClass=class
    u8: byte;
    u16: word;
    u32: longword;
{$ifdef fpc}
    u64: qword;
{$endif}
    s8: shortint;
    s16: smallint;
    s32: longint;
    s64: int64;

    cy: currency;

    b: boolean;
    bb: bytebool;
    wb: wordbool;
    lb: longbool;

    sgl: single;
    dbl: double;
    ext: extended;
    dt: TDateTime;

    fsstr: shortstring;
    fastr: ansistring;
    fwstr: widestring;
{$ifdef fpc}
    fustr: unicodestring;
{$endif}

    fvar: Variant;
    fintf: IInterface;
    fdisp: IDispatch;

    property u8prop: Byte read u8;
    property u16prop: Word read u16;
    property u32prop: LongWord read u32;
{$ifdef fpc}
    property u64prop: QWord read u64;
{$endif}
    property s8prop: ShortInt read s8;
    property s16prop: SmallInt read s16;
    property s32prop: LongInt read s32;
    property s64prop: Int64 read s64;

    property cyprop: currency read cy;
    property bprop: boolean read b;
    property bbprop: bytebool read bb;
    property wbprop: wordbool read wb;
    property lbprop: longbool read lb;

    property sglprop: single read sgl;
    property dblprop: double read dbl;
    property extprop: extended read ext;
    property dtprop: TDateTime read dt;

    property varprop: Variant read fvar;
    property intfprop: IInterface read fintf;
    property dispprop: IDispatch read fdisp;

    property sstr: shortstring read fsstr;
    property astr: ansistring read fastr;
    property wstr: widestring read fwstr;
{$ifdef fpc}
    property ustr: unicodestring read fustr;
{$endif}
  end;

var
  cv: TCustomVariantType;
  code: Integer;
  cl: TTestClass;
  v: Variant;

// using negative values of Expected to check that arg is passed by-value only
procedure test(const id: string; const act: Variant; expected: Integer);
var
  tmp: word;
  absexp: Integer;
begin
  tmp := act;
  absexp := abs(expected);
  write(id, WordRec(tmp).Lo,', ', WordRec(tmp).Hi);
  if (expected >= 0) and (WordRec(tmp).Lo <> (expected or $80)) then
  begin
    write(' BYREF failed');
    Code := Code or 1;
  end;  
  if WordRec(tmp).Hi <> absexp then
  begin
    write(' BYVAL failed');
    Code := Code or 2;
  end;
  writeln;
end;

begin
  Code := 0;
  cv := TTest.Create;
  cl := TTestClass.Create;
  TVarData(v).vType := cv.VarType;

  test('u8:    ', v.foo(cl.u8, cl.u8prop), varbyte);
  
  test('u16:    ', v.foo(cl.u16, cl.u16prop), varword);       // (Uncertain) D7: treated as Integer
  test('u32:    ', v.foo(cl.u32, cl.u32prop), varlongword);   // (Uncertain) D7: treated as Integer ByRef
  test('s8:     ', v.foo(cl.s8, cl.s8prop), varshortint);     // (Uncertain) D7: treated as Integer

  test('s16:    ', v.foo(cl.s16, cl.s16prop), varsmallint);
  test('s32:    ', v.foo(cl.s32, cl.s32prop), varinteger);
  test('s64:    ', v.foo(cl.s64, cl.s64prop), varint64);
{$ifdef fpc}
  test('u64:    ', v.foo(cl.u64, cl.u64prop), varword64);
{$endif}
  
  test('wordbool:', v.foo(cl.wb, cl.wbprop), varBoolean);
  test('curncy:  ', v.foo(cl.cy, cl.cyprop), varCurrency);
  
  test('single:  ', v.foo(cl.sgl, cl.sglprop), varSingle);
  test('double:  ', v.foo(cl.dbl, cl.dblprop), varDouble);
  test('extended:', v.foo(cl.ext, cl.extprop), -varDouble);  // not a COM type, passed by value
  
  test('date:    ', v.foo(cl.dt, cl.dtprop), varDate);

  test('ansistr: ', v.foo(cl.fastr, cl.astr), varStrArg);
  test('widestr: ', v.foo(cl.fwstr, cl.wstr), varOleStr);
{$ifdef fpc}
  test('unistr:  ', v.foo(cl.fustr, cl.ustr), varUStrArg);
{$endif}
  test('variant: ', v.foo(cl.fvar, cl.varprop), varVariant);
  
  test('IUnknown:', v.foo(cl.fintf, cl.intfprop), varUnknown);
  test('IDispatch:', v.foo(cl.fdisp, cl.dispprop), varDispatch);
  
  // not an COM type, passed by value; Delphi uses varStrArg
  test('shortstr:', v.foo(cl.fsstr, cl.sstr), -varOleStr);
  // not an COM type, passed by value
  test('longbool:', v.foo(cl.lb, cl.lbprop), -varBoolean);

  // typecasted ordinals (only one arg is actually used)
  test('u8+cast: ', v.foo(byte(55), byte(55)), -varByte);
  test('u16+cast:', v.foo(word(55), word(55)), -varWord);
  test('u32+cast:', v.foo(longword(55), longword(55)), -varLongWord);
{$ifdef fpc}
  test('u64+cast:', v.foo(qword(55), qword(55)), -varQWord);
{$endif}
  test('s8+cast:', v.foo(shortint(55), shortint(55)), -varShortInt);
  test('s16+cast:', v.foo(smallint(55), smallint(55)), -varSmallInt);
  test('s32+cast:', v.foo(longint(55), longint(55)), -varInteger);
  test('s64+cast:', v.foo(int64(55), int64(55)), -varInt64);

  cl.Free;
  if Code <> 0 then
    writeln('Errors: ', Code);
  Halt(Code);

end.