---------------------------------------------------------------
                      [ENHANCED] VFP 9.0 FIX - TYPE() FUNCTION
                               January 2024
                 ---------------------------------------------------------------
                                     CCB



1. BUG:

     In vfp9 (and vfp6, vfp7, vfp8), the TYPE() function cannot be used to evaluate UDFs (user-defined functions),
     now the TYPE(cExpression,2) function can be used to evaluate UDFs (user-defined functions).

     There is a test program:

     *PROC testtypefunction

     SET STEP OFF
     SET ECHO OFF
     SET DEBUG OFF
     SET ESCAPE OFF
     SET TALK OFF
     SET SAFETY OFF

     ?TYPE("testtypefunction2()") && Displays "U"
     WAIT

     ?TYPE("testtypefunction2()",2) && Displays "C"
     WAIT

     RETURN

     * END OF PROC TESTTYPEFUNCTION.

     PROC testtypefunction2

     RETU "aaa"

     * END OF PROC TESTTYPEFUNCTION2.


2. CAUSE:

     There are some BUGs in the following code.


3. RESOLUTION:

     We can write some code to fix the BUG.

     Fun5035d4 :: ; proc near
             push ebp                                                        ;0x005035d4 :        55
             mov ebp , esp                                                   ;0x005035d5 :        8bec
             push ecx                                                        ;0x005035d7 :        51
             mov eax ,  dword ptr [ Data93936c ]                             ;0x005035d8 :        a16c939300
             push ebx                                                        ;0x005035dd :        53
             push esi                                                        ;0x005035de :        56
             mov  esi , dword ptr [ ebp + 12 ]                               ;0x005035df :        8b750c
             mov ecx , esi                                                   ;0x005035e2 :        8bce
             sub ecx , eax                                                   ;0x005035e4 :        2bc8
             mov eax , 02E8BA2E9h                                            ;0x005035e6 :        b8e9a28b2e
             imul ecx                                                        ;0x005035eb :        f7e9
             sar edx , 03h                                                   ;0x005035ed :        c1fa03
             mov ebx , edx                                                   ;0x005035f0 :        8bda
             shr ebx , 01Fh                                                  ;0x005035f2 :        c1eb1f
             push edi                                                        ;0x005035f5 :        57
             mov eax , esi                                                   ;0x005035f6 :        8bc6
             add ebx , edx                                                   ;0x005035f8 :        03da
             cmp  dword ptr [ esi + 8 ] , 02000h                             ;0x005035ff :        817e0800200000
             jnb Label5923eb                                                 ;0x00503606 :        0f83dfed0800

     Label50360c ::
             xor eax , eax                                                   ;0x0050360c :        33c0
             call Fun42c118                                                  ;0x0050360e :        e8058bf2ff
             mov  eax , dword ptr [ esi + 8 ]                                ;0x00503613 :        8b4608
             mov  edi , dword ptr [ Data9370c4 ]                             ;0x00503616 :        8b3dc4709300
             mov ecx , esp                                                   ;0x0050361c :        8bcc
             inc eax                                                         ;0x0050361e :        40
             sub ecx , edi                                                   ;0x0050361f :        2bcf
             cmp ecx , eax                                                   ;0x00503621 :        3bc8
             jle Label59243f                                                 ;0x00503623 :        0f8e16ee0800
             add eax , 07h                                                   ;0x00503629 :        83c007
             and eax , 0FFFFFFF8h                                            ;0x0050362c :        83e0f8
             call Fun42c118                                                  ;0x0050362f :        e8e48af2ff
             mov eax , esp                                                   ;0x00503634 :        8bc4
             test eax , eax                                                  ;0x00503636 :        85c0
             mov  dword ptr [ ebp - 4 ] , eax                                ;0x00503638 :        8945fc
             je Label59241a                                                  ;0x0050363b :        0f84d9ed0800

     Label503641 ::
             mov  ecx , dword ptr [ esi + 8 ]                                ;0x00503641 :        8b4e08
             lea eax ,  dword ptr [ ecx + 1 ]                                ;0x00503644 :        8d4101
             cmp eax , ecx                                                   ;0x00503647 :        3bc1
             jb Label50364d                                                  ;0x00503649 :        7202
             mov eax , ecx                                                   ;0x0050364b :        8bc1

     Label50364d ::
             mov  edx , dword ptr [ esi + 32 ]                               ;0x0050364d :        8b5620
             mov  esi , dword ptr [edx]                                      ;0x00503650 :        8b32
             mov  edi , dword ptr [ ebp - 4 ]                                ;0x00503652 :        8b7dfc
             mov ecx , eax                                                   ;0x00503655 :        8bc8
             mov edx , ecx                                                   ;0x00503657 :        8bd1
             shr ecx , 02h                                                   ;0x00503659 :        c1e902
             rep movsd                                                       ;0x0050365c :        f3a5
             mov ecx , edx                                                   ;0x0050365e :        8bca
             mov  edx , dword ptr [ ebp + 8 ]                                ;0x00503660 :        8b5508
             and ecx , 03h                                                   ;0x00503663 :        83e103
             rep movsb                                                       ;0x00503666 :        f3a4
             mov  ecx , dword ptr [ ebp - 4 ]                                ;0x00503668 :        8b4dfc
             mov  byte ptr [ eax + ecx ] , 00h                               ;0x0050366b :        c6040800
             mov  eax , dword ptr [ ebp + 12 ]                               ;0x0050366f :        8b450c
             push eax                                                        ;0x00503672 :        50


     ;
     ;                 ----------------------------------------------------
     ;                      VFP 9.0 FIX - TYPE() FUNCTION
     ;                                  June 2022
     ;                 ----------------------------------------------------
     ;                                     CCB
     ;
     ; In vfp9, the TYPE() function cannot be used to evaluate UDFs (user-defined functions),
     ; now the TYPE(cExpression,2) function can be used to evaluate UDFs (user-defined functions).
     ;
     ; 2022/6/22, by ccb
     ;

             cmp dword ptr [ ebp + 04h ] , offset Label501113 ;; Label50114d ..
             jne Label503673
             cmp  dword ptr [ Data936f1c ] , 02h
             jne Label503673
             cmp  dword ptr [ eax + 56 ] , 02h
             jne Label503673
             pushd 01h
             jmp Label503675


     Label503673 ::
             pushd 00h                                                       ;0x00503673 :        6a00

     Label503675 ::
             push edx                                                        ;0x00503675 :        52
             call Fun503696                                                  ;0x00503676 :        e81b000000
             imul ebx , ebx , 02Ch                                           ;0x0050367b :        6bdb2c
             add  ebx , dword ptr [ Data93936c ]                             ;0x0050367e :        031d6c939300
             mov  dword ptr [ Data9370f0 ] , ebx                             ;0x00503684 :        891df0709300
             lea esp ,  dword ptr [ ebp - 16 ]                               ;0x0050368a :        8d65f0
             pop edi                                                         ;0x0050368d :        5f
             pop esi                                                         ;0x0050368e :        5e
             pop ebx                                                         ;0x0050368f :        5b
             mov esp , ebp                                                   ;0x00503690 :        8be5
             pop ebp                                                         ;0x00503692 :        5d
             ret 08h                                                         ;0x00503693 :        c20800


4. APPLIES TO:

     VFP 6.0.8167.0
     VFP 6.0.8961.0 (SP5)

     VFP 7.0.0.9262
     VFP 7.0.0.9465 (SP1)

     VFP 8.0.0.2521
     VFP 8.0.0.3117 (SP1)

     VFP 9.0.0.2412
     VFP 9.0.0.3504 (SP1)
     VFP 9.0.0.4611 (SP2)
     VFP 9.0.0.5015 (SP2)
     VFP 9.0.0.5411 (SP2)
     VFP 9.0.0.5721 (SP2)
     VFP 9.0.0.5815 (SP2)
     VFP 9.0.0.6303 (SP2)
     VFP 9.0.0.6602 (SP2)
     VFP 9.0.0.7423 (SP2)

     The bug has been fixed in VFP Advanced.


5. REFERENCE WEBSITES:

     1, baiyujia.com:
     http://www.baiyujia.com


6. OTHER:

     For reference only, there is no guarantees.

     Any questions or suggestions, please send me an email at ccb2000@163.com.