-
Notifications
You must be signed in to change notification settings - Fork 0
Coln compiler as WebAssembly library #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7487df3
06060f0
bdd4617
f311429
8a1cb41
1298b7e
b48cdf5
3334bda
c5f2890
9eeabba
7be7fb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| -- SPDX-FileCopyrightText: 2026 Coln contributors | ||
| -- | ||
| -- SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| module Lib () where | ||
|
|
||
| import Coln.Backend.IR qualified as I | ||
| import Coln.Backend.Lower | ||
| import Coln.Core.Globals | ||
| import Coln.Diagnostics | ||
| import Coln.Frontend.Driver | ||
| import Control.Monad | ||
| import Data.Aeson.Text qualified as Aeson | ||
| import Data.Foldable | ||
| import Data.IORef | ||
| import Data.Map.Ordered qualified as OMap | ||
| import Data.Text (Text) | ||
| import Data.Text qualified as T | ||
| import Data.Text.Lazy qualified as TL | ||
| import Diagnostician | ||
| import Diagnostician.HTML (diagnosticToHtml) | ||
| import Foreign.StablePtr | ||
| import GHC.Wasm.Prim | ||
| import Lucid qualified | ||
| import Prettyprinter | ||
| import Prettyprinter.Render.Text qualified as Text | ||
|
|
||
| data CompileResult = CompileResult | ||
| { ir :: [I.FlatRealm] | ||
| , diagnostics :: [Diagnostic ColnCode] | ||
| } | ||
| foreign export javascript "freeCompileResult" freeStablePtr :: StablePtr CompileResult -> IO () | ||
|
|
||
| compile :: JSString -> IO (StablePtr CompileResult) | ||
| compile src = do | ||
| ref <- newIORef [] | ||
| globals <- topFromText (pureReporter ref) (newFile "<wasm>" $ textFromJSString src) | ||
| let ir = map (uncurry lowerRealm) $ OMap.assocs globals.realms | ||
| diagnostics <- reverse <$> readIORef ref | ||
| newStablePtr CompileResult{ir, diagnostics} | ||
| foreign export javascript "compile" compile :: JSString -> IO (StablePtr CompileResult) | ||
|
|
||
| getDiagnostics :: Bool -> StablePtr CompileResult -> IO JSVal | ||
| getDiagnostics asHtml = jsStringArray . map (textToJSString . TL.toStrict . render) . (.diagnostics) <=< deRefStablePtr | ||
| where | ||
| render = | ||
| if asHtml | ||
| then Lucid.renderText . diagnosticToHtml | ||
| else Text.renderLazy . layoutPretty defaultLayoutOptions . dpretty | ||
| foreign export javascript "getDiagnostics" getDiagnostics :: Bool -> StablePtr CompileResult -> IO JSVal | ||
|
|
||
| prettyIr :: StablePtr CompileResult -> IO JSVal | ||
| prettyIr = jsStringArray . map (textToJSString . T.show) . (.ir) <=< deRefStablePtr | ||
| foreign export javascript "prettyIr" prettyIr :: StablePtr CompileResult -> IO JSVal | ||
|
|
||
| irToJson :: StablePtr CompileResult -> IO JSString | ||
| irToJson = fmap (textToJSString . TL.toStrict . Aeson.encodeToLazyText . (.ir)) . deRefStablePtr | ||
| foreign export javascript "irToJson" irToJson :: StablePtr CompileResult -> IO JSString | ||
|
|
||
| textToJSString :: Text -> JSString | ||
| textToJSString = toJSString . T.unpack | ||
| textFromJSString :: JSString -> Text | ||
| textFromJSString = T.pack . fromJSString | ||
|
|
||
| foreign import javascript unsafe "[]" js_new_array :: IO JSVal | ||
| foreign import javascript unsafe "$1.push($2)" js_push_string :: JSVal -> JSString -> IO () | ||
| jsStringArray :: [JSString] -> IO JSVal | ||
| jsStringArray ss = do | ||
| arr <- js_new_array | ||
| for_ ss $ js_push_string arr | ||
| pure arr |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe best to call this
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. I'll save that until last in order to simplify the diffs. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| cabal-version: 3.4 | ||
| name: coln-wasm | ||
| version: 0.1 | ||
|
|
||
| executable coln-wasm | ||
|
olynch marked this conversation as resolved.
|
||
| main-is: Lib.hs | ||
| hs-source-dirs: . | ||
| default-language: GHC2024 | ||
| default-extensions: | ||
| NoFieldSelectors | ||
| OverloadedRecordDot | ||
| OverloadedStrings | ||
|
|
||
| build-depends: | ||
| aeson, | ||
| base, | ||
| coln-compiler, | ||
| containers, | ||
| diagnostician, | ||
| diagnostician-html, | ||
| fnotation, | ||
| ghc-experimental, | ||
| lucid, | ||
| ordered-containers, | ||
| prettyprinter, | ||
| text, | ||
|
|
||
| ghc-options: | ||
| -Wall | ||
| -no-hs-main | ||
| -optl-mexec-model=reactor | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that we're keeping GHC 9.12 for native builds, but using 9.14 for Wasm, since that's moving fast and improvements haven't been getting backported.
Fortunately, no code in Coln is broken by the update, and ecosystem support is mostly there now. Just the two packages needing bounds tweaked. One of them is mine and should now actually work, but I don't want to force everyone in to a
cabal update.