From f21ca0dc1956a47b8f7ac6e0ea67d7d246c55280 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 6 Jun 2022 01:17:43 -0400 Subject: Added extensions to JS interop for working with "undefined" values. --- stdlib/source/library/lux/ffi.lux | 83 ++++++++++++++++++++-- stdlib/source/library/lux/ffi.old.lux | 2 +- .../library/lux/tool/compiler/default/init.lux | 2 +- .../compiler/language/lux/phase/generation/js.lux | 2 +- .../language/lux/phase/generation/js/runtime.lux | 2 +- stdlib/source/library/lux/world/file.lux | 2 +- stdlib/source/program/compositor.lux | 2 +- stdlib/source/test/lux/ffi.js.lux | 3 + 8 files changed, 87 insertions(+), 11 deletions(-) (limited to 'stdlib') diff --git a/stdlib/source/library/lux/ffi.lux b/stdlib/source/library/lux/ffi.lux index 46475ae7d..1ff34a943 100644 --- a/stdlib/source/library/lux/ffi.lux +++ b/stdlib/source/library/lux/ffi.lux @@ -1,13 +1,13 @@ (.using [library - [lux (.except Symbol Alias Global global function type_of) - ["@" target] + [lux (.except Symbol Alias Global global function type_of undefined) ["[0]" meta] [abstract [monad (.only do)]] [control ["[0]" io] ["[0]" maybe (.open: "[1]#[0]" functor)] + ["[0]" try] ["<>" parser (.open: "[1]#[0]" monad) ["<[0]>" code (.only Parser)]]] [data @@ -20,6 +20,8 @@ [syntax (.only syntax:)] ["[0]" code] ["[0]" template]] + ["@" target (.only) + ["[0]" js]] [type [primitive (.except)]]]]) @@ -334,7 +336,7 @@ {#Procedure (Named Procedure)} {#Global Global})) - (def: import + (def: importP (Parser Import) (all <>.or ..class @@ -558,7 +560,7 @@ (virtual_method_definition class alias namespace (the #member it)))) (syntax: .public (import [host_module (<>.maybe .text) - it ..import]) + it ..importP]) (let [host_module_import! (is (List Code) (case host_module {.#Some host_module} @@ -670,6 +672,77 @@ (as Text) (text#= "[object process]"))) (maybe.else false))) - ) + + ... These extensions must be defined this way because importing any of the modules + ... normally used when writing extensions would introduce a circular dependency + ... because the Archive type depends on Binary, and that module depends on this ffi module. + (syntax: (extension_name []) + (do meta.monad + [module meta.current_module_name + unique_id meta.seed] + (in (list (code.text (%.format module " " (%.nat unique_id))))))) + + (with_expansions [ (..extension_name) + (..extension_name)] + (these (template: (extension_call ) + [{5 #1 [ ]}]) + + (template: (analysis ) + [("lux def analysis" + (.function (_ name phase archive inputs) + (.function (_ state) + (let [ [name phase archive state]] + (case (.result inputs) + {try.#Failure error} + {try.#Failure (%.format "Invalid inputs for extension: " (%.text name) + text.\n error)} + + {try.#Success } + )))))]) + + (template: (generation ) + [("lux def generation" + (.function (_ name phase archive inputs) + (.function (_ state) + (let [ [name phase archive state]] + (`` (case inputs + (pattern (list (~~ (template.spliced )))) + + + _ + {try.#Failure (%.format "Invalid inputs for extension: " (%.text name))}))))))]) + + (analysis + [name phase archive state] + .end + _ + {try.#Success [state (extension_call name (list))]}) + + (generation + [name phase archive state] + [] + {try.#Success [state js.undefined]}) + + (template: .public (undefined) + [(.is .Any ())]) + + (analysis + [name phase archive state] + .any + it + (do try.monad + [[state it] (phase archive it state)] + (in [state (extension_call name (list it))]))) + + (generation + [name phase archive state] + [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 ))))]) + ))) (these)) ) diff --git a/stdlib/source/library/lux/ffi.old.lux b/stdlib/source/library/lux/ffi.old.lux index 1d2603f79..951d58f18 100644 --- a/stdlib/source/library/lux/ffi.old.lux +++ b/stdlib/source/library/lux/ffi.old.lux @@ -10,7 +10,7 @@ ["[0]" io] ["[0]" maybe] ["[0]" try (.only Try)] - ["<>" parser + ["<>" parser (.only) ["<[0]>" code (.only Parser)]]] [data ["[0]" product] diff --git a/stdlib/source/library/lux/tool/compiler/default/init.lux b/stdlib/source/library/lux/tool/compiler/default/init.lux index 6ddca02cf..c253743dd 100644 --- a/stdlib/source/library/lux/tool/compiler/default/init.lux +++ b/stdlib/source/library/lux/tool/compiler/default/init.lux @@ -49,7 +49,7 @@ [meta ["[0]" archive (.only Archive) ["[0]" registry (.only Registry)] - ["[0]" module + ["[0]" module (.only) ["[0]" descriptor] ["[0]" document]]]]]]) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js.lux index 02540a965..fa1cabf00 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js.lux @@ -20,7 +20,7 @@ ["/[1]" // ["[1][0]" reference] ["/[1]" // - ["[1][0]" extension + ["[1][0]" extension (.only) [generation [js ["[1]/[0]" common]]]] diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux index 532f3c826..c8f77a38e 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux @@ -1,6 +1,6 @@ (.using [library - [lux (.except i64) + [lux (.except i64 left right) ["[0]" meta] [abstract ["[0]" monad (.only do)]] diff --git a/stdlib/source/library/lux/world/file.lux b/stdlib/source/library/lux/world/file.lux index cfbc7ce2c..e88322775 100644 --- a/stdlib/source/library/lux/world/file.lux +++ b/stdlib/source/library/lux/world/file.lux @@ -24,7 +24,7 @@ ["[0]" array (.only Array)] ["[0]" list (.open: "[1]#[0]" functor)] ["[0]" dictionary (.only Dictionary)]]] - ["[0]" ffi + ["[0]" ffi (.only) (~~ (.for "JavaScript" (~~ (.these ["[0]" node_js])) "{old}" (~~ (.these ["node_js" //control/thread])) (~~ (.these))))] diff --git a/stdlib/source/program/compositor.lux b/stdlib/source/program/compositor.lux index 986e1f161..cd6a49dbb 100644 --- a/stdlib/source/program/compositor.lux +++ b/stdlib/source/program/compositor.lux @@ -36,7 +36,7 @@ ["[0]" syntax] ["[0]" generation] ["[0]" directive] - ["[0]" analysis + ["[0]" analysis (.only) [macro (.only Expander)]] [phase [extension (.only Extender)]]]] diff --git a/stdlib/source/test/lux/ffi.js.lux b/stdlib/source/test/lux/ffi.js.lux index fa58458ae..6fe42ec08 100644 --- a/stdlib/source/test/lux/ffi.js.lux +++ b/stdlib/source/test/lux/ffi.js.lux @@ -143,6 +143,9 @@ (|> (TextDecoder::new encoding) (TextDecoder::decode binary))) )))) + (_.coverage [/.undefined /.undefined?] + (and (not (/.undefined? number)) + (/.undefined? (/.undefined)))) $/export.test ))))) -- cgit v1.2.3