Skip to content

Commit

Permalink
File.openAsync API (#1)
Browse files Browse the repository at this point in the history
* Add simple script to make a binary file with predictable sequence

* Start stubbing out File, add LWT dependencies

* Update lockfile

* Start sketching out a binary file open API

* Initial File.openAsync API
  • Loading branch information
bryphe authored Sep 29, 2018
1 parent 857762b commit bb9f0c7
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 26 deletions.
12 changes: 8 additions & 4 deletions esy.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
"reason-glfw": "*",
"reason-gl-matrix": "*",
"reason-reactify": "*",
"ocaml": "~4.6.0",
"@opam/js_of_ocaml": "*"
"@opam/lwt": "^4.0.0",
"@opam/lwt_ppx": "^1.1.0",
"@opam/js_of_ocaml": "*",
"@opam/js_of_ocaml-lwt": "*"
},
"peerDependencies": {
"ocaml": "~4.6.0"
},
"resolutions": {
"reason-glfw": "github:bryphe/reason-glfw#82ea419",
Expand All @@ -25,7 +30,6 @@
"devDependencies": {
"ocaml": "~4.6.0",
"@opam/dune": "*",
"@opam/js_of_ocaml-compiler": "*",
"@opam/js_of_ocaml-lwt": "*"
"@opam/js_of_ocaml-compiler": "*"
}
}
35 changes: 18 additions & 17 deletions esy.lock.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"hash": "d01dd9df3419402f9844a0633785faa8",
"hash": "7cee57e249e10e60991a8110ca529db5",
"root": "revery@path:.",
"node": {
"revery@path:.": {
Expand All @@ -11,10 +11,11 @@
"opam": null
},
"dependencies": [
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.2.1",
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.3.0",
"@opam/js_of_ocaml@opam:3.2.0",
"@opam/js_of_ocaml-compiler@opam:3.2.1",
"@opam/js_of_ocaml-lwt@opam:3.2.1", "[email protected]",
"@opam/js_of_ocaml-lwt@opam:3.2.1", "@opam/lwt@opam:4.1.0",
"@opam/lwt_ppx@opam:1.2.1", "[email protected]",
"reason-gl-matrix@github:bryphe/reason-gl-matrix#24499b2",
"reason-glfw@github:bryphe/reason-glfw#82ea419",
"reason-reactify@github:bryphe/reason-reactify#a62c8e4"
Expand Down Expand Up @@ -43,7 +44,7 @@
"opam": null
},
"dependencies": [
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.2.1",
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.3.0",
"@opam/lwt@opam:4.1.0", "[email protected]", "[email protected]"
]
},
Expand All @@ -56,7 +57,7 @@
"opam": null
},
"dependencies": [
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.2.1",
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.3.0",
"@opam/js_of_ocaml-compiler@opam:3.2.1", "@opam/lwt@opam:4.1.0",
"@opam/lwt_ppx@opam:1.2.1",
"esy-cmake@github:bryphe/esy-cmake#8afd4a1",
Expand All @@ -73,7 +74,7 @@
"opam": null
},
"dependencies": [
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.2.1",
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.3.0",
"@opam/js_of_ocaml-compiler@opam:3.2.1", "[email protected]",
"[email protected]"
]
Expand Down Expand Up @@ -364,7 +365,7 @@
}
},
"dependencies": [
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.2.1",
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.3.0",
"@opam/result@opam:1.3", "[email protected]"
]
},
Expand Down Expand Up @@ -585,7 +586,7 @@
}
},
"dependencies": [
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.2.1",
"@esy-ocaml/[email protected]", "@opam/dune@opam:1.3.0",
"@opam/ocamlfind@opam:1.8.0", "[email protected]"
]
},
Expand All @@ -609,18 +610,18 @@
"[email protected]"
]
},
"@opam/dune@opam:1.2.1": {
"@opam/dune@opam:1.3.0": {
"record": {
"name": "@opam/dune",
"version": "opam:1.2.1",
"version": "opam:1.3.0",
"source":
"archive:https://github.com/ocaml/dune/releases/download/1.2.1/dune-1.2.1.tbz#md5:f96bdf1a893a2178c2ad9c388439bd18",
"archive:https://github.com/ocaml/dune/releases/download/1.3.0/dune-1.3.0.tbz#md5:d7c926bd6b7549cb54d5463aaccf0c91",
"files": [],
"opam": {
"name": "dune",
"version": "1.2.1",
"version": "1.3.0",
"opam":
"opam-version: \"2.0\"\nname: \"dune\"\nversion: \"1.2.1\"\nsynopsis: \"Fast, portable and opinionated build system\"\ndescription: \"\"\"\ndune is a build system that was designed to simplify the release of\nJane Street packages. It reads metadata from \"dune\" files following a\nvery simple s-expression syntax.\n\ndune is fast, it has very low-overhead and support parallel builds on\nall platforms. It has no system dependencies, all you need to build\ndune and packages using dune is OCaml. You don't need or make or bash\nas long as the packages themselves don't use bash explicitly.\n\ndune supports multi-package development by simply dropping multiple\nrepositories into the same directory.\n\nIt also supports multi-context builds, such as building against\nseveral opam roots/switches simultaneously. This helps maintaining\npackages across several versions of OCaml and gives cross-compilation\nfor free.\"\"\"\nmaintainer: \"[email protected]\"\nauthors: \"Jane Street Group, LLC <[email protected]>\"\nlicense: \"MIT\"\nhomepage: \"https://github.com/ocaml/dune\"\nbug-reports: \"https://github.com/ocaml/dune/issues\"\ndepends: [\n \"ocaml\" {>= \"4.02.3\"}\n]\nconflicts: [\n \"jbuilder\" {!= \"transition\"}\n]\nbuild: [\n [\"ocaml\" \"configure.ml\" \"--libdir\" lib] {opam-version < \"2\"}\n [\"ocaml\" \"bootstrap.ml\"]\n [\"./boot.exe\" \"--release\" \"--subst\"] {pinned}\n [\"./boot.exe\" \"--release\" \"-j\" jobs]\n]\ndev-repo: \"git+https://github.com/ocaml/dune.git\"\nurl {\n src: \"https://github.com/ocaml/dune/releases/download/1.2.1/dune-1.2.1.tbz\"\n checksum: \"md5=f96bdf1a893a2178c2ad9c388439bd18\"\n}",
"opam-version: \"2.0\"\nname: \"dune\"\nversion: \"1.3.0\"\nsynopsis: \"Fast, portable and opinionated build system\"\ndescription: \"\"\"\ndune is a build system that was designed to simplify the release of\nJane Street packages. It reads metadata from \"dune\" files following a\nvery simple s-expression syntax.\n\ndune is fast, it has very low-overhead and support parallel builds on\nall platforms. It has no system dependencies, all you need to build\ndune and packages using dune is OCaml. You don't need or make or bash\nas long as the packages themselves don't use bash explicitly.\n\ndune supports multi-package development by simply dropping multiple\nrepositories into the same directory.\n\nIt also supports multi-context builds, such as building against\nseveral opam roots/switches simultaneously. This helps maintaining\npackages across several versions of OCaml and gives cross-compilation\nfor free.\"\"\"\nmaintainer: \"[email protected]\"\nauthors: \"Jane Street Group, LLC <[email protected]>\"\nlicense: \"MIT\"\nhomepage: \"https://github.com/ocaml/dune\"\nbug-reports: \"https://github.com/ocaml/dune/issues\"\ndepends: [\n \"ocaml\" {>= \"4.02\"}\n]\nconflicts: [\n \"jbuilder\" {!= \"transition\"}\n]\nbuild: [\n [\"ocaml\" \"configure.ml\" \"--libdir\" lib] {opam-version < \"2\"}\n [\"ocaml\" \"bootstrap.ml\"]\n [\"./boot.exe\" \"--release\" \"--subst\"] {pinned}\n [\"./boot.exe\" \"--release\" \"-j\" jobs]\n]\ndev-repo: \"git+https://github.com/ocaml/dune.git\"\nurl {\n src: \"https://github.com/ocaml/dune/releases/download/1.3.0/dune-1.3.0.tbz\"\n checksum: \"md5=d7c926bd6b7549cb54d5463aaccf0c91\"\n}",
"override": {
"build": [
[ "ocaml", "bootstrap.ml" ],
Expand Down Expand Up @@ -661,11 +662,11 @@
"name": "conf-which",
"version": "1",
"opam":
"opam-version: \"2.0\"\nname: \"conf-which\"\nversion: \"1\"\nsynopsis: \"Virtual package relying on which\"\ndescription:\n \"This package can only install if the which program is installed on the system.\"\nmaintainer: \"[email protected]\"\nauthors: \"Carlo Wood\"\nlicense: \"GPL-2+\"\nhomepage: \"http://www.gnu.org/software/which/\"\nbug-reports: \"https://github.com/ocaml/opam-repository/issues\"\ndepends: [\"ocaml\"]\nbuild: [\"which\" \"which\"]\ndepexts: [\n [\"which\"] {os-distribution = \"centos\"}\n [\"which\"] {os-distribution = \"fedora\"}\n [\"which\"] {os-distribution = \"opensuse\"}\n [\"debianutils\"] {os-distribution = \"debian\"}\n [\"debianutils\"] {os-distribution = \"ubuntu\"}\n [\"which\"] {os-distribution = \"nixos\"}\n [\"which\"] {os-distribution = \"archlinux\"}\n]",
"opam-version: \"2.0\"\nname: \"conf-which\"\nversion: \"1\"\nsynopsis: \"Virtual package relying on which\"\ndescription:\n \"This package can only install if the which program is installed on the system.\"\nmaintainer: \"[email protected]\"\nauthors: \"Carlo Wood\"\nlicense: \"GPL-2+\"\nhomepage: \"http://www.gnu.org/software/which/\"\nbug-reports: \"https://github.com/ocaml/opam-repository/issues\"\nbuild: [\"which\" \"which\"]\ndepexts: [\n [\"which\"] {os-distribution = \"centos\"}\n [\"which\"] {os-distribution = \"fedora\"}\n [\"which\"] {os-distribution = \"opensuse\"}\n [\"debianutils\"] {os-distribution = \"debian\"}\n [\"debianutils\"] {os-distribution = \"ubuntu\"}\n [\"which\"] {os-distribution = \"nixos\"}\n [\"which\"] {os-distribution = \"archlinux\"}\n]",
"override": null
}
},
"dependencies": [ "@esy-ocaml/[email protected]", "[email protected]" ]
"dependencies": [ "@esy-ocaml/[email protected]" ]
},
"@opam/conf-m4@opam:1": {
"record": {
Expand All @@ -677,11 +678,11 @@
"name": "conf-m4",
"version": "1",
"opam":
"opam-version: \"2.0\"\nname: \"conf-m4\"\nversion: \"1\"\nsynopsis: \"Virtual package relying on m4\"\ndescription:\n \"This package can only install if the m4 binary is installed on the system.\"\nmaintainer: \"[email protected]\"\nlicense: \"GPL-3\"\nhomepage: \"http://www.gnu.org/software/m4/m4.html\"\nbug-reports: \"https://github.com/ocaml/opam-repository/issues\"\ndepends: [\"ocaml\"]\nbuild: [\"sh\" \"-exc\" \"echo | m4\"]\ndepexts: [\n [\"m4\"] {os-distribution = \"debian\"}\n [\"m4\"] {os-distribution = \"ubuntu\"}\n [\"m4\"] {os-distribution = \"fedora\"}\n [\"m4\"] {os-distribution = \"rhel\"}\n [\"m4\"] {os-distribution = \"centos\"}\n [\"m4\"] {os-distribution = \"alpine\"}\n [\"m4\"] {os-distribution = \"nixos\"}\n [\"m4\"] {os-distribution = \"opensuse\"}\n [\"m4\"] {os-distribution = \"oraclelinux\"}\n [\"m4\"] {os-distribution = \"archlinux\"}\n]",
"opam-version: \"2.0\"\nname: \"conf-m4\"\nversion: \"1\"\nsynopsis: \"Virtual package relying on m4\"\ndescription:\n \"This package can only install if the m4 binary is installed on the system.\"\nmaintainer: \"[email protected]\"\nauthors: \"GNU Project\"\nlicense: \"GPL-3\"\nhomepage: \"http://www.gnu.org/software/m4/m4.html\"\nbug-reports: \"https://github.com/ocaml/opam-repository/issues\"\nbuild: [\"sh\" \"-exc\" \"echo | m4\"]\ndepexts: [\n [\"m4\"] {os-distribution = \"debian\"}\n [\"m4\"] {os-distribution = \"ubuntu\"}\n [\"m4\"] {os-distribution = \"fedora\"}\n [\"m4\"] {os-distribution = \"rhel\"}\n [\"m4\"] {os-distribution = \"centos\"}\n [\"m4\"] {os-distribution = \"alpine\"}\n [\"m4\"] {os-distribution = \"nixos\"}\n [\"m4\"] {os-distribution = \"opensuse\"}\n [\"m4\"] {os-distribution = \"oraclelinux\"}\n [\"m4\"] {os-distribution = \"archlinux\"}\n]",
"override": null
}
},
"dependencies": [ "@esy-ocaml/[email protected]", "[email protected]" ]
"dependencies": [ "@esy-ocaml/[email protected]" ]
},
"@opam/cmdliner@opam:1.0.2": {
"record": {
Expand Down
9 changes: 9 additions & 0 deletions examples/Bin.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ open Revery;

let init = (app) => {
let _ = app#createWindow("test");

let%lwt b = File.openAsync("binary.dat");
let length = Bigarray.Array1.dim(b);

for(i in 0 to length - 1) {
print_endline (string_of_int(Bigarray.Array1.get(b, i)));
}

Lwt.return ();
};

/* Js.Unsafe.fun_call(Js.Unsafe.js_expr("console.log"), [||]); */
Expand Down
Binary file added examples/binary.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
(executable
(name Bin)
(preprocess (pps lwt_ppx))
(public_name Bin)
(js_of_ocaml)
(libraries
js_of_ocaml
Revery
))
(install
(section bin)
(files binary.dat index.html Bin.bc.js))
16 changes: 16 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html>
<head>
<title>This is the title of the webpage!</title>
<style>
html, body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<p>This is an example paragraph. Anything in the <strong>body</strong> tag will appear on the page, just like this <strong>p</strong> tag and its contents.</p>
<script src="Bin.bc.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"build": "esy b",
"build:js": "esy b dune build examples/Bin.bc.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down
10 changes: 10 additions & 0 deletions scripts/make-binary-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fs = require("fs");

const testData = new Uint8Array(5);
testData[0] = 255;
testData[1] = 0;
testData[2] = 1;
testData[3] = 2;
testData[4] = 128;

fs.writeFileSync("binary.dat", new Buffer(testData));
5 changes: 2 additions & 3 deletions src/App.re
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let native_loop = (loop) => {
while (1 > 0) {
loop();
glfwPollEvents();
Unix.sleepf(1. /. 240.);
};
};

Expand All @@ -47,11 +48,9 @@ let start = (initFunc) => {
};

let _ = glfwInit();

initFunc(appInstance);
let _ = initFunc(appInstance);

let appLoop = () => {
print_endline ("app loop");
List.iter((w) => w#render(), appInstance#getWindows);
};

Expand Down
19 changes: 19 additions & 0 deletions src/File.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

type byteArray = Bigarray.Array1.t(int, Bigarray.int8_unsigned_elt, Bigarray.c_layout);

exception FileOpenException(string);

type successFunction = (byteArray) => unit;
type failureFunction = (string) => unit;

external open_sync_raw: (string, successFunction, failureFunction) => unit = "caml_open_sync_raw";

let openAsync = (p) => {
let (promise, resolver) = Lwt.task();

let success = (ba) => Lwt.wakeup_later(resolver, ba);
let failure = (message) => Lwt.wakeup_later_exn(resolver, FileOpenException(message));

open_sync_raw(p, success, failure);
promise
};
9 changes: 9 additions & 0 deletions src/File.rei
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

type byteArray = Bigarray.Array1.t(int, Bigarray.int8_unsigned_elt, Bigarray.c_layout);

exception FileOpenException(string);

type successFunction = (byteArray) => unit;
type failureFunction = (string) => unit;

let openAsync: (string) => Lwt.t(byteArray);
3 changes: 2 additions & 1 deletion src/Window.re
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class window (name: string) = {
glfwMakeContextCurrent(w);
glClearColor(0.39, 0.58, 0.93, 1.0);
glfwSwapBuffers(w);
}
};

}

/* type window = Glfw.window; */
Expand Down
4 changes: 3 additions & 1 deletion src/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(library
(name Revery)
(public_name Revery)
(libraries Reglfw))
(js_of_ocaml (javascript_files file.js))
(cxx_names file)
(libraries lwt lwt.unix reglfw))
38 changes: 38 additions & 0 deletions src/file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>

#include <caml/mlvalues.h>
#include <caml/bigarray.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/callback.h>

#include <string.h>

extern "C" {

CAMLprim value
caml_open_sync_raw(value vFileName, value vSuccess, value vFailure) {
CAMLparam1(vFileName);
FILE *pFile = fopen("binary.dat", "rb");

if (!pFile) {
caml_callback(vFailure, caml_copy_string("Unable to load file"));
} else {
fseek(pFile, 0, SEEK_END);
long lSize = ftell(pFile);
fseek(pFile, 0, SEEK_SET);

char *pData = (char *)malloc(lSize + 1);
fread(pData, lSize, 1, pFile);

fclose(pFile);

long dims[1];
dims[0] = lSize;
value ba = caml_ba_alloc(CAML_BA_UINT8 | CAML_BA_C_LAYOUT, 1, pData, (long long int *)dims);
caml_callback(vSuccess, ba);
}

CAMLreturn(Val_unit);
}
}
51 changes: 51 additions & 0 deletions src/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Provides: caml_thread_create
function caml_thread_create() { }

// Provides: caml_thread_initialize
function caml_thread_initialize() { }

// Provides: caml_mutex_new
function caml_mutex_new() { }

// Provides: lwt_unix_iov_max
function lwt_unix_iov_max() { }

// Provides: lwt_unix_init_notification
function lwt_unix_init_notification() { }

// Provides: lwt_unix_init_signals
function lwt_unix_init_signals() { }

// Provides: lwt_unix_set_signal
function lwt_unix_set_signal() { }

// Provides: lwt_unix_system_byte_order
function lwt_unix_system_byte_order() { }

// Provides: lwt_unix_get_page_size
function lwt_unix_get_page_size() { }

// Provides: caml_open_sync_raw
function caml_open_sync_raw(fileName, onSuccess, onFailure) {
var f = caml_js_from_string(fileName);
console.log(f);
var data = new Uint8Array([17, 211, 2]);

var result = null;

function onSuccess () {
var res = new Uint8Array(oReq.response);
var ba = caml_ba_create_from(res, null, 0, 0, 0, [res.length])
onSuccess(ba);
};

function onFailure () {
onFailure(caml_js_to_string("failed to load file: " + f));
};

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", onSuccess, onFailure);
oReq.responseType = "arraybuffer"
oReq.open("GET", fileName, true);
oReq.send();
}

0 comments on commit bb9f0c7

Please sign in to comment.