Title: [fork-ffi] enums: toCharCode — toCharCode is implemented as `c:byte()`, which returns the value of… · Issue #79 · purescript-lua/purescript-lua · GitHub
Open Graph Title: [fork-ffi] enums: toCharCode — toCharCode is implemented as `c:byte()`, which returns the value of… · Issue #79 · purescript-lua/purescript-lua
X Title: [fork-ffi] enums: toCharCode — toCharCode is implemented as `c:byte()`, which returns the value of… · Issue #79 · purescript-lua/purescript-lua
Description: Package: purescript-lua-enums File: src/Data/Enum.lua Function: toCharCode Class: semantics Severity: high toCharCode is implemented as c:byte(), which returns the value of the FIRST BYTE (0..255) of the argument's UTF-8 encoding, not th...
Open Graph Description: Package: purescript-lua-enums File: src/Data/Enum.lua Function: toCharCode Class: semantics Severity: high toCharCode is implemented as c:byte(), which returns the value of the FIRST BYTE (0..255) ...
X Description: Package: purescript-lua-enums File: src/Data/Enum.lua Function: toCharCode Class: semantics Severity: high toCharCode is implemented as c:byte(), which returns the value of the FIRST BYTE (0..255) ...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/79
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[fork-ffi] enums: toCharCode — toCharCode is implemented as `c:byte()`, which returns the value of…","articleBody":"**Package:** purescript-lua-enums \n**File:** `src/Data/Enum.lua` \n**Function:** `toCharCode` \n**Class:** semantics **Severity:** high\n\ntoCharCode is implemented as `c:byte()`, which returns the value of the FIRST BYTE (0..255) of the argument's UTF-8 encoding, not the character's code point. The pslua compiler emits a PureScript Char literal as a Lua string holding the character's UTF-8 bytes (Lua.hs LiteralChar -> String of Text.singleton c, printed raw via dquotes(pretty t)). So any Char above U+007F arrives as a multi-byte UTF-8 string and c:byte() returns the leading byte. Confirmed on Lua 5.1: U+00E9 ('é', UTF-8 C3 A9) -> 195 instead of 233; U+FFFF (top, UTF-8 EF BF BF) -> 239 instead of 65535. JS FFI does `c.charCodeAt(0)`, returning the UTF-16 code unit (0..65535). This also breaks Data.Enum's `cardinality = toCharCode top - toCharCode bottom` (evaluates to 239-0 = 239 instead of 65535) and `fromEnum :: Char -> Int`. Correct only for the ASCII subrange 0..127.\n\n**Current (Lua):**\n```lua\ntoCharCode = (function(c) return c:byte() end)\n```\n\n**Expected:** JS: c.charCodeAt(0) returns the code point/UTF-16 code unit of the first character (0..65535). For 'A' -> 65, 'é'(U+00E9) -> 233, top(U+FFFF) -> 65535.\n\n**Proposed fix:**\n```lua\nDecode the first UTF-8 code point of the string instead of taking the first byte. E.g.:\n toCharCode = function(c)\n local b1 = c:byte(1)\n if b1 \u003c 0x80 then return b1 end\n if b1 \u003c 0xE0 then return (b1 - 0xC0) * 0x40 + (c:byte(2) - 0x80) end\n if b1 \u003c 0xF0 then return (b1 - 0xE0) * 0x1000 + (c:byte(2) - 0x80) * 0x40 + (c:byte(3) - 0x80) end\n return (b1 - 0xF0) * 0x40000 + (c:byte(2) - 0x80) * 0x1000 + (c:byte(3) - 0x80) * 0x40 + (c:byte(4) - 0x80)\n end\nVerified on Lua 5.1: 'A'->65, 'é'->233, top->65535.\n```\n\n_Found by the FFI audit; reproduced under Lua 5.1._","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-06-15T11:23:58Z","url":"https://github.com/purescript-lua/purescript-lua/issues/79"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d40f4f60-a4ab-375d-5fe6-6ecc8e0918d7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A5D2:1DB641:8A5EBB:B48099:6B00DB5A |
| html-safe-nonce | 2b7f29f5924c312ac7616fa6dc8e1417cad1a0d1415195164090e671612077bf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNUQyOjFEQjY0MTo4QTVFQkI6QjQ4MDk5OjZCMDBEQjVBIiwidmlzaXRvcl9pZCI6IjQ3NjM0MDYwODI1MjgyODUwNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 95628f0453e1d3adf879a92be813941e8ff8d8112dc9ab988532b8ff8ea31dc5 |
| hovercard-subject-tag | issue:4664607113 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/purescript-lua/purescript-lua/79/issue_layout |
| twitter:image | https://opengraph.githubassets.com/034f1eec0eb2e72591f4ce76be7c7a72d495f7ed54b509a3870cb852d2c07e01/purescript-lua/purescript-lua/issues/79 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/034f1eec0eb2e72591f4ce76be7c7a72d495f7ed54b509a3870cb852d2c07e01/purescript-lua/purescript-lua/issues/79 |
| og:image:alt | Package: purescript-lua-enums File: src/Data/Enum.lua Function: toCharCode Class: semantics Severity: high toCharCode is implemented as c:byte(), which returns the value of the FIRST BYTE (0..255) ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Unisay |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| turbo-cache-control | no-preview |
| go-import | github.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git |
| octolytics-dimension-user_id | 131581790 |
| octolytics-dimension-user_login | purescript-lua |
| octolytics-dimension-repository_id | 587200487 |
| octolytics-dimension-repository_nwo | purescript-lua/purescript-lua |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 587200487 |
| octolytics-dimension-repository_network_root_nwo | purescript-lua/purescript-lua |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width