summary refs log tree commit diff
path: root/lib/proto.fnl
blob: 13b65e9ca42f7195dde497511dadacd420588992 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
;; function set in the prototype via its identity, i.e.
;; (local blah (meta-fn :blah)
;; (blah (setmetadata {} {blah (fn [] 4)})) ;; -> 4
(fn meta-fn [name]
  (local x {})
  (setmetatable
    x
    {:__call (fn [_ obj ...] ((. (getmetatable obj) x) ...))
     :__name name
     :__fennelview (fn [] [name])}))

(fn meta-method [name]
  (local x {})
  (setmetatable
    x
    {:__call (fn [_ obj ...] ((. (getmetatable obj) x) obj ...))
     :__name name
     :__fennelview (fn [] [name])}))

;; function set in the prototype via its identity, i.e.
;; (local blah (meta-fn :blah)
;; however, these functions are optional, and nop if left out
;; (blah (setmetadata {} {})) ;; -> nil
(fn meta-fn-opt [name fallback]
  (local x {})
  (setmetatable
    x
    {:__call (fn [_ obj ...] (if (and (getmetatable obj)
                                      (. (getmetatable obj) x))
                                 ((. (getmetatable obj) x) ...)
                                 fallback
                                 (fallback ...)
                                 nil))
     :__name name
     :__fennelview (fn [] [name])}))

(fn meta-method-opt [name fallback]
  (local x {})
  (setmetatable
    x
    {:__call (fn [_ obj ...] (if (and (getmetatable obj)
                                      (. (getmetatable obj) x))
                                 ((. (getmetatable obj) x) obj ...)
                                 fallback
                                 (fallback obj ...)
                                 nil))
     :__name name
     :__fennelview (fn [] [name])}))

;; value set in the table via its identity, i.e.
;; (local blah (table-value :blah))
;; (blah {blah 4}) ;; -> (. {blah 4} blah) -> 4
(fn table-value [name]
  (local x {})
  (setmetatable
    x
    {:__call (fn [_ obj ...] (. obj x))
     :__name name
     :__fennelview (fn [] [name])}))

(fn table-fn [name]
  (local x {})
  (setmetatable
    x
    {:__call (fn [_ obj ...] ((. obj x) ...))
     :__name name
     :__fennelview (fn [] [name])}))

;; methods have an extra "self" param
(fn table-method [name]
  (local x {})
  (setmetatable
    x
    {:__call (fn [_ obj ...] ((. obj x) obj ...))
     :__name name
     :__fennelview (fn [] [name])}))

{: meta-fn : meta-fn-opt : meta-method : meta-method-opt : table-value : table-fn : table-method}