aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/js/eval.jvm.lux
blob: 94136f356bffcd36cd7933eaa821c8259590acca (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
(.module:
  lux
  (lux (control ["ex" exception #+ exception:])
       (data [bit]
             [maybe]
             ["e" error #+ Error]
             text/format
             (coll [array]))
       [host])
  (luxc [lang]
        (lang (host [js #+ JS Expression Statement])))
  [//])

(do-template [<name>]
  [(exception: #export (<name> {message Text})
     message)]

  [Unknown-Kind-Of-JS-Object]
  [Null-Has-No-Lux-Representation]

  [Cannot-Evaluate]
  )

(host.import java/lang/Object
  (toString [] String))

(host.import java/lang/Integer
  (longValue [] Long))

(host.import java/lang/Number
  (doubleValue [] double)
  (longValue [] Long)
  (intValue [] Integer))

(host.import javax/script/ScriptEngine
  (eval [String] #try #? Object))

(host.import jdk/nashorn/api/scripting/JSObject
  (isArray [] boolean)
  (isFunction [] boolean)
  (getMember [String] #? Object)
  (hasMember [String] boolean))

(host.import jdk/nashorn/api/scripting/AbstractJSObject)

(host.import jdk/nashorn/api/scripting/ScriptObjectMirror
  (size [] int))

(host.import jdk/nashorn/internal/runtime/Undefined)

(host.import luxc/lang/translation/js/IntValue
  (getValue [] Long))

(host.import luxc/lang/translation/js/StructureValue
  (getValue [] (Array Object)))

(def: (int js-object)
  (-> ScriptObjectMirror (Maybe Int))
  (case [(JSObject::getMember [//.int-high-field] js-object)
         (JSObject::getMember [//.int-low-field] js-object)]
    (^multi [(#.Some high) (#.Some low)]
            (and (host.instance? Number high)
                 (host.instance? Number low))
            [[(Number::longValue [] (:! Number high))
              (Number::longValue [] (:! Number low))]
             [high low]])
    (#.Some (.int (n/+ (|> high (:! Nat) (bit.left-shift +32))
                       (if (i/< 0 (:! Int low))
                         (|> low (:! Nat) (bit.left-shift +32) (bit.logical-right-shift +32))
                         (|> low (:! Nat))))))

    _
    #.None))

(def: (variant lux-object js-object)
  (-> (-> Object (Error Any)) ScriptObjectMirror (Maybe Any))
  (case [(JSObject::getMember [//.variant-tag-field] js-object)
         (JSObject::getMember [//.variant-flag-field] js-object)
         (JSObject::getMember [//.variant-value-field] js-object)]
    (^multi [(#.Some tag) ?flag (#.Some value)]
            (host.instance? Number tag)
            [[(Number::intValue [] (:! Number tag))
              (lux-object value)]
             [tag (#.Some value)]])
    (#.Some [tag (maybe.default (host.null) ?flag) value])

    _
    #.None))

(def: (array lux-object js-object)
  (-> (-> Object (Error Any)) ScriptObjectMirror (Maybe (Array Object)))
  (if (JSObject::isArray [] js-object)
    (let [init-num-keys (.nat (ScriptObjectMirror::size [] js-object))]
      (loop [num-keys init-num-keys
             idx +0
             output (: (Array Object)
                       (array.new init-num-keys))]
        (if (n/< num-keys idx)
          (let [idx-key (|> idx .int %i)]
            (case (JSObject::getMember idx-key js-object)
              (#.Some member)
              (case (lux-object member)
                (#e.Success parsed-member)
                (recur num-keys (inc idx) (array.write idx (:! Object parsed-member) output))

                (#e.Error error)
                #.None)
              
              #.None
              (recur num-keys (inc idx) output)))
          (#.Some output))))
    #.None))

(def: (lux-object js-object)
  (-> Object (Error Any))
  (`` (cond (host.null? js-object)
            (ex.throw Null-Has-No-Lux-Representation "")
            
            (host.instance? Integer js-object)
            (ex.return (Integer::longValue [] (:! Integer js-object)))

            (or (host.instance? java/lang/Boolean js-object)
                (host.instance? java/lang/String js-object))
            (ex.return js-object)

            (host.instance? Number js-object)
            (ex.return (Number::doubleValue [] (:! Number js-object)))

            (~~ (do-template [<interface> <method>]
                  [(host.instance? <interface> js-object)
                   (ex.return (<method> [] (:! <interface> js-object)))]
                  
                  [StructureValue StructureValue::getValue]
                  [IntValue       IntValue::getValue]))
            
            (host.instance? ScriptObjectMirror js-object)
            (let [js-object (:! ScriptObjectMirror js-object)]
              (case (int js-object)
                (#.Some value)
                (ex.return value)

                #.None
                (case (variant lux-object js-object)
                  (#.Some value)
                  (ex.return value)

                  #.None
                  (case (array lux-object js-object)
                    (#.Some value)
                    (ex.return value)

                    #.None
                    ## (JSObject::isFunction [] js-object)
                    ## js-object

                    ## else
                    (ex.throw Unknown-Kind-Of-JS-Object (Object::toString [] (:! Object js-object)))))))
            
            ## else
            (ex.throw Unknown-Kind-Of-JS-Object (Object::toString [] (:! Object js-object))))))

(def: #export (eval code)
  (-> Expression (Meta Any))
  (function (_ compiler)
    (case (|> compiler
              (get@ #.host)
              (:! //.Host)
              (get@ #//.interpreter)
              (ScriptEngine::eval [code]))
      (#e.Error error)
      ((lang.throw Cannot-Evaluate error) compiler)
      
      (#e.Success output)
      (case output
        #.None
        (#e.Success [compiler []])

        (#.Some output)
        (case (lux-object output)
          (#e.Success parsed-output)
          (#e.Success [compiler parsed-output])

          (#e.Error error)
          ((lang.throw Cannot-Evaluate error) compiler))))))