MCPcopy Create free account
hub / github.com/MonaSolutions/MonaServer / CreateObject

Function CreateObject

MonaServer/sources/Script.h:535–593  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

533
534 template<typename LUAType,typename Type>
535 static void CreateObject(lua_State *pState, const Type& object) {
536
537 // Create table to represent our object
538 lua_newtable(pState);
539
540 // metatable
541 lua_newtable(pState);
542
543 // |this
544 lua_pushlightuserdata(pState,(void*)&object);
545 lua_setfield(pState,-2,"|this");
546
547 // |type
548 lua_pushlightuserdata(pState,(void*)&typeid(Type));
549 lua_setfield(pState,-2,"|type");
550
551 // get
552 lua_pushcfunction(pState,&LUAType::Get);
553 lua_setfield(pState,-2,"__index");
554
555 // toString
556 lua_pushcfunction(pState, &Script::ToString<LUAType>);
557 lua_setfield(pState,-2,"__tostring");
558
559 // set
560 lua_pushcfunction(pState,&LUAType::Set);
561 lua_setfield(pState,-2,"__newindex");
562
563 // hide metatable
564#if !defined(_DEBUG)
565 lua_pushliteral(pState,"change metatable of this object is prohibited");
566 lua_setfield(pState,-2,"__metatable");
567#endif
568
569 lua_setmetatable(pState, -2);
570
571 // record the pointer
572 lua_getfield(pState,LUA_REGISTRYINDEX,"|pointers");
573 if(lua_isnil(pState,-1)) {
574 lua_pop(pState,1);
575 lua_newtable(pState); // table
576 lua_newtable(pState); // metatable
577
578 lua_pushliteral(pState,"v");
579 lua_setfield(pState,-2,"__mode");
580
581 lua_setmetatable(pState,-2); // remove metatable
582
583 lua_pushvalue(pState,-1); // table
584 lua_setfield(pState,LUA_REGISTRYINDEX,"|pointers");
585 }
586
587 lua_pushlightuserdata(pState, (void*)&object);
588 lua_pushvalue(pState, -3);
589 lua_settable(pState, -3);
590 lua_pop(pState,1); // remove |pointers
591
592 LUAType::Init(pState, (Type&)object);

Callers

nothing calls this directly

Calls 1

InitFunction · 0.85

Tested by

no test coverage detected