Skip to content

Instantly share code, notes, and snippets.

@sasq64
Last active February 14, 2016 20:49
Show Gist options
  • Save sasq64/805d21ff6eb86cce1368 to your computer and use it in GitHub Desktop.
Save sasq64/805d21ff6eb86cce1368 to your computer and use it in GitHub Desktop.
#include <coreutils/utils.h>
#include <coreutils/log.h>
#include "v8/v8.h"
#include <string>
using namespace std;
struct vec3 {
float x;
float y;
float z;
string toString() { return utils::format("[%f %f %f]", x, y, z); }
};
struct Obj3D {
vec3 *position = new vec3();
vec3 *rotation = new vec3();
string name;
double alpha;
};
int main() {
V8Interpreter v8;
v8.registerFunction("print", [](string l) {
logging::log(l);
});
v8.registerFunction("testme", [&](double a, string x) {
LOGD("Got %s/%f", x,a);
});
v8.registerClass<vec3>()
.field("x", &vec3::x)
.field("y", &vec3::y)
.field("z", &vec3::z)
.method("toString", &vec3::toString)
;
v8.registerClass<Obj3D>()
.field("position", &Obj3D::position)
.field("rotation", &Obj3D::rotation)
.field("name", &Obj3D::name)
.field("alpha", &Obj3D::alpha)
;
v8.registerFunction("double", [](vec3 v) -> vec3 {
v.x *= 2;
v.y *= 2;
v.z *= 2;
LOGD("Doubled to %s", v.toString());
return v;
});
v8.registerFunction("makeObj", [&](string name, vec3 pos) -> Obj3D* {
auto *obj = new Obj3D();
LOGD("pos %s", pos.toString());
*obj->position = pos;
obj->name = name;
*obj->rotation = { 0, 0, 0 };
obj->alpha = 1.0;
return obj;
});
v8.start();
v8.exec(R"(
testme(3.14, 'hello');
v = double( {x:3, y:4, z:5} );
print(v.toString());
vv = double(v);
print("RESULT: " + vv.y);
o = makeObj("cube", { x: 100, y: 100, z: 100 });
print("CUBE: " + o.alpha);
o.rotation.y = 180;
testme(0, '');
)");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment