LuaBinder.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <tests/framework/Framework.h>
  6. #include <anki/Script.h>
  7. #include <anki/Math.h>
  8. static const char* script = R"(
  9. b = Vec4.new(0, 0, 0, 1.1)
  10. if math.abs(b:getW() - 1.1) > 0.00001 then
  11. print(b:getW())
  12. error("oh no!!")
  13. end
  14. b:setX(3.0)
  15. b:setY(4.0)
  16. v4:copy(v4 * b)
  17. v3:setZ(0.1)
  18. )";
  19. ANKI_TEST(Script, LuaBinder)
  20. {
  21. ScriptManager sm;
  22. ANKI_TEST_EXPECT_NO_ERR(sm.init(allocAligned, nullptr));
  23. Vec4 v4(2.0, 3.0, 4.0, 5.0);
  24. Vec3 v3(1.1, 2.2, 3.3);
  25. sm.exposeVariable("v4", &v4);
  26. sm.exposeVariable("v3", &v3);
  27. ANKI_TEST_EXPECT_NO_ERR(sm.evalString(script));
  28. ANKI_TEST_EXPECT_EQ(v4, Vec4(6, 12, 0, 5.5));
  29. ANKI_TEST_EXPECT_EQ(v3, Vec3(1.1, 2.2, 0.1));
  30. }
  31. ANKI_TEST(Script, LuaBinderThreads)
  32. {
  33. ScriptManager sm;
  34. ANKI_TEST_EXPECT_NO_ERR(sm.init(allocAligned, nullptr));
  35. ScriptEnvironmentPtr env;
  36. ANKI_TEST_EXPECT_NO_ERR(sm.newScriptEnvironment(env));
  37. static const char* script = R"(
  38. vec = Vec4.new(0, 0, 0, 0)
  39. function myFunc()
  40. vec:setX(vec:getX() + 1)
  41. logi(string.format("The number is %f", vec:getX()))
  42. end
  43. )";
  44. ANKI_TEST_EXPECT_NO_ERR(env->evalString(script));
  45. static const char* script1 = R"(
  46. myFunc()
  47. )";
  48. ANKI_TEST_EXPECT_NO_ERR(env->evalString(script1));
  49. ANKI_TEST_EXPECT_NO_ERR(env->evalString(script1));
  50. }