Rapptz / sol

A C++11 Lua wrapper
MIT License
209 stars 32 forks source link

Nullptr when passing one userdata to another #46

Closed notlion closed 10 years ago

notlion commented 10 years ago

Userdata is becoming null for some reason when passed to a method of another userdata class. When used by the same class there's no problem. Simple test case:

struct Vec2 {
  float x = 0;
  float y = 0;
  Vec2(float x_, float y_) : x{x_}, y{y_} {}
};

struct Incr {
  Vec2 incr(const Vec2& v) {
    return Vec2(v.x + 1, v.y + 1); // Eek! v is nullptr
  }
};

lua.new_userdata<Vec2, float, float>("Vec2");
lua.new_userdata<Incr>("Incr", "incr", &Incr::incr);

lua.script("a = Vec2.new(10, 20)\n"
           "i = Incr.new()\n"
           "b = i:incr(v)\n"
           "print(b.x, b.y)");
Rapptz commented 10 years ago

In your test case, v doesn't exist. Use a instead.

notlion commented 10 years ago

Well I feel like an idiot. You're right. That test case actually works when the proper variable name is used.

I did track down the actual bug, though. I had typed b = i.incr(a) instead of b = i:incr(a). With the dot notation incr() is actually called, but is passed a reference to a null value.

I'm not sure if you'd consider this a bug or not, but it was confusing. Sorry for the false alarm.

Rapptz commented 10 years ago

No problem. Things happen.