Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obj.magic() causes code execution to fail #224

Open
jozanza opened this issue Jul 10, 2019 · 8 comments
Open

Obj.magic() causes code execution to fail #224

jozanza opened this issue Jul 10, 2019 · 8 comments

Comments

@jozanza
Copy link

jozanza commented Jul 10, 2019

I'm really loving Sketch 🎉, however, I ran into problems trying to use OCaml's Queue module -- the types all checked out, and code execution ran without issue, until I actually called Queue.create().

After some digging, I realized that Queue uses some unsafe type coercion via Obj.magic() in a number of methods; Queue.create() is one of them.

Further testing revealed that Obj.magic() worked in cases involving bool, int, and float, but broke code execution when used to assign string or record type.

I have a Sketch that illustrates the problem in more detail:
https://sketch.sh/s/O5RAG6SC1rDRGzTu3d6GaU/

Right now, it seems like the only possible workaround is to roll my own Queue module. I'm still fairly new when it comes to reason/ocaml, but I'd love to help out if possible.

@thangngoc89
Copy link
Member

Hello, thank you for using and loving Sketch.sh. Queue isn't part of the stdlib, though it's often shipped with the compiler. Currently Sketch only supports the stdlib (at OCaml 4.06). There are plans to support loading external packages.

Regardings your problem, I think this is an upstream issue (with JSOO) or maybe how your Queue is implemented. I will have tho dig deeper to see what the actual issue is.

In the mean time, here is a Queue implementation you can use that works with Sketch.

https://sketch.sh/s/YAdMoaIgTvVcdGGeOs1njq/

@Drup
Copy link

Drup commented Jul 10, 2019

@thangngoc89
Copy link
Member

@Drup I'd only consider anythings that's linked with -I +stdlib to be the real stdlib. Anything else isn't. For ex: Str, Unix

@Drup
Copy link

Drup commented Jul 10, 2019

Yes, and Queue is part of that. The content of the stdlib is shown here.

@thangngoc89
Copy link
Member

@Drup interesting. I wonder why sketch doesn't have Queue module

@jozanza
Copy link
Author

jozanza commented Jul 12, 2019

Thanks for the alternate implementation @thangngoc89!

FWIW, I've been using the reasonml docs as a stdlib reference. When I realized Queue references broke my Sketch, I just copy-pasted the vendored ocaml dependency from bsb-native, which appears to have been authored by INRIA back in 2002. That's where the Obj.magic() came from in the first place. I never even knew that was a thing prior 😆

EDIT:
I also just rolled my own simple Queue module that is API-compatible with the stdlib Queue 👍 https://sketch.sh/s/ZygnP0cDT7SpYg6YwD4T1q/

@Drup
Copy link

Drup commented Jul 18, 2019

FWIW, the current version of Queue doesn't use Obj.magic

@jozanza
Copy link
Author

jozanza commented Jul 22, 2019

@Drup Thanks that's exactly what I needed! Not sure why bsb-native is using a weird out-of-date version 🤷‍♂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants