Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

12.10 Serializing Syntax🔗ℹ

procedure

(syntax-serialize 
  stx 
  #:preserve-property-keys preserve-property-keys 
  [#:provides-namespace provides-namespace 
  #:base-module-path-index base-module-path-index]) 
  any/c
  stx : syntax?
  preserve-property-keys : (listof symbol?)
  provides-namespace : (or/c namespace? #f)
   = (current-namespace)
  base-module-path-index : (or/c module-path-index? #f) = #f
Converts stx to a serialized form that is suitable for use with s-exp->fasl or serialize. Although stx could be serialized with (compile `(quote-syntax ,stx)) and then writing the compiled form, syntax-serialize provides more control over serialization:

  • The preserve-property-keys lists syntax-property keys to whose values should be preserved in serialization, even if the property value was not added as preserved with syntax-property (so it would be discarded in compiled form). The values associated with the properties to preserve must be serializable in the sense required by syntax-property for a preserved property.

  • The provides-namespace argument constrains how much the serialized syntax object can rely on bulk bindings, which are shared binding tables provided by exporting modules. If provides-namespace is #f, then complete binding information is recorded in the syntax object’s serialized form, and no bulk bindings will be needed from the namespace at deserialization. Otherwise, bulk bindings will be used only for modules declared in provides-namespace (i.e., the deserialize-time namespace will have the same module declarations as provides-namespace); note that supplying a namespace with no module bindings is equivalent to supplying #f.

  • The base-module-path-index argument specifies a module path index to which binding information in stx is relative. For example, if a syntax object originates from quote-syntax in the body of a module, then base-module-path-index could usefully be the enclosing module’s module path index as produced by (variable-reference->module-path-index (#%variable-reference)) within the module. On deserialization, a different module path index can be supplied to substitute in place of base-module-path-index, which shifts any binding that is relative to the serialize-time module’s identity to be relative to the module identity supplied at deserialize time. If base-module-path-index is #f, then no shifting is supported at deserialize time, and any base-module-path-index supplied at that time is ignored.

A serialized syntax object is otherwise similar to compiled code: it is version-specific, and deserialization will require a sufficiently powerful code inspector.

Added in version 8.0.0.13 of package base.

procedure

(syntax-deserialize 
  v 
  [#:base-module-path-index base-module-path-index]) 
  syntax?
  v : any/c
  base-module-path-index : (or/c module-path-index? #f) = #f
Converts the result of syntax-serialize back to a syntax object. See syntax-serialize for more information.

Added in version 8.0.0.13 of package base.