Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <hivert.is.coming@gmail.com>
  • Loading branch information
ghivert committed Apr 17, 2024
0 parents commit 724eaf4
Show file tree
Hide file tree
Showing 20 changed files with 1,519 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: test

on:
push:
branches:
- master
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: erlef/setup-beam@v1
with:
otp-version: "26.0.2"
gleam-version: "1.0.0"
rebar3-version: "3"
# elixir-version: "1.15.4"
- run: gleam deps download
- run: gleam test
- run: gleam format --check src test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
7 changes: 7 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 Guillaume Hivert

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Tardis

[![Package Version](https://img.shields.io/hexpm/v/tardis)](https://hex.pm/packages/tardis)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/tardis/)

```sh
gleam add tardis
```
```gleam
import tardis
pub fn main() {
// TODO: An example of the project in use
}
```

Further documentation can be found at <https://hexdocs.pm/tardis>.

## Development

```sh
gleam run # Run the project
gleam test # Run the tests
gleam shell # Run an Erlang shell
```
20 changes: 20 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.
name = "tardis"
target = "javascript"
version = "0.1.0"

description = "Time traveller debugger, tailor-made for Lustre"
licences = ["MIT"]
# links = [{title = "Website", href = "https://gleam.run"}]
internal_modules = ["tardis/internals", "tardis/internals/*"]
repository = {type = "github", user = "ghivert", repo = "tardis"}

[dependencies]
gleam_stdlib = ">= 0.36.0 and < 1.0.0"
lustre = ">= 4.1.8 and < 5.0.0"
plinth = ">= 0.2.0 and < 1.0.0"
sketch = ">= 2.0.0 and < 3.0.0"

[dev-dependencies]
gleeunit = ">= 1.1.2 and < 2.0.0"
22 changes: 22 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_javascript", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "14D5B7E1A70681E0776BF0A0357F575B822167960C844D3D3FA114D3A75F05A8" },
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
{ name = "lustre", version = "4.1.8", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_json", "gleam_otp", "gleam_stdlib"], otp_app = "lustre", source = "hex", outer_checksum = "55C3D26AEE6BF6B859927616B1F5BC25841926E4EA6A2A72ECC90D6769560F04" },
{ name = "plinth", version = "0.2.0", build_tools = ["gleam"], requirements = ["gleam_javascript", "gleam_json", "gleam_stdlib"], otp_app = "plinth", source = "hex", outer_checksum = "83211E672D83F3CE14681D0ECD3AD883EE7588E423E7C9DDDB460014AD60AC24" },
{ name = "sketch", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "lustre", "plinth"], otp_app = "sketch", source = "hex", outer_checksum = "2FE8458F79F2A92267A70F9230151A92038554C19C6948B01B7EAAE0B888B9BB" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
]

[requirements]
gleam_stdlib = { version = ">= 0.36.0 and < 1.0.0" }
gleeunit = { version = ">= 1.1.2 and < 2.0.0"}
lustre = { version = ">= 4.1.8 and < 5.0.0" }
plinth = { version = ">= 0.2.0 and < 1.0.0" }
sketch = { version = ">= 2.0.0 and < 3.0.0" }
194 changes: 194 additions & 0 deletions src/tardis.ffi.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import Dict from '../gleam_stdlib/dict.mjs'
import { None, Some } from '../gleam_stdlib/gleam/option.mjs'
import { BitArray, CustomType, List, UtfCodepoint } from './gleam.mjs'
import {
DataBitArray,
DataBool,
DataConstant,
DataCustomType,
DataDate,
DataDict,
DataFunction,
DataList,
DataNil,
DataNumber,
DataObject,
DataRegex,
DataSet,
DataString,
DataTuple,
DataUtfCodepoint,
} from './tardis/internals/data.mjs'

export function toString(data) {
return inspect(data)
}

export function inspect(v) {
const t = typeof v
if (v === true) return new DataBool('True')
if (v === false) return new DataBool('False')
if (v === null) return new DataConstant('//js(null)')
if (v === undefined) return new DataNil()
if (t === 'string') return new DataString(JSON.stringify(v))
if (t === 'bigint' || t === 'number') return new DataNumber(v.toString())
if (Array.isArray(v)) return new DataTuple(List.fromArray(v.map(inspect)))
if (v instanceof List) return inspectList(v)
if (v instanceof UtfCodepoint) return inspectUtfCodepoint(v)
if (v instanceof BitArray) return inspectBitArray(v)
if (v instanceof CustomType) return inspectCustomType(v)
if (v instanceof Dict) return inspectDict(v)
if (v instanceof Set) return DataSet(List.fromArray([...v].map(inspect)))
if (v instanceof RegExp) return new DataRegex(`//js(${v})`)
if (v instanceof Date) return new DataDate(`//js(Date("${v.toISOString()}"))`)
if (v instanceof Function) {
const args = []
for (const i of Array(v.length).keys()) args.push(String.fromCharCode(i + 97))
return new DataFunction(`//fn(${args.join(', ')}) { ... }`)
}
return inspectObject(v)
}

function inspectDict(map) {
const data = []
map.forEach((value, key) => data.push([inspect(key), inspect(value)]))
return new DataDict(List.fromArray(data))
}

function inspectObject(v) {
const name = Object.getPrototypeOf(v)?.constructor?.name || 'Object'
const props = []
for (const k of Object.keys(v)) {
props.push([inspect(k), inspect(v[k])])
}
const head = name === 'Object' ? '' : name + ' '
return new DataObject(head, List.fromArray(props))
}

function inspectCustomType(record) {
const props = List.fromArray(
Object.keys(record).map(label => {
const value = inspect(record[label])
return isNaN(parseInt(label)) ? [new Some(label + ': '), value] : [new None(), value]
})
)
return new DataCustomType(record.constructor.name, props)
}

export function inspectList(list) {
return new DataList(List.fromArray(list.toArray().map(inspect)))
}

export function inspectBitArray(bits) {
return new DataBitArray(`<<${Array.from(bits.buffer).join(', ')}>>`)
}

export function inspectUtfCodepoint(codepoint) {
return new DataUtfCodepoint(`//utfcodepoint(${String.fromCodePoint(codepoint.value)})`)
}

export function stringify(v) {
const t = typeof v
if (v === true) return 'True'
if (v === false) return 'False'
if (v === null) return '//js(null)'
if (v === undefined) return 'Nil'
if (t === 'string') return JSON.stringify(v)
if (t === 'bigint' || t === 'number') return v.toString()
if (Array.isArray(v)) return `#(${v.map(stringify).join(', ')})`
if (v instanceof List) return stringifyList(v)
if (v instanceof UtfCodepoint) return stringifyUtfCodepoint(v)
if (v instanceof BitArray) return stringifyBitArray(v)
if (v instanceof CustomType) return stringifyCustomType(v)
if (v instanceof Dict) return stringifyDict(v)
if (v instanceof Set) return `//js(Set(${[...v].map(stringify).join(', ')}))`
if (v instanceof RegExp) return `//js(${v})`
if (v instanceof Date) return `//js(Date("${v.toISOString()}"))`
if (v instanceof Function) {
const args = []
for (const i of Array(v.length).keys()) args.push(String.fromCharCode(i + 97))
return `//fn(${args.join(', ')}) { ... }`
}
return stringifyObject(v)
}

function stringifyDict(map) {
let body = 'dict.from_list(['
let first = true
map.forEach((value, key) => {
if (!first) body = body + ', '
body = body + '#(' + stringify(key) + ', ' + stringify(value) + ')'
first = false
})
return body + '])'
}

function stringifyObject(v) {
const name = Object.getPrototypeOf(v)?.constructor?.name || 'Object'
const props = []
for (const k of Object.keys(v)) {
props.push(`${stringify(k)}: ${stringify(v[k])}`)
}
const body = props.length ? ' ' + props.join(', ') + ' ' : ''
const head = name === 'Object' ? '' : name + ' '
return `//js(${head}{${body}})`
}

function stringifyCustomType(record) {
const props = Object.keys(record)
.map(label => {
const value = stringify(record[label])
return isNaN(parseInt(label)) ? `${label}: ${value}` : value
})
.join(', ')
return props ? `${record.constructor.name}(${props})` : record.constructor.name
}

export function stringifyList(list) {
return `[${list.toArray().map(stringify).join(', ')}]`
}

export function stringifyBitArray(bits) {
return `<<${Array.from(bits.buffer).join(', ')}>>`
}

export function stringifyUtfCodepoint(codepoint) {
return `//utfcodepoint(${String.fromCodePoint(codepoint.value)})`
}

export function base16_encode(bit_array) {
let result = ''
for (const byte of bit_array.buffer) {
result += byte.toString(16).padStart(2, '0').toUpperCase()
}
return result
}

export function base16_decode(string) {
const bytes = new Uint8Array(string.length / 2)
for (let i = 0; i < string.length; i += 2) {
const a = parseInt(string[i], 16)
const b = parseInt(string[i + 1], 16)
if (isNaN(a) || isNaN(b)) return new Error(Nil)
bytes[i / 2] = a * 16 + b
}
return new Ok(new BitArray(bytes))
}

export function isDarkTheme() {
const matches = matchMedia('(prefers-color-scheme: dark)')
return matches.matches
}

export function addCustomStyles(content) {
const stylesheet = new CSSStyleSheet()
stylesheet.replace(content)
document.adoptedStyleSheets.push(stylesheet)
}

export function updateLustre(application, initMapper, updateMapper) {
return application.withFields({
update: updateMapper(application.update),
init: initMapper(application.init)
})
}
Loading

0 comments on commit 724eaf4

Please sign in to comment.