最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How can I run javascript logic from a string in React Native? - Stack Overflow

matteradmin6PV0评论

In my application, third party game developers will write simple javascript logic based on my template. They will enter their code into a textbox (online), and I will store their code as Strings.

For example, one game developer would write this:

const examineItem = (user, item, app) => { //we will pass `user` and `item` with our own methods
    //Developer writes his/her own logic
    if(user.intelligence > 50){
        return({ result: "You see a rock!"});
    }else{
        return({ result: "You see nothing"});
    }
};

In my React Native app, how can I "convert" this string code into a runnable function? Do I need some interpreter? Do I use eval (which is deprecated)?

Where do I get started?

In my application, third party game developers will write simple javascript logic based on my template. They will enter their code into a textbox (online), and I will store their code as Strings.

For example, one game developer would write this:

const examineItem = (user, item, app) => { //we will pass `user` and `item` with our own methods
    //Developer writes his/her own logic
    if(user.intelligence > 50){
        return({ result: "You see a rock!"});
    }else{
        return({ result: "You see nothing"});
    }
};

In my React Native app, how can I "convert" this string code into a runnable function? Do I need some interpreter? Do I use eval (which is deprecated)?

Where do I get started?

Share Improve this question asked Jul 11, 2018 at 2:44 TIMEXTIMEX 273k368 gold badges802 silver badges1.1k bronze badges 1
  • javascript.info/new-function – Isaac Commented Jul 11, 2018 at 3:04
Add a ment  | 

2 Answers 2

Reset to default 5

It's actually not too hard. Use the "new function" operator like so:

const examineItem = (user, item, app) => {
  // Developer writes his/her own logic
  const devString = 'return user.intelligence > 50'

  // You can pass as many arguments as you want, just keep the string last:
  const devFunc = new Function('user', 'item', 'app', devString) 

  // Make sure to pass all the arguments to the call:
  if(devFunc(user, item, app)){
    return({ result: "You see a rock!"});
  } else{
    return({ result: "You see nothing"});
  }
};

// Testing it:
const user = { intelligence: 60 }
const res = examineItem(user, null, null)
console.log(res) // { result: "You see a rock!"}

I just tested this code, and it should work. You could pull the "devString" from wherever you wanted.

The main danger with this would just be to make sure that all of your devs know which parameters will be passed to the "string" that they write, as well as what the data model is for those parameters.

I made a similar project once, dont forget that custom user code can alter your variables if not properly encapsulated I highly remend you to read about IIFEs functions.

Ex. user.intelligence = 1000; // will alter all the user object

Post a comment

comment list (0)

  1. No comments so far