mfikes / esprit

ClojureScript on the ESP32 using Espruino
Eclipse Public License 2.0
138 stars 13 forks source link

Uncaught SyntaxError: Got EOF expected '}' #12

Closed kiranshila closed 4 years ago

kiranshila commented 4 years ago

As far as I can tell, every time I try to compile and load my own code (not writing code in the repl) the last four characters are getting lost somewhere.

I have a cljs file on the classpath like so

(ns my-project.core
  (:require [esprit.repl]))

; Configure WiFi
(def wifi (js/require "Wifi"))
(def ssid "REDACTED")
(def pass "REDACTED")

(.connect wifi ssid #js{:password pass})

(defn do-math [x]
  (+ (* x x) x))

I build the project with clj -m cljs.main -co {:optimizations :simple :target :none :browser-repl false :process-shim false} -c my-project.core and build the rom and upload following your example.

However, I get the error

Uncaught SyntaxError: Got EOF expected '}'
 at line 2138 col 530
...math=function(a){return a*a
                              ^

The REPL does start and I can connect, but the do-math function is of course undefined.

If I add more code to the cljs file, it always misses the last four characters - in this case +a};

kiranshila commented 4 years ago

To add to this, the binary does in fact have those characters - which makes this even more strange to me

000d5a20: 756e 6374 696f 6e28 6129 7b72 6574 7572  unction(a){retur
000d5a30: 6e20 612a 612b 617d 3b                   n a*a+a};
kiranshila commented 4 years ago

So, I modified the binary and incremented the size by four by the header and it fixed the problem. Even stranger.

kiranshila commented 4 years ago

Huzzah! https://github.com/mfikes/esprit/blob/adf193769a16d48ca970fea377b37262dfeb7fff/src/esprit/make_rom.clj#L18 returns the number of characters, NOT the number of bytes. I think there is some assumption in count that there exists a fixed length encoding.

So on my machine,

(def js (slurp "out/main.js"))
(count js)

is not equal to

(count (.getBytes js))

And the latter matches the actual byte count

kiranshila commented 4 years ago

But then why is there non UTF-8 stuff in main.js :thinking:

kiranshila commented 4 years ago

Lol so compiling with advanced optimizations removes it, but with :simple, it looks like there is some strange unicode in main.js The two I found were

goog.string.Unicode={NBSP:" "}

and

goog.string.isUnicodeChar=function(a){return 1==a.length&&" "<=a&&"~">=a||"€"<=a&&"�">=a}
kiranshila commented 4 years ago

Ok, so this file has to be UTF-8 due to this. This makes sense as the web and JS is utf-8, so it would follow that espruino works with it. Alright so the solution is just ensuring we count the number of bytes correctly with .getBytes

PR inbound