├── .gitignore ├── .travis-ci.sh ├── .travis.yml ├── .travis └── gh-js.enc ├── LICENSE ├── README.md └── toplevel ├── .depend ├── LICENSE ├── Makefile ├── colorize.cppo.ml ├── colorize.mli ├── eval.html ├── eval.ml ├── examples.ml ├── indent.cppo.ml ├── indent.mli ├── index.html ├── reason.png ├── server.ml ├── test_dynlink.ml └── toplevel.cppo.ml /.gitignore: -------------------------------------------------------------------------------- 1 | *.annot 2 | *.cmo 3 | *.cma 4 | *.cmi 5 | *.a 6 | *.o 7 | *.cmx 8 | *.cmxs 9 | *.cmxa 10 | 11 | # ocamlbuild working directory 12 | _build/ 13 | 14 | # ocamlbuild targets 15 | *.byte 16 | *.native 17 | 18 | # oasis generated files 19 | setup.data 20 | setup.log 21 | -------------------------------------------------------------------------------- /.travis-ci.sh: -------------------------------------------------------------------------------- 1 | wget https://raw.githubusercontent.com/ocaml/ocaml-travisci-skeleton/master/.travis-ocaml.sh 2 | sh .travis-ocaml.sh 3 | 4 | eval `opam config env` 5 | 6 | 7 | opam install --yes tyxml deriving reactivedata react graphics js_of_ocaml 8 | opam pin add --yes BetterErrors https://github.com/chenglou/BetterErrors.git 9 | opam pin add --yes merlin https://github.com/the-lambda-church/merlin.git#87ea0e7998c04f16e4821676c27f19d3879dc2d1 10 | opam pin add --yes merlin_extend https://github.com/def-lkb/merlin-extend.git#ef634252a793542b05ec00a90f3c17de8fe0a357 11 | opam pin add --yes higlo https://github.com/Engil/higlo.git#reason-lexer 12 | opam pin add --yes reason https://github.com/facebook/reason.git 13 | cd toplevel 14 | WITH_GRAPHICS=YES opam config exec -- make 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | sudo: required 3 | script: bash -ex .travis-ci.sh 4 | os: 5 | - linux 6 | env: 7 | - OCAML_VERSION=4.02 8 | before_install: 9 | - openssl aes-256-cbc -K $encrypted_cffc400c9eab_key -iv $encrypted_cffc400c9eab_iv 10 | -in .travis/gh-js.enc -out gh-js -d 11 | 12 | after_script: 13 | - eval "$(ssh-agent -s)" 14 | - chmod 600 gh-js 15 | - ssh-add gh-js 16 | - git clone git@github.com:Engil/reason-web-toplevel.git deploy 17 | - cd deploy 18 | - git checkout gh-pages 19 | - cp ../toplevel/*.js ../toplevel/*.html ../toplevel/*.png . 20 | - git add *.js *.html *.png 21 | - git config --global user.email "travis@travis-ci.com" 22 | - git config --global user.name "Travis" 23 | - git commit -m "snapshot $(date '+%m/%d/%y %H:%M')" 24 | - git push origin gh-pages -------------------------------------------------------------------------------- /.travis/gh-js.enc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abbysmal/reason-web-toplevel/c55d1a2d77bb2a2f1591edab51d20380ba060948/.travis/gh-js.enc -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Code under the toplevel/ directory is extracted from the js_of_ocaml distribution and is released under the same license as its core project. 2 | See toplevel/LICENSE for more details. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # reason-web-toplevel [![Build Status](https://travis-ci.org/Engil/reason-web-toplevel.svg?branch=master)](https://travis-ci.org/Engil/reason-web-toplevel) 2 | A Reason web toplevel built using js_of_ocaml 3 | 4 | To make this word, you need 5 | 6 | - The latest git version of BetterErrors: the one released on opam contains a typo preventing us to build correctly a javascript version of it. 7 | 8 | 9 | - `js_of_ocaml` 2.7, haven't tested 2.8. 10 | 11 | 12 | - Higlo must be installed, preferably from this branch to get reason syntax highlighting workinig: https://github.com/Engil/higlo/tree/reason-lexer 13 | -------------------------------------------------------------------------------- /toplevel/.depend: -------------------------------------------------------------------------------- 1 | examples.cmo : 2 | examples.cmx : 3 | server.cmo : server.cmo 4 | server.cmx : server.cmx 5 | toplevel.cmo : ../compiler/pretty_print.cmi ../compiler/option.cmi \ 6 | ../compiler/driver.cmi 7 | toplevel.cmx : ../compiler/pretty_print.cmx ../compiler/option.cmx \ 8 | ../compiler/driver.cmx 9 | -------------------------------------------------------------------------------- /toplevel/LICENSE: -------------------------------------------------------------------------------- 1 | In the following, "the Compiler and Library" refers to all files 2 | marked "Copyright CNRS Université Paris Diderot" in the following 3 | directories and their sub-directories: 4 | 5 | compiler, lib, runtime, toplevel, tools 6 | 7 | and "the Examples" refers to all files marked "Copyright CNRS 8 | Université Paris Diderot" in directory "examples" and its 9 | sub-directories. 10 | 11 | The Compiler and Library are distributed under the terms of the GNU 12 | Library General Public License version 2 (included below). 13 | 14 | The Examples are distributed under the terms of the GNU Library 15 | General Public License version 2 (included below). 16 | 17 | As a special exception to the GNU Library General Public License, you 18 | may link, statically or dynamically, a "work that uses the Library" 19 | with a publicly distributed version of the Library to produce an 20 | executable file containing portions of the Library, and distribute 21 | that executable file under terms of your choice, without any of the 22 | additional requirements listed in clause 6 of the GNU Library General 23 | Public License. By "a publicly distributed version of the Library", 24 | we mean either the unmodified Library, or a modified version of the 25 | Library that is distributed under the conditions defined in clause 3 26 | of the GNU Library General Public License. This exception does not 27 | however invalidate any other reasons why the executable file might be 28 | covered by the GNU Library General Public License. 29 | 30 | ---------------------------------------------------------------------- 31 | 32 | GNU GENERAL PUBLIC LICENSE 33 | Version 2, June 1991 34 | 35 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 36 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 37 | Everyone is permitted to copy and distribute verbatim copies 38 | of this license document, but changing it is not allowed. 39 | 40 | Preamble 41 | 42 | The licenses for most software are designed to take away your 43 | freedom to share and change it. By contrast, the GNU General Public 44 | License is intended to guarantee your freedom to share and change free 45 | software--to make sure the software is free for all its users. This 46 | General Public License applies to most of the Free Software 47 | Foundation's software and to any other program whose authors commit to 48 | using it. (Some other Free Software Foundation software is covered by 49 | the GNU Lesser General Public License instead.) You can apply it to 50 | your programs, too. 51 | 52 | When we speak of free software, we are referring to freedom, not 53 | price. Our General Public Licenses are designed to make sure that you 54 | have the freedom to distribute copies of free software (and charge for 55 | this service if you wish), that you receive source code or can get it 56 | if you want it, that you can change the software or use pieces of it 57 | in new free programs; and that you know you can do these things. 58 | 59 | To protect your rights, we need to make restrictions that forbid 60 | anyone to deny you these rights or to ask you to surrender the rights. 61 | These restrictions translate to certain responsibilities for you if you 62 | distribute copies of the software, or if you modify it. 63 | 64 | For example, if you distribute copies of such a program, whether 65 | gratis or for a fee, you must give the recipients all the rights that 66 | you have. You must make sure that they, too, receive or can get the 67 | source code. And you must show them these terms so they know their 68 | rights. 69 | 70 | We protect your rights with two steps: (1) copyright the software, and 71 | (2) offer you this license which gives you legal permission to copy, 72 | distribute and/or modify the software. 73 | 74 | Also, for each author's protection and ours, we want to make certain 75 | that everyone understands that there is no warranty for this free 76 | software. If the software is modified by someone else and passed on, we 77 | want its recipients to know that what they have is not the original, so 78 | that any problems introduced by others will not reflect on the original 79 | authors' reputations. 80 | 81 | Finally, any free program is threatened constantly by software 82 | patents. We wish to avoid the danger that redistributors of a free 83 | program will individually obtain patent licenses, in effect making the 84 | program proprietary. To prevent this, we have made it clear that any 85 | patent must be licensed for everyone's free use or not licensed at all. 86 | 87 | The precise terms and conditions for copying, distribution and 88 | modification follow. 89 | 90 | GNU GENERAL PUBLIC LICENSE 91 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 92 | 93 | 0. This License applies to any program or other work which contains 94 | a notice placed by the copyright holder saying it may be distributed 95 | under the terms of this General Public License. The "Program", below, 96 | refers to any such program or work, and a "work based on the Program" 97 | means either the Program or any derivative work under copyright law: 98 | that is to say, a work containing the Program or a portion of it, 99 | either verbatim or with modifications and/or translated into another 100 | language. (Hereinafter, translation is included without limitation in 101 | the term "modification".) Each licensee is addressed as "you". 102 | 103 | Activities other than copying, distribution and modification are not 104 | covered by this License; they are outside its scope. The act of 105 | running the Program is not restricted, and the output from the Program 106 | is covered only if its contents constitute a work based on the 107 | Program (independent of having been made by running the Program). 108 | Whether that is true depends on what the Program does. 109 | 110 | 1. You may copy and distribute verbatim copies of the Program's 111 | source code as you receive it, in any medium, provided that you 112 | conspicuously and appropriately publish on each copy an appropriate 113 | copyright notice and disclaimer of warranty; keep intact all the 114 | notices that refer to this License and to the absence of any warranty; 115 | and give any other recipients of the Program a copy of this License 116 | along with the Program. 117 | 118 | You may charge a fee for the physical act of transferring a copy, and 119 | you may at your option offer warranty protection in exchange for a fee. 120 | 121 | 2. You may modify your copy or copies of the Program or any portion 122 | of it, thus forming a work based on the Program, and copy and 123 | distribute such modifications or work under the terms of Section 1 124 | above, provided that you also meet all of these conditions: 125 | 126 | a) You must cause the modified files to carry prominent notices 127 | stating that you changed the files and the date of any change. 128 | 129 | b) You must cause any work that you distribute or publish, that in 130 | whole or in part contains or is derived from the Program or any 131 | part thereof, to be licensed as a whole at no charge to all third 132 | parties under the terms of this License. 133 | 134 | c) If the modified program normally reads commands interactively 135 | when run, you must cause it, when started running for such 136 | interactive use in the most ordinary way, to print or display an 137 | announcement including an appropriate copyright notice and a 138 | notice that there is no warranty (or else, saying that you provide 139 | a warranty) and that users may redistribute the program under 140 | these conditions, and telling the user how to view a copy of this 141 | License. (Exception: if the Program itself is interactive but 142 | does not normally print such an announcement, your work based on 143 | the Program is not required to print an announcement.) 144 | 145 | These requirements apply to the modified work as a whole. If 146 | identifiable sections of that work are not derived from the Program, 147 | and can be reasonably considered independent and separate works in 148 | themselves, then this License, and its terms, do not apply to those 149 | sections when you distribute them as separate works. But when you 150 | distribute the same sections as part of a whole which is a work based 151 | on the Program, the distribution of the whole must be on the terms of 152 | this License, whose permissions for other licensees extend to the 153 | entire whole, and thus to each and every part regardless of who wrote it. 154 | 155 | Thus, it is not the intent of this section to claim rights or contest 156 | your rights to work written entirely by you; rather, the intent is to 157 | exercise the right to control the distribution of derivative or 158 | collective works based on the Program. 159 | 160 | In addition, mere aggregation of another work not based on the Program 161 | with the Program (or with a work based on the Program) on a volume of 162 | a storage or distribution medium does not bring the other work under 163 | the scope of this License. 164 | 165 | 3. You may copy and distribute the Program (or a work based on it, 166 | under Section 2) in object code or executable form under the terms of 167 | Sections 1 and 2 above provided that you also do one of the following: 168 | 169 | a) Accompany it with the complete corresponding machine-readable 170 | source code, which must be distributed under the terms of Sections 171 | 1 and 2 above on a medium customarily used for software interchange; or, 172 | 173 | b) Accompany it with a written offer, valid for at least three 174 | years, to give any third party, for a charge no more than your 175 | cost of physically performing source distribution, a complete 176 | machine-readable copy of the corresponding source code, to be 177 | distributed under the terms of Sections 1 and 2 above on a medium 178 | customarily used for software interchange; or, 179 | 180 | c) Accompany it with the information you received as to the offer 181 | to distribute corresponding source code. (This alternative is 182 | allowed only for noncommercial distribution and only if you 183 | received the program in object code or executable form with such 184 | an offer, in accord with Subsection b above.) 185 | 186 | The source code for a work means the preferred form of the work for 187 | making modifications to it. For an executable work, complete source 188 | code means all the source code for all modules it contains, plus any 189 | associated interface definition files, plus the scripts used to 190 | control compilation and installation of the executable. However, as a 191 | special exception, the source code distributed need not include 192 | anything that is normally distributed (in either source or binary 193 | form) with the major components (compiler, kernel, and so on) of the 194 | operating system on which the executable runs, unless that component 195 | itself accompanies the executable. 196 | 197 | If distribution of executable or object code is made by offering 198 | access to copy from a designated place, then offering equivalent 199 | access to copy the source code from the same place counts as 200 | distribution of the source code, even though third parties are not 201 | compelled to copy the source along with the object code. 202 | 203 | 4. You may not copy, modify, sublicense, or distribute the Program 204 | except as expressly provided under this License. Any attempt 205 | otherwise to copy, modify, sublicense or distribute the Program is 206 | void, and will automatically terminate your rights under this License. 207 | However, parties who have received copies, or rights, from you under 208 | this License will not have their licenses terminated so long as such 209 | parties remain in full compliance. 210 | 211 | 5. You are not required to accept this License, since you have not 212 | signed it. However, nothing else grants you permission to modify or 213 | distribute the Program or its derivative works. These actions are 214 | prohibited by law if you do not accept this License. Therefore, by 215 | modifying or distributing the Program (or any work based on the 216 | Program), you indicate your acceptance of this License to do so, and 217 | all its terms and conditions for copying, distributing or modifying 218 | the Program or works based on it. 219 | 220 | 6. Each time you redistribute the Program (or any work based on the 221 | Program), the recipient automatically receives a license from the 222 | original licensor to copy, distribute or modify the Program subject to 223 | these terms and conditions. You may not impose any further 224 | restrictions on the recipients' exercise of the rights granted herein. 225 | You are not responsible for enforcing compliance by third parties to 226 | this License. 227 | 228 | 7. If, as a consequence of a court judgment or allegation of patent 229 | infringement or for any other reason (not limited to patent issues), 230 | conditions are imposed on you (whether by court order, agreement or 231 | otherwise) that contradict the conditions of this License, they do not 232 | excuse you from the conditions of this License. If you cannot 233 | distribute so as to satisfy simultaneously your obligations under this 234 | License and any other pertinent obligations, then as a consequence you 235 | may not distribute the Program at all. For example, if a patent 236 | license would not permit royalty-free redistribution of the Program by 237 | all those who receive copies directly or indirectly through you, then 238 | the only way you could satisfy both it and this License would be to 239 | refrain entirely from distribution of the Program. 240 | 241 | If any portion of this section is held invalid or unenforceable under 242 | any particular circumstance, the balance of the section is intended to 243 | apply and the section as a whole is intended to apply in other 244 | circumstances. 245 | 246 | It is not the purpose of this section to induce you to infringe any 247 | patents or other property right claims or to contest validity of any 248 | such claims; this section has the sole purpose of protecting the 249 | integrity of the free software distribution system, which is 250 | implemented by public license practices. Many people have made 251 | generous contributions to the wide range of software distributed 252 | through that system in reliance on consistent application of that 253 | system; it is up to the author/donor to decide if he or she is willing 254 | to distribute software through any other system and a licensee cannot 255 | impose that choice. 256 | 257 | This section is intended to make thoroughly clear what is believed to 258 | be a consequence of the rest of this License. 259 | 260 | 8. If the distribution and/or use of the Program is restricted in 261 | certain countries either by patents or by copyrighted interfaces, the 262 | original copyright holder who places the Program under this License 263 | may add an explicit geographical distribution limitation excluding 264 | those countries, so that distribution is permitted only in or among 265 | countries not thus excluded. In such case, this License incorporates 266 | the limitation as if written in the body of this License. 267 | 268 | 9. The Free Software Foundation may publish revised and/or new versions 269 | of the General Public License from time to time. Such new versions will 270 | be similar in spirit to the present version, but may differ in detail to 271 | address new problems or concerns. 272 | 273 | Each version is given a distinguishing version number. If the Program 274 | specifies a version number of this License which applies to it and "any 275 | later version", you have the option of following the terms and conditions 276 | either of that version or of any later version published by the Free 277 | Software Foundation. If the Program does not specify a version number of 278 | this License, you may choose any version ever published by the Free Software 279 | Foundation. 280 | 281 | 10. If you wish to incorporate parts of the Program into other free 282 | programs whose distribution conditions are different, write to the author 283 | to ask for permission. For software which is copyrighted by the Free 284 | Software Foundation, write to the Free Software Foundation; we sometimes 285 | make exceptions for this. Our decision will be guided by the two goals 286 | of preserving the free status of all derivatives of our free software and 287 | of promoting the sharing and reuse of software generally. 288 | 289 | NO WARRANTY 290 | 291 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 292 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 293 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 294 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 295 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 296 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 297 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 298 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 299 | REPAIR OR CORRECTION. 300 | 301 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 302 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 303 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 304 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 305 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 306 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 307 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 308 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 309 | POSSIBILITY OF SUCH DAMAGES. 310 | 311 | END OF TERMS AND CONDITIONS 312 | 313 | How to Apply These Terms to Your New Programs 314 | 315 | If you develop a new program, and you want it to be of the greatest 316 | possible use to the public, the best way to achieve this is to make it 317 | free software which everyone can redistribute and change under these terms. 318 | 319 | To do so, attach the following notices to the program. It is safest 320 | to attach them to the start of each source file to most effectively 321 | convey the exclusion of warranty; and each file should have at least 322 | the "copyright" line and a pointer to where the full notice is found. 323 | 324 | 325 | Copyright (C) 326 | 327 | This program is free software; you can redistribute it and/or modify 328 | it under the terms of the GNU General Public License as published by 329 | the Free Software Foundation; either version 2 of the License, or 330 | (at your option) any later version. 331 | 332 | This program is distributed in the hope that it will be useful, 333 | but WITHOUT ANY WARRANTY; without even the implied warranty of 334 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 335 | GNU General Public License for more details. 336 | 337 | You should have received a copy of the GNU General Public License along 338 | with this program; if not, write to the Free Software Foundation, Inc., 339 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 340 | 341 | Also add information on how to contact you by electronic and paper mail. 342 | 343 | If the program is interactive, make it output a short notice like this 344 | when it starts in an interactive mode: 345 | 346 | Gnomovision version 69, Copyright (C) year name of author 347 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 348 | This is free software, and you are welcome to redistribute it 349 | under certain conditions; type `show c' for details. 350 | 351 | The hypothetical commands `show w' and `show c' should show the appropriate 352 | parts of the General Public License. Of course, the commands you use may 353 | be called something other than `show w' and `show c'; they could even be 354 | mouse-clicks or menu items--whatever suits your program. 355 | 356 | You should also get your employer (if you work as a programmer) or your 357 | school, if any, to sign a "copyright disclaimer" for the program, if 358 | necessary. Here is a sample; alter the names: 359 | 360 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 361 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 362 | 363 | , 1 April 1989 364 | Ty Coon, President of Vice 365 | 366 | This General Public License does not permit incorporating your program into 367 | proprietary programs. If your program is a subroutine library, you may 368 | consider it more useful to permit linking proprietary applications with the 369 | library. If this is what you want to do, use the GNU Lesser General 370 | Public License instead of this License. 371 | 372 | ---------------------------------------------------------------------- 373 | 374 | GNU LESSER GENERAL PUBLIC LICENSE 375 | Version 2.1, February 1999 376 | 377 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 378 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 379 | Everyone is permitted to copy and distribute verbatim copies 380 | of this license document, but changing it is not allowed. 381 | 382 | [This is the first released version of the Lesser GPL. It also counts 383 | as the successor of the GNU Library Public License, version 2, hence 384 | the version number 2.1.] 385 | 386 | Preamble 387 | 388 | The licenses for most software are designed to take away your 389 | freedom to share and change it. By contrast, the GNU General Public 390 | Licenses are intended to guarantee your freedom to share and change 391 | free software--to make sure the software is free for all its users. 392 | 393 | This license, the Lesser General Public License, applies to some 394 | specially designated software packages--typically libraries--of the 395 | Free Software Foundation and other authors who decide to use it. You 396 | can use it too, but we suggest you first think carefully about whether 397 | this license or the ordinary General Public License is the better 398 | strategy to use in any particular case, based on the explanations below. 399 | 400 | When we speak of free software, we are referring to freedom of use, 401 | not price. Our General Public Licenses are designed to make sure that 402 | you have the freedom to distribute copies of free software (and charge 403 | for this service if you wish); that you receive source code or can get 404 | it if you want it; that you can change the software and use pieces of 405 | it in new free programs; and that you are informed that you can do 406 | these things. 407 | 408 | To protect your rights, we need to make restrictions that forbid 409 | distributors to deny you these rights or to ask you to surrender these 410 | rights. These restrictions translate to certain responsibilities for 411 | you if you distribute copies of the library or if you modify it. 412 | 413 | For example, if you distribute copies of the library, whether gratis 414 | or for a fee, you must give the recipients all the rights that we gave 415 | you. You must make sure that they, too, receive or can get the source 416 | code. If you link other code with the library, you must provide 417 | complete object files to the recipients, so that they can relink them 418 | with the library after making changes to the library and recompiling 419 | it. And you must show them these terms so they know their rights. 420 | 421 | We protect your rights with a two-step method: (1) we copyright the 422 | library, and (2) we offer you this license, which gives you legal 423 | permission to copy, distribute and/or modify the library. 424 | 425 | To protect each distributor, we want to make it very clear that 426 | there is no warranty for the free library. Also, if the library is 427 | modified by someone else and passed on, the recipients should know 428 | that what they have is not the original version, so that the original 429 | author's reputation will not be affected by problems that might be 430 | introduced by others. 431 | 432 | Finally, software patents pose a constant threat to the existence of 433 | any free program. We wish to make sure that a company cannot 434 | effectively restrict the users of a free program by obtaining a 435 | restrictive license from a patent holder. Therefore, we insist that 436 | any patent license obtained for a version of the library must be 437 | consistent with the full freedom of use specified in this license. 438 | 439 | Most GNU software, including some libraries, is covered by the 440 | ordinary GNU General Public License. This license, the GNU Lesser 441 | General Public License, applies to certain designated libraries, and 442 | is quite different from the ordinary General Public License. We use 443 | this license for certain libraries in order to permit linking those 444 | libraries into non-free programs. 445 | 446 | When a program is linked with a library, whether statically or using 447 | a shared library, the combination of the two is legally speaking a 448 | combined work, a derivative of the original library. The ordinary 449 | General Public License therefore permits such linking only if the 450 | entire combination fits its criteria of freedom. The Lesser General 451 | Public License permits more lax criteria for linking other code with 452 | the library. 453 | 454 | We call this license the "Lesser" General Public License because it 455 | does Less to protect the user's freedom than the ordinary General 456 | Public License. It also provides other free software developers Less 457 | of an advantage over competing non-free programs. These disadvantages 458 | are the reason we use the ordinary General Public License for many 459 | libraries. However, the Lesser license provides advantages in certain 460 | special circumstances. 461 | 462 | For example, on rare occasions, there may be a special need to 463 | encourage the widest possible use of a certain library, so that it becomes 464 | a de-facto standard. To achieve this, non-free programs must be 465 | allowed to use the library. A more frequent case is that a free 466 | library does the same job as widely used non-free libraries. In this 467 | case, there is little to gain by limiting the free library to free 468 | software only, so we use the Lesser General Public License. 469 | 470 | In other cases, permission to use a particular library in non-free 471 | programs enables a greater number of people to use a large body of 472 | free software. For example, permission to use the GNU C Library in 473 | non-free programs enables many more people to use the whole GNU 474 | operating system, as well as its variant, the GNU/Linux operating 475 | system. 476 | 477 | Although the Lesser General Public License is Less protective of the 478 | users' freedom, it does ensure that the user of a program that is 479 | linked with the Library has the freedom and the wherewithal to run 480 | that program using a modified version of the Library. 481 | 482 | The precise terms and conditions for copying, distribution and 483 | modification follow. Pay close attention to the difference between a 484 | "work based on the library" and a "work that uses the library". The 485 | former contains code derived from the library, whereas the latter must 486 | be combined with the library in order to run. 487 | 488 | GNU LESSER GENERAL PUBLIC LICENSE 489 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 490 | 491 | 0. This License Agreement applies to any software library or other 492 | program which contains a notice placed by the copyright holder or 493 | other authorized party saying it may be distributed under the terms of 494 | this Lesser General Public License (also called "this License"). 495 | Each licensee is addressed as "you". 496 | 497 | A "library" means a collection of software functions and/or data 498 | prepared so as to be conveniently linked with application programs 499 | (which use some of those functions and data) to form executables. 500 | 501 | The "Library", below, refers to any such software library or work 502 | which has been distributed under these terms. A "work based on the 503 | Library" means either the Library or any derivative work under 504 | copyright law: that is to say, a work containing the Library or a 505 | portion of it, either verbatim or with modifications and/or translated 506 | straightforwardly into another language. (Hereinafter, translation is 507 | included without limitation in the term "modification".) 508 | 509 | "Source code" for a work means the preferred form of the work for 510 | making modifications to it. For a library, complete source code means 511 | all the source code for all modules it contains, plus any associated 512 | interface definition files, plus the scripts used to control compilation 513 | and installation of the library. 514 | 515 | Activities other than copying, distribution and modification are not 516 | covered by this License; they are outside its scope. The act of 517 | running a program using the Library is not restricted, and output from 518 | such a program is covered only if its contents constitute a work based 519 | on the Library (independent of the use of the Library in a tool for 520 | writing it). Whether that is true depends on what the Library does 521 | and what the program that uses the Library does. 522 | 523 | 1. You may copy and distribute verbatim copies of the Library's 524 | complete source code as you receive it, in any medium, provided that 525 | you conspicuously and appropriately publish on each copy an 526 | appropriate copyright notice and disclaimer of warranty; keep intact 527 | all the notices that refer to this License and to the absence of any 528 | warranty; and distribute a copy of this License along with the 529 | Library. 530 | 531 | You may charge a fee for the physical act of transferring a copy, 532 | and you may at your option offer warranty protection in exchange for a 533 | fee. 534 | 535 | 2. You may modify your copy or copies of the Library or any portion 536 | of it, thus forming a work based on the Library, and copy and 537 | distribute such modifications or work under the terms of Section 1 538 | above, provided that you also meet all of these conditions: 539 | 540 | a) The modified work must itself be a software library. 541 | 542 | b) You must cause the files modified to carry prominent notices 543 | stating that you changed the files and the date of any change. 544 | 545 | c) You must cause the whole of the work to be licensed at no 546 | charge to all third parties under the terms of this License. 547 | 548 | d) If a facility in the modified Library refers to a function or a 549 | table of data to be supplied by an application program that uses 550 | the facility, other than as an argument passed when the facility 551 | is invoked, then you must make a good faith effort to ensure that, 552 | in the event an application does not supply such function or 553 | table, the facility still operates, and performs whatever part of 554 | its purpose remains meaningful. 555 | 556 | (For example, a function in a library to compute square roots has 557 | a purpose that is entirely well-defined independent of the 558 | application. Therefore, Subsection 2d requires that any 559 | application-supplied function or table used by this function must 560 | be optional: if the application does not supply it, the square 561 | root function must still compute square roots.) 562 | 563 | These requirements apply to the modified work as a whole. If 564 | identifiable sections of that work are not derived from the Library, 565 | and can be reasonably considered independent and separate works in 566 | themselves, then this License, and its terms, do not apply to those 567 | sections when you distribute them as separate works. But when you 568 | distribute the same sections as part of a whole which is a work based 569 | on the Library, the distribution of the whole must be on the terms of 570 | this License, whose permissions for other licensees extend to the 571 | entire whole, and thus to each and every part regardless of who wrote 572 | it. 573 | 574 | Thus, it is not the intent of this section to claim rights or contest 575 | your rights to work written entirely by you; rather, the intent is to 576 | exercise the right to control the distribution of derivative or 577 | collective works based on the Library. 578 | 579 | In addition, mere aggregation of another work not based on the Library 580 | with the Library (or with a work based on the Library) on a volume of 581 | a storage or distribution medium does not bring the other work under 582 | the scope of this License. 583 | 584 | 3. You may opt to apply the terms of the ordinary GNU General Public 585 | License instead of this License to a given copy of the Library. To do 586 | this, you must alter all the notices that refer to this License, so 587 | that they refer to the ordinary GNU General Public License, version 2, 588 | instead of to this License. (If a newer version than version 2 of the 589 | ordinary GNU General Public License has appeared, then you can specify 590 | that version instead if you wish.) Do not make any other change in 591 | these notices. 592 | 593 | Once this change is made in a given copy, it is irreversible for 594 | that copy, so the ordinary GNU General Public License applies to all 595 | subsequent copies and derivative works made from that copy. 596 | 597 | This option is useful when you wish to copy part of the code of 598 | the Library into a program that is not a library. 599 | 600 | 4. You may copy and distribute the Library (or a portion or 601 | derivative of it, under Section 2) in object code or executable form 602 | under the terms of Sections 1 and 2 above provided that you accompany 603 | it with the complete corresponding machine-readable source code, which 604 | must be distributed under the terms of Sections 1 and 2 above on a 605 | medium customarily used for software interchange. 606 | 607 | If distribution of object code is made by offering access to copy 608 | from a designated place, then offering equivalent access to copy the 609 | source code from the same place satisfies the requirement to 610 | distribute the source code, even though third parties are not 611 | compelled to copy the source along with the object code. 612 | 613 | 5. A program that contains no derivative of any portion of the 614 | Library, but is designed to work with the Library by being compiled or 615 | linked with it, is called a "work that uses the Library". Such a 616 | work, in isolation, is not a derivative work of the Library, and 617 | therefore falls outside the scope of this License. 618 | 619 | However, linking a "work that uses the Library" with the Library 620 | creates an executable that is a derivative of the Library (because it 621 | contains portions of the Library), rather than a "work that uses the 622 | library". The executable is therefore covered by this License. 623 | Section 6 states terms for distribution of such executables. 624 | 625 | When a "work that uses the Library" uses material from a header file 626 | that is part of the Library, the object code for the work may be a 627 | derivative work of the Library even though the source code is not. 628 | Whether this is true is especially significant if the work can be 629 | linked without the Library, or if the work is itself a library. The 630 | threshold for this to be true is not precisely defined by law. 631 | 632 | If such an object file uses only numerical parameters, data 633 | structure layouts and accessors, and small macros and small inline 634 | functions (ten lines or less in length), then the use of the object 635 | file is unrestricted, regardless of whether it is legally a derivative 636 | work. (Executables containing this object code plus portions of the 637 | Library will still fall under Section 6.) 638 | 639 | Otherwise, if the work is a derivative of the Library, you may 640 | distribute the object code for the work under the terms of Section 6. 641 | Any executables containing that work also fall under Section 6, 642 | whether or not they are linked directly with the Library itself. 643 | 644 | 6. As an exception to the Sections above, you may also combine or 645 | link a "work that uses the Library" with the Library to produce a 646 | work containing portions of the Library, and distribute that work 647 | under terms of your choice, provided that the terms permit 648 | modification of the work for the customer's own use and reverse 649 | engineering for debugging such modifications. 650 | 651 | You must give prominent notice with each copy of the work that the 652 | Library is used in it and that the Library and its use are covered by 653 | this License. You must supply a copy of this License. If the work 654 | during execution displays copyright notices, you must include the 655 | copyright notice for the Library among them, as well as a reference 656 | directing the user to the copy of this License. Also, you must do one 657 | of these things: 658 | 659 | a) Accompany the work with the complete corresponding 660 | machine-readable source code for the Library including whatever 661 | changes were used in the work (which must be distributed under 662 | Sections 1 and 2 above); and, if the work is an executable linked 663 | with the Library, with the complete machine-readable "work that 664 | uses the Library", as object code and/or source code, so that the 665 | user can modify the Library and then relink to produce a modified 666 | executable containing the modified Library. (It is understood 667 | that the user who changes the contents of definitions files in the 668 | Library will not necessarily be able to recompile the application 669 | to use the modified definitions.) 670 | 671 | b) Use a suitable shared library mechanism for linking with the 672 | Library. A suitable mechanism is one that (1) uses at run time a 673 | copy of the library already present on the user's computer system, 674 | rather than copying library functions into the executable, and (2) 675 | will operate properly with a modified version of the library, if 676 | the user installs one, as long as the modified version is 677 | interface-compatible with the version that the work was made with. 678 | 679 | c) Accompany the work with a written offer, valid for at 680 | least three years, to give the same user the materials 681 | specified in Subsection 6a, above, for a charge no more 682 | than the cost of performing this distribution. 683 | 684 | d) If distribution of the work is made by offering access to copy 685 | from a designated place, offer equivalent access to copy the above 686 | specified materials from the same place. 687 | 688 | e) Verify that the user has already received a copy of these 689 | materials or that you have already sent this user a copy. 690 | 691 | For an executable, the required form of the "work that uses the 692 | Library" must include any data and utility programs needed for 693 | reproducing the executable from it. However, as a special exception, 694 | the materials to be distributed need not include anything that is 695 | normally distributed (in either source or binary form) with the major 696 | components (compiler, kernel, and so on) of the operating system on 697 | which the executable runs, unless that component itself accompanies 698 | the executable. 699 | 700 | It may happen that this requirement contradicts the license 701 | restrictions of other proprietary libraries that do not normally 702 | accompany the operating system. Such a contradiction means you cannot 703 | use both them and the Library together in an executable that you 704 | distribute. 705 | 706 | 7. You may place library facilities that are a work based on the 707 | Library side-by-side in a single library together with other library 708 | facilities not covered by this License, and distribute such a combined 709 | library, provided that the separate distribution of the work based on 710 | the Library and of the other library facilities is otherwise 711 | permitted, and provided that you do these two things: 712 | 713 | a) Accompany the combined library with a copy of the same work 714 | based on the Library, uncombined with any other library 715 | facilities. This must be distributed under the terms of the 716 | Sections above. 717 | 718 | b) Give prominent notice with the combined library of the fact 719 | that part of it is a work based on the Library, and explaining 720 | where to find the accompanying uncombined form of the same work. 721 | 722 | 8. You may not copy, modify, sublicense, link with, or distribute 723 | the Library except as expressly provided under this License. Any 724 | attempt otherwise to copy, modify, sublicense, link with, or 725 | distribute the Library is void, and will automatically terminate your 726 | rights under this License. However, parties who have received copies, 727 | or rights, from you under this License will not have their licenses 728 | terminated so long as such parties remain in full compliance. 729 | 730 | 9. You are not required to accept this License, since you have not 731 | signed it. However, nothing else grants you permission to modify or 732 | distribute the Library or its derivative works. These actions are 733 | prohibited by law if you do not accept this License. Therefore, by 734 | modifying or distributing the Library (or any work based on the 735 | Library), you indicate your acceptance of this License to do so, and 736 | all its terms and conditions for copying, distributing or modifying 737 | the Library or works based on it. 738 | 739 | 10. Each time you redistribute the Library (or any work based on the 740 | Library), the recipient automatically receives a license from the 741 | original licensor to copy, distribute, link with or modify the Library 742 | subject to these terms and conditions. You may not impose any further 743 | restrictions on the recipients' exercise of the rights granted herein. 744 | You are not responsible for enforcing compliance by third parties with 745 | this License. 746 | 747 | 11. If, as a consequence of a court judgment or allegation of patent 748 | infringement or for any other reason (not limited to patent issues), 749 | conditions are imposed on you (whether by court order, agreement or 750 | otherwise) that contradict the conditions of this License, they do not 751 | excuse you from the conditions of this License. If you cannot 752 | distribute so as to satisfy simultaneously your obligations under this 753 | License and any other pertinent obligations, then as a consequence you 754 | may not distribute the Library at all. For example, if a patent 755 | license would not permit royalty-free redistribution of the Library by 756 | all those who receive copies directly or indirectly through you, then 757 | the only way you could satisfy both it and this License would be to 758 | refrain entirely from distribution of the Library. 759 | 760 | If any portion of this section is held invalid or unenforceable under any 761 | particular circumstance, the balance of the section is intended to apply, 762 | and the section as a whole is intended to apply in other circumstances. 763 | 764 | It is not the purpose of this section to induce you to infringe any 765 | patents or other property right claims or to contest validity of any 766 | such claims; this section has the sole purpose of protecting the 767 | integrity of the free software distribution system which is 768 | implemented by public license practices. Many people have made 769 | generous contributions to the wide range of software distributed 770 | through that system in reliance on consistent application of that 771 | system; it is up to the author/donor to decide if he or she is willing 772 | to distribute software through any other system and a licensee cannot 773 | impose that choice. 774 | 775 | This section is intended to make thoroughly clear what is believed to 776 | be a consequence of the rest of this License. 777 | 778 | 12. If the distribution and/or use of the Library is restricted in 779 | certain countries either by patents or by copyrighted interfaces, the 780 | original copyright holder who places the Library under this License may add 781 | an explicit geographical distribution limitation excluding those countries, 782 | so that distribution is permitted only in or among countries not thus 783 | excluded. In such case, this License incorporates the limitation as if 784 | written in the body of this License. 785 | 786 | 13. The Free Software Foundation may publish revised and/or new 787 | versions of the Lesser General Public License from time to time. 788 | Such new versions will be similar in spirit to the present version, 789 | but may differ in detail to address new problems or concerns. 790 | 791 | Each version is given a distinguishing version number. If the Library 792 | specifies a version number of this License which applies to it and 793 | "any later version", you have the option of following the terms and 794 | conditions either of that version or of any later version published by 795 | the Free Software Foundation. If the Library does not specify a 796 | license version number, you may choose any version ever published by 797 | the Free Software Foundation. 798 | 799 | 14. If you wish to incorporate parts of the Library into other free 800 | programs whose distribution conditions are incompatible with these, 801 | write to the author to ask for permission. For software which is 802 | copyrighted by the Free Software Foundation, write to the Free 803 | Software Foundation; we sometimes make exceptions for this. Our 804 | decision will be guided by the two goals of preserving the free status 805 | of all derivatives of our free software and of promoting the sharing 806 | and reuse of software generally. 807 | 808 | NO WARRANTY 809 | 810 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 811 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 812 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 813 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 814 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 815 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 816 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 817 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 818 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 819 | 820 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 821 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 822 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 823 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 824 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 825 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 826 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 827 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 828 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 829 | DAMAGES. 830 | 831 | END OF TERMS AND CONDITIONS 832 | 833 | How to Apply These Terms to Your New Libraries 834 | 835 | If you develop a new library, and you want it to be of the greatest 836 | possible use to the public, we recommend making it free software that 837 | everyone can redistribute and change. You can do so by permitting 838 | redistribution under these terms (or, alternatively, under the terms of the 839 | ordinary General Public License). 840 | 841 | To apply these terms, attach the following notices to the library. It is 842 | safest to attach them to the start of each source file to most effectively 843 | convey the exclusion of warranty; and each file should have at least the 844 | "copyright" line and a pointer to where the full notice is found. 845 | 846 | 847 | Copyright (C) 848 | 849 | This library is free software; you can redistribute it and/or 850 | modify it under the terms of the GNU Lesser General Public 851 | License as published by the Free Software Foundation; either 852 | version 2.1 of the License, or (at your option) any later version. 853 | 854 | This library is distributed in the hope that it will be useful, 855 | but WITHOUT ANY WARRANTY; without even the implied warranty of 856 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 857 | Lesser General Public License for more details. 858 | 859 | You should have received a copy of the GNU Lesser General Public 860 | License along with this library; if not, write to the Free Software 861 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 862 | 863 | Also add information on how to contact you by electronic and paper mail. 864 | 865 | You should also get your employer (if you work as a programmer) or your 866 | school, if any, to sign a "copyright disclaimer" for the library, if 867 | necessary. Here is a sample; alter the names: 868 | 869 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 870 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 871 | 872 | , 1 April 1990 873 | Ty Coon, President of Vice 874 | 875 | That's all there is to it! 876 | -------------------------------------------------------------------------------- /toplevel/Makefile: -------------------------------------------------------------------------------- 1 | all: toplevel.js eval.js 2 | 3 | 4 | HIGLO=$(shell ocamlfind query higlo.reason -format "-package %p -rectypes" 2> /dev/null) 5 | DERIVING=$(shell ocamlfind query js_of_ocaml.deriving -format "%p" 2> /dev/null) 6 | 7 | ifneq ($(HIGLO),) 8 | CPPO_OPT+= -D higlo 9 | endif 10 | 11 | ifneq ($(DERIVING),) 12 | DERIVING_SYNTAX = js_of_ocaml.deriving.syntax deriving.syntax.std 13 | endif 14 | 15 | ifneq ($(NO_SYNTAXES),1) 16 | SYNTAXES= lwt.syntax js_of_ocaml.syntax $(DERIVING_SYNTAX) 17 | endif 18 | 19 | JSFILES= +weak.js +toplevel.js +dynlink.js +nat.js 20 | 21 | PACKAGES= \ 22 | lwt bigarray tyxml.functor \ 23 | react reactiveData \ 24 | js_of_ocaml $(DERIVING)\ 25 | js_of_ocaml.tyxml \ 26 | js_of_ocaml.toplevel \ 27 | str \ 28 | reason.lib \ 29 | dynlink 30 | 31 | ifeq ($(WITH_GRAPHICS),YES) 32 | JSFILES += +graphics.js 33 | PACKAGES += graphics js_of_ocaml.graphics 34 | CPPO_OPT+= -D graphics 35 | endif 36 | 37 | #-jsopt "--pretty" 38 | MKTOP=jsoo_mktop -verbose $(SAFESTRING) \ 39 | ${addprefix -top-syntax , ${SYNTAXES}} \ 40 | -dont-export-unit gc \ 41 | -jsopt "--disable shortvar" \ 42 | ${addprefix -export-package , ${PACKAGES}} \ 43 | ${addprefix -export-unit , html_types html_sigs xml_wrap } \ 44 | 45 | TOPLEVEL_NAME=toplevel 46 | TOPLEVEL_OBJS=colorize.cmo indent.cmo toplevel.cmo 47 | $(TOPLEVEL_NAME).js: $(TOPLEVEL_OBJS) examples.ml test_dynlink.cmo 48 | $(MKTOP) \ 49 | $(OCPINDENT) $(HIGLO) \ 50 | $(TOPLEVEL_OBJS) \ 51 | ${addprefix -jsopt , ${JSFILES}} \ 52 | ${addprefix -jsopt , -I ./ --file examples.ml} \ 53 | ${addprefix -jsopt , -I ./ --file test_dynlink.cmo} \ 54 | -package base64 \ 55 | -o $(TOPLEVEL_NAME).byte 56 | 57 | EVAL_NAME=eval 58 | EVAL_OBJS=eval.cmo 59 | $(EVAL_NAME).js: $(EVAL_OBJS) 60 | $(MKTOP) $(EVAL_OBJS) \ 61 | ${addprefix -jsopt , ${JSFILES}} \ 62 | -o $(EVAL_NAME).byte 63 | 64 | %.cmis.js: 65 | jsoo_mkcmis $* 66 | 67 | server.%: server.ml 68 | ocamlbuild -use-ocamlfind -package findlib,cohttp.lwt server.$* 69 | 70 | indent.cmo: indent.cmi 71 | colorize.cmo: colorize.cmi 72 | 73 | %.ml: %.cppo.ml 74 | cppo ${CPPO_OPT} $< -o $@ 75 | 76 | %.cmi:%.mli 77 | ocamlfind ocamlc -c -package js_of_ocaml,js_of_ocaml.tyxml $< 78 | %.cmo:%.ml 79 | ocamlfind ocamlc -c -syntax camlp4o $(SAFESTRING) \ 80 | -package js_of_ocaml.syntax,lwt,str,reason.lib,js_of_ocaml.tyxml,js_of_ocaml.toplevel \ 81 | -package base64 \ 82 | -I camlp4 \ 83 | $(HIGLO) \ 84 | $< -c $@ 85 | clean:: 86 | rm -f *.cm[io] *.byte *.native *.js 87 | -------------------------------------------------------------------------------- /toplevel/colorize.cppo.ml: -------------------------------------------------------------------------------- 1 | let text ~a_class:cl s = 2 | Tyxml_js.Html5.(span ~a:[a_class [cl]] [pcdata s]) 3 | 4 | #ifdef higlo 5 | let ocaml ~a_class:cl s = 6 | let tks = Higlo.parse ~lang:"reason" s in 7 | let span' cl s = Tyxml_js.Html5.(span ~a:[a_class [cl]] [pcdata s]) in 8 | let make_span = function 9 | | Higlo.Bcomment s -> span' "comment" s 10 | | Higlo.Constant s -> span' "constant" s 11 | | Higlo.Directive s -> span' "directive" s 12 | | Higlo.Escape s -> span' "escape" s 13 | | Higlo.Id s -> span' "id" s 14 | | Higlo.Keyword (level,s) -> span' (Printf.sprintf "kw%d" level) s 15 | | Higlo.Lcomment s -> span' "comment" s 16 | | Higlo.Numeric s -> span' "numeric" s 17 | | Higlo.String s -> span' "string" s 18 | | Higlo.Symbol (level,s) -> span' (Printf.sprintf "sym%d" level) s 19 | | Higlo.Text s -> span' "text" s in 20 | Tyxml_js.Html5.(div ~a:[a_class [cl]] (List.map make_span tks)) 21 | #else 22 | let ocaml = text 23 | #endif 24 | 25 | 26 | #ifdef higlo 27 | let highlight (`Pos from_) to_ e = 28 | let _ = 29 | List.fold_left (fun pos e -> 30 | match Js.Opt.to_option (Dom_html.CoerceTo.element e) with 31 | | None -> pos 32 | | Some e -> 33 | let size = Js.Opt.case (e##textContent) (fun () -> 0) (fun t -> t##length) in 34 | if pos + size > from_ && (to_ = `Last || `Pos pos < to_) 35 | then e##classList##add(Js.string "errorloc"); 36 | pos + size) 0 (Dom.list_of_nodeList (e##childNodes)) in 37 | ();; 38 | #else 39 | let highlight from_ to_ e = 40 | match Js.Opt.to_option e##textContent with 41 | | None -> assert false 42 | | Some x -> 43 | let x = Js.to_string x in 44 | let `Pos from_ = from_ in 45 | let to_ = match to_ with `Pos n -> n | `Last -> String.length x - 1 in 46 | e##innerHTML <- Js.string ""; 47 | let span kind s = 48 | if s <> "" 49 | then 50 | let span = Tyxml_js.Html5.(span ~a:[a_class [kind]] [pcdata s]) in 51 | Dom.appendChild e (Tyxml_js.To_dom.of_element span) in 52 | span "normal" (String.sub x 0 from_); 53 | span "errorloc" (String.sub x from_ (to_ - from_)); 54 | span "normal" (String.sub x to_ (String.length x - to_)) 55 | #endif 56 | -------------------------------------------------------------------------------- /toplevel/colorize.mli: -------------------------------------------------------------------------------- 1 | val text : a_class:string -> string 2 | -> [> Html_types.div_content ] Tyxml_js.Html5.elt 3 | 4 | val ocaml : a_class:string -> string 5 | -> [> Html_types.div_content ] Tyxml_js.Html5.elt 6 | 7 | 8 | val highlight : [`Pos of int] -> [`Last | `Pos of int] 9 | -> Dom_html.element Js.t -> unit 10 | -------------------------------------------------------------------------------- /toplevel/eval.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 20 |
21 | 22 | 196 |
197 | 198 | 199 | -------------------------------------------------------------------------------- /toplevel/eval.ml: -------------------------------------------------------------------------------- 1 | (* Js_of_ocaml toplevel 2 | * http://www.ocsigen.org/js_of_ocaml/ 3 | * Copyright (C) 2011 Jérôme Vouillon 4 | * Laboratoire PPS - CNRS Université Paris Diderot 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, with linking exception; 9 | * either version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 | *) 20 | 21 | let append_string output cl s = 22 | let d = Dom_html.window##document in 23 | let span = Dom_html.createDiv d in 24 | span##classList##add(Js.string cl); 25 | Dom.appendChild span (d##createTextNode (Js.string s)); 26 | Dom.appendChild output span 27 | 28 | let configure o chan attr default = 29 | try 30 | let v = o##getAttribute(Js.string attr) in 31 | match Js.Opt.to_option v with 32 | | None -> raise Not_found 33 | | Some id -> 34 | let dom = Dom_html.getElementById (Js.to_string id) in 35 | Sys_js.set_channel_flusher chan (append_string dom attr) 36 | with Not_found -> Sys_js.set_channel_flusher chan default 37 | 38 | 39 | let _ = Lwt.bind (Lwt_js_events.domContentLoaded ()) (fun () -> 40 | let toploop_ = open_out "/dev/null" in 41 | let toploop_ppf = Format.formatter_of_out_channel toploop_ in 42 | Lwt.async_exception_hook:= (fun exc -> Format.eprintf "exc during Lwt.async: %s@." (Printexc.to_string exc)); 43 | JsooTop.initialize (); 44 | let scripts = Dom_html.window##document##getElementsByTagName(Js.string "script") in 45 | let default_stdout = Format.printf "%s@." in 46 | let default_stderr = Format.eprintf "%s@." in 47 | let default_toploop x = () in 48 | for i = 0 to scripts##length - 1 do 49 | let item_opt = scripts##item(i) in 50 | let elt_opt = Js.Opt.bind item_opt Dom_html.CoerceTo.element in 51 | match Dom_html.opt_tagged elt_opt with 52 | | Some (Dom_html.Script script) -> 53 | if script##_type = Js.string "text/ocaml" 54 | then begin 55 | let txt = Js.to_string script##text in 56 | configure script stdout "stdout" default_stdout; 57 | configure script stderr "stderr" default_stderr; 58 | configure script toploop_ "toploop" default_toploop; 59 | let _ret = JsooTop.use toploop_ppf txt in 60 | () 61 | end 62 | else () 63 | | _ -> () 64 | done; 65 | Lwt.return_unit) 66 | -------------------------------------------------------------------------------- /toplevel/examples.ml: -------------------------------------------------------------------------------- 1 | (** Overview *) 2 | let x = 10+10; 3 | let y = x * 3; 4 | let c = String.make x 'a'; 5 | let sin1 = sin 1.; 6 | let rec fact n => if (n == 0) { 1. } else { float n *. fact (n - 1)}; 7 | Printf.printf "fact 20 = %f\n" (fact 20); 8 | "abc" < "def"; 9 | 10 | (** Graphics: PingPong *) 11 | open Graphics_js; 12 | let c = 3; 13 | let x0 = 0; 14 | let x1 = size_x (); 15 | let y0 = 0; 16 | let y1 = size_y (); 17 | 18 | let draw_ball x y => { 19 | set_color foreground; 20 | fill_circle x y c; }; 21 | 22 | 23 | let state = ref (Lwt.task ()); 24 | 25 | let wait () => { fst(!state) }; 26 | 27 | let rec pong_aux x y dx dy => { 28 | draw_ball x y; 29 | let new_x = x + dx; 30 | let new_y = y + dy; 31 | let new_dx = 32 | if (new_x - c <= x0 || new_x + c >= x1) { (- dx) } else { dx }; 33 | let new_dy = 34 | if (new_y - c <= y0 || new_y + c >= y1) { (- dy) } else { dy }; 35 | Lwt.bind (wait ()) (fun () => pong_aux new_x new_y new_dx new_dy) }; 36 | 37 | let rec start () => { 38 | let t = Lwt.task (); 39 | let (_,w) = !state; 40 | state := t; 41 | clear_graph(); 42 | Lwt.wakeup w (); 43 | Lwt.bind (Lwt_js.sleep (1./.60.)) start }; 44 | 45 | let pong x y dx dy => { pong_aux x y dx dy }; 46 | 47 | pong 111 87 2 3; 48 | pong 28 57 5 3; 49 | start (); 50 | -------------------------------------------------------------------------------- /toplevel/indent.cppo.ml: -------------------------------------------------------------------------------- 1 | let indent s in_lines = s 2 | 3 | let textarea textbox = 4 | let rec loop s acc (i,pos') = 5 | try 6 | let pos = String.index_from s pos' '\n' in 7 | loop s ((i,(pos',pos))::acc) (succ i,succ pos) 8 | with _ -> List.rev ((i,(pos',String.length s)) :: acc) in 9 | let rec find (l : (int * (int * int)) list ) c = 10 | match l with 11 | | [] -> assert false 12 | | (i,(lo,up))::_ when up >= c -> c,i,lo,up 13 | | (_,(lo,up))::rem -> find rem c in 14 | let v = textbox##value in 15 | let pos = 16 | let c1 = (Js.Unsafe.coerce textbox)##selectionStart 17 | and c2 = (Js.Unsafe.coerce textbox)##selectionEnd in 18 | if Js.Opt.test (Js.Opt.return c1) && Js.Opt.test (Js.Opt.return c2) 19 | then begin 20 | let l = loop (Js.to_string v) [] (0,0) in 21 | Some (find l c1,find l c2) 22 | end 23 | else None in 24 | let f = match pos with 25 | | None -> (fun _ -> true) 26 | | Some ((c1,line1,lo1,up1),(c2,line2,lo2,up2)) -> (fun l -> l>=(line1+1) && l<=(line2+1)) in 27 | let v = indent (Js.to_string v) f in 28 | textbox##value<-Js.string v; 29 | begin 30 | match pos with 31 | | Some ((c1,line1,lo1,up1),(c2,line2,lo2,up2)) -> 32 | let l = loop v [] (0,0) in 33 | let (lo1'',up1'') = List.assoc line1 l in 34 | let (lo2'',up2'') = List.assoc line2 l in 35 | let n1 = max (c1 + up1'' - up1) lo1'' in 36 | let n2 = max (c2 + up2'' - up2) lo2'' in 37 | let () = (Js.Unsafe.coerce textbox)##setSelectionRange(n1,n2) in 38 | textbox##focus(); 39 | () 40 | | None -> () 41 | end 42 | -------------------------------------------------------------------------------- /toplevel/indent.mli: -------------------------------------------------------------------------------- 1 | val textarea : Dom_html.textAreaElement Js.t -> unit 2 | -------------------------------------------------------------------------------- /toplevel/index.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Reason toplevel 7 | 8 | 9 | 10 | 11 | 202 | 241 | 242 | 243 | 244 |
245 |

246 |       
247 |
248 | 249 |
250 |
251 |
252 | 253 | 254 |
255 | 261 |
262 |
263 |
264 |

Reason

265 |
A new developer experience for rapidly building fast, safe systems.
266 |

267 | Reason is a new interface to OCaml - a highly expressive dialect of the ML language featuring type inference and static type checking.

268 | 269 |

Reason provides a new syntax and toolchain for editing, building, and sharing code, and will evolve in the open as a community collaboration. Reason already has contributors across several organizations/companies.

270 |
About this toplevel
271 |

272 | This is a full Reason toplevel running directly inside your browser. 273 |

274 |

275 | Every required parts of the OCaml compiler and the Reason toolchain have been compiled to JavaScript (using js_of_ocaml) and sent to your browser, and every statements are evaluated on your machine, without relying on a distant server. (See by yourself and disable your network card!) 276 |

277 |
Credits
278 |

279 | This toplevel was initially developped as a part of the js_of_ocaml project. More about js_of_ocaml 280 |

281 |

282 | Many thanks to Hugo Heuzard, Jerome Vouillon, and the rest of the Ocsigen Team. 283 |

284 |
285 |
286 |

Command

287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 |
Enter/ReturnSubmit code
Ctrl + EnterNewline
Up / DownBrowse history
Ctrl + lClear display
Ctrl + kReset toplevel
311 |
312 | 313 |
314 |

Try to execute samples

315 |
316 | 317 |
318 | 319 |
320 |

See the generated javascript code

321 |
322 | 	  
323 |
324 |
325 |

Share this REPL session

326 |
327 | URL 328 | 329 |
330 |
331 | 332 | 333 | 334 | -------------------------------------------------------------------------------- /toplevel/reason.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abbysmal/reason-web-toplevel/c55d1a2d77bb2a2f1591edab51d20380ba060948/toplevel/reason.png -------------------------------------------------------------------------------- /toplevel/server.ml: -------------------------------------------------------------------------------- 1 | open Lwt 2 | open Cohttp 3 | open Cohttp_lwt_unix 4 | open Re 5 | 6 | let address = ref "127.0.0.1" 7 | let port = ref 8888 8 | let _ = Findlib.init () 9 | let filesys = ref (Findlib.default_location ()) 10 | 11 | let server () = 12 | 13 | let re_filesys = compile (seq [ str "/filesys/"; group (seq [ str !filesys; rep any]); eos ]) in 14 | 15 | let header typ = 16 | let h = Header.init () in 17 | let h = Header.add h "Content-Type" typ in 18 | let h = Header.add h "Server" "iocaml" in 19 | h 20 | in 21 | let header_html = header "text/html; charset=UTF-8" in 22 | let header_js = header "application/javascript; charset=UTF-8" in 23 | let header_css = header "text/css; charset=UTF-8" in 24 | let header_plain_user_charset = header "text/plain; charset=x-user-defined" in 25 | 26 | let callback conn_id req body = 27 | let uri = Request.uri req in 28 | let path = Uri.path uri in 29 | 30 | try 31 | (* send binary file *) 32 | let fname = get (exec re_filesys path) 1 in 33 | Lwt_io.eprintf "filesys: %s\n" fname >>= fun () -> 34 | Server.respond_file ~headers:header_plain_user_charset ~fname:fname () 35 | with _ -> 36 | (* send static file *) 37 | let fname = Server.resolve_file ~docroot:"." ~uri:uri in 38 | Lwt_io.eprintf "static: %s\n" fname >>= fun () -> 39 | let headers = 40 | if Filename.check_suffix fname ".css" 41 | then header_css 42 | else if Filename.check_suffix fname ".js" 43 | then header_js 44 | else header_html in 45 | Server.respond_file ~headers ~fname () 46 | 47 | in 48 | let conn_closed conn_id () = () in 49 | let config = { Server.callback; conn_closed } in 50 | Server.create ~address:!address ~port:!port config 51 | 52 | let () = Lwt_main.run (server()) 53 | -------------------------------------------------------------------------------- /toplevel/test_dynlink.ml: -------------------------------------------------------------------------------- 1 | let _ = print_endline "Dynlink OK" 2 | -------------------------------------------------------------------------------- /toplevel/toplevel.cppo.ml: -------------------------------------------------------------------------------- 1 | (* Js_of_ocaml toplevel 2 | * http://www.ocsigen.org/js_of_ocaml/ 3 | * Copyright (C) 2011 Jérôme Vouillon 4 | * Laboratoire PPS - CNRS Université Paris Diderot 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, with linking exception; 9 | * either version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 | *) 20 | 21 | open Lwt 22 | let reason_header = 23 | " ___ _______ ________ _ __ 24 | / _ \\\/ __/ _ | / __/ __ \\\/ |/ / 25 | / , _/ _// __ |_\\\ \\\/ /_/ / / 26 | /_/|_/___/_/ |_/___/\\\____/_/|_/ " 27 | 28 | let compiler_name = "OCaml" 29 | 30 | let by_id s = Dom_html.getElementById s 31 | let by_id_coerce s f = Js.Opt.get (f (Dom_html.getElementById s)) (fun () -> raise Not_found) 32 | let do_by_id s f = try f (Dom_html.getElementById s) with Not_found -> () 33 | 34 | (* load file using a synchronous XMLHttpRequest *) 35 | let load_resource_aux url = 36 | try 37 | let xml = XmlHttpRequest.create () in 38 | xml##_open(Js.string "GET", url, Js._false); 39 | xml##send(Js.null); 40 | if xml##status = 200 then Some (xml##responseText) else None 41 | with _ -> None 42 | 43 | let load_resource scheme (_,suffix) = 44 | let url = (Js.string scheme)##concat(suffix) in 45 | load_resource_aux url 46 | 47 | let setup_pseudo_fs () = 48 | Sys_js.register_autoload' "/dev/" (fun s -> Some (Js.string "")); 49 | Sys_js.register_autoload' "/http/" (fun s -> load_resource "http://" s); 50 | Sys_js.register_autoload' "/https/" (fun s -> load_resource "https://" s); 51 | Sys_js.register_autoload' "/ftp/" (fun s -> load_resource "ftp://" s); 52 | Sys_js.register_autoload' "/" (fun (_,s) -> load_resource_aux ((Js.string "filesys/")##concat(s))) 53 | 54 | let exec' s = 55 | let res : bool = JsooTop.use Format.std_formatter s in 56 | if not res then Format.eprintf "error while evaluating %s@." s 57 | 58 | let setup_toplevel () = 59 | JsooTop.initialize (); 60 | Toploop.parse_toplevel_phrase := (Reason_util.correctly_catch_parse_errors Reason_toolchain.JS.canonical_toplevel_phrase); 61 | Toploop.print_out_value := Reason_oprint.print_out_value; 62 | Toploop.print_out_type := Reason_oprint.print_out_type; 63 | Toploop.print_out_class_type := Reason_oprint.print_out_class_type; 64 | Toploop.print_out_module_type := Reason_oprint.print_out_module_type; 65 | Toploop.print_out_type_extension := Reason_oprint.print_out_type_extension; 66 | Toploop.print_out_sig_item := Reason_oprint.print_out_sig_item; 67 | Toploop.print_out_signature := Reason_oprint.print_out_signature; 68 | Toploop.print_out_phrase := Reason_oprint.print_out_phrase; 69 | 70 | Sys.interactive := false; 71 | exec' ("module Lwt_main = struct 72 | let run t = match Lwt.state t with 73 | | Lwt.Return x -> x 74 | | Lwt.Fail e -> raise e 75 | | Lwt.Sleep -> failwith \"Lwt_main.run: thread didn't return\" 76 | end"); 77 | let header1 = Printf.sprintf 78 | "%s\n\nReason Meta Language Utility, running OCaml version %%s" reason_header in 79 | let header2 = Printf.sprintf 80 | "Compiled with Js_of_ocaml version %s\n\n" Sys_js.js_of_ocaml_version in 81 | exec' (Printf.sprintf "Format.printf \"%s@.\" Sys.ocaml_version;;" header1); 82 | exec' (Printf.sprintf "Format.printf \"%s@.\";;" header2); 83 | exec' ("#enable \"pretty\";;"); 84 | exec' ("#enable \"shortvar\";;"); 85 | Sys.interactive := true; 86 | () 87 | 88 | let resize ~container ~textbox () = 89 | Lwt.pause () >>= fun () -> 90 | textbox##style##height <- Js.string "auto"; 91 | textbox##style##height <- Js.string (Printf.sprintf "%dpx" (max 18 textbox##scrollHeight)); 92 | container##scrollTop <- container##scrollHeight; 93 | Lwt.return () 94 | 95 | let setup_printers () = 96 | (* MetaOcaml *) 97 | #ifdef metaocaml 98 | Topdirs.dir_install_printer Format.std_formatter 99 | (Longident.(Ldot(Lident "Print_code", "print_code"))); 100 | Topdirs.dir_install_printer Format.std_formatter 101 | (Longident.(Ldot(Lident "Print_code", "print_closed_code"))); 102 | #endif 103 | exec'("let _print_error fmt e = Format.pp_print_string fmt (Js.string_of_error e)"); 104 | Topdirs.dir_install_printer Format.std_formatter (Longident.(Lident "_print_error")); 105 | exec'("let _print_unit fmt (_ : 'a) : 'a = Format.pp_print_string fmt \"()\""); 106 | Topdirs.dir_install_printer Format.std_formatter (Longident.(Lident "_print_unit")) 107 | 108 | let setup_examples ~container ~textbox = 109 | let r = Regexp.regexp "^\\(\\*+(.*)\\*+\\)$" in 110 | let all = ref [] in 111 | begin try 112 | let ic = open_in "/static/examples.ml" in 113 | while true do 114 | let line = input_line ic in 115 | match Regexp.string_match r line 0 with 116 | | Some res -> 117 | let name = match Regexp.matched_group res 1 with Some s -> s | None -> assert false in 118 | all := `Title name :: !all 119 | | None -> all := `Content line :: !all 120 | done; 121 | assert false 122 | with _ -> () end; 123 | let example_container = by_id "toplevel-examples" in 124 | let _ = List.fold_left (fun acc tok -> 125 | match tok with 126 | | `Content line -> line ^ "\n" ^ acc 127 | | `Title name -> 128 | let a = Tyxml_js.Html5.(a ~a:[ 129 | a_class ["list-group-item"]; 130 | a_onclick (fun _ -> 131 | textbox##value <- (Js.string acc)##trim(); 132 | Lwt.async(fun () -> 133 | resize ~container ~textbox () >>= fun () -> 134 | textbox##focus(); 135 | Lwt.return_unit); 136 | true 137 | )] [pcdata name]) in 138 | Dom.appendChild example_container (Tyxml_js.To_dom.of_a a); 139 | "" 140 | ) "" !all in 141 | () 142 | 143 | (* we need to compute the hash form href to avoid different encoding behavior 144 | across browser. see Url.get_fragment *) 145 | let parse_hash () = 146 | let frag = Url.Current.get_fragment () in 147 | Url.decode_arguments frag 148 | 149 | let rec iter_on_sharp ~f x = 150 | Js.Opt.iter (Dom_html.CoerceTo.element x) 151 | (fun e -> if Js.to_bool (e##classList##contains(Js.string "sharp")) then f e); 152 | match Js.Opt.to_option x##nextSibling with 153 | | None -> () 154 | | Some n -> iter_on_sharp ~f n 155 | 156 | let setup_share_button ~output = 157 | do_by_id "share-tab" (fun e -> 158 | e##style##display <- Js.string "block"; 159 | e##onclick <- Dom_html.handler (fun _ -> 160 | (* get all ocaml code *) 161 | let code = ref [] in 162 | Js.Opt.iter 163 | (output##firstChild) 164 | (iter_on_sharp ~f:(fun e -> 165 | code := Js.Opt.case (e##textContent) 166 | (fun () -> "") 167 | (Js.to_string) :: !code)); 168 | let code_encoded = B64.encode (String.concat "" (List.rev !code)) in 169 | let url,is_file = match Url.Current.get () with 170 | | Some (Url.Http url) -> Url.Http ({url with Url.hu_fragment = "" }),false 171 | | Some (Url.Https url) -> Url.Https ({url with Url.hu_fragment= "" }), false 172 | | Some (Url.File url) -> Url.File ({url with Url.fu_fragment= "" }), true 173 | | _ -> assert false in 174 | let frag = 175 | let frags = parse_hash () in 176 | let frags = List.remove_assoc "code" frags @ ["code",code_encoded] in 177 | Url.encode_arguments frags in 178 | let uri = Url.string_of_url url ^ "#" ^ frag in 179 | let node = Js.Unsafe.coerce @@ by_id "share-link" in 180 | let append_url str = 181 | node##value <- (Js.string str) 182 | in 183 | Lwt.async (fun () -> 184 | Lwt.catch (fun () -> 185 | if is_file 186 | then failwith "Cannot shorten url with file scheme" 187 | else 188 | let uri = Printf.sprintf "https://is.gd/create.php?format=json&url=%s" (Url.urlencode uri) in 189 | Lwt.bind (Jsonp.call uri) (fun o -> 190 | let str = Js.to_string o##shorturl in 191 | append_url str; 192 | Lwt.return_unit) 193 | ) 194 | (fun exn -> 195 | append_url uri; 196 | Lwt.return_unit)); 197 | Js._false)) 198 | 199 | let setup_js_preview () = 200 | let ph = by_id "last-js" in 201 | let runcode : (string -> 'a) = Js.Unsafe.global##toplevelEval in 202 | Js.Unsafe.global##toplevelEval <- (fun bc -> 203 | ph##innerHTML <- Js.string bc; 204 | runcode bc 205 | ) 206 | 207 | let current_position = ref 0 208 | let highlight_location loc = 209 | let x = ref 0 in 210 | let output = by_id "output" in 211 | let first = Js.Opt.get (output##childNodes##item(!current_position)) (fun _ -> assert false) in 212 | iter_on_sharp first 213 | ~f:(fun e -> 214 | incr x; 215 | let _file1,line1,col1 = Location.get_pos_info (loc.Location.loc_start) in 216 | let _file2,line2,col2 = Location.get_pos_info (loc.Location.loc_end) in 217 | if !x >= line1 && !x <= line2 218 | then 219 | let from_ = if !x = line1 then `Pos col1 else `Pos 0 in 220 | let to_ = if !x = line2 then `Pos col2 else `Last in 221 | Colorize.highlight from_ to_ e) 222 | 223 | 224 | let append colorize output cl s = 225 | Dom.appendChild output (Tyxml_js.To_dom.of_element (colorize ~a_class:cl s)) 226 | 227 | module History = struct 228 | let data = ref [|""|] 229 | let idx = ref 0 230 | let get_storage () = 231 | match Js.Optdef.to_option Dom_html.window##localStorage with 232 | | None -> raise Not_found 233 | | Some t -> t 234 | 235 | let setup () = 236 | try 237 | let s = get_storage () in 238 | match Js.Opt.to_option (s##getItem(Js.string "history")) with 239 | | None -> raise Not_found 240 | | Some s -> let a = Json.unsafe_input s in 241 | data:=a; idx:=Array.length a - 1 242 | with _ -> () 243 | 244 | let push text = 245 | let l = Array.length !data in 246 | let n = Array.make (l + 1) "" in 247 | Array.set !data (l - 1) text; 248 | Array.blit !data 0 n 0 l; 249 | data := n; idx := l; 250 | try 251 | let s = get_storage () in 252 | let str = Json.output !data in 253 | s##setItem(Js.string "history", str) 254 | with Not_found -> () 255 | 256 | let current text = !data.(!idx) <- text 257 | let previous textbox = 258 | if !idx > 0 259 | then begin decr idx; textbox##value <- Js.string (!data.(!idx)) end 260 | let next textbox = 261 | if !idx < Array.length !data - 1 262 | then begin incr idx; textbox##value <- Js.string (!data.(!idx)) end 263 | end 264 | 265 | let run _ = 266 | let container = by_id "toplevel-container" in 267 | let output = by_id "output" in 268 | let textbox : 'a Js.t = by_id_coerce "userinput" Dom_html.CoerceTo.textarea in 269 | 270 | let sharp_chan = open_out "/dev/null0" in 271 | let sharp_ppf = Format.formatter_of_out_channel sharp_chan in 272 | 273 | let caml_chan = open_out "/dev/null1" in 274 | let caml_ppf = Format.formatter_of_out_channel caml_chan in 275 | 276 | let execute () = 277 | let content = Js.to_string (textbox##value##trim()) in 278 | let content' = 279 | let len = String.length content in 280 | if try content <> "" && content.[len-1] <> ';' && content.[len-2] <> ';' with _ -> true 281 | then content ^ ";" 282 | else content in 283 | current_position := output##childNodes##length; 284 | textbox##value <- Js.string ""; 285 | History.push content; 286 | JsooTop.execute true ~pp_code:sharp_ppf ~highlight_location caml_ppf content'; 287 | resize ~container ~textbox () >>= fun () -> 288 | container##scrollTop <- container##scrollHeight; 289 | textbox##focus(); 290 | Lwt.return_unit in 291 | 292 | let history_down e = 293 | let txt = Js.to_string textbox##value in 294 | let pos = (Js.Unsafe.coerce textbox)##selectionStart in 295 | try 296 | (if String.length txt = pos then raise Not_found); 297 | let _ = String.index_from txt pos '\n' in 298 | Js._true 299 | with Not_found -> 300 | History.current txt; 301 | History.next textbox; 302 | Js._false 303 | in 304 | let history_up e = 305 | let txt = Js.to_string textbox##value in 306 | let pos = (Js.Unsafe.coerce textbox)##selectionStart - 1 in 307 | try 308 | (if pos < 0 then raise Not_found); 309 | let _ = String.rindex_from txt pos '\n' in 310 | Js._true 311 | with Not_found -> 312 | History.current txt; 313 | History.previous textbox; 314 | Js._false 315 | in 316 | 317 | let meta e = 318 | let b = Js.to_bool in 319 | b e##ctrlKey || b e##metaKey in 320 | 321 | let shift e = Js.to_bool e##shiftKey in 322 | 323 | begin (* setup handlers *) 324 | textbox##onkeyup <- Dom_html.handler (fun _ -> Lwt.async (resize ~container ~textbox); Js._true); 325 | textbox##onchange <- Dom_html.handler (fun _ -> Lwt.async (resize ~container ~textbox); Js._true); 326 | textbox##onkeydown <- Dom_html.handler (fun e -> 327 | match e##keyCode with 328 | | 13 when not (meta e) && not (shift e) -> Lwt.async execute; Js._false 329 | | 13 when (shift e) || (meta e) -> Lwt.async (resize ~container ~textbox); Js._true 330 | | 09 -> Indent.textarea (Js.Unsafe.coerce textbox); Js._false 331 | | 76 when meta e -> output##innerHTML <- Js.string ""; Js._true 332 | | 75 when meta e -> setup_toplevel (); Js._false 333 | | 38 -> history_up e 334 | | 40 -> history_down e 335 | | _ -> Js._true 336 | ); 337 | end; 338 | 339 | Lwt.async_exception_hook:=(fun exc -> 340 | Format.eprintf "exc during Lwt.async: %s@." (Printexc.to_string exc); 341 | match exc with 342 | | Js.Error e -> Firebug.console##log(e##stack) 343 | | _ -> ()); 344 | 345 | Lwt.async (fun () -> 346 | resize ~container ~textbox () >>= fun () -> 347 | textbox##focus (); 348 | Lwt.return_unit); 349 | 350 | #ifdef graphics 351 | Graphics_js.open_canvas (by_id_coerce "test-canvas" Dom_html.CoerceTo.canvas); 352 | #endif 353 | 354 | Sys_js.set_channel_flusher caml_chan (append Colorize.ocaml output "caml"); 355 | Sys_js.set_channel_flusher sharp_chan (append Colorize.ocaml output "sharp"); 356 | Sys_js.set_channel_flusher stdout (append Colorize.text output "stdout"); 357 | Sys_js.set_channel_flusher stderr (append Colorize.text output "stderr"); 358 | 359 | let readline () = 360 | Js.Opt.case 361 | (Dom_html.window##prompt 362 | (Js.string "The toplevel expects inputs:", Js.string "")) 363 | (fun () -> "") 364 | (fun s -> Js.to_string s ^ "\n") in 365 | Sys_js.set_channel_filler stdin readline; 366 | 367 | setup_share_button ~output; 368 | setup_examples ~container ~textbox; 369 | setup_pseudo_fs (); 370 | setup_toplevel (); 371 | setup_js_preview (); 372 | setup_printers (); 373 | History.setup (); 374 | 375 | textbox##value <- Js.string ""; 376 | (* Run initial code if any *) 377 | try 378 | let code = List.assoc "code" (parse_hash ()) in 379 | textbox##value <- Js.string (B64.decode code); 380 | Lwt.async execute 381 | with 382 | | Not_found -> () 383 | | exc -> Firebug.console##log_3(Js.string "exception", Js.string (Printexc.to_string exc), exc) 384 | 385 | let _ = Dom_html.window##onload <- Dom_html.handler (fun _ -> run (); Js._false) 386 | --------------------------------------------------------------------------------