From 46a525574121efd4e2ad5d1420d2ba11a67e9a1c Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 10 Oct 2015 23:19:32 -0700 Subject: Struct finalizers should be registered first. * share/txr/stdlib/struct.tl (defstruct): Rearrange the generated instance initialization function so that if a :fini block was specified, it is performed first. Thus if the remaining initialization code is abandoned, it is possible to intercept that and invoke the finalizer, which can clean up any mess left behind. * txr.1: Document changed order. --- share/txr/stdlib/struct.tl | 26 +++++++++++++------------- txr.1 | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/share/txr/stdlib/struct.tl b/share/txr/stdlib/struct.tl index dcfcc864..7213d9eb 100644 --- a/share/txr/stdlib/struct.tl +++ b/share/txr/stdlib/struct.tl @@ -121,19 +121,19 @@ (static-slot-set ,arg-sym ',@2 ,@3))) stat-si-forms))) ,(if (or inst-si-forms instance-init-form instance-fini-form) - ^(lambda (,arg-sym) - ,*(if inst-si-forms - ^((let ((,type-sym (struct-type ,arg-sym))) - ,*(mapcar (aret ^(unless (static-slot-p ,type-sym ',@2) - (slotset ,arg-sym ',@2 ,@3))) - inst-si-forms)))) - ,*(if (cdr instance-init-form) - ^((let ((,(car instance-init-form) ,arg-sym)) - ,*(cdr instance-init-form)))) - ,*(if (cdr instance-fini-form) - ^((finalize ,arg-sym (lambda (,(car instance-fini-form)) - ,*(cdr instance-fini-form)) - t))))) + ^(lambda (,arg-sym) + ,*(if (cdr instance-fini-form) + ^((finalize ,arg-sym (lambda (,(car instance-fini-form)) + ,*(cdr instance-fini-form)) + t))) + ,*(if inst-si-forms + ^((let ((,type-sym (struct-type ,arg-sym))) + ,*(mapcar (aret ^(unless (static-slot-p ,type-sym ',@2) + (slotset ,arg-sym ',@2 ,@3))) + inst-si-forms)))) + ,*(if (cdr instance-init-form) + ^((let ((,(car instance-init-form) ,arg-sym)) + ,*(cdr instance-init-form)))))) ,(when args (when (> (countql : args) 1) (throwf 'eval-error "~s: multiple colons in boa syntax" diff --git a/txr.1 b/txr.1 index 09220df0..911eb2f8 100644 --- a/txr.1 +++ b/txr.1 @@ -18025,8 +18025,8 @@ a finalization function which is associated with the structure instance, as if by use of the .code finalize function. This finalization registration takes place -as the last step when an instance of the structure -is created, after the slots are initialized and +as the first step when an instance of the structure +is created, before the slots are initialized and the .code :init code, if any, has been executed. The registration -- cgit v1.2.3