Giter VIP home page Giter VIP logo

wasm_of_ocaml's Introduction

Wasm_of_ocaml

Wasm_of_ocaml is a fork of Js_of_ocaml which compiles OCaml bytecode to WebAssembly.

Requirements

Wasm_of_ocaml relies on the Binaryen toolchain. At the moment, you need to install it from the main branch on GitHub.

Supported engines

The generated code works with Chrome beta and node V8 canary. For Chrome, you need to enable WebAssembly Garbage Collection and WebAssembly Stringref from chrome://flags/. For node, you need to use the following flags:--experimental-wasm-gc --experimental-wasm-stringref.

Installation

The following commands will perform a minimal installation:

opam pin add -n --with-version 5.3.0 .
opam install wasm_of_ocaml-compiler

You may want to install additional packages. For instance:

opam install js_of_ocaml-ppx js_of_ocaml-lwt

Usage

You can try compiling the program in examples/cubes. Your program must first be compiled using the OCaml bytecode compiler ocamlc. JavaScript bindings are provided by the js_of_ocaml package. The syntax extension is provided by js_of_ocaml-ppx package. Package js_of_ocaml-lwt provides Javascript specific Lwt functions.

ocamlfind ocamlc -package js_of_ocaml,js_of_ocaml-ppx,js_of_ocaml-lwt -linkpkg -o cubes.byte cubes.mli cubes.ml

Then, run the wasm_of_ocaml compiler to produce WebAssembly code:

wasm_of_ocaml cubes.byte

This outputs a file cubes.js which loads the WebAssembly code from file cube.wasm. For debugging, we currently also output the generated WebAssembly code in text file to cube.wat. Since Chrome does not allow loading from the filesystem, you need to serve the files using some Web server. For instance:

python3 -m http.server 8000 --directory .

As a larger example, you can try CAMLBOY. You need to install a forked version of Brr. Once the Js_of_ocaml UI is compiled (with dune build), you can generate WebAssembly code instead with the following command:

wasm_of_ocaml _build/default/bin/web/index.bc-for-jsoo

Implementation status

A large part of the runtime is implemented. File-related functions, marshaling and dynamic linking are not supported yet.

Separate compilation is not implemented yet.

Compatibility with Js_of_ocaml

Since the value representation is different, some adaptations are necessary.

The most notable change is that, except for integers, OCaml numbers are no longer mapped to JavaScript numbers. So, explicit conversions Js.to_float and Js.float are now necessary to convert between OCaml floats and JavaScript numbers. The typing of JavaScript Typed Arrays has also been changed to deal with this.

Additionally, OCaml physical equality will not work properly on JavaScript objects (it compares boxed values instead of values themselves). You should use Js.string_equals instead.

Some forked versions of Brr and Gen_js_api are compatible with Wasm_of_ocaml.

wasm_of_ocaml's People

Contributors

hhugo avatar vouillon avatar hnrgrgr avatar chambart avatar drup avatar vasilisp avatar kit-ty-kate avatar tyoverby avatar slegrand45 avatar smorimoto avatar sagotch avatar abbysmal avatar let-def avatar pveber avatar raphael-proust avatar alainfrisch avatar keleshev avatar pmwhite avatar kakadu avatar balat avatar fxfactorial avatar phated avatar rgrinberg avatar benozol avatar monstasat avatar alifirat avatar xclerc avatar gpetiot avatar dannywillems avatar little-arhat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.