Hello,
I have this source (fichier.nim)
proc readf*( filename:string) :string {.exportc.} =
result = readFile( filename)
I compile it to javascript with nim js fichier.nim.
When used with this html
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"/></head>
<body>
<script src="fichier.js"></script>
<script>
console.log( readf("test.txt"));
</script>
</body>
<html>
Without surprise, i get an error in javascript console.
Uncaught ReferenceError: fopen is not defined
open_1770829 file:///home/jseb/(…)/fichier.js:529
readFile_1806214 file:///home/jseb/(…)/fichier.js:734
readf file:///home/jseb/(…)/fichier.js:761
<anonymous> file:///home/jseb/(…)/utiliser_fichier.html:10
fichier.js:529:22
The error itself is clear: no fopen for javascript.
So i could have a codepath for loading files, with a javascript chunk (maybe just <script src="binary.js"></script> , with binary.js a big array generated at compilation ? Or true javascript function for loading files upon request).
Is this the way to go ?
And how separate javascript and libc functions in my source file ?
Using the when: macro ?
Thank you.
On browser you can not read files like that because sandbox.
On nodejs https://juancarlospaco.github.io/nodejs/nodejs/jsfs.html
You can use staticRead on both.
Ok, understood for nodeJS (good to know).
So for javascript in browser, i will generate some glue code for filling a javascript buffer, i guess.
Thank you for your answer.