apps/runner/src/eu/runner/script.base.cc
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "eu/runner/script.base.h" | ||
| 2 | |||
| 3 | #include "eu/runner/script.h" | ||
| 4 | |||
| 5 | namespace | ||
| 6 | { | ||
| 7 | template<typename T, typename F> | ||
| 8 | ✗ | void bind_binary(lax::ClassAdder<T>* k, lax::Lax* lax, const char* const name, F op) | |
| 9 | { | ||
| 10 | ✗ | k->add_static_function(name, [lax, op](lax::Callable*, lax::ArgumentHelper& arguments)->std::shared_ptr<lax::Object> | |
| 11 | { | ||
| 12 | ✗ | auto lhs = arguments.require_native<T>("lhs"); | |
| 13 | ✗ | auto rhs = arguments.require_native<T>("rhs"); | |
| 14 | ✗ | if (arguments.complete()) | |
| 15 | { | ||
| 16 | ✗ | return lax::make_nil(); | |
| 17 | } | ||
| 18 | ✗ | return lax->make_native<T>(op(*lhs, *rhs)); | |
| 19 | ✗ | }); | |
| 20 | ✗ | } | |
| 21 | } | ||
| 22 | |||
| 23 | namespace eu::runner | ||
| 24 | { | ||
| 25 | // todo(Gustav): add option to switch lax from double to float, or just switch to float and stop caring about doubles | ||
| 26 | ✗ | float fl(lax::Tf d) { return static_cast<float>(d); } | |
| 27 | ✗ | lax::Tf lf(float f) { return static_cast<lax::Tf>(f); } | |
| 28 | |||
| 29 | ✗ | void register_vec3(lax::Lax* lax, lax::Scope* pkg) | |
| 30 | { | ||
| 31 | ✗ | auto k = pkg->define_native_class<v3>("v3", [](auto& args) | |
| 32 | { | ||
| 33 | ✗ | const auto x = fl(args.require_float("x")); | |
| 34 | ✗ | const auto y = fl(args.require_float("y")); | |
| 35 | ✗ | const auto z = fl(args.require_float("z")); | |
| 36 | ✗ | if (args.complete()) | |
| 37 | { | ||
| 38 | ✗ | return v3{ 0,0,0 }; | |
| 39 | } | ||
| 40 | ✗ | return v3{ x, y, z }; | |
| 41 | ✗ | }); | |
| 42 | ✗ | k.add_property<lax::Tf>("x", | |
| 43 | ✗ | [](const v3& v) -> lax::Tf{ return lf(v.x); }, | |
| 44 | ✗ | [](auto& v, lax::Tf value) { v.x = fl(value); } | |
| 45 | ); | ||
| 46 | ✗ | k.add_property<lax::Tf>("y", | |
| 47 | ✗ | [](const v3& v) -> lax::Tf{ return lf(v.y); }, | |
| 48 | ✗ | [](auto& v, lax::Tf value) { v.y = fl(value); } | |
| 49 | ); | ||
| 50 | ✗ | k.add_property<lax::Tf>("z", | |
| 51 | ✗ | [](const v3& v) -> lax::Tf{ return lf(v.z); }, | |
| 52 | ✗ | [](auto& v, lax::Tf value) { v.z = fl(value); } | |
| 53 | ); | ||
| 54 | ✗ | bind_binary(&k, lax, lax::named::functions::add, [](const v3& lhs, const v3& rhs) { return lhs + rhs; }); | |
| 55 | ✗ | bind_binary(&k, lax, lax::named::functions::sub, [](const v3& lhs, const v3& rhs) { return lhs - rhs; }); | |
| 56 | |||
| 57 | ✗ | pkg->add_native_getter("up", [lax] {return lax->make_native(kk::up);}); | |
| 58 | ✗ | pkg->add_native_getter("down", [lax] {return lax->make_native(kk::down); }); | |
| 59 | ✗ | pkg->add_native_getter("left", [lax] {return lax->make_native(kk::left); }); | |
| 60 | ✗ | pkg->add_native_getter("right", [lax] {return lax->make_native(kk::right); }); | |
| 61 | ✗ | pkg->add_native_getter("in", [lax] {return lax->make_native(kk::in); }); | |
| 62 | ✗ | pkg->add_native_getter("out", [lax] {return lax->make_native(kk::out); }); | |
| 63 | ✗ | } | |
| 64 | |||
| 65 | ✗ | void register_base(Script* script) | |
| 66 | { | ||
| 67 | ✗ | auto pkg = script->lax.in_package("eu"); | |
| 68 | ✗ | register_vec3(&script->lax, pkg.get()); | |
| 69 | ✗ | } | |
| 70 | |||
| 71 | } | ||
| 72 |