2

I am trying to create a function that takes a classname and parameters as arguments, and tries to define them inside a sandbox environment.

E.g., the function may be called with arguments name: "Dog", args: {name: "string", isGoodBoy: "boolean"}. I then would like to define a class inside a sandbox named Dog that has 2 class variables, name: string and isGoodBoy: boolean.

I've tried to define the code with the vm module (see test example below), but to no avail - Node.js fails to execute the code given to the context.

    vm.runInContext(
        "export class eval(object.name) extends CoreObject {\n" +
        "    constructor() {\n" +
        "        console.log(\"I am here\");\n" +
        "    }\n" +
        "}", context);
a_hausb
  • 161
  • 1
  • 9
  • Not sure about making the function completely dynamic. However, you can map the params and instantiate classes accordingly. Take a look at this: https://stackoverflow.com/questions/34655616/create-an-instance-of-a-class-in-es6-with-a-dynamic-name Might wanna ask yourself why you need to have this capability. There could be other ways to solve the problem you're having. – Prashant Ghimire Jul 11 '20 at 20:03
  • I want this too. My case is to run decorators with my class in a sandbox, then get the log of decorating functions out of the sandbox for analytics. – Huan Jun 30 '21 at 18:05

0 Answers0