typelevel / cats-effect

The pure asynchronous runtime for Scala
https://typelevel.org/cats-effect/
Apache License 2.0
1.99k stars 511 forks source link

Support for V8 JavaScript Engine? #4056

Open LukeKeywalker opened 2 months ago

LukeKeywalker commented 2 months ago

I try to use FS2 from inside Unreal engine via Unreal.js plugin, which essentially is a wrapper around V8 JavaScript engine, but I get into undefined required script 'perf_hooks', and missing setTimeout method.

LogJavascript: Warning: Undefined required script 'perf_hooks'
LogJavascript: Error: file:///c:/Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/main.js:480: ReferenceError: setTimeout is not defined
LogJavascript: Error:     at file:///c:/Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/main.js:480:327
LogJavascript: Error:     at H.d (file:///c:/Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/main.js:421:173)
LogJavascript: Error:     at Pz.wk (file:///c:/Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/main.js:481:37)
LogJavascript: Error:     at Ic.wk (file:///c:/Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/main.js:434:364)
LogJavascript: Error:     at Ho (file:///c:/Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/main.js:204:233)
LogJavascript: Error:     at jn (file:///c:/Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/main.js:203:288)
LogJavascript: Error:     at Wm (file:///c:/Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/main.js:192:252)
LogJavascript: Error:     at Vm.sayHello (file:///c:/Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/main.js:192:308)
LogJavascript: Error:     at file:///../../../../../../Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/on-start.js:5:14
LogJavascript: Error:     at file:///../../../../../../Users/Lukasz.Michniewicz/projects/UnrealScalaJS/Content/Scripts/on-start.js:7:3

As far as I know this error occurs in non-browser environments like Node.js, and I guess V8 falls into this category as well. Is there any way of compiling code using FS2 which does not use setTimeout internally?

armanbilge commented 2 months ago

As far as I know this error occurs in non-browser environments like Node.js

Hm, that can't be right. We test both FS2 and Cats Effect on Node.js in CI :)

Is there any way of compiling code using FS2 which does not use setTimeout internally?

setTimeout is used for implementing IO.sleep. Without an alternative API that we could use for this, it's impossible to have a fully functional runtime. What kind of program are you hoping to run?


BTW, I have made changes before to better support V8. I believe it does support setTimeout.

djspiewak commented 2 months ago

I think the bigger problem you might run into will be the lack of setImmediate. I'm not sure if bare V8 has that, and if it doesn't, it's certainly not going to support any of the polyfill shenanigans we do to replace it, so you're probably going to end up with a lot of setTimeout(0, ...). That would be an interesting thing to look at as well.