diff options
author | Benoit Germain <b n t DOT g e r m a i n AT g m a i l DOT c o m> | 2018-11-07 19:16:36 +0100 |
---|---|---|
committer | Benoit Germain <b n t DOT g e r m a i n AT g m a i l DOT c o m> | 2018-11-07 19:16:36 +0100 |
commit | 7781eca76710efe3a5269083150956bb57e0e568 (patch) | |
tree | 327e4e6c53b9dd8c26142c066ddb2eef12f849de | |
parent | e7983b7ad9e23e3a33e169653761d12a203febed (diff) | |
download | lanes-7781eca76710efe3a5269083150956bb57e0e568.tar.gz lanes-7781eca76710efe3a5269083150956bb57e0e568.tar.bz2 lanes-7781eca76710efe3a5269083150956bb57e0e568.zip |
__lanesclone mechanism should actually work now
-rw-r--r-- | index.html | 65 |
1 files changed, 59 insertions, 6 deletions
@@ -70,7 +70,7 @@ | |||
70 | </p> | 70 | </p> |
71 | 71 | ||
72 | <p> | 72 | <p> |
73 | This document was revised on 4-Nov-18, and applies to version <tt>3.13</tt>. | 73 | This document was revised on 7-Nov-18, and applies to version <tt>3.13</tt>. |
74 | </p> | 74 | </p> |
75 | </font> | 75 | </font> |
76 | </center> | 76 | </center> |
@@ -1516,15 +1516,68 @@ events to a common Linda, but... :).</font> | |||
1516 | <table border="1" bgcolor="#FFFFE0" cellpadding="10" style="width:50%"><tr><td><pre> | 1516 | <table border="1" bgcolor="#FFFFE0" cellpadding="10" style="width:50%"><tr><td><pre> |
1517 | static int clonable_lanesclone( lua_State* L) | 1517 | static int clonable_lanesclone( lua_State* L) |
1518 | { | 1518 | { |
1519 | // no need to set the metatable, the Lane copying mechanism will take care of it | 1519 | switch( lua_gettop( L)) |
1520 | struct s_MyClonableUserdata* self = lua_touserdata( L, 1); | 1520 | { |
1521 | struct s_MyClonableUserdata* to = lua_newuserdata( L, sizeof( struct s_MyClonableUserdata)); | 1521 | case 0: |
1522 | memcpy( to, self, sizeof(struct s_MyClonableUserdata)); | 1522 | lua_pushinteger( L, sizeof( struct s_MyClonableUserdata)); |
1523 | return 1; | ||
1524 | |||
1525 | case 2: | ||
1526 | { | ||
1527 | struct s_MyClonableUserdata* self = lua_touserdata( L, 1); | ||
1528 | struct s_MyClonableUserdata* from = lua_touserdata( L, 2); | ||
1529 | *self = *from; | ||
1530 | return 0; | ||
1531 | } | ||
1532 | |||
1533 | default: | ||
1534 | (void) luaL_error( L, "Lanes called clonable_lanesclone with unexpected parameters"); | ||
1535 | } | ||
1536 | return 0; | ||
1537 | } | ||
1538 | </pre></td></tr></table> | ||
1539 | </p> | ||
1540 | |||
1541 | <p> | ||
1542 | Of course, more complex objects may require smarter cloning behavior than a simple <tt>memcpy</tt>. Also, the module initialisation code should make each metatable accessible from the module table itself as in: | ||
1543 | <table border="1" bgcolor="#FFFFE0" cellpadding="10" style="width:50%"><tr><td><pre> | ||
1544 | int luaopen_deep_test(lua_State* L) | ||
1545 | { | ||
1546 | luaL_newlib( L, deep_module); | ||
1547 | |||
1548 | // preregister the metatables for the types we can instanciate so that Lanes can know about them | ||
1549 | if( luaL_newmetatable( L, "clonable")) | ||
1550 | { | ||
1551 | luaL_setfuncs( L, clonable_mt, 0); | ||
1552 | lua_pushvalue(L, -1); | ||
1553 | lua_setfield(L, -2, "__index"); | ||
1554 | } | ||
1555 | lua_setfield(L, -2, "__clonableMT"); // actual name is not important | ||
1556 | |||
1557 | if( luaL_newmetatable( L, "deep")) | ||
1558 | { | ||
1559 | luaL_setfuncs( L, deep_mt, 0); | ||
1560 | lua_pushvalue(L, -1); | ||
1561 | lua_setfield(L, -2, "__index"); | ||
1562 | } | ||
1563 | lua_setfield(L, -2, "__deepMT"); // actual name is not important | ||
1564 | |||
1565 | return 1; | ||
1566 | } | ||
1567 | </pre></td></tr></table> | ||
1568 | </p> | ||
1569 | |||
1570 | <p> | ||
1571 | Then a new clonable userdata instance can just do like any non-Lanes aware userdata, as long as its metatable contains the aforementionned <tt>__lanesclone</tt> method. Note that the current implementation doesn't support uservalues on such userdata. | ||
1572 | <table border="1" bgcolor="#FFFFE0" cellpadding="10" style="width:50%"><tr><td><pre> | ||
1573 | int luaD_new_clonable( lua_State* L) | ||
1574 | { | ||
1575 | lua_newuserdata( L, sizeof( struct s_MyClonableUserdata)); | ||
1576 | luaL_setmetatable( L, "clonable"); | ||
1523 | return 1; | 1577 | return 1; |
1524 | } | 1578 | } |
1525 | </pre></td></tr></table> | 1579 | </pre></td></tr></table> |
1526 | </p> | 1580 | </p> |
1527 | Of course, more complex objects may require smarter cloning behavior than a simple <tt>memcpy</tt>. | ||
1528 | 1581 | ||
1529 | <h3 id="deep_userdata">Deep userdata in your own apps</h3> | 1582 | <h3 id="deep_userdata">Deep userdata in your own apps</h3> |
1530 | 1583 | ||