简体   繁体   English

js_of_ocaml 从 js 调用 ocaml 中的 function

[英]js_of_ocaml calling a function in ocaml from js

I have a function that uses a mutable variable that takes strings and returns strings.我有一个 function 使用一个可变变量,该变量接受字符串并返回字符串。 (its a read eval print loop interpreter) (它是一个读取评估打印循环解释器)

I tried exporting it as such:我尝试这样导出它:

let () =
  Js.export_all
  (object%js
    method js_run_repl = Js.wrap_callback js_run_repl
  end)

Heres a snippet of the function im exporting这是 function 我正在导出的片段

let js_run_repl str =
  match String.(compare str "quit") with
  | 0 -> "bye"
  | _ -> ...

regardless of my input it always returns bye , calling the function directly in ocaml produced the expected behaviour.无论我的输入如何,它总是返回bye ,直接在 ocaml 中调用 function 会产生预期的行为。 Heres the output from node:这是来自节点的 output:

> var mod = require('./main.bc');
undefined
> mod.js_run("constant P : Prop");
MlBytes { t: 0, c: 'bye', l: 3 }
> 

Its also peculiar why the function is called js_run instead of js_run_repl .为什么 function 被称为js_run而不是js_run_repl也很奇怪。 the latter is undefined according to node.后者根据节点未定义。

let () =
  Js.export_all
  (object%js
    method js_run_repl str = 
      str
      |> Js.to_string
      |> js_run_repl
      |> Js.string
  end)

I had to convert the strings explicitly to ocaml strings and back to js我必须将字符串显式转换为 ocaml 字符串并返回 js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM