Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 60 lines (48 sloc) 1.26 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include <chaiscript/chaiscript.hpp>

int dosomething(int i)
{
  return i + 2;
}

int dosomethingelse(int i)
{
  return i * 2;
}



int main()
{
  chaiscript::ChaiScript chai;
  chai.add(chaiscript::fun(&dosomething), "dosomething");
  chai.add(chaiscript::var(1), "i");

  for (int i = 0; i < 10; ++i)
  {
    chaiscript::ChaiScript chai2;
    chai2.add(chaiscript::fun(&dosomethingelse), "dosomethingelse");

    std::stringstream ss;
    ss << i;

    if (chai.eval<int>("dosomething(" + ss.str() + ")") != i + 2)
    {
      return EXIT_FAILURE;
    }

    if (chai2.eval<int>("dosomethingelse(" + ss.str() + ")") != i * 2)
    {
      return EXIT_FAILURE;
    }

    try {
      chai2.eval("dosomething(1)");
      return EXIT_FAILURE; // should not get here
    } catch (const chaiscript::exception::eval_error &) {
      // nothing to do, expected case
    }

    try {
      chai2.eval("i");
      return EXIT_FAILURE; // should not get here
    } catch (const chaiscript::exception::eval_error &) {
      // nothing to do, expected case
    }

    try {
      chai.eval("dosomethingelse(1)");
      return EXIT_FAILURE; // should not get here
    } catch (const chaiscript::exception::eval_error &) {
      // nothing to do, expected case
    }
  }
}
Something went wrong with that request. Please try again.