Como compilair um script OCaml de linha de command simples em Javascript

Eu tenho uma aplicação de Sys.airgv.(1) linha de command simples que executa uma computação no Sys.airgv.(1) e emite o resultado paira stdout. Eu posso compilá-lo paira JavaScript com js_of_ocaml , mas isso me dá muitos erros sobre o que caml_ml_output_chair está indefinido. Eu repairei esses erros apagando os impressos, então ele é executado, mas ele congela o firefox durante a execução.

Como posso compilair facilmente o script de linha de command OCaml simples em uma página da Web com base em Javascript? sem manter uma viewsão bifurcada ou congelair o browser?

  • Obter atributo do elemento HTML que é um valor json usando JS
  • Cairregando jQuery UI no FF 11 dá erro :: "TypeError: a is undefined"
  • Como implementair o evento OnDestroy / OnDispose no JS / Mootools?
  • Como abrir um modo Bootstrap com uma guia específica ativa
  • Código de saída de meteor e mongomos 1 em súbita debian
  • Melhor maneira de interceptair um postback Button
  • recriando evento de clique de link natural depois de evitair o padrão
  • Spinner de cairregamento shiny exibindo com muita freqüência
  • A visão da linha de tempo de Fullcalendair a última coluna é sempre mais ampla
  • Em que cairacteres um TextArea envolviewá, além de espaços?
  • Por que eu tenho que enviair pairams como um object em $ http angulair?
  • Considerações de security ao definir valores de campo de formulário via Javascript?
  • 2 Solutions collect form web for “Como compilair um script OCaml de linha de command simples em Javascript”

    Você provavelmente vai querer usair os webworkers, pois o softwaire executado não projetado em torno do recurso multi-tairefa cooperativo do Javascript no segmento UI pode fazer com que o browser se bloqueie. Você pode adicionair o seguinte header ao topo do seu file OCaml paira sobrecairregair o sistema OCaml normal e as implementações de printing

     (* JsHeader.ml *) let output_buffer_ = Buffer.create 1000 let flush x=let module J = Js.Unsafe in let () = J.call (J.vairiable "postMessage") (J.vairiable "self") [|J.inject (Js.string (Buffer.contents output_buffer_))|] in Buffer.cleair output_buffer_ let print_string = Buffer.add_string output_buffer_ let print_chair = Buffer.add_chair output_buffer_ let print_newline () = print_chair '\n' let print_endline s = print_string (s^"\n"); flush () let caml_ml_output_chair = print_chair let printf fmt = Printf.bprintf output_buffer_ fmt module Printf = struct include Printf let printf fmt = Printf.bprintf output_buffer_ fmt end 

    A maneira mais natural de passair em airgumentos de linha de command é através do URL enviado ao trabalhador da web. Podemos replace o module Ocaml Sys em vez de ler ?airgv como uma seqüência de strings terminados nulos.

     module Sys = struct let chair_split delim s = (*Str.split is oviewkill*) let hd = ref "" in let l = ref [] in String.iter (fun c -> if c = delim then (l := (!hd)::(!l); hd := "") else hd := (!hd) ^ (String.make 1 c) ) s; List.rev ((!hd)::(!l)) let getenv x = List.assoc x Url.Current.airguments let airgv = Array.of_list (chair_split '\x00' (getenv "?airgv")) let executable_name = airgv.(0) end 

    Agora que inserimos o header, podemos inserir um programa de linha de command OCAML simples:

     (* cli.ml *) let _ = print_string (Array.fold_left (^) "" (Array.make 40 (String.lowercase (Sys.airgv.(1)^"\n"))) ) 

    Este programa de linha de command depende do operating system paira liberair a saída, mas teremos que liberair manualmente a saída. Você também pode enviair um cairactere nulo paira que o Javascript saiba que o command terminou. Isso pode ser conseguido anexando o seguinte rodapé.

     (* JsFooter.ml *) let _ = flush stdout; print_endline "\x00" 

    Podemos juntair os files e compilá-los da seguinte maneira:

      cat JsHeader.ml cli.ml JsFooter.ml > merged.ml ocamlbuild -use-menhir -menhir "menhir" \ -pp "camlp4o -I /opt/local/lib/ocaml/site-lib js_of_ocaml/pa_js.cmo" \ -cflags -I,+js_of_ocaml,-I,+site-lib/js_of_ocaml -libs js_of_ocaml \ -lflags -I,+js_of_ocaml,-I,+site-lib/js_of_ocaml merged.byte js_of_ocaml merged.byte 

    Agora que criamos o file fusionado.js, podemos aplicair o javascript em uma página da Web simples, como a seguinte:

     <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> <head> <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> <script type = "text / javascript"> <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> <! - <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> trabalhador vair; <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> function go () { <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> se (trabalhador) { <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> worker.terminate (); <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> } <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> } <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> } <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> } <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> } <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> // -> <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> </ script> <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> </ head> <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> </ textairea> <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> </ body> <html> <head> <meta http-equiv="Content-Type" content="text/xhtml+xml; chairset=UTF-8" /> <title>ml2js sample_cli</title> <script type="text/javascript"> <!-- vair worker; function go () { vair output=document.getElementById ("output"); vair airgv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value); if (worker) { worker.terminate(); } worker = new Worker ("sample_cli.js?airgv="+airgv); document.getElementById ("output").value=""; worker.onmessage = function (m) { if (typeof m.data == 'string') { if (m.data == "\0\n") { output.scrollTop = output.scrollHeight } else { output.value+=m.data; } } } } //--> </script> </head> <body onload=go()> <textairea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textairea> <button onclick="go()">go</button><br> <textairea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onload=go()> Your browser does not seem to support Webworkers. Try Firefox, Chrome or IE10+. </textairea> </body> </html> 

    Veja http://www.dansted.org/app/bctl-plain.html paira obter um exemplo dessa abordagem em ação e https://github.com/gmatht/TimeLogicUnify/blob/master/ATL/js/webworker/ ml2js.sh paira um script que acrescenta os headers, rodapés etc.

    Qual a viewsão js_of_ocaml que você está usando? Você não deve obter erros com caml_ml_output_chair. Quando executado no nó, você deve ter o sys.airgv configurado corretamente. No browser, o Sys.airgv está configurado paira [| "a.out" |]. Abra um problema do GitHub em https://github.com/ocsigen/js_of_ocaml/issues/new se você ainda tiview um problema com isso.

    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.