* require in plugins

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
User avatar
Ron Melby
Megastar
Posts: 878
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

require in plugins

Post by Ron Melby » 29 Oct 2021 00:49

as you may or may not know, I make extensive use of requires. mine are """"""non-standard"""""" but any program of any usefulness will need some requires, lfs, iup, luacom, something.

almost universally, I put them at the top lines of my plugin so the first thing a plugin does is "bind" the required "stuff'

For you prolific plugin authors, is there anyone who late binds requires, that is rather than declare them first thing, bury them in functions or even mainline code after it has started executing, and if so, can you give me some of the names of those plugins?

This has a point.

I am making a profiler, and 'late binding' requires has very large implications in my code complexity, and real execution times.

So, thanks in advance for your replies.
FH V.6.2.7 Win 10 64 bit

User avatar
Mark1834
Megastar
Posts: 2147
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: require in plugins

Post by Mark1834 » 29 Oct 2021 07:55

The CP house style and recommendation to plugin authors is to put all requires at the top of the script as global variables, so I always follow that.
Mark Draper

User avatar
tatewise
Megastar
Posts: 27079
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: require in plugins

Post by tatewise » 29 Oct 2021 11:29

The majority of my Plugins do not require all the library modules initially.

My large Plugins that include my 'standard library' of functions often only invoke the require needed within each function.
e.g.
Adjust AKA Names for Reports
Ancestral Sources Data Entry
Backup and Restore Family Historian Settings
Change Any Fact Tag
Export Gedcom File
Find Duplicate Individuals
Improve Website or CD DVD HTML
Lookup Missing BMD Records
Lookup Missing Census Facts
Map Life Facts
Search and Replace
Show Project Statistics

Some smaller Plugins only need a function or two that invokes the require when needed.
They may have some global require statements but not for all the libraries that might be needed.
e.g.
All Pool Relatives Except Partner Ancestors
Convert Legacy Places
Flexible CSV Importer
Install Library Modules
Move Local Media to Media Records
Where Used Record Links
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
Ron Melby
Megastar
Posts: 878
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: require in plugins

Post by Ron Melby » 30 Oct 2021 00:30

uff da. ok, thanks, we go the long route.
FH V.6.2.7 Win 10 64 bit

User avatar
Ron Melby
Megastar
Posts: 878
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: require in plugins

Post by Ron Melby » 03 Nov 2021 23:24

the first part relatively painless, I have hit a surprising road block, and it is compounded by the fact that you cant really debug a program that does debug.

I hope I have enough here that you can follow what is occuring and see my problem.

_etbl entry:

_etbl is an enumeration of _G
(entry under discussion)

["matADR"] =
{
["cfn"] = "_STD_GPS.matADR",
["name"] = "matADR",
["pkg"] = "_STD_GPS.",
["lvl"] = 1,
},

here is the setup oc information coming into the function
send to rtv_dbg_rcd
depth: 3
ccwhat: Lua
ccnamewhat: global
ccname: matADR
clldfn: 305
cccln: 398
ccendl: 407
ccnups: 0
ccsrc: @C:\ProgramData\Calico Pie\Family Historian\Plugins\_STD_GPS.lua
ccssrc: ...ata\Calico Pie\Family Historian\Plugins\_STD_GPS.lua
>>XXXXXXXXXX<<

this is the function in question

arguements:
func is the hexidecimal 'value' of the function (it will be abandoned it is an ephemeral value and not useful as a handle)
force is always 1
info is the debug.getinfo(depth) table as above

function rtv_dbg_rcd(func, force, info)
-- func = mat_fn_name(func)
-- ** Find the function ref for 'func' (if not present, create one)
if info then
if info.name
or info.what then
this is inoperative at the moment and not at issue
end
end

local _fnrcd = _dbgrcd[func]

if not _fnrcd then
if force ~= 1 then
print('no force') -- never executeo
return
else
-- Build a new _fnrcd entry
print('****new record****')
_fnrcd =
{
func = func,
info = (info or ''),
children = {},
children_time = {},
anon_child_time = 0,
named_child_time = 0,
count = 0,
time = 0,
}
I am building a table with other profiling information for this record

