aboutsummaryrefslogtreecommitdiff
path: root/stdlib
diff options
context:
space:
mode:
authorEduardo Julian2017-03-15 00:04:09 -0400
committerEduardo Julian2017-03-15 00:04:09 -0400
commit6f554dc5a4172cd2afd7bde30b5edcaf0266f63d (patch)
tree45493898fce20ab24243ab2a6373a79907b2bfb1 /stdlib
parent011547aae8e4664ecd63c8ebcefada7f8d9d940d (diff)
- Implemented custom JS host procedures.
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/host.js.lux83
-rw-r--r--stdlib/source/lux/host.jvm.lux (renamed from stdlib/source/lux/host.lux)0
-rw-r--r--stdlib/test/test/lux/host.js.lux32
-rw-r--r--stdlib/test/test/lux/host.jvm.lux (renamed from stdlib/test/test/lux/host.lux)0
4 files changed, 115 insertions, 0 deletions
diff --git a/stdlib/source/lux/host.js.lux b/stdlib/source/lux/host.js.lux
new file mode 100644
index 000000000..f935dc8d6
--- /dev/null
+++ b/stdlib/source/lux/host.js.lux
@@ -0,0 +1,83 @@
+(;module:
+ lux
+ (lux (control monad)
+ (data (coll [list #* "L/" Fold<List>]))
+ [compiler #+ with-gensyms]
+ (macro [ast]
+ ["s" syntax #+ syntax: Syntax])
+ ))
+
+(do-template [<name> <type>]
+ [(type: #export <name> (#;HostT <type> #;Nil))]
+
+ [Object "object"]
+ [Function "function"]
+ [Symbol "symbol"]
+ [Undefined "undefined"]
+ )
+
+(do-template [<name> <type>]
+ [(type: #export <name> <type>)]
+
+ [String Text]
+ [Number Real]
+ [Boolean Bool]
+ )
+
+## [Syntax]
+(syntax: #export (set! field-name field-value object)
+ {#;doc (doc "A way to set fields from objects."
+ (set! "foo" 1234 some-object))}
+ (wrap (list (` (;_lux_proc ["js" "set-field"] [(~ object) (~ field-name) (~ field-value)])))))
+
+(syntax: #export (delete! field-name object)
+ {#;doc (doc "A way to delete fields from objects."
+ (delete! "foo" some-object))}
+ (wrap (list (` (;_lux_proc ["js" "delete-field"] [(~ object) (~ field-name)])))))
+
+(syntax: #export (get field-name type object)
+ {#;doc (doc "A way to get fields from objects."
+ (get "ceil" (ref "Math"))
+ (get "ceil" (-> Real Real) (ref "Math")))}
+ (wrap (list (` (:! (~ type)
+ (;_lux_proc ["js" "get-field"] [(~ object) (~ field-name)]))))))
+
+(syntax: #export (object [kvs (s;some (s;seq s;any s;any))])
+ {#;doc (doc "A way to create JavaScript objects."
+ (object)
+ (object "foo" foo "bar" (inc bar)))}
+ (wrap (list (L/fold (lambda [[k v] object]
+ (` (set! (~ k) (~ v) (~ object))))
+ (` (;_lux_proc ["js" "object"] []))
+ kvs))))
+
+(syntax: #export (ref [name s;text] [type (s;opt s;any)])
+ {#;doc (doc "A way to refer to JavaScript variables."
+ (ref "document")
+ (ref "Math.ceil" (-> Real Real)))}
+ (wrap (list (` (:! (~ (default (' ;;Object) type))
+ (;_lux_proc ["js" "ref"] [(~ (ast;text name))]))))))
+
+(do-template [<name> <proc> <doc>]
+ [(syntax: #export (<name>)
+ {#;doc (doc <doc>
+ (<name>))}
+ (wrap (list (` (;_lux_proc ["js" <proc>] [])))))]
+
+ [null "null" "Null object reference."]
+ [undef "undefined" "Undefined."]
+ )
+
+(syntax: #export (call! [shape (s;alt ($_ s;seq s;any (s;tuple (s;some s;any)) (s;opt s;any))
+ ($_ s;seq s;any s;text (s;tuple (s;some s;any)) (s;opt s;any)))])
+ {#;doc (doc "A way to call JavaScript functions and methods."
+ (call! (ref "Math.ceil") [123.45])
+ (call! (ref "Math") "ceil" [123.45]))}
+ (case shape
+ (#;Left [function args ?type])
+ (wrap (list (` (:! (~ (default (' ;;Object) ?type))
+ (;_lux_proc ["js" "call"] [(~ function) (~@ args)])))))
+
+ (#;Right [object field args ?type])
+ (wrap (list (` (:! (~ (default (' ;;Object) ?type))
+ (;_lux_proc ["js" "object-call"] [(~ object) (~ (ast;text field)) (~@ args)])))))))
diff --git a/stdlib/source/lux/host.lux b/stdlib/source/lux/host.jvm.lux
index 41d567165..41d567165 100644
--- a/stdlib/source/lux/host.lux
+++ b/stdlib/source/lux/host.jvm.lux
diff --git a/stdlib/test/test/lux/host.js.lux b/stdlib/test/test/lux/host.js.lux
new file mode 100644
index 000000000..4c2b55485
--- /dev/null
+++ b/stdlib/test/test/lux/host.js.lux
@@ -0,0 +1,32 @@
+(;module:
+ lux
+ (lux [io]
+ (control monad)
+ (data text/format)
+ ["&" host]
+ ["R" random]
+ pipe)
+ lux/test)
+
+(test: "JavaScript operations"
+ ($_ seq
+ (assert "Null equals itself."
+ (is (&;null) (&;null)))
+
+ (assert "Undefined equals itself."
+ (is (&;undef) (&;undef)))
+
+ (assert "Can reference JavaScript objects."
+ (is (&;ref "Math") (&;ref "Math")))
+
+ (assert "Can create objects and access their fields."
+ (|> (&;object "foo" "BAR")
+ (&;get "foo" Text)
+ (is "BAR")))
+
+ (assert "Can call JavaScript functions"
+ (and (is 124.0
+ (&;call! (&;ref "Math.ceil" &;Function) [123.45] Real))
+ (is 124.0
+ (&;call! (&;ref "Math") "ceil" [123.45] Real))))
+ ))
diff --git a/stdlib/test/test/lux/host.lux b/stdlib/test/test/lux/host.jvm.lux
index 54e6cf4b9..54e6cf4b9 100644
--- a/stdlib/test/test/lux/host.lux
+++ b/stdlib/test/test/lux/host.jvm.lux