5.21 Virtual Machine Primitives

► |  | The Racket Foreign Interface

1 | Overview
2 | Loading Foreign Libraries
3 | C Types
4 | Pointer Functions
5 | Derived Utilities
6 | Miscellaneous Support
7 | Unexported Primitive Functions
| Bibliography
| Index

▼ | 5 | Derived Utilities

5.1 | Safe Homogenous Vectors
5.2 | Safe C Vectors
5.3 | Tagged C Pointer Types
5.4 | Serializable C Struct Types
5.5 | Static Callout and Callback Cores
5.6 | Defining Bindings
5.7 | Allocation and Finalization
5.8 | Custodian Shutdown Registration
5.9 | Atomic Execution
5.10 | Speculatively Atomic Execution
5.11 | Thread Scheduling
5.12 | Ports
5.13 | Process-Wide and Place-Wide Registration
5.14 | Operating System Threads
5.15 | Garbage Collection Callbacks
5.16 | Objective-C FFI
5.17 | Cocoa Foundation
5.18 | COM (Common Object Model)
5.19 | File Security-Guard Checks
5.20 | Windows API Helpers
5.21 | Virtual Machine Primitives

On this page:

vm-primitive
vm-eval

Racket
top contents ← prev up next →

### 5.21 Virtual Machine Primitives🔗ℹ

(require ffi/unsafe/vm) | package: base

The ffi/unsafe/vm library provides access to functionality in the underlying virtual machine that is used to implement Racket.

Added in version 7.6.0.7 of package base.

> procedure (vm-primitive name) → any/c
> name : symbol?

Accesses a primitive values at the level of the running Racket virtual machine, or returns #f if name is not the name of a primitive.

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).

> procedure (vm-eval s-expr) → any/c
> s-expr : any/c

Evaluates s-expr using the most primitive available evaluator:

- (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.

top contents ← prev up next →