Eval in makecode

Something I have been trying to do recently was getting eval into makecode. This sounds hard or impossible to do if you are not familiar with shims, and sounds easy when you are. In Javascript, eval is a function that executes Javascript. So eval("console.log(1);") logs 1 to the console, eval("let t = eval("['a', 'b', 'c'].join()");") makes a variable t with the data abc, etc. Since eval is not a function in makecode, I first searched and found that this file is the only file that has eval in a way that you can shim it. The only issue is that it’s inside pxsim.Runtime’s constructor, and it’s impossible to shim a class. Thankfully, I found that this file calls that constructor. In that function, msg is a SimulatorRunMessage, which is an interface, another thing you can’t shim. You can pass an object instead of an interface, and it will work fine. Sadly, when you create an object in makecode, it gets turned into a RefMap, which is a class and is defined here. I have no idea how to pass in an object and not a RefMap, though. There is a toAny function attached to RefMap, but I don’t know how to call it. Does anybody have any suggestions? Here was my attempt at it. Also, if you want to help, use devTools. In the console, you should see an error when playing my attempt. If you click the upper-most link on the error, it should send you to const entryPoint = .... If not, then hit ctrl+f and type eval(msg.code). Place a breakpoint on that line. Then, play the simulator. when it finishes, if you type msg.code in the simulator then you can see an the code. you can do console.log(msg.code.replaceAll("\\n", "\n")), then click copy (I forgot what the actual button is called) and paste it to something like notepad. The total code is around 1.9 MB, an empty project is 1.8 MB, and 2.1MB is probably the highest anything gets. If I do discover something, then I would make this an extension. Although, the @moderators would probably stop it from being possible.

2 Likes

no, this is never something that is going to be allowed, this is something we specifically avoid allowing for security. If you want to do something like that it has to be something you control (e.g. github pages page with message passing), but then there’s pretty much no reason to do so in the first place.

4 Likes

that’s what I thought