Compiled TIFF.js with Emscripten does not have access to FS

290 views Asked by At

I'm trying to compile TIFF.js into WASM file and cloned latest version of https://github.com/seikichi/tiff.js/ It's currently present in npmjs repository but in asmjs form. It compiles without errors(it needed some minor tweaks to build properly) but somehow code from "native" side completely ignores Emscripten in-memory filesystem. From JavaScript side I can run something like this:

var fp = Module.FS.open("/testfile.tiff", "w");
Module.FS.write(fp, new Uint8Array([22,23,24,25,26,27]), 0,6,0,true);
Module.FS.close(fp);

It works great -- I can FS.stat() this file from JavaScript size. However, if I'll add a function like this to native code

bool FileExist(char *filename) {
  struct stat   buffer;   
  return (stat (filename, &buffer) == 0);
}

And call it from JavaScript, it always says false -- it does not see the file. Even if I create file on native side and then check if it exists, it will not be there. I'm doing something wrong here, but cannot see what and where?

I've also tried to compile asmjs version from sources and got same result -- cannot see files in native code, so I suppose it's not something related to compiling WASM and more general.

I've tried making separate project, just to check if FS works for me, and in a very simple single-file setup everything works just fine -- I can create files from JS and Native and see everything on both sides.

Edit #1: I've tried to test on a web page from gh-pages of tiff.js repo and this is the code I've put to native part, into "export.c"

bool TestFile(char *filename)
{
  struct stat buffer;
  bool result = (stat(filename, &buffer) == 0);

  perror(0);

  return result;
}

This is what is being executed once I received TIFF image

const uint8arr = new Uint8Array(buffer);

console.log(uint8arr.length);

const filename = `image.tif`;
const path = `/${filename}`;

const stream = FS.open(path, "w");
FS.write(stream, uint8arr, 0, uint8arr.length, 0, true);
FS.close(stream);

console.log('FS.stat: ', FS.stat(path));

console.log('Module._TestFile: ', Module._TestFile(path));

And this is what I get in browser console

8609
upload.html:86 FS.stat:  {dev: 1, ino: 20, mode: 33206, nlink: 1, uid: 0, …}
tiff.js:1 No such file or directory
   put_char @ tiff.js:1
   write @ tiff.js:1
   write @ tiff.js:1
   doWritev @ tiff.js:1
   _fd_write @ tiff.js:1
   (anonymous) @ wasm-00509d86-761:1
   (anonymous) @ wasm-00509d86-768:1
   (anonymous) @ wasm-00509d86-769:1
   (anonymous) @ wasm-00509d86-774:1
   (anonymous) @ wasm-00509d86-37:1
   Module._TestFile @ tiff.js:1
   init @ upload.html:88
   (anonymous) @ upload.html:174
   load (async)
   show @ upload.html:163
   (anonymous) @ upload.html:207
   dispatch @ jquery-2.0.3.min.js:5
   y.handle @ jquery-2.0.3.min.js:5
upload.html:88 Module._TestFile:  0

When I do the same on an empty project, it works fine.

0

There are 0 answers