diff options
author | David Walter Seikel | 2014-03-31 22:00:53 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-03-31 22:00:53 +1000 |
commit | f2c5263901f7ca287b8b322ae8ecaf0a5b24ff72 (patch) | |
tree | cc32f9084fd80e130efd5c60e150d65bf17379b3 | |
parent | Notes++ (diff) | |
download | SledjHamr-f2c5263901f7ca287b8b322ae8ecaf0a5b24ff72.zip SledjHamr-f2c5263901f7ca287b8b322ae8ecaf0a5b24ff72.tar.gz SledjHamr-f2c5263901f7ca287b8b322ae8ecaf0a5b24ff72.tar.bz2 SledjHamr-f2c5263901f7ca287b8b322ae8ecaf0a5b24ff72.tar.xz |
More ways to call thing, and rename it thingasm.
Finally, a use for __call(). lol
-rw-r--r-- | ClientHamr/GuiLua/skang.lua | 128 | ||||
-rw-r--r-- | ClientHamr/GuiLua/test.lua | 23 | ||||
-rw-r--r-- | ClientHamr/GuiLua/test_c.c | 14 |
3 files changed, 95 insertions, 70 deletions
diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index ed1589f..9e3dfd8 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua | |||
@@ -311,18 +311,6 @@ Other Thing things are - | |||
311 | NOTE that skang.thing{} doesn't care what other names you pass in, they all get assigned to the thing. | 311 | NOTE that skang.thing{} doesn't care what other names you pass in, they all get assigned to the thing. |
312 | 312 | ||
313 | 313 | ||
314 | Maybe I can rename this to thingasm? B-) | ||
315 | Then widget becomes skang? | ||
316 | local thingasm = require 'thingasm' | ||
317 | thingasm('foo', 'help text) -- In this case the surrounding Lua environment becomes the module of foo. | ||
318 | -- If the first argument (or first in the table) is a string, then it's this form. | ||
319 | -- All others include the module as the first argument, which would be a table. | ||
320 | foo('bar', 'some help', types='table') -- ___call(foo, 'bar', ...) And now foo is the module. | ||
321 | foo.bar{'baz', types='Stuff'} -- thingasm({foo.bar, 'baz', ...}) | ||
322 | foo.bar.baz{'field0'} -- thingasm({foo.bar.baz, 'field0'}) | ||
323 | foo.bar.baz{'field1'} | ||
324 | |||
325 | |||
326 | Widget - | 314 | Widget - |
327 | Merging widgets might work to. B-) | 315 | Merging widgets might work to. B-) |
328 | This does make the entire "Things with the same name link automatically" deal work easily, since they ARE the same Thing. | 316 | This does make the entire "Things with the same name link automatically" deal work easily, since they ARE the same Thing. |
@@ -370,17 +358,12 @@ Maybe I can rename this to thingasm? B-) | |||
370 | If module.foo is a table, it can have it's own metatable(foo). | 358 | If module.foo is a table, it can have it's own metatable(foo). |
371 | Such a table will use Thing as a proxy table via __index and __newindex, as it does now. | 359 | Such a table will use Thing as a proxy table via __index and __newindex, as it does now. |
372 | 360 | ||
373 | stuff:stuff('s') -> getmetatable(stuff).__stuff['s'] | ||
374 | |||
375 | test.foo -> test.__index(test, 'foo') -> test.__values[foo]; if that's nil, and test.__stuff[foo], then return an empty table instead? | 361 | test.foo -> test.__index(test, 'foo') -> test.__values[foo]; if that's nil, and test.__stuff[foo], then return an empty table instead? |
376 | 362 | ||
377 | test.foo(a) -> test.__index(test, 'foo') -> test.__values[foo](a) (but it's not a function) -> test.__values[foo].__call(test.__values[foo], a) | ||
378 | Doesn't seem useful. | ||
379 | If test.foo is a Thing then that might be -> test.foo.__call(test.foo, a) -> could do something useful with this. | ||
380 | So far I have entirely failed to find a good use, since they all are catered for anyway. lol | ||
381 | |||
382 | stuff.s = {a='foo'} -> changes a, deletes everything else, or should. | 363 | stuff.s = {a='foo'} -> changes a, deletes everything else, or should. |
383 | 364 | ||
365 | stuff:stuff('s') -> getmetatable(stuff).__stuff['s'] | ||
366 | |||
384 | stuff.S[key]={...} -> stuff is a Thing, stuff.S is a Thing, stuff.S[key] NOT a Thing. | 367 | stuff.S[key]={...} -> stuff is a Thing, stuff.S is a Thing, stuff.S[key] NOT a Thing. |
385 | Unless set up before hand, which it is not since [key] is arbitrary. | 368 | Unless set up before hand, which it is not since [key] is arbitrary. |
386 | We should be re using the same Thing for each [key], but change the Thing.module. | 369 | We should be re using the same Thing for each [key], but change the Thing.module. |
@@ -405,11 +388,6 @@ Maybe I can rename this to thingasm? B-) | |||
405 | stuff:read('someIndex') -> Grabs a single row that has the key 'someIndex', or perhaps multiple rows since this might have SQL under it. | 388 | stuff:read('someIndex') -> Grabs a single row that has the key 'someIndex', or perhaps multiple rows since this might have SQL under it. |
406 | stuff = db:read('stuff', 'select * from someTable where key='someIndex') | 389 | stuff = db:read('stuff', 'select * from someTable where key='someIndex') |
407 | 390 | ||
408 | -- stuff:stuff('') -> getmetatable(stuff).__stuff[''] | ||
409 | -- stuff:stuff() -> getmetatable(stuff).__stuff[''] | ||
410 | |||
411 | -- stuff:stuff('field1') -> stuff:stuff().__stuff['field1'] | ||
412 | |||
413 | stuff:write() -> Write all rows to the database table. | 391 | stuff:write() -> Write all rows to the database table. |
414 | stuff:write(1) -> Write one row to the database table. | 392 | stuff:write(1) -> Write one row to the database table. |
415 | stuff:stuff('field1').isValid = someFunction -- Should work, all stuff[key] shares the same stuff. | 393 | stuff:stuff('field1').isValid = someFunction -- Should work, all stuff[key] shares the same stuff. |
@@ -459,7 +437,6 @@ public class PersonStuff extends SquealStuff | |||
459 | }; | 437 | }; |
460 | } | 438 | } |
461 | 439 | ||
462 | |||
463 | ]] | 440 | ]] |
464 | 441 | ||
465 | -- Default things values. | 442 | -- Default things values. |
@@ -607,27 +584,75 @@ Thing.__newindex = function (module, key, value) | |||
607 | rawset(module, key, value) -- Stuff it normally. | 584 | rawset(module, key, value) -- Stuff it normally. |
608 | end | 585 | end |
609 | 586 | ||
610 | -- TODO - Seemed like a good idea at the time, but do we really need it? | 587 | thingasm = function () |
611 | --Thing.__call = function (func, ...) | 588 | end |
612 | -- return func.func(...) | 589 | |
613 | -- end | 590 | Thing.__call = function (func, ...) |
591 | return thingasm(func, ...) -- (func, {...}) | ||
592 | end | ||
593 | |||
614 | 594 | ||
595 | -- skang.thingasm() Creates a new Thing, or changes an existing one. | ||
596 | --[[ It can be called in many different ways - | ||
597 | |||
598 | It can be called with positional arguments - (names, help, default, types, widget, required, acl, boss) | ||
599 | Or it can be called with a table - {names, help, pattern='...', acl='rwx'} | ||
600 | |||
601 | The first argument can be another Thing (the parent), or a string list of names (see below). | ||
602 | |||
603 | It can be called by itself, with no parent specified - | ||
604 | thingasm('foo', 'help text) | ||
605 | |||
606 | In this case the surrounding Lua environment becomes the module of foo. | ||
607 | If the first argument (or first in the table) is a string, then it's this form. | ||
608 | All others include the module as the first argument, which would be a table. | ||
609 | |||
610 | It can be called by calling the parent as a function - | ||
611 | foo('bar', 'some help', types='table') -- ___call(foo, 'bar', ...) And now foo is the module. | ||
612 | foo.bar{'baz', types='Stuff'} -- thingasm({foo.bar, 'baz', ...}) | ||
613 | foo.bar.baz{'field0'} -- thingasm({foo.bar.baz, 'field0'}) | ||
614 | foo.bar.baz{'field1'} | ||
615 | ]] | ||
615 | 616 | ||
616 | -- skang.thing() Creates a new Thing, or changes an existing one. | ||
617 | -- It can be called with positional arguments - (names, help, default, types, widget, required, acl, boss) | ||
618 | -- Or it can be called with a table - {names, help, pattern='...', acl='rwx'} | ||
619 | -- names - a comma seperated list of names, aliases, and shortcuts. The first one is the official name. | 617 | -- names - a comma seperated list of names, aliases, and shortcuts. The first one is the official name. |
620 | -- If this is not a new thing, then only the first one is used to look it up. | 618 | -- If this is not a new thing, then only the first one is used to look it up. |
621 | -- So to change names, use skang.thing{'oldName', names='newName,otherNewName'} | 619 | -- So to change names, use skang.thingasm{'oldName', names='newName,otherNewName'} |
622 | thing = function (names, ...) | 620 | thingasm = function (names, ...) |
623 | local params = {...} | 621 | local params = {...} |
624 | local new = false | 622 | local new = false |
625 | 623 | local module | |
626 | -- Check if it was called as a table, and pull the names out of the table. | 624 | |
627 | if 'table' == type(names) then | 625 | -- Check how we were called, and re arrange stuff to match. |
628 | params = names | 626 | if 0 == #params then |
629 | names = params[1] | 627 | if ('table' == type(names)) then |
630 | table.remove(params, 1) | 628 | -- thingasm{...} |
629 | params = names | ||
630 | names = params[1] | ||
631 | table.remove(params, 1) | ||
632 | if 'table' == type(names) then | ||
633 | -- thingasm{module, 'foo', ...} | ||
634 | module = names | ||
635 | names = params[1] | ||
636 | table.remove(params, 1) | ||
637 | end | ||
638 | else | ||
639 | -- thingasm("foo") | ||
640 | end | ||
641 | else | ||
642 | if 'table' == type(names) then | ||
643 | module = names | ||
644 | if 'string' == type(...) then | ||
645 | -- C or __call(table, string, ..) | ||
646 | params = {...} | ||
647 | elseif 'table' == type(...) then | ||
648 | -- __call(table, table) | ||
649 | params = ... | ||
650 | end | ||
651 | names = params[1] | ||
652 | table.remove(params, 1) | ||
653 | else | ||
654 | -- thingasm('foo', ...) | ||
655 | end | ||
631 | end | 656 | end |
632 | 657 | ||
633 | -- Break out the names. | 658 | -- Break out the names. |
@@ -638,7 +663,7 @@ thing = function (names, ...) | |||
638 | -- TODO - Double check this comment - No need to bitch and return if no names, this will crash for us. | 663 | -- TODO - Double check this comment - No need to bitch and return if no names, this will crash for us. |
639 | 664 | ||
640 | -- Grab the environment of the calling function if no module was passed in. | 665 | -- Grab the environment of the calling function if no module was passed in. |
641 | local module = (params.module or params[8]) or getfenv(2) | 666 | module = module or getfenv(2) |
642 | local modThing = getmetatable(module) | 667 | local modThing = getmetatable(module) |
643 | if nil == modThing then | 668 | if nil == modThing then |
644 | modThing = {} | 669 | modThing = {} |
@@ -697,8 +722,9 @@ thing = function (names, ...) | |||
697 | if '' == types then types = typ end | 722 | if '' == types then types = typ end |
698 | thingy.types = csv2table(types) | 723 | thingy.types = csv2table(types) |
699 | 724 | ||
700 | if 'table' == thingy.types[1] then | 725 | if ('Stuff' == thingy.types[1]) or ('table' == thingy.types[1]) then |
701 | if '' == thingy.default then thingy.default = {} end | 726 | if '' == thingy.default then thingy.default = {} end |
727 | setmetatable(thingy.default, {__call = Thing.__call}) | ||
702 | end | 728 | end |
703 | 729 | ||
704 | -- Remove old names, then stash the Thing under all of it's new names. | 730 | -- Remove old names, then stash the Thing under all of it's new names. |
@@ -792,9 +818,9 @@ set = function (stuff, key, name, value) | |||
792 | end | 818 | end |
793 | end | 819 | end |
794 | 820 | ||
795 | thing('get', 'Get the current value of an existing Thing or metadata.', get, 'thing,key,name') | 821 | thingasm('get', 'Get the current value of an existing Thing or metadata.', get, 'thing,key,name') |
796 | thing('reset', 'Reset the current value of an existing Thing or metadata.', reset, 'thing,key,name') | 822 | thingasm('reset', 'Reset the current value of an existing Thing or metadata.', reset, 'thing,key,name') |
797 | thing('set', 'Set the current value of an existing Thing or metadata.', set, 'thing,key,name,data') | 823 | thingasm('set', 'Set the current value of an existing Thing or metadata.', set, 'thing,key,name,data') |
798 | 824 | ||
799 | 825 | ||
800 | -- TODO - Some function stubs, for now. Fill them up later. | 826 | -- TODO - Some function stubs, for now. Fill them up later. |
@@ -812,12 +838,12 @@ end | |||
812 | quit = function () | 838 | quit = function () |
813 | end | 839 | end |
814 | 840 | ||
815 | thing('nada', 'Do nothing.', nada) | 841 | thingasm('nada', 'Do nothing.', nada) |
816 | thing('clear', 'The current skin is cleared of all widgets.', clear) | 842 | thingasm('clear', 'The current skin is cleared of all widgets.', clear) |
817 | thing('window', 'The size and title of the application Frame.', window, 'x,y,name', nil, nil, 'GGG') | 843 | thingasm('window', 'The size and title of the application Frame.', window, 'x,y,name', nil, nil, 'GGG') |
818 | thing('module', 'Load a module.', module, 'file,acl') | 844 | thingasm('module', 'Load a module.', module, 'file,acl') |
819 | thing('skang', 'Parse the contents of a skang file or URL.', skang, 'URL') | 845 | thingasm('skang', 'Parse the contents of a skang file or URL.', skang, 'URL') |
820 | thing('quit', 'Quit, exit, remove thyself.', quit) | 846 | thingasm('quit', 'Quit, exit, remove thyself.', quit) |
821 | 847 | ||
822 | 848 | ||
823 | moduleEnd(_M) | 849 | moduleEnd(_M) |
diff --git a/ClientHamr/GuiLua/test.lua b/ClientHamr/GuiLua/test.lua index 9f98520..aebe4ae 100644 --- a/ClientHamr/GuiLua/test.lua +++ b/ClientHamr/GuiLua/test.lua | |||
@@ -31,12 +31,12 @@ local fool | |||
31 | 31 | ||
32 | -- TODO - Could have a table of tables, and ipair through the top level, passing the inner ones to skang.thing{}. | 32 | -- TODO - Could have a table of tables, and ipair through the top level, passing the inner ones to skang.thing{}. |
33 | 33 | ||
34 | skang.thing('fooble,f', 'Help text goes here', 1, nil, '"edit", "The fooble:", 1, 1, 10, 50', true) | 34 | skang.thingasm('fooble,f', 'Help text goes here', 1, nil, '"edit", "The fooble:", 1, 1, 10, 50', true) |
35 | skang.thing('bar', 'Help text', "Default") | 35 | skang.thingasm('bar', 'Help text', "Default") |
36 | skang.thing('foo') | 36 | skang.thingasm('foo') |
37 | 37 | ||
38 | -- We can use inline functions if we don't need the function internally. | 38 | -- We can use inline functions if we don't need the function internally. |
39 | skang.thing('ffunc', 'Help Text', function (arg1, arg2) | 39 | skang.thingasm('ffunc', 'Help Text', function (arg1, arg2) |
40 | print('Inside test.ffunc(' .. arg1 .. ', ' .. arg2 .. ')') | 40 | print('Inside test.ffunc(' .. arg1 .. ', ' .. arg2 .. ')') |
41 | end, 'number,string') | 41 | end, 'number,string') |
42 | 42 | ||
@@ -100,7 +100,6 @@ test.fooble = 42 | |||
100 | test.fooble = 'Should fail.' | 100 | test.fooble = 'Should fail.' |
101 | test.fooble = 42 | 101 | test.fooble = 42 |
102 | test.fooble = nil | 102 | test.fooble = nil |
103 | test.fooble = nil | ||
104 | test.fooble = 42 | 103 | test.fooble = 42 |
105 | test.fooble = true | 104 | test.fooble = true |
106 | test.f = 42 | 105 | test.f = 42 |
@@ -121,12 +120,12 @@ print('') | |||
121 | local stuff = {} | 120 | local stuff = {} |
122 | stuff.t = {} | 121 | stuff.t = {} |
123 | 122 | ||
124 | skang.thing{'a', module=stuff, help = 'A test stufflet'} | 123 | skang.thingasm{stuff, 'a', help = 'A test stufflet'} |
125 | skang.thing{'b', module=stuff.t, help = 'A sub stufflet'} | 124 | skang.thingasm{stuff.t, 'b', help = 'A sub stufflet'} |
126 | skang.thing{'c', module=stuff.t, help = 'Another sub stufflet'} | 125 | skang.thingasm{stuff.t, 'c', help = 'Another sub stufflet'} |
127 | skang.thing{'s', module=stuff, help = 'A Stuff', types='table'} | 126 | skang.thingasm{stuff, 's', help = 'A Stuff', types='table'} |
128 | skang.thing{'sa,a', module=stuff.s, help = 'A stufflet in a Stuff'} | 127 | stuff.s{'sa,a', help = 'A stufflet in a Stuff'} |
129 | skang.thing{'sb,b', module=stuff.s, help = 'Another stufflet in a Stuff'} | 128 | stuff.s{'sb,b', help = 'Another stufflet in a Stuff'} |
130 | 129 | ||
131 | print('*********************************') | 130 | print('*********************************') |
132 | skang.fixNames(skang, 'skang') | 131 | skang.fixNames(skang, 'skang') |
@@ -142,7 +141,7 @@ print(skang.get(stuff.t, 'c', 'help')) | |||
142 | print(skang.get(stuff, 's', 'help')) | 141 | print(skang.get(stuff, 's', 'help')) |
143 | print(skang.get(stuff.s, 'sa', 'help')) | 142 | print(skang.get(stuff.s, 'sa', 'help')) |
144 | print(skang.get(stuff.s, 'sb', 'help')) | 143 | print(skang.get(stuff.s, 'sb', 'help')) |
145 | skang.thing{'baz,b', module=test, help = 'A test stufflet for test'} | 144 | skang.thingasm{test, 'baz,b', help = 'A test stufflet for test'} |
146 | print(skang.get(test, 'b', 'help')) | 145 | print(skang.get(test, 'b', 'help')) |
147 | print(skang.get(test, 'f', 'help')) | 146 | print(skang.get(test, 'f', 'help')) |
148 | --skang.printTableStart(getmetatable(stuff.s), '', 'stuff.s metatable') | 147 | --skang.printTableStart(getmetatable(stuff.s), '', 'stuff.s metatable') |
diff --git a/ClientHamr/GuiLua/test_c.c b/ClientHamr/GuiLua/test_c.c index 47cb72d..10af91c 100644 --- a/ClientHamr/GuiLua/test_c.c +++ b/ClientHamr/GuiLua/test_c.c | |||
@@ -120,7 +120,8 @@ int luaopen_test_c(lua_State *L) | |||
120 | // http://www.lua.org/pil/25.3.html seems the most reasonable of the examples I've found. | 120 | // http://www.lua.org/pil/25.3.html seems the most reasonable of the examples I've found. |
121 | 121 | ||
122 | // skang.thing('cfooble,c', 'Help text goes here', 1, 'number', \"'edit', 'The fooble:', 1, 1, 10, 50\", true) | 122 | // skang.thing('cfooble,c', 'Help text goes here', 1, 'number', \"'edit', 'The fooble:', 1, 1, 10, 50\", true) |
123 | lua_getfield(L, skang, "thing"); | 123 | lua_getfield(L, skang, "thingasm"); |
124 | lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. | ||
124 | lua_pushstring(L, "cfooble,c"); | 125 | lua_pushstring(L, "cfooble,c"); |
125 | lua_pushstring(L, "Help text"); | 126 | lua_pushstring(L, "Help text"); |
126 | lua_pushnumber(L, 1); | 127 | lua_pushnumber(L, 1); |
@@ -129,11 +130,11 @@ int luaopen_test_c(lua_State *L) | |||
129 | lua_pushboolean(L, 1); // Is required. | 130 | lua_pushboolean(L, 1); // Is required. |
130 | lua_pushnil(L); // Default ACL. | 131 | lua_pushnil(L); // Default ACL. |
131 | lua_pushnil(L); // No boss. | 132 | lua_pushnil(L); // No boss. |
132 | lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. | ||
133 | lua_call(L, 9, 0); | 133 | lua_call(L, 9, 0); |
134 | 134 | ||
135 | // skang.thing('cbar', 'Help text', 'Default') | 135 | // skang.thing('cbar', 'Help text', 'Default') |
136 | lua_getfield(L, skang, "thing"); | 136 | lua_getfield(L, skang, "thingasm"); |
137 | lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. | ||
137 | lua_pushstring(L, "cbar"); | 138 | lua_pushstring(L, "cbar"); |
138 | lua_pushstring(L, "Help text"); | 139 | lua_pushstring(L, "Help text"); |
139 | lua_pushstring(L, "Default"); | 140 | lua_pushstring(L, "Default"); |
@@ -142,11 +143,10 @@ int luaopen_test_c(lua_State *L) | |||
142 | lua_pushnil(L); // Not required. | 143 | lua_pushnil(L); // Not required. |
143 | lua_pushnil(L); // Default ACL. | 144 | lua_pushnil(L); // Default ACL. |
144 | lua_pushnil(L); // No boss. | 145 | lua_pushnil(L); // No boss. |
145 | lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. | ||
146 | lua_call(L, 9, 0); | 146 | lua_call(L, 9, 0); |
147 | 147 | ||
148 | // skang.thing('cfoo') | 148 | // skang.thing('cfoo') |
149 | lua_getfield(L, skang, "thing"); | 149 | lua_getfield(L, skang, "thingasm"); |
150 | lua_pushstring(L, "cfoo"); | 150 | lua_pushstring(L, "cfoo"); |
151 | lua_pushnil(L); // No help. | 151 | lua_pushnil(L); // No help. |
152 | lua_pushnil(L); // No default. | 152 | lua_pushnil(L); // No default. |
@@ -159,7 +159,8 @@ int luaopen_test_c(lua_State *L) | |||
159 | lua_call(L, 9, 0); | 159 | lua_call(L, 9, 0); |
160 | 160 | ||
161 | // skang.thing('cfunc', 'Help Text', ffunc, 'number,string') | 161 | // skang.thing('cfunc', 'Help Text', ffunc, 'number,string') |
162 | lua_getfield(L, skang, "thing"); | 162 | lua_getfield(L, skang, "thingasm"); |
163 | lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. | ||
163 | lua_pushstring(L, "cfunc"); | 164 | lua_pushstring(L, "cfunc"); |
164 | lua_pushstring(L, "cfunc does nothing really"); | 165 | lua_pushstring(L, "cfunc does nothing really"); |
165 | lua_pushcfunction(L, cfunc); | 166 | lua_pushcfunction(L, cfunc); |
@@ -168,7 +169,6 @@ int luaopen_test_c(lua_State *L) | |||
168 | lua_pushnil(L); // Not required. | 169 | lua_pushnil(L); // Not required. |
169 | lua_pushnil(L); // Default ACL. | 170 | lua_pushnil(L); // Default ACL. |
170 | lua_pushnil(L); // No boss. | 171 | lua_pushnil(L); // No boss. |
171 | lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. | ||
172 | lua_call(L, 9, 0); | 172 | lua_call(L, 9, 0); |
173 | 173 | ||
174 | // skang.moduleEnd(_M) | 174 | // skang.moduleEnd(_M) |