# What OCaml value does this JS value encode?

**URL:** <https://discuss.ocaml.org/t/what-ocaml-value-does-this-js-value-encode/12751>\
**Category:** Learning\
**Created:** [August 2, 2023, 4:03pm UTC](https://discuss.ocaml.org/t/what-ocaml-value-does-this-js-value-encode/12751 "2023-08-02T16:03:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [August 2, 2023, 4:03pm UTC](https://discuss.ocaml.org/t/what-ocaml-value-does-this-js-value-encode/12751/1 "2023-08-02T16:03:19Z")

</div>

Context: writing some serialization / deserialization routines for iframe/iframe communication. I am expecting a MessagePort, but instead got this monster:

```auto
(2) [0, 0]
0
: 
0
1
: 
Array(2)
0
: 
2
1
: 
MessagePort {onmessage: null, onmessageerror: null}
length
: 
2
[[Prototype]]
: 
Array(0)
length
: 
2
[[Prototype]]
: 
Array(0)

```

This looks like something that (list? array?) that contains a MessagePort but I am not sure what it is.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [August 2, 2023, 6:28pm UTC](https://discuss.ocaml.org/t/what-ocaml-value-does-this-js-value-encode/12751/2 "2023-08-02T18:28:31Z")

</div>

Figured it out. It was a situation where I wrote:

```auto
let x = ...

```

when I should have written a

```auto
let%bind x = ...

```

and I believe the printed out expr is the expr used for completed Async\_Kernel.Deferred.IVar’s .

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [August 2, 2023, 7:37pm UTC](https://discuss.ocaml.org/t/what-ocaml-value-does-this-js-value-encode/12751/3 "2023-08-02T19:37:54Z")

</div>

Ah but that’s not enough! We want to know which component’s type discipline is at fault here :–) In `js_of_ocaml` APIs, values usually are sufficiently well-typed for these things not happen.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [August 2, 2023, 8:59pm UTC](https://discuss.ocaml.org/t/what-ocaml-value-does-this-js-value-encode/12751/4 "2023-08-02T20:59:18Z")

</div>

I screwed up because my OCaml/Rust FFI boundary was untyped.

iframeA (OCaml) gets a MessagePort from iframeB (OCaml)  
iframeA (OCaml) gets this as a MessagePort Deferred.t – and because the OCaml/Rust FFI is untyped, passes a MessagePort Deferred.t to Rust , and Rust goes wtf.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [August 2, 2023, 9:04pm UTC](https://discuss.ocaml.org/t/what-ocaml-value-does-this-js-value-encode/12751/5 "2023-08-02T21:04:47Z")

</div>

I’m getting a bit curious about your architecture. So you compile OCaml to JavaScript and interoperate JavaScript with (native?) rust. What’s your setup like (if you want to elaborate) ?

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [August 2, 2023, 10:09pm UTC](https://discuss.ocaml.org/t/what-ocaml-value-does-this-js-value-encode/12751/6 "2023-08-02T22:09:30Z")

</div>

Everything runs in Chrome. Node is not used at runtime.

OCaml is compiled to JS via js\_of\_ocaml.  
Rust is compiled to wasm32 via `-target=wasm32-unknown-unknown`

OCaml / Rust talk to each other via FFI (JS functions, JS calling convention) and MessagePort.

For FFI:

- OCaml side: you are more familiar than me
- Rust side: [Introduction - The `wasm-bindgen` Guide](https://rustwasm.github.io/wasm-bindgen/)
