emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/trie bc12ecb 072/111: Exploit lexical closures to allow


From: Stefan Monnier
Subject: [elpa] externals/trie bc12ecb 072/111: Exploit lexical closures to allow byte-compilation of wrapped functions.
Date: Mon, 14 Dec 2020 11:35:23 -0500 (EST)

branch: externals/trie
commit bc12ecbbefddaf2b56f7efd8beada5f33f251878
Author: Toby S. Cubitt <toby-predictive@dr-qubit.org>
Commit: Toby S. Cubitt <toby-predictive@dr-qubit.org>

    Exploit lexical closures to allow byte-compilation of wrapped functions.
    
    A number of functions are wrapped in a thin anonymous function before being
    stored in the data structures. The wrappers were constructed as quoted 
lambda
    forms, so were not byte-compiled. Lexical closures allow the
    wrapper-constructing functions to use unquoted lambdas, enabling
    byte-compilation of the wrapped functions.
    
    We fall back to old-style function wrapping if lexical binding unsupported.
---
 trie.el | 54 ++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 16 deletions(-)

diff --git a/trie.el b/trie.el
index 50fb951..3fe434c 100644
--- a/trie.el
+++ b/trie.el
@@ -225,22 +225,44 @@
   transform-for-print transform-from-read print-form)
 
 
-(defun trie--wrap-cmpfun (cmpfun)
-  ;; wrap CMPFUN for use in a subtree
-  `(lambda (a b)
-     (setq a (trie--node-split a)
-          b (trie--node-split b))
-     (cond ((eq a trie--terminator)
-           (if (eq b trie--terminator) nil t))
-          ((eq b trie--terminator) nil)
-          (t (,cmpfun a b)))))
-
-
-(defun trie--construct-equality-function (comparison-function)
-  ;; create equality function from trie comparison function
-  `(lambda (a b)
-     (and (not (,comparison-function a b))
-         (not (,comparison-function b a)))))
+(defmacro trie-lexical-binding-p ()
+  "Return non-nil if lexical binding is in effect, nil otherwise."
+  (let ((tempvar (make-symbol "x")))
+    `(let ((,tempvar nil)
+           (f (let ((,tempvar t)) (lambda () ,tempvar))))
+       (funcall f))))
+
+
+;; wrap CMPFUN for use in a subtree
+(if (trie-lexical-binding-p)
+    (defun trie--wrap-cmpfun (cmpfun)
+      (lambda (a b)
+       (setq a (trie--node-split a)
+             b (trie--node-split b))
+       (cond ((eq a trie--terminator)
+              (if (eq b trie--terminator) nil t))
+             ((eq b trie--terminator) nil)
+             (t (funcall cmpfun a b)))))
+  (defun trie--wrap-cmpfun (cmpfun)
+    `(lambda (a b)
+       (setq a (trie--node-split a)
+            b (trie--node-split b))
+       (cond ((eq a trie--terminator)
+             (if (eq b trie--terminator) nil t))
+            ((eq b trie--terminator) nil)
+            (t (,cmpfun a b))))))
+
+
+;; create equality function from trie comparison function
+(if (trie-lexical-binding-p)
+    (defun trie--construct-equality-function (comparison-function)
+      (lambda (a b)
+        (and (not (funcall comparison-function a b))
+             (not (funcall comparison-function b a)))))
+  (defun trie--construct-equality-function (comparison-function)
+    `(lambda (a b)
+       (and (not (,comparison-function a b))
+           (not (,comparison-function b a))))))
 
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]