5.21 Virtual Machine Primitives
(require ffi/unsafe/vm) | package: base |
Added in version 7.6.0.7 of package base.
procedure
(vm-primitive name) → any/c
name : symbol?
Virtual-machine primitives are the ones that can be referenced in a linklet body. The specific set of primitives depends on the virtual machine. Many “primitives” at the racket/base level or even the '#%kernel level are not primitives at the virtual-machine level. For example, if 'eval is available as a primitive, it is not the eval from racket/base.
In general, primitives are unsafe and can only be used with enough knowledge about Racket’s implementation. Here are some tips for currently available virtual machines:
(system-type 'vm) is 'racket —
The primitives in this virtual machine are mostly the same as the ones available from libraries like racket/base and racket/unsafe/ops. As a result, accessing virtual machine primitives with vm-primitive is rarely useful. (system-type 'vm) is 'chez-scheme —
The primitives in this virtual machine are Chez Scheme primitives, except as replaced by a Racket compatibility layer. The 'eval primitive is Chez Scheme’s eval. Beware of directly calling a Chez Scheme primitive that uses Chez Scheme parameters or dynamic-wind internally. Note that eval, in particular, is such a primitive. The problem is that Chez Scheme’s dynamic-wind does not automatically cooperate with Racket’s continuations or threads. To call such primitives, use the call-with-system-wind primitive, which takes a procedure of no arguments to run in a context that bridges Chez Scheme’s dynamic-wind and Racket continuations and threads. For example,
(define primitive-eval (vm-primitive 'eval)) (define call-with-system-wind (vm-primitive 'call-with-system-wind)) (define (vm-eval s) (call-with-system-wind (lambda () (primitive-eval s)))) is how vm-eval is implemented on Chez Scheme.
Symbols, numbers, booleans, pairs, vectors, boxes, strings, byte strings (i.e., bytevectors), and structures (i.e., records) are interchangeable between Racket and Chez Scheme. A Chez Scheme procedure is a Racket procedure, but not all Racket procedures are Chez Scheme procedures. To call a Racket procedure from Chez Scheme, use the #%app form that is defined in the Chez Scheme environment when it hosts Racket.
Note that you can access Chez Scheme primitives, including ones that are shadowed by Racket’s primitives, through the Chez Scheme $primitive form. For example, (vm-eval '($primitive call-with-current-continuation)) accesses the Chez Scheme call-with-current-continuation primitive instead of Racket’s replacement (where the replacement works with Racket continuations and threads).
(system-type 'vm) is 'racket —
Uses compile-linklet and instantiate-linklet. (system-type 'vm) is 'chez-scheme —
Uses Chez Scheme’s eval.
See vm-primitive for some information about how virtual-machine primitives interact with Racket.