jeroen / V8

Embedded JavaScript Engine for R
https://cran.r-project.org/package=V8
Other
199 stars 29 forks source link

ReferenceError with TextEncoder #143

Open fangzhou-xie opened 2 years ago

fangzhou-xie commented 2 years ago

Thanks for the package for bridging JS into R! I was reading JavaScript for R, Chapter 18 and was thinking if I can browserify jsdom to be used in R.

I have in.js:

global.jsdom = require('jsdom');

converted by:

browserify in.js -o jsdom.js

and then called in R:

library(V8)
jsdom <- v8()
jsdom$source("node/jsdom.js")

But then I got an error ReferenceError: TextEncoder is not defined.

My searching on Google told me that it could be Node version problem (using Node 18 first, but noticed V8 engine version 9.6.180.12 and then tried Node 16). But downgrading didn't work. I also tried to hack in whatwg-url source code, but then I got TypeError: util.TextEncoder is not a constructor error.

I am not sure why this is the case (sorry for my not being very proficient in JS), but jsdom should work fine with browserify and I think this translation should work out. And potentially I can call jsdom from R via V8.

Any help/insight will be appreciated! Thank!

mahideel commented 1 year ago

Hi , have you managed to solve your issue? I have the same problem, all the function I tried calling came up with ReferenceError: Function is not defined.

Function - whichever function I tried calling.

Thanks.

fangzhou-xie commented 1 year ago

Unfortunately no. I am not very familiar with JS and I only tried this because it seems straightforward. But maybe it is not as easy as it seems to be.

jeroen commented 1 year ago

You are probably trying to use functions which are not part of core JavaScript. See this vignette: https://cran.r-project.org/web/packages/V8/vignettes/npm.html

mahideel commented 1 year ago

Hi,

Thanks for your reply.

I mostly use R but there are some libraries and modules in JS that are needed for my work, hence your V8 appeals a lot as it seems to enable JS to be run from R.

So re: functions - V8 only works for functions that are part of core JavaScript? ‘

The modules/libraries I am trying to load mljs.github.io/spectra-processing/ http://mljs.github.io/spectra-processing/.

Since I am not familiar about JavaScript, I am afraid I can’t tell you if their functions are part of core JavaScript or not.

Is it possible to run the call the modules directly from my local drive (I’ve cloned the repo)?

In your Introduction to V8 for R: ct$source(system.file("js/underscore.js", package="V8”))

Is “js/underscore.js” the source file for package V8? So the package part is to state which module we are calling from the .js file?

Thanks.

On 28 Nov 2022, at 10:09 pm, Jeroen Ooms @.***> wrote:

You are probably trying to use functions which are not part of core JavaScript. See this vignette: https://cran.r-project.org/web/packages/V8/vignettes/npm.html https://cran.r-project.org/web/packages/V8/vignettes/npm.html — Reply to this email directly, view it on GitHub https://github.com/jeroen/V8/issues/143#issuecomment-1329173079, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJGZ7NKAU7PP2HKG7KOVNCDWKS4H3ANCNFSM5Y66EUDA. You are receiving this because you are subscribed to this thread.

nanxstats commented 1 year ago

I used the hints here and saved text-encoding explicitly as another dependency. Doing this will eliminate the error "ReferenceError: TextEncoder is not defined" when sourcing jsdom.js. See my blog post for details: https://nanx.me/blog/post/r-readability-parser/