From 7abf2d0ac55c229a8793bbff31f132596ffcb275 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 11 Jun 2022 01:53:31 -0400 Subject: Added extension to JS interop for writing object literals. --- stdlib/source/library/lux/ffi.lux | 85 ++++++++++++++++++++++++++++++++------- stdlib/source/test/lux/ffi.js.lux | 15 +++++++ 2 files changed, 85 insertions(+), 15 deletions(-) diff --git a/stdlib/source/library/lux/ffi.lux b/stdlib/source/library/lux/ffi.lux index 510918308..25d65c691 100644 --- a/stdlib/source/library/lux/ffi.lux +++ b/stdlib/source/library/lux/ffi.lux @@ -3,7 +3,7 @@ [lux (.except Symbol Alias Global global function type_of undefined) ["[0]" meta] [abstract - [monad (.only do)]] + ["[0]" monad (.only do)]] [control ["[0]" io] ["[0]" maybe (.open: "[1]#[0]" functor)] @@ -688,10 +688,14 @@ (in (list (code.text (%.format module " " (%.nat unique_id)))))))) (with_expansions [ (..extension_name) - (..extension_name)] - (these (template: (extension_call ) + (..extension_name) + (..extension_name)] + (these (template: (extension_analysis ) [{5 #1 [ ]}]) + (template: (text_analysis ) + [{0 #0 {5 #1 }}]) + (template: (analysis ) [("lux def analysis" (.function (_ name phase archive inputs) @@ -710,44 +714,95 @@ (.function (_ name phase archive inputs) (.function (_ state) (let [ [name phase archive state]] - (`` (case inputs - (pattern (list (~~ (template.spliced )))) - + (case inputs + (pattern ) + - _ - {try.#Failure (%.format "Invalid inputs for extension: " (%.text name))}))))))]) + _ + {try.#Failure (%.format "Invalid inputs for extension: " (%.text name))})))))]) (analysis [name phase archive state] .end _ - {try.#Success [state (extension_call name (list))]}) + {try.#Success [state (extension_analysis name (list))]}) (generation [name phase archive state] - [] + (list) {try.#Success [state js.undefined]}) (template: .public (undefined) - [(.is .Any ())]) + [(.is ..Undefined ())]) (analysis [name phase archive state] .any it (do try.monad - [[state it] (phase archive it state)] - (in [state (extension_call name (list it))]))) + [[state it] (phase archive (` (.is .Any (~ it))) state)] + (in [state (extension_analysis name (list it))]))) (generation [name phase archive state] - [it] + (list it) (do try.monad [[state it] (phase archive it state)] (in [state (js.= js.undefined it)]))) (template: .public (undefined? ) - [(.as .Bit (.is .Any ( (.is .Any ))))]) + [(.as .Bit (.is .Any ( )))]) + + (analysis + [name phase archive state] + (<>.some (<>.and .text .any)) + it + (do [! try.monad] + [[state output] (monad.mix ! (.function (_ [key value] [state output]) + (do ! + [[state value] (phase archive (` (.is .Any (~ value))) state)] + (in [state (partial_list value (text_analysis key) output)]))) + [state (list)] + it)] + (in [state (extension_analysis name (list.reversed output))]))) + + (template: (text_synthesis ) + [{0 #0 {2 #1 }}]) + + (def: (pairs it) + (All (_ a) (-> (List a) (List [a a]))) + (case it + (pattern (partial_list left right tail)) + (partial_list [left right] (pairs tail)) + + (pattern (list)) + (list) + + _ + (.undefined))) + + (generation + [name phase archive state] + (partial_list head_key head_value tail) + (do [! try.monad] + [[state output] (monad.mix ! + (.function (_ [key value] [state output]) + (case key + (pattern (text_synthesis key)) + (do try.monad + [[state value] (phase archive value state)] + (in [state (partial_list [key value] output)])) + + _ + (.undefined))) + [state (list)] + (pairs (partial_list head_key head_value tail)))] + (in [state (js.object (list.reversed output))]))) + + (def: .public object + (syntax (_ [it (<>.some .any)]) + (in (list (` (.as (..Object .Any) + ( (~+ it)))))))) ))) (these)) ) diff --git a/stdlib/source/test/lux/ffi.js.lux b/stdlib/source/test/lux/ffi.js.lux index 6fe42ec08..ade4d78a8 100644 --- a/stdlib/source/test/lux/ffi.js.lux +++ b/stdlib/source/test/lux/ffi.js.lux @@ -146,6 +146,21 @@ (_.coverage [/.undefined /.undefined?] (and (not (/.undefined? number)) (/.undefined? (/.undefined)))) + (_.coverage [/.object] + (let [it (/.object + "my_boolean" boolean + "my_number" number + "my_string" string + "my_function" function + "my_object" object + "my_undefined" (/.undefined))] + (and (same? boolean ("js object get" "my_boolean" it)) + (same? number ("js object get" "my_number" it)) + (same? string ("js object get" "my_string" it)) + (same? function ("js object get" "my_function" it)) + (same? object ("js object get" "my_object" it)) + (same? (/.undefined) ("js object get" "my_undefined" it)) + (/.undefined? ("js object get" "my_yolo" it))))) $/export.test ))))) -- cgit v1.2.3