编程语言
首页 > 编程语言> > 如何将简单的命令行OCaml脚本编译成Javascript

如何将简单的命令行OCaml脚本编译成Javascript

作者:互联网

我有一个简单的命令行OCaml应用程序,它在Sys.argv.(1)上执行计算并将结果输出到stdout.我可以使用js_of_ocaml将其编译为Javascript,但它给了我很多关于caml_ml_output_char未定义的错误.我通过删除printfs修复了这些错误,因此它运行,但它在运行时冻结了firefox.

如何将简单的OCaml命令行脚本干净地编译到基于Javascript的网页中;没有维护分叉版本或冻结浏览器?

解决方法:

您可能希望使用Webworkers,因为运行的软件不是围绕Javascript在UI线程中的合作多任务设计的,可能会导致浏览器锁定.
您可以将以下标头添加到OCaml文件的顶部,以使正常的OCaml Sys和打印实现过载

(* JsHeader.ml *)
let output_buffer_ = Buffer.create 1000
let flush x=let module J = Js.Unsafe in let () = J.call 
        (J.variable "postMessage") (J.variable "self")
        [|J.inject (Js.string (Buffer.contents output_buffer_))|]
     in Buffer.clear output_buffer_

let print_string = Buffer.add_string output_buffer_
let print_char = Buffer.add_char output_buffer_
let print_newline () = print_char '\n'
let print_endline s = print_string (s^"\n"); flush ()
let caml_ml_output_char = print_char
let printf fmt = Printf.bprintf output_buffer_ fmt
module Printf = struct
    include Printf
    let printf fmt = Printf.bprintf output_buffer_ fmt
end

传递命令行参数的最自然方式是通过发送给Web worker的URL.我们可以覆盖Ocaml Sys模块而不是读取?argv作为一系列空终止字符串.

module Sys = struct
    let char_split delim s = (*Str.split is overkill*)
        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.arguments
    let argv = Array.of_list (char_split '\x00' (getenv "?argv"))
    let executable_name = argv.(0)
end

现在我们已经输入了标题,我们可以输入一个简单的OCaml命令行程序:

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

此命令行程序依赖于OS来刷新输出,但我们必须手动刷新输出.
您可能还想发送一个空字符,以便Javascript知道该命令已完成.
这可以通过附加以下页脚来实现.

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

我们可以加入文件并按如下方式编译它们:

 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

现在我们已经创建了merged.js文件,我们可以将javascript包装在一个简单的网页中,如下所示:

<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml+xml; charset=UTF-8" />
<title>ml2js sample_cli</title>
<script type="text/javascript">
<!--
var worker;
function go () {
  var output=document.getElementById ("output");
  var argv = encodeURIComponent("/bin/sample_cli\0"+document.getElementById ("input").value);
  if (worker) {
    worker.terminate();
  }
  worker = new Worker ("sample_cli.js?argv="+argv);
  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 onl oad=go()>
<textarea id="input" rows="2" cols="60" onkeyup="go()" onchange="go()" style="width:90%">SAMPLE_INPUT</textarea> 
<button onclick="go()">go</button><br>
<textarea id="output" rows="0" cols="60" style="width:100%;height:90%" readonly onl oad=go()>
Your browser does not seem to support Webworkers.
Try Firefox, Chrome or IE10+. 
</textarea>
</body>

</html>

有关此方法的示例,请参阅http://www.dansted.org/app/bctl-plain.html;有关附加相应页眉,页脚等的脚本,请参阅https://github.com/gmatht/TimeLogicUnify/blob/master/ATL/js/webworker/ml2js.sh.

标签:javascript,ocaml,js-of-ocaml
来源: https://codeday.me/bug/20190623/1268871.html