PasteRack.org
Paste # 54556
2018-04-09 11:00:31

Fork as a new paste.

Paste viewed 64 times.


Embed:

  1. #lang racket
  2.  
  3. (require ffi/unsafe
  4.          ffi/unsafe/define)
  5.  
  6.  
  7. (define scheme_get_type_name (get-ffi-obj 'scheme_get_type_name
  8.                                           #f
  9.                                           (_fun _short -> _string)))
  10.  
  11. (define (value->type-string v)
  12.   (scheme_get_type_name (if (fixnum? v)
  13.                             51
  14.                             (ptr-ref (cast v _racket _pointer) _short))))
  15.  
  16.  
  17. ;; test
  18.  
  19. (for ([value (list 1
  20.                    1.0
  21.                    1/2
  22.                    1+2i
  23.                    (λ () #f)
  24.                    #t
  25.                    #f
  26.                    (vector 1)
  27.                    #\a
  28.                    '()
  29.                    '(1)
  30.                    "1"
  31.                    eof
  32.                    void
  33.                    (void)
  34.                    )])
  35.   (printf "~a   ~v~%" (value->type-string value) value))
  36.  
  37.  
  38. #|
  39.  
  40.   /racket/src/racket/src/stypes.h
  41.   -------------------------------
  42.  
  43.   scheme_integer_type,                  /* 51 */
  44.  
  45.  
  46.  
  47.   racket/src/racket/include/scheme.h
  48.   ----------------------------------
  49.  
  50.   #define SCHEME_INTP(obj) (OBJ_TO_LONG(obj) & 0x1)
  51.  
  52.   #define SCHEME_TYPE(obj) (SCHEME_INTP(obj) ?
  53.                                (Scheme_Type)scheme_integer_type
  54.                              : ((Scheme_Object *)(obj))->type)
  55.  
  56.  
  57.   typedef struct Scheme_Object
  58.   {
  59.     Scheme_Type type; /* Anything that starts with a type field
  60.                          can be a Scheme_Object */
  61.  
  62.     /* For precise GC, the keyex field is used for all object types to
  63.        store a hash key extension. The low bit is not used for this
  64.        purpose, though. For string, pair, vector, and box values in all
  65.        variants of Racket, the low bit is set to 1 to indicate that
  66.        the object is immutable. Thus, the keyex field is needed even in
  67.        non-precise GC mode, so such structures embed
  68.        Scheme_Inclhash_Object */
  69.  
  70.     MZ_HASH_KEY_EX
  71.   } Scheme_Object;
  72. |#

=>

eval:2:0: get-ffi-obj: access disallowed by code inspector

to protected variable from module:

"/home/pasterack/racket68/collects/ffi/unsafe.rkt"

  in: get-ffi-obj

eval:3:0: ptr-ref: access disallowed by code inspector to

protected variable from module: '#%foreign

  in: ptr-ref

value->type-string: undefined;

 cannot reference an identifier before its definition

  in module: 'm