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 | a142eb1e1ee81919d10b55bb7fa2e33636098d85 (patch) | |
| tree | 21ef5c830ce4b4e845454af4274beabd073cc720 /docs | |
| parent | 91155c74fc10fa98ad6257d5309bfd13d4a61cf0 (diff) | |
| download | lanes-a142eb1e1ee81919d10b55bb7fa2e33636098d85.tar.gz lanes-a142eb1e1ee81919d10b55bb7fa2e33636098d85.tar.bz2 lanes-a142eb1e1ee81919d10b55bb7fa2e33636098d85.zip | |
__lanesclone mechanism should actually work now
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/index.html | 65 |
1 files changed, 59 insertions, 6 deletions
diff --git a/docs/index.html b/docs/index.html index aa12a36..9c76bef 100644 --- a/docs/index.html +++ b/docs/index.html | |||
| @@ -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 | ||
