Cannot create a JavaScript "Function" object

When attempting to create a JavaScript “Function” object, the Adobe XD runtime issues an error stating “Code generation from strings disallowed for this context”.

We understand that Adobe prevents from this in order to make the plugin as save as possible, but in our case, we really need to run a small JavaScript code coming from an external JavaScript file, which is unknown at the time of the plugin publication.

How could we work this around, please?

You could use a JS interpreter. Take a look at @kerrishotts comment in Can I use `eval` or similar functions? for some options. May I ask what you’ll use that for? As you’ve said, the prevention of something like this is there for a reason (and one should therefore not just work around that without being extremely careful)…

Thank you for your very responsive answer, we are going to explore the JS interpreter option.

1 Like

There are several options available; I’ve been able to use sval to good effect. Some interpreters only go up to ES5, although supposedly there are some out there that will also let you use modern JavaScript (haven’t tried yet).

What’s cool about sval is you can set it up to use the current context, so you can expose XD’s APIs to it with a single line of code. Super handy :slight_smile:

1 Like

Nifty! Can you share that line of code? :wink: