JS Interpreter question

84 views
Skip to first unread message

William Li

unread,
Apr 24, 2023, 10:52:18 PM4/24/23
to Blockly
Hi, 

I'm looking into ways to evaluate JS code generated from Blockly. As of right now I just inject a script into the HTML of my electron app. However, I wanted a way to be able to start/stop the code from running so they can edit the code and rerun, as well as maintain security so that malicious code can't be executed. I noticed that the docs recommended JS Interpreter, which is sandboxed and allows for starting/stopping. However, I wanted to be able to import specific classes that I defined in a different JS file in my project. I inserted the files as modules in the HTML. I also wanted to be able to require('path'). There are also some other libraries which I import into the project HTML from online scripts. Is there a way to allow these specific objects to be used in JS Interpreter? 

Thanks in advance!

Maribeth Bottorff

unread,
Apr 26, 2023, 7:54:34 PM4/26/23
to Blockly
Hello,

Yes, you can use external APIs with JS Interpreter. You can find information about how to do so in the docs.

Best,
Maribeth

William Li

unread,
Apr 27, 2023, 1:47:54 AM4/27/23
to Blockly
Thanks, another thing I was worried about which I forgot to mention was that the docs said that JS Interpreter runs 200 times slower than native JS, is there a faster alternative?

Gregory Dyke

unread,
Apr 27, 2023, 10:35:47 AM4/27/23
to blo...@googlegroups.com
I haven't found another javascript interpreter written in javascript/javascript sandbox that I felt as confident was a good fit as js interpreter. It should be noted that 200x slower than blazingly fast is still pretty fast for most use cases.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/38afa480-f984-4c22-b32a-b0b06c422ad2n%40googlegroups.com.

Neil Fraser

unread,
Apr 27, 2023, 10:42:44 AM4/27/23
to blo...@googlegroups.com
Worth mentioning is that the "200x" number is not very real.  Some operations (e.g. Math or string operations) are exactly as performant as native JS.  Others (e.g. array operations) are significantly slower.  So the speed difference varies wildly based on what one is doing.  The performance profile is completely different from native JS.



--
Neil Fraser, Switzerland
https://neil.fraser.name

William Li

unread,
Apr 27, 2023, 12:11:16 PM4/27/23
to Blockly
I see, thank you very much!
Reply all
Reply to author
Forward
0 new messages