aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/js/eval.jvm.lux
blob: bcf70bcaef9ae655b5b653305b7192b2d41e2218 (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
(.module:
  lux
  (lux (control ["ex" exception #+ exception:])
       (data [bit]
             ["e" error #+ Error]
             text/format
             (coll [array]))
       [host])
  (luxc [lang])
  [//])

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

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

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

(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 (nat-to-int (n/+ (|> high (:! Int) int-to-nat (bit.shift-left +32))
                             (|> low (:! Int) int-to-nat))))

    _
    #.None))

(def: (extend-array by input)
  (All [a] (-> Nat (Array a) (Array a)))
  (let [size (array.size input)]
    (|> (array.new (n/+ by size))
        (array.copy size +0 input +0))))

(def: (array element-parser js-object)
  (-> (-> Object (Error Top)) ScriptObjectMirror (Maybe (Array Object)))
  (if (JSObject::isArray [] js-object)
    (let [init-num-keys (int-to-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 nat-to-int %i)]
            (case (JSObject::getMember idx-key js-object)
              (#.Some member)
              (case (element-parser member)
                (#e.Success parsed-member)
                (recur num-keys
                       (n/inc idx)
                       (array.write idx (:! Object parsed-member) output))

                (#e.Error error)
                #.None)
              
              #.None
              (recur (n/inc num-keys)
                     (n/inc idx)
                     (extend-array +1 output))))
          (#.Some output))))
    #.None))

(exception: #export Unknown-Kind-Of-JS-Object)
(exception: #export Null-Has-No-Lux-Representation)

(def: (lux-object js-object)
  (-> Object (Error Top))
  (`` (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 (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))))))

(exception: #export Cannot-Evaluate)

(def: #export (eval code)
  (-> //.Expression (Meta Top))
  (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))))))