rails / execjs

Run JavaScript code from Ruby
MIT License
539 stars 283 forks source link

Can't call a function with "NaN" input when using MiniRacer #139

Closed dvkch closed 10 months ago

dvkch commented 10 months ago

I'm currently using ExecJS to allow mini scripts to be written in a backoffice, to create small functions that can convert an input value into another.

While using Duktape worked wonders, it doesn't support recent JS syntax (like string interpolation, String.prototype.padStart, etc), I switched to MiniRacer to avoid using an external runtime.

I am now facing the following error :

JS script
function transform(value) {
   return value
}
Ruby code
def run(value)
  ExecJS.runtime = ExecJS::Runtimes::MiniRacer
  ctx = ExecJS.compile('')
  ctx.call(js_code, value)
end
Exception
JSON::GeneratorError: NaN not allowed in JSON

This seems linked to the fact that input values are sent as JSON in MiniRacerRuntime, see https://github.com/rails/execjs/blob/5f78865d36976f85b42e8529dec272285d8f3b85/lib/execjs/mini_racer_runtime.rb#L33.

Would there be any other way to improve the situation ? I'm open to using another interpreter, but keeping a recent enough dialect of JS + disallowing external runtime seems to limit the choices only to MiniRacer (unless you are running ruby truffle)

Thanks!

byroot commented 10 months ago

ExecJS is a compatibility layer to allow switching between multiple backends, as such it limits the capability to the common denominator of all possible backends. That only make sense for libraries that need to execute some JS code but don't want to force a specific backend on their users.

In your case it seems you don't need this switching capability anyway, and you require specific features. So you might as well use MiniRacer directly without the ExecJS layer.

dvkch commented 10 months ago

Fair enough, I'll look into that, thank you for your answer :)