Current section
26 Versions
Jump to
Current section
26 Versions
Compare versions
335
files changed
+19967
additions
-5559
deletions
| @@ -0,0 +1,36 @@ | |
| 1 | + exported_locals_without_parens = [ |
| 2 | + # Hologram.Component |
| 3 | + prop: 2, |
| 4 | + prop: 3, |
| 5 | + |
| 6 | + # Hologram.Page |
| 7 | + layout: 1, |
| 8 | + layout: 2, |
| 9 | + param: 2, |
| 10 | + param: 3, |
| 11 | + route: 1 |
| 12 | + ] |
| 13 | + |
| 14 | + test_locals_without_parens = [ |
| 15 | + assert_error: 3, |
| 16 | + use_module_stub: 1 |
| 17 | + ] |
| 18 | + |
| 19 | + [ |
| 20 | + export: [locals_without_parens: exported_locals_without_parens], |
| 21 | + import_deps: [:phoenix], |
| 22 | + inputs: |
| 23 | + Enum.flat_map( |
| 24 | + [ |
| 25 | + "*.{ex,exs}", |
| 26 | + "{benchmarks,config,lib}/**/*.{ex,exs}", |
| 27 | + "test/elixir/**/*.{ex,exs}" |
| 28 | + ], |
| 29 | + &Path.wildcard(&1, match_dot: true) |
| 30 | + ) -- |
| 31 | + [ |
| 32 | + "test/elixir/support/fixtures/compiler/transformer/module_96.ex", |
| 33 | + "test/elixir/support/fixtures/compiler/transformer/module_101.ex" |
| 34 | + ], |
| 35 | + locals_without_parens: exported_locals_without_parens ++ test_locals_without_parens |
| 36 | + ] |
| @@ -0,0 +1,201 @@ | |
| 1 | + Apache License |
| 2 | + Version 2.0, January 2004 |
| 3 | + http://www.apache.org/licenses/ |
| 4 | + |
| 5 | + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION |
| 6 | + |
| 7 | + 1. Definitions. |
| 8 | + |
| 9 | + "License" shall mean the terms and conditions for use, reproduction, |
| 10 | + and distribution as defined by Sections 1 through 9 of this document. |
| 11 | + |
| 12 | + "Licensor" shall mean the copyright owner or entity authorized by |
| 13 | + the copyright owner that is granting the License. |
| 14 | + |
| 15 | + "Legal Entity" shall mean the union of the acting entity and all |
| 16 | + other entities that control, are controlled by, or are under common |
| 17 | + control with that entity. For the purposes of this definition, |
| 18 | + "control" means (i) the power, direct or indirect, to cause the |
| 19 | + direction or management of such entity, whether by contract or |
| 20 | + otherwise, or (ii) ownership of fifty percent (50%) or more of the |
| 21 | + outstanding shares, or (iii) beneficial ownership of such entity. |
| 22 | + |
| 23 | + "You" (or "Your") shall mean an individual or Legal Entity |
| 24 | + exercising permissions granted by this License. |
| 25 | + |
| 26 | + "Source" form shall mean the preferred form for making modifications, |
| 27 | + including but not limited to software source code, documentation |
| 28 | + source, and configuration files. |
| 29 | + |
| 30 | + "Object" form shall mean any form resulting from mechanical |
| 31 | + transformation or translation of a Source form, including but |
| 32 | + not limited to compiled object code, generated documentation, |
| 33 | + and conversions to other media types. |
| 34 | + |
| 35 | + "Work" shall mean the work of authorship, whether in Source or |
| 36 | + Object form, made available under the License, as indicated by a |
| 37 | + copyright notice that is included in or attached to the work |
| 38 | + (an example is provided in the Appendix below). |
| 39 | + |
| 40 | + "Derivative Works" shall mean any work, whether in Source or Object |
| 41 | + form, that is based on (or derived from) the Work and for which the |
| 42 | + editorial revisions, annotations, elaborations, or other modifications |
| 43 | + represent, as a whole, an original work of authorship. For the purposes |
| 44 | + of this License, Derivative Works shall not include works that remain |
| 45 | + separable from, or merely link (or bind by name) to the interfaces of, |
| 46 | + the Work and Derivative Works thereof. |
| 47 | + |
| 48 | + "Contribution" shall mean any work of authorship, including |
| 49 | + the original version of the Work and any modifications or additions |
| 50 | + to that Work or Derivative Works thereof, that is intentionally |
| 51 | + submitted to Licensor for inclusion in the Work by the copyright owner |
| 52 | + or by an individual or Legal Entity authorized to submit on behalf of |
| 53 | + the copyright owner. For the purposes of this definition, "submitted" |
| 54 | + means any form of electronic, verbal, or written communication sent |
| 55 | + to the Licensor or its representatives, including but not limited to |
| 56 | + communication on electronic mailing lists, source code control systems, |
| 57 | + and issue tracking systems that are managed by, or on behalf of, the |
| 58 | + Licensor for the purpose of discussing and improving the Work, but |
| 59 | + excluding communication that is conspicuously marked or otherwise |
| 60 | + designated in writing by the copyright owner as "Not a Contribution." |
| 61 | + |
| 62 | + "Contributor" shall mean Licensor and any individual or Legal Entity |
| 63 | + on behalf of whom a Contribution has been received by Licensor and |
| 64 | + subsequently incorporated within the Work. |
| 65 | + |
| 66 | + 2. Grant of Copyright License. Subject to the terms and conditions of |
| 67 | + this License, each Contributor hereby grants to You a perpetual, |
| 68 | + worldwide, non-exclusive, no-charge, royalty-free, irrevocable |
| 69 | + copyright license to reproduce, prepare Derivative Works of, |
| 70 | + publicly display, publicly perform, sublicense, and distribute the |
| 71 | + Work and such Derivative Works in Source or Object form. |
| 72 | + |
| 73 | + 3. Grant of Patent License. Subject to the terms and conditions of |
| 74 | + this License, each Contributor hereby grants to You a perpetual, |
| 75 | + worldwide, non-exclusive, no-charge, royalty-free, irrevocable |
| 76 | + (except as stated in this section) patent license to make, have made, |
| 77 | + use, offer to sell, sell, import, and otherwise transfer the Work, |
| 78 | + where such license applies only to those patent claims licensable |
| 79 | + by such Contributor that are necessarily infringed by their |
| 80 | + Contribution(s) alone or by combination of their Contribution(s) |
| 81 | + with the Work to which such Contribution(s) was submitted. If You |
| 82 | + institute patent litigation against any entity (including a |
| 83 | + cross-claim or counterclaim in a lawsuit) alleging that the Work |
| 84 | + or a Contribution incorporated within the Work constitutes direct |
| 85 | + or contributory patent infringement, then any patent licenses |
| 86 | + granted to You under this License for that Work shall terminate |
| 87 | + as of the date such litigation is filed. |
| 88 | + |
| 89 | + 4. Redistribution. You may reproduce and distribute copies of the |
| 90 | + Work or Derivative Works thereof in any medium, with or without |
| 91 | + modifications, and in Source or Object form, provided that You |
| 92 | + meet the following conditions: |
| 93 | + |
| 94 | + (a) You must give any other recipients of the Work or |
| 95 | + Derivative Works a copy of this License; and |
| 96 | + |
| 97 | + (b) You must cause any modified files to carry prominent notices |
| 98 | + stating that You changed the files; and |
| 99 | + |
| 100 | + (c) You must retain, in the Source form of any Derivative Works |
| 101 | + that You distribute, all copyright, patent, trademark, and |
| 102 | + attribution notices from the Source form of the Work, |
| 103 | + excluding those notices that do not pertain to any part of |
| 104 | + the Derivative Works; and |
| 105 | + |
| 106 | + (d) If the Work includes a "NOTICE" text file as part of its |
| 107 | + distribution, then any Derivative Works that You distribute must |
| 108 | + include a readable copy of the attribution notices contained |
| 109 | + within such NOTICE file, excluding those notices that do not |
| 110 | + pertain to any part of the Derivative Works, in at least one |
| 111 | + of the following places: within a NOTICE text file distributed |
| 112 | + as part of the Derivative Works; within the Source form or |
| 113 | + documentation, if provided along with the Derivative Works; or, |
| 114 | + within a display generated by the Derivative Works, if and |
| 115 | + wherever such third-party notices normally appear. The contents |
| 116 | + of the NOTICE file are for informational purposes only and |
| 117 | + do not modify the License. You may add Your own attribution |
| 118 | + notices within Derivative Works that You distribute, alongside |
| 119 | + or as an addendum to the NOTICE text from the Work, provided |
| 120 | + that such additional attribution notices cannot be construed |
| 121 | + as modifying the License. |
| 122 | + |
| 123 | + You may add Your own copyright statement to Your modifications and |
| 124 | + may provide additional or different license terms and conditions |
| 125 | + for use, reproduction, or distribution of Your modifications, or |
| 126 | + for any such Derivative Works as a whole, provided Your use, |
| 127 | + reproduction, and distribution of the Work otherwise complies with |
| 128 | + the conditions stated in this License. |
| 129 | + |
| 130 | + 5. Submission of Contributions. Unless You explicitly state otherwise, |
| 131 | + any Contribution intentionally submitted for inclusion in the Work |
| 132 | + by You to the Licensor shall be under the terms and conditions of |
| 133 | + this License, without any additional terms or conditions. |
| 134 | + Notwithstanding the above, nothing herein shall supersede or modify |
| 135 | + the terms of any separate license agreement you may have executed |
| 136 | + with Licensor regarding such Contributions. |
| 137 | + |
| 138 | + 6. Trademarks. This License does not grant permission to use the trade |
| 139 | + names, trademarks, service marks, or product names of the Licensor, |
| 140 | + except as required for reasonable and customary use in describing the |
| 141 | + origin of the Work and reproducing the content of the NOTICE file. |
| 142 | + |
| 143 | + 7. Disclaimer of Warranty. Unless required by applicable law or |
| 144 | + agreed to in writing, Licensor provides the Work (and each |
| 145 | + Contributor provides its Contributions) on an "AS IS" BASIS, |
| 146 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 147 | + implied, including, without limitation, any warranties or conditions |
| 148 | + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A |
| 149 | + PARTICULAR PURPOSE. You are solely responsible for determining the |
| 150 | + appropriateness of using or redistributing the Work and assume any |
| 151 | + risks associated with Your exercise of permissions under this License. |
| 152 | + |
| 153 | + 8. Limitation of Liability. In no event and under no legal theory, |
| 154 | + whether in tort (including negligence), contract, or otherwise, |
| 155 | + unless required by applicable law (such as deliberate and grossly |
| 156 | + negligent acts) or agreed to in writing, shall any Contributor be |
| 157 | + liable to You for damages, including any direct, indirect, special, |
| 158 | + incidental, or consequential damages of any character arising as a |
| 159 | + result of this License or out of the use or inability to use the |
| 160 | + Work (including but not limited to damages for loss of goodwill, |
| 161 | + work stoppage, computer failure or malfunction, or any and all |
| 162 | + other commercial damages or losses), even if such Contributor |
| 163 | + has been advised of the possibility of such damages. |
| 164 | + |
| 165 | + 9. Accepting Warranty or Additional Liability. While redistributing |
| 166 | + the Work or Derivative Works thereof, You may choose to offer, |
| 167 | + and charge a fee for, acceptance of support, warranty, indemnity, |
| 168 | + or other liability obligations and/or rights consistent with this |
| 169 | + License. However, in accepting such obligations, You may act only |
| 170 | + on Your own behalf and on Your sole responsibility, not on behalf |
| 171 | + of any other Contributor, and only if You agree to indemnify, |
| 172 | + defend, and hold each Contributor harmless for any liability |
| 173 | + incurred by, or claims asserted against, such Contributor by reason |
| 174 | + of your accepting any such warranty or additional liability. |
| 175 | + |
| 176 | + END OF TERMS AND CONDITIONS |
| 177 | + |
| 178 | + APPENDIX: How to apply the Apache License to your work. |
| 179 | + |
| 180 | + To apply the Apache License to your work, attach the following |
| 181 | + boilerplate notice, with the fields enclosed by brackets "[]" |
| 182 | + replaced with your own identifying information. (Don't include |
| 183 | + the brackets!) The text should be enclosed in the appropriate |
| 184 | + comment syntax for the file format. We also recommend that a |
| 185 | + file or class name and description of purpose be included on the |
| 186 | + same "printed page" as the copyright notice for easier |
| 187 | + identification within third-party archives. |
| 188 | + |
| 189 | + Copyright 2020 Bart Blast |
| 190 | + |
| 191 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 192 | + you may not use this file except in compliance with the License. |
| 193 | + You may obtain a copy of the License at |
| 194 | + |
| 195 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 196 | + |
| 197 | + Unless required by applicable law or agreed to in writing, software |
| 198 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 199 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 200 | + See the License for the specific language governing permissions and |
| 201 | + limitations under the License. |
| @@ -1,98 +1,138 @@ | |
| 1 1 | # Hologram |
| 2 2 | |
| 3 | - ## Roadmap |
| 3 | + Hologram is a full-stack isomorphic Elixir web framework that can be used on top of Phoenix. |
| 4 4 | |
| 5 | - :white_check_mark: == DONE |
| 5 | + ## Inspired by |
| 6 6 | |
| 7 | - :construction: == IN PROGRESS (partially done, some features work) |
| 7 | + Hologram was inspired by Elm, Phoenix LiveView, Surface, Svelte, Vue.js and Ruby on Rails. |
| 8 8 | |
| 9 | - :x: == TODO |
| 9 | + ## How it works |
| 10 10 | |
| 11 | - ### Runtime |
| 11 | + Hologram simplifies web app development by breaking it down into basic building blocks: Pages and Components. |
| 12 12 | |
| 13 | - #### Core |
| 13 | + Hologram analyzes the content of your Pages, which need to follow certain conventions. Based on this analysis, it decides which code should run on the client (in the web browser) and which should run on the server. Then, it converts the client-side code into JavaScript. |
| 14 14 | |
| 15 | - | Feature | Status | Comments | |
| 16 | - | :---------- | :----------------: | :-------------------------------------------------------------- | |
| 17 | - | Actions | :white_check_mark: | | |
| 18 | - | Commands | :white_check_mark: | | |
| 19 | - | Routing | :construction: | done: paths without params, todo: params | |
| 20 | - | Session | :x: | | |
| 15 | + Keeping the state (data) on the client side makes the programming model simpler. With stateless or stateful components, the app becomes more scalable. |
| 21 16 | |
| 22 | - #### Template Engine |
| 17 | + In Hologram, code meant for the client is organized into "actions," while code meant for the server is organized into "commands." Actions can trigger commands, and vice versa. Both actions and commands can be directly triggered by user interactions with the web page. |
| 23 18 | |
| 24 | - | Feature | Status | Comments | |
| 25 | - | :-------------- | :----------------: | :-------------------------------------------------------------- | |
| 26 | - | Components | :construction: | done: stateless/stateful components, todo: props DSL | |
| 27 | - | If Directive | :construction: | done: element nodes, todo: component nodes | |
| 28 | - | Interpolation | :white_check_mark: | | |
| 29 | - | Layouts | :white_check_mark: | | |
| 30 | - | Navigation | :white_check_mark: | | |
| 31 | - | Pages | :white_check_mark: | | |
| 32 | - | Templates | :construction: | done: template in module, todo: template in separate file | |
| 33 | - | Two-Way Binding | :x: | | |
| 19 | + For communication between the client and server, Hologram uses WebSockets. No additional boilerplate code is necessary - Hologram handles the setup automatically. |
| 34 20 | |
| 35 | - #### Events |
| 21 | + ## I want to see some code! |
| 36 22 | |
| 37 | - | Event | Status | Comments | |
| 38 | - | :------------- | :----------------: | :-------------------------------------------------------------- | |
| 39 | - | Blur | :white_check_mark: | | |
| 40 | - | Change | :construction: | done: form tags, todo: input, select, textarea tags | |
| 41 | - | Click | :construction: | done: event handling, todo: event metadata | |
| 42 | - | Focus | :x: | | |
| 43 | - | Key Down | :x: | | |
| 44 | - | Key Press | :x: | | |
| 45 | - | Key Up | :x: | | |
| 46 | - | Mouse Move | :x: | | |
| 47 | - | Params | :x: | | |
| 48 | - | Pointer Down | :construction: | done: event handling, todo: event metadata | |
| 49 | - | Pointer Up | :construction: | done: event handling, todo: event metadata | |
| 50 | - | Resize | :x: | | |
| 51 | - | Scroll | :x: | | |
| 52 | - | Select | :x: | | |
| 53 | - | Submit | :white_check_mark: | | |
| 54 | - | Tap | :x: | | |
| 55 | - | Target | :x: | | |
| 56 | - | Touch Cancel | :x: | | |
| 57 | - | Touch End | :x: | | |
| 58 | - | Touch Move | :x: | | |
| 59 | - | Touch Start | :x: | | |
| 60 | - | Transition End | :white_check_mark: | | |
| 23 | + To understand the structure of a Hologram app and view some actual code, take a look at the feature tests app: [hologram/test/features](https://github.com/bartblast/hologram/tree/master/test/features) |
| 61 24 | |
| 62 | - #### Tools |
| 25 | + ## Basic example |
| 63 26 | |
| 64 | - | Tool | Status | |
| 65 | - | :------------- | :----------------: | |
| 66 | - | Authentication | :x: | |
| 67 | - | Authorization | :x: | |
| 68 | - | Caching | :x: | |
| 69 | - | Code Reload | :x: | |
| 70 | - | Localization | :x: | |
| 71 | - | Time Travel | :x: | |
| 27 | + ```elixir |
| 28 | + defmodule MyPage do |
| 29 | + use Hologram.Page |
| 30 | + |
| 31 | + route "/my-page-path" |
| 32 | + |
| 33 | + def init(_params, component, _server) do |
| 34 | + put_state(component, :count, 0) |
| 35 | + end |
| 36 | + |
| 37 | + def template do |
| 38 | + ~H""" |
| 39 | + <div>Count is {@count}</div> |
| 40 | + <button $click={:increment, by: 3}>Increment by</button> |
| 41 | + <Link to={MyOtherPage}>Go to other page</Link> |
| 42 | + """ |
| 43 | + end |
| 44 | + |
| 45 | + def action(:increment, params, component) do |
| 46 | + put_state(component, :count, component.state.count + params.by) |
| 47 | + end |
| 48 | + |
| 49 | + def command(:save_record, params, server) do |
| 50 | + # Do something on the server, e.g. save a record to the database |
| 51 | + end |
| 52 | + end |
| 53 | + ``` |
| 54 | + |
| 55 | + ## Selling Points |
| 56 | + |
| 57 | + * State stays on the client - solving various problems as outlined below: |
| 58 | + |
| 59 | + * No latency issues arise since most of the code runs immediately on the client. This enables the creation of rich UIs or even games. Currently, with solutions that keep the state on the server (like LiveView), creating rich UIs usually requires multiple servers (in different locations) close to the users. However, latency persists, and response time cannot be guaranteed due to inherent variability. Additionally, some JavaScript or Alpine JS is still necessary to implement more advanced UI functionality. Until someone devises a solution like quantum internet (utilizing entanglement), there are no workarounds for this issue. I'm not sure if this is even technically feasible, though :wink: |
| 60 | + |
| 61 | + * Enhanced offline support, addressing scenarios such as internet disconnection or poor signal. With the bulk of code executed on the client, Hologram functions offline for extended periods. This facilitates the development of Progressive Web Apps (PWAs) or mobile apps via WebView, particularly when incorporating mechanisms like LocalStorage. |
| 62 | + |
| 63 | + * Reduced server RAM usage as the state resides in the browser. |
| 64 | + |
| 65 | + * Lower CPU utilization since the browser handles most code execution, alleviating server workload. |
| 66 | + |
| 67 | + * Decreased bandwidth consumption, as only commands necessitate communication with the server, eliminating the need to transmit component diffs for re-rendering. |
| 68 | + |
| 69 | + * Elimination of state synchronization issues, with the state centralized in the browser and WebSocket communication remaining stateless. |
| 70 | + |
| 71 | + * Minimal reliance on JS except for interfacing with select third-party scripts or widgets. This can be mitigated through standardized libraries tailored to popular packages, streamlining interoperability. |
| 72 | + |
| 73 | + * Particularly welcoming to new Elixir converts or novice developers, prioritizing DX and intuitive usability to streamline feature development without excessive technical troubleshooting or writing boilerplate code. |
| 74 | + |
| 75 | + ## Features |
| 76 | + |
| 77 | + Please note that the "Readme" file is currently undergoing an overhaul, and the "Features" section may not be up to date. |
| 78 | + |
| 79 | + ### Template Engine |
| 80 | + |
| 81 | + #### UI |
| 82 | + |
| 83 | + | Feature | Status | |
| 84 | + | :--------- | :----------------: | |
| 85 | + | Layouts | :white_check_mark: | |
| 86 | + | Navigation | :white_check_mark: | |
| 87 | + | Routing | :white_check_mark: | |
| 88 | + |
| 89 | + #### Markup |
| 90 | + |
| 91 | + | Markup | Status | |
| 92 | + | :--------------------------- | :----------------: | |
| 93 | + | Component (node) | :white_check_mark: | |
| 94 | + | Element (node) | :white_check_mark: | |
| 95 | + | Interpolation | :white_check_mark: | |
| 96 | + | For Block (iteration) | :white_check_mark: | |
| 97 | + | If Block (conditional) | :white_check_mark: | |
| 98 | + | Public Comment | :white_check_mark: | |
| 99 | + | Raw Block (markup disabling) | :white_check_mark: | |
| 100 | + | Text (node) | :white_check_mark: | |
| 72 101 | |
| 73 102 | ### Elixir Syntax |
| 74 103 | |
| 75 | - #### Types |
| 104 | + #### Data Types |
| 76 105 | |
| 77 | - | Type | Status | |
| 106 | + | Type | Status | |
| 107 | + | :-------- | :----------------: | |
| 108 | + | Atom | :white_check_mark: | |
| 109 | + | Bitstring | :white_check_mark: | |
| 110 | + | Float | :white_check_mark: | |
| 111 | + | Function | :white_check_mark: | |
| 112 | + | Integer | :white_check_mark: | |
| 113 | + | List | :white_check_mark: | |
| 114 | + | Map | :white_check_mark: | |
| 115 | + | PID | :white_check_mark: | |
| 116 | + | Tuple | :white_check_mark: | |
| 117 | + |
| 118 | + #### Control Flow |
| 119 | + |
| 120 | + | Expression | Status | |
| 121 | + | :--------- | :----------------: | |
| 122 | + | Case | :white_check_mark: | |
| 123 | + | Cond | :white_check_mark: | |
| 124 | + | If | :white_check_mark: | |
| 125 | + | Unless | :white_check_mark: | |
| 126 | + | With | :x: | |
| 127 | + |
| 128 | + #### Function Calls |
| 129 | + |
| 130 | + | Function Type | Status | |
| 78 131 | | :----------------- | :----------------: | |
| 79 | - | Anonymous Function | :x: | |
| 80 | - | Atom | :white_check_mark: | |
| 81 | - | Binary | :x: | |
| 82 | - | Bitstring | :x: | |
| 83 | - | Boolean | :white_check_mark: | |
| 84 | - | Exception | :x: | |
| 85 | - | Float | :white_check_mark: | |
| 86 | - | Integer | :white_check_mark: | |
| 87 | - | IO Data | :x: | |
| 88 | - | List | :white_check_mark: | |
| 89 | - | Map | :white_check_mark: | |
| 90 | - | Nil | :white_check_mark: | |
| 91 | - | Range | :x: | |
| 92 | - | Regex | :x: | |
| 93 | - | String | :white_check_mark: | |
| 94 | - | Struct | :white_check_mark: | |
| 95 | - | Tuple | :white_check_mark: | |
| 132 | + | Anonymous Function | :white_check_mark: | |
| 133 | + | Function Capture | :white_check_mark: | |
| 134 | + | Local Function | :white_check_mark: | |
| 135 | + | Remote Function | :white_check_mark: | |
| 96 136 | |
| 97 137 | #### Operators |
| 98 138 | |
| @@ -100,159 +140,110 @@ | |
| 100 140 | |
| 101 141 | | Operator | Status | |
| 102 142 | | :------- | :----------------: | |
| 103 | - | unary + | :x: | |
| 104 | - | unary - | :x: | |
| 143 | + | unary + | :white_check_mark: | |
| 144 | + | unary - | :white_check_mark: | |
| 105 145 | | + | :white_check_mark: | |
| 106 | - | - | :x: | |
| 107 | - | * | :x: | |
| 108 | - | / | :x: | |
| 109 | - | ++ | :x: | |
| 110 | - | -- | :x: | |
| 111 | - | and | :x: | |
| 146 | + | - | :white_check_mark: | |
| 147 | + | * | :white_check_mark: | |
| 148 | + | / | :white_check_mark: | |
| 149 | + | ++ | :white_check_mark: | |
| 150 | + | -- | :white_check_mark: | |
| 151 | + | and | :white_check_mark: | |
| 112 152 | | && | :white_check_mark: | |
| 113 | - | or | :x: | |
| 114 | - | \|\| | :x: | |
| 115 | - | not | :x: | |
| 116 | - | ! | :x: | |
| 117 | - | in | :x: | |
| 118 | - | not in | :x: | |
| 153 | + | or | :white_check_mark: | |
| 154 | + | \|\| | :white_check_mark: | |
| 155 | + | not | :white_check_mark: | |
| 156 | + | ! | :white_check_mark: | |
| 157 | + | in | :white_check_mark: | |
| 158 | + | not in | :white_check_mark: | |
| 119 159 | | @ | :white_check_mark: | |
| 120 | - | .. | :x: | |
| 121 | - | <> | :x: | |
| 122 | - | \|> | :x: | |
| 160 | + | .. | :white_check_mark: | |
| 161 | + | ..// | :white_check_mark: | |
| 162 | + | <> | :white_check_mark: | |
| 163 | + | \|> | :white_check_mark: | |
| 123 164 | | =~ | :x: | |
| 165 | + | ** | :x: | |
| 124 166 | |
| 125 | - ##### Non-Overridable General Operators |
| 167 | + ##### Special Form Operators |
| 126 168 | |
| 127 169 | | Operator | Status | |
| 128 170 | | :------- | :----------------: | |
| 129 | - | ^ | :x: | |
| 171 | + | ^ | :white_check_mark: | |
| 130 172 | | . | :white_check_mark: | |
| 131 173 | | = | :white_check_mark: | |
| 132 | - | & | :x: | |
| 133 | - | :: | :x: | |
| 174 | + | & | :white_check_mark: | |
| 175 | + | :: | :white_check_mark: | |
| 134 176 | |
| 135 177 | ##### Comparison Operators |
| 136 178 | |
| 137 179 | | Operator | Status | |
| 138 180 | | :------- | :----------------: | |
| 139 181 | | == | :white_check_mark: | |
| 140 | - | === | :x: | |
| 141 | - | != | :x: | |
| 142 | - | !== | :x: | |
| 143 | - | < | :x: | |
| 144 | - | > | :x: | |
| 145 | - | <= | :x: | |
| 146 | - | => | :x: | |
| 182 | + | === | :white_check_mark: | |
| 183 | + | != | :white_check_mark: | |
| 184 | + | !== | :white_check_mark: | |
| 185 | + | < | :white_check_mark: | |
| 186 | + | > | :white_check_mark: | |
| 187 | + | <= | :white_check_mark: | |
| 188 | + | >= | :white_check_mark: | |
| 147 189 | |
| 148 190 | ##### Bitwise Module Operators |
| 149 191 | |
| 150 192 | | Operator | Status | |
| 151 193 | | :------- | :----------------: | |
| 152 194 | | &&& | :x: | |
| 153 | - | ^^^ | :x: | |
| 154 | - | <<< | :x: | |
| 155 | - | >>> | :x: | |
| 156 | - | \|\|\| | :x: | |
| 157 | - | ~~~ | :x: | |
| 195 | + | <<< | :x: | |
| 196 | + | >>> | :x: | |
| 197 | + | \|\|\| | :x: | |
| 158 198 | |
| 159 | - #### Pattern Matching |
| 199 | + ##### Custom and Overriden Operators |
| 160 200 | |
| 161 | - | Type | Status | |
| 162 | - | :------------ | :----------------: | |
| 163 | - | Binary | :x: | |
| 164 | - | Bitstring | :x: | |
| 165 | - | Case | :x: | |
| 166 | - | Comprehension | :x: | |
| 167 | - | List | :x: | |
| 168 | - | Map | :white_check_mark: | |
| 169 | - | Range | :x: | |
| 170 | - | Struct | :x: | |
| 171 | - | Tuple | :x: | |
| 201 | + | Operator | Status | |
| 202 | + | :-------- | :----------------: | |
| 203 | + | custom | :white_check_mark: | |
| 204 | + | overriden | :white_check_mark: | |
| 172 205 | |
| 173 | - #### Control Flow |
| 206 | + #### Error Handling |
| 174 207 | |
| 175 | - | Structure | Status | |
| 176 | - | :------------ | :----------------: | |
| 177 | - | After | :x: | |
| 178 | - | Case | :x: | |
| 179 | - | Catch | :x: | |
| 180 | - | Comprehension | :x: | |
| 181 | - | Cond | :x: | |
| 182 | - | Else (If) | :white_check_mark: | |
| 183 | - | Else (Rescue) | :x: | |
| 184 | - | Guards | :x: | |
| 185 | - | If | :white_check_mark: | |
| 186 | - | Raise | :x: | |
| 187 | - | Rescue | :x: | |
| 188 | - | Throw | :x: | |
| 189 | - | Unless | :x: | |
| 190 | - | With | :x: | |
| 208 | + | Keyword | Status | |
| 209 | + | :------ | :----------------: | |
| 210 | + | After | :x: | |
| 211 | + | Catch | :x: | |
| 212 | + | Else | :x: | |
| 213 | + | Raise | :x: | |
| 214 | + | Rescue | :x: | |
| 215 | + | Throw | :x: | |
| 216 | + | Try | :x: | |
| 191 217 | |
| 192 | - #### Definitions |
| 218 | + #### Guards |
| 193 219 | |
| 194 | - | Structure | Status | |
| 195 | - | :--------------- | :----------------: | |
| 196 | - | Exception | :x: | |
| 197 | - | Function Head | :white_check_mark: | |
| 198 | - | Macro | :x: | |
| 199 | - | Module | :white_check_mark: | |
| 200 | - | Module Attribute | :white_check_mark: | |
| 201 | - | Private Function | :white_check_mark: | |
| 202 | - | Public Function | :white_check_mark: | |
| 220 | + | Construct | Status | |
| 221 | + | :---------------------- | :----------------: | |
| 222 | + | Anonymous Function | :white_check_mark: | |
| 223 | + | Case Expression | :white_check_mark: | |
| 224 | + | Comprehension Generator | :x: | |
| 225 | + | Private Local Function | :x: | |
| 226 | + | Public Local Function | :x: | |
| 203 227 | |
| 228 | + #### Other Syntax |
| 204 229 | |
| 205 | - #### Directives |
| 230 | + | Construct | Status | |
| 231 | + | :------------- | :----------------: | |
| 232 | + | Comprehensions | :x: | |
| 206 233 | |
| 207 | - | Directive | Status | |
| 208 | - | :--------------- | :----------------: | |
| 209 | - | Alias | :white_check_mark: | |
| 210 | - | Import | :white_check_mark: | |
| 211 | - | Multi-Alias | :x: | |
| 212 | - | Require | :white_check_mark: | |
| 213 | - | Use | :white_check_mark: | |
| 234 | + ### Misc Features |
| 214 235 | |
| 215 | - #### Sigils |
| 236 | + | Feature | Status | |
| 237 | + | :------------------ | :----------------: | |
| 238 | + | Regular expressions | :x: | |
| 216 239 | |
| 217 | - | Sigil | Status | |
| 218 | - | :---- | :----------------: | |
| 219 | - | ~c | :x: | |
| 220 | - | ~C | :x: | |
| 221 | - | ~D | :x: | |
| 222 | - | ~N | :x: | |
| 223 | - | ~r | :x: | |
| 224 | - | ~R | :x: | |
| 225 | - | ~s | :x: | |
| 226 | - | ~S | :x: | |
| 227 | - | ~T | :x: | |
| 228 | - | ~U | :x: | |
| 229 | - | ~w | :x: | |
| 230 | - | ~W | :x: | |
| 240 | + ### Framework Runtime |
| 231 241 | |
| 232 | - #### Other |
| 242 | + #### Core |
| 233 243 | |
| 234 | - | Feature | Status | |
| 235 | - | :----------------------------- | :----------------: | |
| 236 | - | Behaviours | :x: | |
| 237 | - | Codepoints | :x: | |
| 238 | - | Custom Sigils | :x: | |
| 239 | - | Default Arguments | :x: | |
| 240 | - | Function Capturing | :x: | |
| 241 | - | Map Update Syntax | :x: | |
| 242 | - | Module Attribute Accumulation | :x: | |
| 243 | - | Module \_\_info\_\_/1 callback | :white_check_mark: | |
| 244 | - | Module Nesting | :x: | |
| 245 | - | Protocols | :x: | |
| 246 | - | Variable rebinding | :x: | |
| 247 | - |
| 248 | - #### Not on Roadmap |
| 249 | - * Types: PID, Port, Reference |
| 250 | - * Control Flow: Exit, Receive |
| 251 | - * Operators: Custom, Overriding |
| 252 | - * Other: Erlang Libraries |
| 253 | - |
| 254 | - ### Work in progress |
| 255 | - |
| 256 | - #### Runtime / Core / Commands |
| 257 | - Done: commands trigerred by action\ |
| 258 | - Todo: commands trigerred by event |
| \ No newline at end of file | |
| 244 | + | Feature | Status | |
| 245 | + | :------- | :----------------: | |
| 246 | + | Actions | :white_check_mark: | |
| 247 | + | Commands | :white_check_mark: | |
| 248 | + | Cookies | :x: | |
| 249 | + | Session | :x: | |
| \ No newline at end of file |
| @@ -0,0 +1,32 @@ | |
| 1 | + "use strict"; |
| 2 | + |
| 3 | + import Type from "./type.mjs"; |
| 4 | + |
| 5 | + export default class AssetPathRegistry { |
| 6 | + static entries = null; |
| 7 | + |
| 8 | + // Deps: [:maps.get/3] |
| 9 | + static lookup(staticPath) { |
| 10 | + return Erlang_Maps["get/3"]( |
| 11 | + staticPath, |
| 12 | + AssetPathRegistry.entries, |
| 13 | + Type.nil(), |
| 14 | + ); |
| 15 | + } |
| 16 | + |
| 17 | + // Deps: [:maps.put/3] |
| 18 | + static populate(assetManifest) { |
| 19 | + AssetPathRegistry.entries = Type.map(); |
| 20 | + |
| 21 | + for (const [staticPath, assetPath] of Object.entries(assetManifest)) { |
| 22 | + const key = Type.bitstring(staticPath); |
| 23 | + const value = Type.bitstring(assetPath); |
| 24 | + |
| 25 | + AssetPathRegistry.entries = Erlang_Maps["put/3"]( |
| 26 | + key, |
| 27 | + value, |
| 28 | + AssetPathRegistry.entries, |
| 29 | + ); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| @@ -0,0 +1,748 @@ | |
| 1 | + "use strict"; |
| 2 | + |
| 3 | + import HologramInterpreterError from "./errors/interpreter_error.mjs"; |
| 4 | + import Interpreter from "./interpreter.mjs"; |
| 5 | + import Type from "./type.mjs"; |
| 6 | + import Utils from "./utils.mjs"; |
| 7 | + |
| 8 | + export default class Bitstring { |
| 9 | + static buildSignedBigIntFromBitArray(bitArray) { |
| 10 | + if (bitArray.length === 0) { |
| 11 | + return 0n; |
| 12 | + } |
| 13 | + |
| 14 | + const signBit = bitArray[0]; |
| 15 | + |
| 16 | + const value = bitArray.slice(1).reduce((acc, bit, index) => { |
| 17 | + return acc | BigInt(bit << (bitArray.length - index - 2)); |
| 18 | + }, 0n); |
| 19 | + |
| 20 | + return signBit === 1 ? -BigInt(2 ** (bitArray.length - 1)) + value : value; |
| 21 | + } |
| 22 | + |
| 23 | + static buildUnsignedBigIntFromBitArray(bitArray) { |
| 24 | + return bitArray.reduce((acc, bit, index) => { |
| 25 | + return acc | BigInt(bit << (bitArray.length - 1 - index)); |
| 26 | + }, 0n); |
| 27 | + } |
| 28 | + |
| 29 | + // TODO: test |
| 30 | + static buildValueFromBitstringChunk(segment, bitArray, offset) { |
| 31 | + switch (segment.type) { |
| 32 | + case "float": |
| 33 | + return Bitstring.#buildFloatFromBitstringChunk( |
| 34 | + segment, |
| 35 | + bitArray, |
| 36 | + offset, |
| 37 | + ); |
| 38 | + |
| 39 | + case "integer": |
| 40 | + return Bitstring.#buildIntegerFromBitstringChunk( |
| 41 | + segment, |
| 42 | + bitArray, |
| 43 | + offset, |
| 44 | + ); |
| 45 | + |
| 46 | + case "utf8": |
| 47 | + return Bitstring.fetchNextCodePointFromUtf8BitstringChunk( |
| 48 | + bitArray, |
| 49 | + offset, |
| 50 | + ); |
| 51 | + |
| 52 | + default: |
| 53 | + throw new HologramInterpreterError( |
| 54 | + `building ${segment.type} value from a bitstring segment is not yet implemented in Hologram`, |
| 55 | + ); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // See: https://en.wikipedia.org/wiki/UTF-8#Encoding |
| 60 | + static fetchNextCodePointFromUtf8BitstringChunk(bitArray, offset) { |
| 61 | + const numRemainingBits = bitArray.length - offset; |
| 62 | + |
| 63 | + let numBytes; |
| 64 | + |
| 65 | + // 0xxxxxxx |
| 66 | + if (numRemainingBits >= 8 && bitArray[offset] === 0) { |
| 67 | + numBytes = 1; |
| 68 | + } else if ( |
| 69 | + // 110xxxxx, 10xxxxxx |
| 70 | + numRemainingBits >= 16 && |
| 71 | + bitArray[offset] === 1 && |
| 72 | + bitArray[offset + 1] === 1 && |
| 73 | + bitArray[offset + 2] === 0 && |
| 74 | + bitArray[offset + 8] == 1 && |
| 75 | + bitArray[offset + 9] == 0 |
| 76 | + ) { |
| 77 | + numBytes = 2; |
| 78 | + } else if ( |
| 79 | + //1110xxxx, 10xxxxxx, 10xxxxxx |
| 80 | + numRemainingBits >= 24 && |
| 81 | + bitArray[offset] === 1 && |
| 82 | + bitArray[offset + 1] === 1 && |
| 83 | + bitArray[offset + 2] === 1 && |
| 84 | + bitArray[offset + 3] === 0 && |
| 85 | + bitArray[offset + 8] == 1 && |
| 86 | + bitArray[offset + 9] == 0 && |
| 87 | + bitArray[offset + 16] == 1 && |
| 88 | + bitArray[offset + 17] == 0 |
| 89 | + ) { |
| 90 | + numBytes = 3; |
| 91 | + } else if ( |
| 92 | + // 11110xxx, 10xxxxxx, 10xxxxxx, 10xxxxxx |
| 93 | + numRemainingBits >= 32 && |
| 94 | + bitArray[offset] === 1 && |
| 95 | + bitArray[offset + 1] === 1 && |
| 96 | + bitArray[offset + 2] === 1 && |
| 97 | + bitArray[offset + 3] === 1 && |
| 98 | + bitArray[offset + 4] === 0 && |
| 99 | + bitArray[offset + 8] == 1 && |
| 100 | + bitArray[offset + 9] == 0 && |
| 101 | + bitArray[offset + 16] == 1 && |
| 102 | + bitArray[offset + 17] == 0 && |
| 103 | + bitArray[offset + 24] == 1 && |
| 104 | + bitArray[offset + 25] == 0 |
| 105 | + ) { |
| 106 | + numBytes = 4; |
| 107 | + } else { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + if (numBytes > 1 && offset + numBytes * 8 > bitArray.length) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + const chunks = []; |
| 116 | + |
| 117 | + switch (numBytes) { |
| 118 | + case 1: |
| 119 | + chunks[0] = bitArray.slice(offset + 1, offset + 8); |
| 120 | + break; |
| 121 | + |
| 122 | + case 2: |
| 123 | + chunks[0] = bitArray.slice(offset + 3, offset + 8); |
| 124 | + chunks[1] = bitArray.slice(offset + 8 + 2, offset + 2 * 8); |
| 125 | + break; |
| 126 | + |
| 127 | + case 3: |
| 128 | + chunks[0] = bitArray.slice(offset + 4, offset + 8); |
| 129 | + chunks[1] = bitArray.slice(offset + 8 + 2, offset + 2 * 8); |
| 130 | + chunks[2] = bitArray.slice(offset + 2 * 8 + 2, offset + 3 * 8); |
| 131 | + break; |
| 132 | + |
| 133 | + case 4: |
| 134 | + chunks[0] = bitArray.slice(offset + 5, offset + 8); |
| 135 | + chunks[1] = bitArray.slice(offset + 8 + 2, offset + 8 + 8); |
| 136 | + chunks[2] = bitArray.slice(offset + 2 * 8 + 2, offset + 3 * 8); |
| 137 | + chunks[3] = bitArray.slice(offset + 3 * 8 + 2, offset + 4 * 8); |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + const codePointBitCount = chunks.reduce( |
| 142 | + (acc, chunk) => acc + chunk.length, |
| 143 | + 0, |
| 144 | + ); |
| 145 | + |
| 146 | + const codePointBitArray = new Uint8Array(codePointBitCount); |
| 147 | + let codePointOffset = 0; |
| 148 | + |
| 149 | + for (const chunk of chunks) { |
| 150 | + codePointBitArray.set(chunk, codePointOffset); |
| 151 | + codePointOffset += chunk.length; |
| 152 | + } |
| 153 | + |
| 154 | + const codePoint = |
| 155 | + Bitstring.buildUnsignedBigIntFromBitArray(codePointBitArray); |
| 156 | + |
| 157 | + return [Type.integer(codePoint), numBytes * 8]; |
| 158 | + } |
| 159 | + |
| 160 | + static from(segments) { |
| 161 | + const bitArrays = segments.map((segment, index) => { |
| 162 | + Bitstring.validateSegment(segment, index + 1); |
| 163 | + return Bitstring.#buildBitArray(segment, index + 1); |
| 164 | + }); |
| 165 | + |
| 166 | + return {type: "bitstring", bits: Utils.concatUint8Arrays(bitArrays)}; |
| 167 | + } |
| 168 | + |
| 169 | + // See: String.printable?/2 |
| 170 | + // https://github.com/elixir-lang/elixir/blob/6bfb95ab884f11475de6da3f99c6528938e025a8/lib/elixir/lib/string.ex#L322 |
| 171 | + static isPrintableCodePoint(codePoint) { |
| 172 | + // 0x20 = 32, 0x7E = 126 |
| 173 | + if (codePoint >= 32 && codePoint <= 126) { |
| 174 | + return true; |
| 175 | + } |
| 176 | + |
| 177 | + // ?\n = 10 |
| 178 | + // ?\r = 13 |
| 179 | + // ?\t = 9 |
| 180 | + // ?\v = 11 |
| 181 | + // ?\b = 8 |
| 182 | + // ?\f = 12 |
| 183 | + // ?\e = 27 |
| 184 | + // ?\d = 127 |
| 185 | + // ?\a = 7 |
| 186 | + if ([10, 13, 9, 11, 8, 12, 27, 127, 7].includes(codePoint)) { |
| 187 | + return true; |
| 188 | + } |
| 189 | + |
| 190 | + // 0xA0 = 160, 0xD7FF = 55295 |
| 191 | + if (codePoint >= 160 && codePoint <= 55295) { |
| 192 | + return true; |
| 193 | + } |
| 194 | + |
| 195 | + // 0xE000 = 57344, 0xFFFD = 65533 |
| 196 | + if (codePoint >= 57344 && codePoint <= 65533) { |
| 197 | + return true; |
| 198 | + } |
| 199 | + |
| 200 | + // 0x10000 = 65536, 0x10FFFF = 1114111 |
| 201 | + if (codePoint >= 65536 && codePoint <= 1114111) { |
| 202 | + return true; |
| 203 | + } |
| 204 | + |
| 205 | + return false; |
| 206 | + } |
| 207 | + |
| 208 | + static isPrintableText(bitstring) { |
| 209 | + if (!Type.isBinary(bitstring)) { |
| 210 | + return false; |
| 211 | + } |
| 212 | + |
| 213 | + let offset = 0; |
| 214 | + |
| 215 | + while (offset < bitstring.bits.length) { |
| 216 | + const codePointInfo = Bitstring.fetchNextCodePointFromUtf8BitstringChunk( |
| 217 | + bitstring.bits, |
| 218 | + offset, |
| 219 | + ); |
| 220 | + |
| 221 | + if (!codePointInfo) { |
| 222 | + return false; |
| 223 | + } |
| 224 | + |
| 225 | + if (!Bitstring.validateCodePoint(codePointInfo[0].value)) { |
| 226 | + return false; |
| 227 | + } |
| 228 | + |
| 229 | + if (!Bitstring.isPrintableCodePoint(codePointInfo[0].value)) { |
| 230 | + return false; |
| 231 | + } |
| 232 | + |
| 233 | + offset += codePointInfo[1]; |
| 234 | + } |
| 235 | + |
| 236 | + return true; |
| 237 | + } |
| 238 | + |
| 239 | + static isText(bitstring) { |
| 240 | + if (!Type.isBinary(bitstring)) { |
| 241 | + return false; |
| 242 | + } |
| 243 | + |
| 244 | + let offset = 0; |
| 245 | + |
| 246 | + while (offset < bitstring.bits.length) { |
| 247 | + const codePointInfo = Bitstring.fetchNextCodePointFromUtf8BitstringChunk( |
| 248 | + bitstring.bits, |
| 249 | + offset, |
| 250 | + ); |
| 251 | + |
| 252 | + if (!codePointInfo) { |
| 253 | + return false; |
| 254 | + } |
| 255 | + |
| 256 | + if (!Bitstring.validateCodePoint(codePointInfo[0].value)) { |
| 257 | + return false; |
| 258 | + } |
| 259 | + |
| 260 | + offset += codePointInfo[1]; |
| 261 | + } |
| 262 | + |
| 263 | + return true; |
| 264 | + } |
| 265 | + |
| 266 | + // Using set() is much more performant than using spread operator, |
| 267 | + // see: https://jsben.ch/jze3P |
| 268 | + static merge(bitstrings) { |
| 269 | + const length = bitstrings.reduce( |
| 270 | + (acc, bitstring) => acc + bitstring.bits.length, |
| 271 | + 0, |
| 272 | + ); |
| 273 | + |
| 274 | + const bits = new Uint8Array(length); |
| 275 | + let offset = 0; |
| 276 | + |
| 277 | + for (const bitstring of bitstrings) { |
| 278 | + bits.set(bitstring.bits, offset); |
| 279 | + offset += bitstring.bits.length; |
| 280 | + } |
| 281 | + |
| 282 | + return Type.bitstring(bits); |
| 283 | + } |
| 284 | + |
| 285 | + static resolveSegmentSize(segment) { |
| 286 | + if (["float", "integer"].includes(segment.type) && segment.size !== null) { |
| 287 | + return segment.size.value; |
| 288 | + } |
| 289 | + |
| 290 | + switch (segment.type) { |
| 291 | + case "float": |
| 292 | + return 64n; |
| 293 | + |
| 294 | + case "integer": |
| 295 | + return 8n; |
| 296 | + |
| 297 | + default: |
| 298 | + throw new HologramInterpreterError( |
| 299 | + `resolving ${segment.type} segment size is not yet implemented in Hologram`, |
| 300 | + ); |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + static resolveSegmentUnit(segment) { |
| 305 | + if ( |
| 306 | + ["binary", "float", "integer"].includes(segment.type) && |
| 307 | + segment.unit !== null |
| 308 | + ) { |
| 309 | + return segment.unit; |
| 310 | + } |
| 311 | + |
| 312 | + switch (segment.type) { |
| 313 | + case "binary": |
| 314 | + return 8n; |
| 315 | + |
| 316 | + case "float": |
| 317 | + case "integer": |
| 318 | + return 1n; |
| 319 | + |
| 320 | + default: |
| 321 | + throw new HologramInterpreterError( |
| 322 | + `resolving ${segment.type} segment unit is not yet implemented in Hologram`, |
| 323 | + ); |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + static toText(bitstring) { |
| 328 | + const byteArray = Bitstring.#convertBitArrayToByteArray(bitstring.bits); |
| 329 | + const decoder = new TextDecoder("utf-8"); |
| 330 | + |
| 331 | + return decoder.decode(byteArray); |
| 332 | + } |
| 333 | + |
| 334 | + static validateCodePoint(codePoint) { |
| 335 | + if (typeof codePoint === "bigint") { |
| 336 | + codePoint = Number(codePoint); |
| 337 | + } |
| 338 | + |
| 339 | + try { |
| 340 | + String.fromCodePoint(codePoint); |
| 341 | + return true; |
| 342 | + } catch (error) { |
| 343 | + if (error instanceof RangeError) { |
| 344 | + return false; |
| 345 | + } else { |
| 346 | + throw error; |
| 347 | + } |
| 348 | + } |
| 349 | + } |
| 350 | + |
| 351 | + static validateSegment(segment, index) { |
| 352 | + switch (segment.type) { |
| 353 | + case "binary": |
| 354 | + return Bitstring.#validateBinarySegment(segment, index); |
| 355 | + |
| 356 | + case "bitstring": |
| 357 | + return Bitstring.#validateBitstringSegment(segment, index); |
| 358 | + |
| 359 | + case "float": |
| 360 | + return Bitstring.#validateFloatSegment(segment, index); |
| 361 | + |
| 362 | + case "integer": |
| 363 | + return Bitstring.#validateIntegerSegment(segment, index); |
| 364 | + |
| 365 | + case "utf8": |
| 366 | + case "utf16": |
| 367 | + case "utf32": |
| 368 | + return Bitstring.#validateUtfSegment(segment, index); |
| 369 | + } |
| 370 | + } |
| 371 | + |
| 372 | + static #buildBitArray(segment, index) { |
| 373 | + switch (segment.value.type) { |
| 374 | + case "bitstring": |
| 375 | + return Bitstring.#buildBitArrayFromBitstring(segment); |
| 376 | + |
| 377 | + case "float": |
| 378 | + return Bitstring.#buildBitArrayFromFloat(segment); |
| 379 | + |
| 380 | + case "integer": |
| 381 | + return Bitstring.#buildBitArrayFromInteger(segment, index); |
| 382 | + |
| 383 | + case "string": |
| 384 | + return Bitstring.#buildBitArrayFromString(segment); |
| 385 | + } |
| 386 | + } |
| 387 | + |
| 388 | + static #buildBitArrayFromBitstring(segment) { |
| 389 | + return new Uint8Array(segment.value.bits); |
| 390 | + } |
| 391 | + |
| 392 | + static #buildBitArrayFromFloat(segment) { |
| 393 | + const value = segment.value.value; |
| 394 | + const size = Bitstring.resolveSegmentSize(segment); |
| 395 | + |
| 396 | + const bitArrays = Array.from(Bitstring.#getBytesFromFloat(value, size)).map( |
| 397 | + (byte) => Bitstring.#convertDataToBitArray(BigInt(byte), 8n, 1n), |
| 398 | + ); |
| 399 | + |
| 400 | + return Utils.concatUint8Arrays(bitArrays); |
| 401 | + } |
| 402 | + |
| 403 | + static #buildBitArrayFromInteger(segment, index) { |
| 404 | + if (segment.type === "float") { |
| 405 | + const segmentWithValueCastedToFloat = { |
| 406 | + ...segment, |
| 407 | + value: Type.float(Number(segment.value.value)), |
| 408 | + }; |
| 409 | + return Bitstring.#buildBitArrayFromFloat(segmentWithValueCastedToFloat); |
| 410 | + } |
| 411 | + |
| 412 | + // Max Unicode code point value is 1,114,112 |
| 413 | + if (["utf8", "utf16", "utf32"].includes(segment.type)) { |
| 414 | + try { |
| 415 | + const str = String.fromCodePoint(Number(segment.value.value)); |
| 416 | + |
| 417 | + const segmentWithValueCastedToString = { |
| 418 | + ...segment, |
| 419 | + value: Type.string(str), |
| 420 | + }; |
| 421 | + |
| 422 | + return Bitstring.#buildBitArrayFromString( |
| 423 | + segmentWithValueCastedToString, |
| 424 | + ); |
| 425 | + } catch { |
| 426 | + Bitstring.#raiseInvalidUnicodeCodePointError(segment, index); |
| 427 | + } |
| 428 | + } |
| 429 | + |
| 430 | + const value = segment.value.value; |
| 431 | + const size = Bitstring.resolveSegmentSize(segment); |
| 432 | + const unit = Bitstring.resolveSegmentUnit(segment); |
| 433 | + |
| 434 | + return Bitstring.#convertDataToBitArray(value, size, unit); |
| 435 | + } |
| 436 | + |
| 437 | + static #buildBitArrayFromString(segment) { |
| 438 | + const value = segment.value.value; |
| 439 | + |
| 440 | + const bitArrays = Array.from( |
| 441 | + Bitstring.#getBytesFromString(value, segment.type), |
| 442 | + ).map((byte) => Bitstring.#convertDataToBitArray(BigInt(byte), 8n, 1n)); |
| 443 | + |
| 444 | + if (segment.size !== null) { |
| 445 | + const unit = Bitstring.resolveSegmentUnit({...segment, type: "binary"}); |
| 446 | + const numBits = segment.size.value * unit; |
| 447 | + |
| 448 | + return Utils.concatUint8Arrays(bitArrays).subarray(0, Number(numBits)); |
| 449 | + } else { |
| 450 | + return Utils.concatUint8Arrays(bitArrays); |
| 451 | + } |
| 452 | + } |
| 453 | + |
| 454 | + static #buildFloatFromBitstringChunk(segment, bitArray, offset) { |
| 455 | + let size = Bitstring.resolveSegmentSize(segment); |
| 456 | + |
| 457 | + if (![16n, 32n, 64n].includes(size)) { |
| 458 | + size = 64n; |
| 459 | + } |
| 460 | + |
| 461 | + if (size === 16n) { |
| 462 | + throw new HologramInterpreterError( |
| 463 | + "16-bit float bitstring segments are not yet implemented in Hologram", |
| 464 | + ); |
| 465 | + } |
| 466 | + |
| 467 | + const unit = Bitstring.resolveSegmentUnit(segment); |
| 468 | + const segmentLen = Number(size * unit); |
| 469 | + |
| 470 | + if (offset + segmentLen > bitArray.length) { |
| 471 | + return false; |
| 472 | + } |
| 473 | + |
| 474 | + const chunk = bitArray.slice(offset, offset + segmentLen); |
| 475 | + const bytesArray = Bitstring.#convertBitArrayToByteArray(chunk); |
| 476 | + const dataView = new DataView(bytesArray.buffer); |
| 477 | + |
| 478 | + const value = |
| 479 | + size === 64n |
| 480 | + ? dataView.getFloat64(0, false) |
| 481 | + : dataView.getFloat32(0, false); |
| 482 | + |
| 483 | + return [Type.float(value), segmentLen]; |
| 484 | + } |
| 485 | + |
| 486 | + static #buildIntegerFromBitstringChunk(segment, bitArray, offset) { |
| 487 | + const size = Bitstring.resolveSegmentSize(segment); |
| 488 | + const unit = Bitstring.resolveSegmentUnit(segment); |
| 489 | + const segmentLen = Number(size * unit); |
| 490 | + |
| 491 | + if (offset + segmentLen > bitArray.length) { |
| 492 | + return false; |
| 493 | + } |
| 494 | + |
| 495 | + const bitArrayChunk = bitArray.slice(offset, offset + segmentLen); |
| 496 | + let value; |
| 497 | + |
| 498 | + if (Bitstring.#resolveSegmentSignedness(segment) === "signed") { |
| 499 | + value = Bitstring.buildSignedBigIntFromBitArray(bitArrayChunk); |
| 500 | + } else { |
| 501 | + value = Bitstring.buildUnsignedBigIntFromBitArray(bitArrayChunk); |
| 502 | + } |
| 503 | + |
| 504 | + return [Type.integer(value), segmentLen]; |
| 505 | + } |
| 506 | + |
| 507 | + static #convertBitArrayToByteArray(bitArray) { |
| 508 | + if (bitArray.length % 8 !== 0) { |
| 509 | + throw new HologramInterpreterError( |
| 510 | + `number of bits must be divisible by 8, got ${bitArray.length} bits`, |
| 511 | + ); |
| 512 | + } |
| 513 | + |
| 514 | + const numBytes = bitArray.length / 8; |
| 515 | + const byteArray = new Uint8Array(numBytes); |
| 516 | + |
| 517 | + for (let i = 0; i < numBytes; ++i) { |
| 518 | + for (let j = 0; j < 8; ++j) { |
| 519 | + if (bitArray[i * 8 + j] === 1) { |
| 520 | + byteArray[i] = Bitstring.#putNumberBit(byteArray[i], 7 - j); |
| 521 | + } |
| 522 | + } |
| 523 | + } |
| 524 | + |
| 525 | + return byteArray; |
| 526 | + } |
| 527 | + |
| 528 | + static #convertDataToBitArray(data, size, unit) { |
| 529 | + // clamp to size number of bits |
| 530 | + const numBits = size * unit; |
| 531 | + const bitmask = 2n ** numBits - 1n; |
| 532 | + const clampedData = data & bitmask; |
| 533 | + |
| 534 | + const bitArr = []; |
| 535 | + |
| 536 | + for (let i = numBits; i >= 1n; --i) { |
| 537 | + bitArr[numBits - i] = Bitstring.#getBit(clampedData, i - 1n); |
| 538 | + } |
| 539 | + |
| 540 | + return new Uint8Array(bitArr); |
| 541 | + } |
| 542 | + |
| 543 | + static #encodeUtf16(str, endianness) { |
| 544 | + const byteArray = new Uint8Array(str.length * 2); |
| 545 | + const view = new DataView(byteArray.buffer); |
| 546 | + |
| 547 | + str |
| 548 | + .split("") |
| 549 | + .forEach((char, index) => |
| 550 | + view.setUint16(index * 2, char.charCodeAt(0), endianness === "little"), |
| 551 | + ); |
| 552 | + |
| 553 | + return byteArray; |
| 554 | + } |
| 555 | + |
| 556 | + static #getBit(value, position) { |
| 557 | + return (value & (1n << position)) === 0n ? 0 : 1; |
| 558 | + } |
| 559 | + |
| 560 | + static #getBytesFromFloat(float, size) { |
| 561 | + let floatArr; |
| 562 | + |
| 563 | + switch (size) { |
| 564 | + case 64n: |
| 565 | + floatArr = new Float64Array([float]); |
| 566 | + break; |
| 567 | + |
| 568 | + case 32n: |
| 569 | + floatArr = new Float32Array([float]); |
| 570 | + break; |
| 571 | + |
| 572 | + case 16n: |
| 573 | + // This case is not possible at the moment, since an error would be raised earlier. |
| 574 | + } |
| 575 | + |
| 576 | + return new Uint8Array(floatArr.buffer).reverse(); |
| 577 | + } |
| 578 | + |
| 579 | + static #getBytesFromString(str, encoding) { |
| 580 | + switch (encoding) { |
| 581 | + case "binary": |
| 582 | + case "bitstring": |
| 583 | + case "utf8": |
| 584 | + return new TextEncoder().encode(str); |
| 585 | + |
| 586 | + case "utf16": |
| 587 | + return Bitstring.#encodeUtf16(str, "big"); |
| 588 | + } |
| 589 | + } |
| 590 | + |
| 591 | + static #putNumberBit(value, position) { |
| 592 | + return value | (1 << position); |
| 593 | + } |
| 594 | + |
| 595 | + static #raiseInvalidUnicodeCodePointError(segment, index) { |
| 596 | + Bitstring.#raiseTypeMismatchError( |
| 597 | + index, |
| 598 | + segment.type, |
| 599 | + "a non-negative integer encodable as " + segment.type, |
| 600 | + segment.value, |
| 601 | + ); |
| 602 | + } |
| 603 | + |
| 604 | + static #raiseTypeMismatchError( |
| 605 | + index, |
| 606 | + segmentType, |
| 607 | + expectedValueTypesStr, |
| 608 | + value, |
| 609 | + ) { |
| 610 | + const inspectedValue = Interpreter.inspect(value); |
| 611 | + const message = `construction of binary failed: segment ${index} of type '${segmentType}': expected ${expectedValueTypesStr} but got: ${inspectedValue}`; |
| 612 | + |
| 613 | + Interpreter.raiseArgumentError(message); |
| 614 | + } |
| 615 | + |
| 616 | + static #resolveSegmentSignedness(segment) { |
| 617 | + if (segment.signedness !== null) { |
| 618 | + return segment.signedness; |
| 619 | + } |
| 620 | + |
| 621 | + return "unsigned"; |
| 622 | + } |
| 623 | + |
| 624 | + static #validateBinarySegment(segment, index) { |
| 625 | + if ( |
| 626 | + segment.value.type === "bitstring" && |
| 627 | + segment.value.bits.length % 8 !== 0 |
| 628 | + ) { |
| 629 | + const inspectedValue = Interpreter.inspect(segment.value); |
| 630 | + |
| 631 | + Interpreter.raiseArgumentError( |
| 632 | + `construction of binary failed: segment ${index} of type 'binary': the size of the value ${inspectedValue} is not a multiple of the unit for the segment`, |
| 633 | + ); |
| 634 | + } |
| 635 | + |
| 636 | + if (["float", "integer"].includes(segment.value.type)) { |
| 637 | + Bitstring.#raiseTypeMismatchError( |
| 638 | + index, |
| 639 | + "binary", |
| 640 | + "a binary", |
| 641 | + segment.value, |
| 642 | + ); |
| 643 | + } |
| 644 | + |
| 645 | + return true; |
| 646 | + } |
| 647 | + |
| 648 | + static #validateBitstringSegment(segment, index) { |
| 649 | + if (["float", "integer"].includes(segment.value.type)) { |
| 650 | + Bitstring.#raiseTypeMismatchError( |
| 651 | + index, |
| 652 | + "binary", |
| 653 | + "a binary", |
| 654 | + segment.value, |
| 655 | + ); |
| 656 | + } |
| 657 | + |
| 658 | + if (segment.signedness !== null || segment.size !== null) { |
| 659 | + Bitstring.#raiseTypeMismatchError( |
| 660 | + index, |
| 661 | + "integer", |
| 662 | + "an integer", |
| 663 | + segment.value, |
| 664 | + ); |
| 665 | + } |
| 666 | + |
| 667 | + return true; |
| 668 | + } |
| 669 | + |
| 670 | + static #validateFloatSegment(segment, index) { |
| 671 | + if ( |
| 672 | + !["float", "integer", "variable_pattern"].includes(segment.value.type) |
| 673 | + ) { |
| 674 | + Bitstring.#raiseTypeMismatchError( |
| 675 | + index, |
| 676 | + "float", |
| 677 | + "a float or an integer", |
| 678 | + segment.value, |
| 679 | + ); |
| 680 | + } |
| 681 | + |
| 682 | + if (segment.size === null && segment.unit !== null) { |
| 683 | + Interpreter.raiseCompileError( |
| 684 | + "integer and float types require a size specifier if the unit specifier is given", |
| 685 | + ); |
| 686 | + } |
| 687 | + |
| 688 | + const size = Bitstring.resolveSegmentSize(segment); |
| 689 | + const unit = Bitstring.resolveSegmentUnit(segment); |
| 690 | + const numBits = size * unit; |
| 691 | + |
| 692 | + if (![16n, 32n, 64n].includes(numBits)) { |
| 693 | + Bitstring.#raiseTypeMismatchError( |
| 694 | + index, |
| 695 | + "integer", |
| 696 | + "an integer", |
| 697 | + segment.value, |
| 698 | + ); |
| 699 | + } |
| 700 | + |
| 701 | + if (numBits !== 64n && numBits !== 32n) { |
| 702 | + throw new HologramInterpreterError( |
| 703 | + `${numBits}-bit float bitstring segments are not yet implemented in Hologram`, |
| 704 | + ); |
| 705 | + } |
| 706 | + |
| 707 | + return true; |
| 708 | + } |
| 709 | + |
| 710 | + static #validateIntegerSegment(segment, index) { |
| 711 | + if (!["integer", "variable_pattern"].includes(segment.value.type)) { |
| 712 | + Bitstring.#raiseTypeMismatchError( |
| 713 | + index, |
| 714 | + "integer", |
| 715 | + "an integer", |
| 716 | + segment.value, |
| 717 | + ); |
| 718 | + } |
| 719 | + |
| 720 | + return true; |
| 721 | + } |
| 722 | + |
| 723 | + static #validateUtfSegment(segment, index) { |
| 724 | + if (["bitstring", "float"].includes(segment.value.type)) { |
| 725 | + Bitstring.#raiseTypeMismatchError( |
| 726 | + index, |
| 727 | + segment.type, |
| 728 | + "a non-negative integer encodable as " + segment.type, |
| 729 | + segment.value, |
| 730 | + ); |
| 731 | + } |
| 732 | + |
| 733 | + if ( |
| 734 | + segment.signedness !== null || |
| 735 | + segment.size !== null || |
| 736 | + segment.unit !== null |
| 737 | + ) { |
| 738 | + Bitstring.#raiseTypeMismatchError( |
| 739 | + index, |
| 740 | + "integer", |
| 741 | + "an integer", |
| 742 | + segment.value, |
| 743 | + ); |
| 744 | + } |
| 745 | + |
| 746 | + return true; |
| 747 | + } |
| 748 | + } |
Loading more files…