Jump to content




Messing With Metatables


  • You cannot reply to this topic
8 replies to this topic

#1 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 28 December 2015 - 02:47 PM

Spoiler

For some reason, calling Object.class() after declaring Window returns "Window" - obviously I don't want that to happen. In addition, calling Window.class() is doing something weird, I get java.lang.arrayIndexOutofBoundsException - it's got to be going in circles somehow.

#2 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 28 December 2015 - 02:53 PM

At a first glance, you don't declare "this" as a table anywhere.

That is the only thing I can find.

Edited by Creator, 28 December 2015 - 02:51 PM.


#3 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 28 December 2015 - 02:56 PM

 __index = function( t, k )
    return t[ k ] or getmetatable( t ).__super[ k ]
end,

This will result in an infinite loop as you are calling t[k] which will call __index.

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 28 December 2015 - 03:09 PM

Good catch SquidDev.

	__index = function( t, k )
		return rawget( t, k ) or getmetatable( t ).__super[ k ]
	end,

That eliminates the ArrayIndexOutOfBounds problem, but the Object class is still becoming "Window".

Oh, and I get an error on line 6:
	mt.inherits[ #mt.inherits + 1 ] = object.class()

Which indicates object.class is nil (specifically, attempt to call nil).

@Creator: I do declare this. Env fun:

	__newindex = function( t, k, v )
		if type( v ) == "function" then
			local env = getfenv( v )
			env.this, env.super = t, getmetatable( t ).__super
		end
		rawset( t, k, v )
	end,


Edited by KingofGamesYami, 28 December 2015 - 03:19 PM.


#5 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 28 December 2015 - 03:42 PM

How did I oversee this? I looked at the code for like 5 minutes.

#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 28 December 2015 - 04:56 PM

I fixed Object.class() returning "Window", but now Window.class() returns "Object".

Spoiler


#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 28 December 2015 - 05:10 PM

Oh goody, it's a problem with function environments. Now to handle this in the most hackish way possible...


Edit: I now broke everything by trying to move the environment stuff into __index. It really should work though... :/

Edit #2: I think I fixed it, but now Object thinks it extends it's own subclasses. *Screams*

Spoiler

Edit #3: code is now here: http://pastebin.com/reEf4fVk

How the heck does this work????
print( Object.extends( "Window" ) )
-->false
print( Window.class() )
-->Window
print( Object.extends( Window.class() ) )
-->true


Edit #4: Everything has been resolved now. I'd post the solutions, but I fixed so many problems that my fixes had to be fixed.

Edited by KingofGamesYami, 29 December 2015 - 02:03 AM.


#8 Exerro

  • Members
  • 801 posts

Posted 29 December 2015 - 10:56 PM

Just so you know, __index is only invoked if you try to access a key in a table that doesn't exist, so...

function mt.__index( t, k )
    return rawget( t, k ) or x -- x will always be returned
end


#9 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 29 December 2015 - 11:02 PM

Yeah, I know. That's why there are no keys in my table :)

Edit: It might be worth mentioning everything other than the pastebin is before I fixed everything. If you were looking at the code in the OP, that's understandable.

Edited by KingofGamesYami, 29 December 2015 - 11:04 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users