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

Lazy files #1190

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/js_of_ocaml/js_of_ocaml_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ void caml_read_file_content () {
fprintf(stderr, "Unimplemented Javascript primitive caml_read_file_content!\n");
exit(1);
}
void caml_register_lazy () {
fprintf(stderr, "Unimplemented Javascript primitive caml_register_lazy!\n");
exit(1);
}
void caml_string_of_array () {
fprintf(stderr, "Unimplemented Javascript primitive caml_string_of_array!\n");
exit(1);
Expand Down
2 changes: 2 additions & 0 deletions lib/js_of_ocaml/sys_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ let js_of_ocaml_version =
if String.equal Lib_version.git_version ""
then Lib_version.s
else Lib_version.s ^ "+" ^ Lib_version.git_version

external register_lazy : string -> unit = "caml_register_lazy"
6 changes: 6 additions & 0 deletions lib/js_of_ocaml/sys_js.mli
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ val create_file : name:string -> content:string -> unit
[create_file ~name ~content] register the file [name] with content [content]
so it can be be opened with [open_in name] *)

val register_lazy : string -> unit
(** Register a lazy file to a Pseudo Filesystem.
[register_lazy name] causes an entry to be made in the filesystem such that it
will be present in a directory list, but the callback will be called when the
file is opened. *)

val update_file : name:string -> content:string -> unit
(** Update a file in the Pseudo Filesystem.
[update_file ~name ~content] update the file [name] with content [content] *)
Expand Down
11 changes: 10 additions & 1 deletion runtime/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ function caml_create_file(name,content) {
return 0;
}


//Provides: jsoo_create_file
//Requires: caml_create_file, caml_string_of_jsbytes
function jsoo_create_file(name,content) {
Expand All @@ -324,6 +323,16 @@ function jsoo_create_file(name,content) {
}


//Provides: caml_register_lazy
//Requires: caml_failwith, resolve_fs_device, caml_string_of_jsbytes
function caml_register_lazy(name) {
var name = (typeof name == "string")?caml_string_of_jsbytes(name):name;
var root = resolve_fs_device(name);
if(! root.device.register_lazy) caml_failwith("cannot register lazy file");
root.device.register_lazy(root.rest);
return 0;
}

//Provides: caml_read_file_content
//Requires: resolve_fs_device, caml_raise_no_such_file, caml_create_bytes, caml_string_of_bytes
//Requires: caml_string_of_jsbytes
Expand Down
15 changes: 13 additions & 2 deletions runtime/fs_fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
//Requires: make_unix_err_args
function MlFakeDevice (root, f) {
this.content={};
this.lazyfiles = [];
this.root = root;
this.lookupFun = f;
}
Expand Down Expand Up @@ -60,13 +61,15 @@ MlFakeDevice.prototype.exists = function(name) {
// Check if a directory exists
var name_slash = this.slash(name);
if(this.content[name_slash]) return 1;
// If it's a lazy file, let's say it exists without doing a lookup.
if(this.lazyfiles.indexOf(name) >= 0) return 1;
// Check if a file exists
this.lookup(name);
return this.content[name]?1:0;
}
MlFakeDevice.prototype.mkdir = function(name,mode, raise_unix) {
var unix_error = raise_unix && caml_named_value('Unix.Unix_error');
if(this.exists(name)) {
if(this.exists(name) || (this.lazyfiles.indexOf(name) >= 0)) {
if (unix_error) {
caml_raise_with_args(unix_error, make_unix_err_args("EEXIST", "mkdir", this.nm(name)));
}
Expand Down Expand Up @@ -140,6 +143,10 @@ MlFakeDevice.prototype.readdir = function(name) {
var m = n.match(r);
if(m && !seen[m[1]]) {seen[m[1]] = true; a.push(m[1])}
}
this.lazyfiles.forEach(function(n) {
var m = n.match(r);
if(m && !seen[m[1]]) {seen[m[1]] = true; a.push(m[1])}
})
return a;
}
MlFakeDevice.prototype.is_dir = function(name) {
Expand All @@ -148,7 +155,7 @@ MlFakeDevice.prototype.is_dir = function(name) {
return this.content[name_slash]?1:0;
}
MlFakeDevice.prototype.unlink = function(name) {
var ok = this.content[name]?true:false;
var ok = (this.content[name]?true:false) || (this.lazyfiles.indexOf(name) >= 0);
delete this.content[name];
return ok;
}
Expand Down Expand Up @@ -195,6 +202,10 @@ MlFakeDevice.prototype.register= function (name,content){
else caml_raise_sys_error(this.nm(name) + " : registering file with invalid content type");
}

MlFakeDevice.prototype.register_lazy = function (name) {
this.lazyfiles.push(name)
}

MlFakeDevice.prototype.constructor = MlFakeDevice

//Provides: MlFakeFile
Expand Down