1
0
mirror of https://codeberg.org/setop/elm-scripting synced 2025-11-08 21:49:57 +00:00

more exploration

This commit is contained in:
2025-09-27 18:10:33 +02:00
parent 7025bc1104
commit 9566f305b5
6 changed files with 22 additions and 6 deletions

View File

@@ -8,13 +8,14 @@ A tool to generate HTML code from Elm source in the terminal using [QuickJS](htt
* do not patch elm compiler output * do not patch elm compiler output
* provide acceptable performances (500ms for a big script) * provide acceptable performances (500ms for a big script)
# Design # Design
QuickJS (Qjs) is a [JavaScript runtime](https://en.wikipedia.org/wiki/List_of_JavaScript_engines), similar to V8 or SpiderMonkey, but lighter and faster. QuickJS (Qjs) is a [JavaScript runtime](https://en.wikipedia.org/wiki/List_of_JavaScript_engines), similar to V8 or SpiderMonkey, but lighter and faster.
As any runtime, Qjs can interpret JavaScript code, but it is not a web browser. It has no concept of an HTML document. As any runtime, Qjs can interpret JavaScript code, but it is not a web browser. It has no concept of an HTML document.
To bridge this gap, we add a minimal [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model) implementation. To bridge this gap, we add a minimal [DOM](https://dom.spec.whatwg.org/) implementation.
Next, we concatenate this with the Elm JavaScript output and an app launcher snippet, then ask Qjs to interpret all of it. Next, we concatenate this with the Elm JavaScript output and an app launcher snippet, then ask Qjs to interpret all of it.
@@ -24,6 +25,8 @@ Next, we concatenate this with the Elm JavaScript output and an app launcher sni
* Hence, no [TEA](https://guide.elm-lang.org/architecture/); the `main` function must return a static view * Hence, no [TEA](https://guide.elm-lang.org/architecture/); the `main` function must return a static view
* Hence, no Time, no Random, no Json Encoder/Decoder (!), no Http * Hence, no Time, no Random, no Json Encoder/Decoder (!), no Http
* Nodes can only have one parent (this should always be the case) * Nodes can only have one parent (this should always be the case)
* Does not scale well : creating thousands of Nodes consumes a [lot of RAM](#Performances)
# Usage # Usage
@@ -51,6 +54,7 @@ Hello World!
Acceptable for small scripts : 250ms on a modest x86_64 CPU and 64MB RAM for a 500 records into a table ; but is does not scale well as everything is loaded before processing ; no streaming contrary to the usual Unix way. Acceptable for small scripts : 250ms on a modest x86_64 CPU and 64MB RAM for a 500 records into a table ; but is does not scale well as everything is loaded before processing ; no streaming contrary to the usual Unix way.
Generate 500k "li" loop took 17s and 900MB RAM.
# Prior Work # Prior Work

View File

@@ -6,8 +6,16 @@
- [ ] from files (use case : SSG) - [ ] from files (use case : SSG)
- [ ] from http (use case : spider) - [ ] from http (use case : spider)
- implements some missing Web API:
- [Events](https://dom.spec.whatwg.org/#events)
- [Fetch](https://fetch.spec.whatwg.org/)(or [XMLHttpRequest](https://xhr.spec.whatwg.org/))
- [Promise](https://webidl.spec.whatwg.org/#a-new-promise) (if needed by the above)
- [ ] find a way to create a standalone executable (maybe with a combination of Google Closure Compiler and qjsc) - [ ] find a way to create a standalone executable (maybe with a combination of Google Closure Compiler and qjsc)
- [ ] find a way to stream instead of having the whole document in memory (output as soon as a node is created ? a child is added ?)
# Minor # Minor
- [ ] support for Elm debug mode (qjs does not implement `console.warn`, only `console.log`) - [ ] support for Elm debug mode (qjs does not implement `console.warn`, only `console.log`)

View File

@@ -1,12 +1,14 @@
#!/bin/sh -eu #!/bin/sh -eu
CMDD=$(dirname $(realpath 0))
w1="$(mktemp out_$$_1_XXXX.js)" w1="$(mktemp out_$$_1_XXXX.js)"
elm make --optimize --output=${w1} $1 1>&2 elm make --optimize --output=${w1} $1 1>&2
w2="$(mktemp out_$$_XXXX.js)" w2="$(mktemp out_$$_XXXX.js)"
cat dom.js ${w1} launch.js > ${w2} cat ${CMDD}/dom.js ${CMDD}/preelm.js ${w1} ${CMDD}/postelm.js > ${w2}
rm ${w1} rm ${w1}

4
dom.js
View File

@@ -52,7 +52,3 @@ document.getElementById = (_id) => { return target}
document.createElement = (tag) => new Node(null, tag); document.createElement = (tag) => new Node(null, tag);
document.createTextNode = (text) => { t = new Node(null, "#text" ); t.text = text; return t } document.createTextNode = (text) => { t = new Node(null, "#text" ); t.text = text; return t }
try {
// here will come the Elm app code

6
preelm.js Normal file
View File

@@ -0,0 +1,6 @@
try {
// here will come the Elm app code