jeroen/V8

Is it possible to upgrade V8 for Windows?

stla opened this issue · 8 comments

stla commented

Hello,

My package prettifyAddins provides a function / an addin to prettify JavaScript code, using the JavaScript library prettier.

That does not work on Windows, that's probably because the V8 engine is too old.

Assume I manage to build V8 from source (I found some instructions to do so), would it be possible to use the built with the V8 R package ?

What error do you get? The V8 engine on Windows is a little older but not that old.

stla commented

With this JS file:

var path = require("path");

module.exports = {
  entry: path.join(__dirname, "srcjs", "toastify.jsx"),
  output: {
    path: path.join(__dirname, "inst/www/shinyToastify/toastify"),
    filename: "toastify.js"
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        options: {
          presets: ["@babel/preset-env", "@babel/preset-react"]
        }
      },
      // For CSS so that import "path/style.css"; works
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"]
      }
    ]
  },
  externals: {
    react: "window.React",
    "react-dom": "window.ReactDOM",
    reactR: "window.reactR"
  },
  stats: {
    colors: true
  },
  devtool: "source-map"
};

I get this error:

t.originalText.slice(...).trimEnd is not a function

I can run the same prettifier with Shiny and get no error.

stla commented
library(V8)
ctx <- v8()
ctx$eval('const str = "   JavaScript   ";')
ctx$eval('const result = str.trimEnd();')
# Error in context_eval(join(src), private$context, serialize) : 
#   TypeError: str.trimEnd is not a function

I'm not quite sure how you manage to load the library from your example, but maybe you can load a polyfill before the library that needs that function, for example:

Upgrading V8 on Windows is tricky because CRAN runs a very old version of Windows on the winbuilder. And common linux distributions also ship with older versions of V8, so it would be recommended to use code that will also work with older JS versions.

stla commented

Thanks. This works with the String.prototype.trimEnd() of the first link. But I'm not sure that the only problem one can encounter. Anyway this work with the Shiny version of the prettifier, so it's not a tragedy.

stla commented

I've just noticed that the Windows version of V8 does not support .trimStart as well. I found its code thanks to your link and I added it to my package.
Would you know whether it is possible to find a list of JavaScript functions which are not supported by V8 Windows?

You would need to google which version of ECMAscript that the given version of V8 (from V8::engine_info())` implements, and then you can see which JavaScript functions are supported.

But typically, a safer solution is to transpile your javascript to ES5 using babel or whatever, such that all the missing functions are automatically polyfilled. Many JavaScript projects already provide such a portable dist bundle that works in older JS .

stla commented

Ok, thank you!