print('from record: ', _fnrcd['info']['name'])
if _etbl[_fnrcd['info']['name']] then
print('inside') this never executes
_fnrcd['info']['short_src'] = _etbl[_fnrcd['info']['name']]['cfn']
else
print('*error*') this always executes
end

this executes
_fnrcd['info']['short_src'] = _fnrcd['info']['short_src']:gsub('.*[/\\]', ''):gsub('%..*', '')

print('--function rtv_dbg_rcd--')
print('what: ', _fnrcd['info']['what'])
print('namewhat:', _fnrcd['info']['namewhat'])
print('name: ', _fnrcd['info']['name'])
print('src: ', _fnrcd['info']['source'])
print('ssrc: ', _fnrcd['info']['short_src'])

_dbgrcd[func] = _fnrcd
end
end

this is the log of what happened
****new record**** not a _dbgrcd so build one
from record: matADR I have a matADR entry in info from the _fnrcd
*error*
--function rtv_dbg_rcd--
what: Lua
namewhat: global
name: matADR


src: @C:\ProgramData\Calico Pie\Family Historian\Plugins\_STD_GPS.lua ****this errors I expect to get STD_GPS.matADR from _etbl[...resolved to matADR... as above.[ [cfn] record

ssrc: _STD_GPS ***this works

I have been beating around with this for weeks, it must be staring me in the face, but I do not see how it is not working.
What is obvious to the rest of the world, that is not obvious to me?
FH V.6.2.7 Win 10 64 bit

User avatar
tatewise
Megastar
Posts: 27079
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: require in plugins

Post by tatewise » 04 Nov 2021 11:00

What has this got to do with require statements?

The simple answer to your question is that _etbl["matADR"] does not exist.

Try replacing the print('from record: ', _fnrcd['info']['name']) statement with the following:

local index = _fnrcd['info']['name']
print( 'from record: ', index, type(index) )
print( '_etbl[index]: ', _etbl[index] )

That tells you the data type of matADR which you suggest is a string, but is that true?
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
Ron Melby
Megastar
Posts: 878
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: require in plugins

Post by Ron Melby » 04 Nov 2021 11:23

it has a great deal to to with the placement of require statements in code I do not write, since this bit of code is seminal to the actual problem of profiling code, and when I should enumerate _G, and what to do if the function is not found in _G, and determining if this is lua internal or fh internal or some other C/C++ code that executes lua framework (for generators, main, etc). lua is run on C/C++ so it has to have a main, for instance.

As you might remember, you helped me with a _G enumerator, and looked askance at my deriving a hex value from a function name. Silly me, I thought they would be handles, they are not. So, I have to revamp that.

my future problem might be if there is such a thing as say:
string.rep
and
join.rep
this situation based on the debugger returns appears to be indiscernable.
FH V.6.2.7 Win 10 64 bit

User avatar
Ron Melby
Megastar
Posts: 878
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: require in plugins

Post by Ron Melby » 04 Nov 2021 11:48

for k, v in pairs(_etbl) do
print(k, ' ', type(k))
end

results in:
matADR string

****new record****
index from record: matADR string
_etbl[index]: nil
from record: matADR
*error*
--function rtv_info_rcd--
info_rcd what: Lua
info_rcd namewhat: global
info_rcd name: matADR
info_rcd src: @C:\ProgramData\Calico Pie\Family Historian\Plugins\_STD_GPS.lua
info_rcd ssrc: _STD_GPS

they both appear to be strings, so I am still not following what is wrong,
FH V.6.2.7 Win 10 64 bit

User avatar
tatewise
Megastar
Posts: 27079
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: require in plugins

Post by tatewise » 04 Nov 2021 13:52

I assume that the print( ) statement output you have posted is copied directly from the FH debug screen.

On close examination, matADR is followed by varying numbers of space characters.
So although they all look much the same they are actually different strings.

So in the print( ) statements include some #k and #index items to show the length of the string.

I suspect in different cases they have varying trailing space characters, i.e. more than 6 characters long.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

Post Reply