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

In my application, third party game developers will write simple javascript logic based on my template.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745624629a4636731.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信