masken
(MM club member)
2007-02-17 04:52 PM
See if an UDF is loaded?

I miss a simple way to check if an UDF is loaded. If you load several external UDF's, they themselves might have dependencies to the same UDF or same UDF name. This results in the same function being declared twice which isn't very good.

If there was a built-in function like "FunctionLoaded(function name)", one can build generic IF-checks into current UDF's and "globally" prevent this from happening.

EDIT
Dammit, posted in the wrong forum, can someone please move it to suggestions?


Les
(KiX Master)
2007-02-17 06:17 PM
Re: See if an UDF is loaded?

This has been discussed before. As a workaround, You can declare global vars and then test with IsDeclared().

I vote for the new function to be called IsDefined() keeping true to the D in UDF.


AllenAdministrator
(KiX Supporter)
2007-02-17 07:16 PM
Re: See if an UDF is loaded?

Glenn talks about his Kgen tool being able to do this.

KGen - the KiXtart UDF Resolver
http://innotechcg.com:2080/products/admin/main.asp?BODY=kixdev.htm


masken
(MM club member)
2007-02-18 12:29 AM
Re: See if an UDF is loaded?

Yes, global vars is one way, and that's what I'm using right now. But it feels like more of a workaround than a generic solution, as also an external tool would be. I think that to solve thing long term, some kind of solution built-into the KiX32.exe itself would really be the best way to go.

LonkeroAdministrator
(KiX Master Guru)
2007-02-18 09:47 PM
Re: See if an UDF is loaded?

hmm...
I think isDeclared() is suitable for that too.
instead of looking for a var, one could look for udf name:

if isDeclared(UDFname)
"udf is declared."
endif


masken
(MM club member)
2007-02-18 11:33 PM
Re: See if an UDF is loaded?

That would have been really nice \:\) Just tried it though, and it doesn't work \:\(

Glenn BarnasAdministrator
(KiX Supporter)
2007-02-19 05:14 AM
Re: See if an UDF is loaded?

Hmmm.. haven't actually tried having multiple udfs with the same name in the library. I think KGen would load only the first one it found, as it would have no way to differentiate between more than one. Keeping all my udfs in one place tends to prevent that from happening.

I haven't completed the temporal auto-cognizance code yet, which would read your mind, determine which duplicate-named UDF you really needed, and would prolly rename the dup udf and all references to that udf in your code while it was at it... ;\)

Glenn


LonkeroAdministrator
(KiX Master Guru)
2007-02-19 06:38 AM
Re: See if an UDF is loaded?

the udf load code changed while back.
now, if you try to load the same udf or at least 2 udf's with same name, you get an error and kixtart exits.


masken
(MM club member)
2007-02-19 11:54 AM
Re: See if an UDF is loaded?

Yeah, it does like Lonk says.

My situation is this; got a script. From the script I'm calling a couple of external UDF's. Each external UDF might contain references to other external UDF's. An example is ReadFile(). In those UDF's, one could generically just do a check before calling ReadFile(), like IF ISDECLARED("ReadFile") <> 1 CALL "@SCRIPDIR\ReadFile.udf" ENDIF.


Sealeopard
(KiX Master)
2007-02-19 02:09 PM
Re: See if an UDF is loaded?

But what if each script calls a different version of ReadFile? It might be better to just error out if this happens and restructure the UDF collection so that it is alwasy ensured that only one UDF of the same name is being loaded

masken
(MM club member)
2007-02-19 02:42 PM
Re: See if an UDF is loaded?

Yes of course, but that is in itself a different problem really. But the suggested solution handles both cases; if you can check if an UDF of a certain name is already loaded, then you can handle this without the script exiting.

LonkeroAdministrator
(KiX Master Guru)
2007-02-19 03:47 PM
Re: See if an UDF is loaded?

although, this case is really rare and when it happens, there is great possibility the UDFs with same name are different in function and thus would cause the script to malfunction anyhow.

but, indeed, isdeclared for udf would be cool.
or better yet, some command, macro that returns all loaded UDFs.


Glenn BarnasAdministrator
(KiX Supporter)
2007-02-20 07:28 PM
Re: See if an UDF is loaded?

I was referring to how KGen would handle a situation where you had two different UDFs with the same name, located in different files with unique names. KGen would load only the first UDF file it found, and your code would break if you were crazy enough to define two different UDFs called with the same name. ;\)

Glenn


Glenn BarnasAdministrator
(KiX Supporter)
2007-02-20 07:31 PM
Re: See if an UDF is loaded?

Again, KGen does that kind of recursive UDF resolution now. I have UDFs that have dependencies on other UDFs, and never give it a second thought.. KGen resolves the dependencies and loads all needed UDFs into the resulting code.

LonkeroAdministrator
(KiX Master Guru)
2007-02-20 09:47 PM
Re: See if an UDF is loaded?

@glenn, that's just plain ignorance.
if one doesn't know what his code does and what it uses...
well, he should figure it out.

@all, FYI:
Some versions back kixtart did load the dublicate UDFs and it caused some trouble:
- kixtart consumed a lot of memory on faulty code (reloaded the same udf zillion times)
- ppl could blame kixtart functioning wrong when in fact they had 2 UDFs loaded with different code and they assumed they didn't

@masken,
I must say it again in another words. reconstruct your scripts. There is no UDF that loads it's dependencies on it's own. it's the script that does the loading. if you load, it's no trouble at all to keep track of what you have loaded.

conclusion, I still think it would be cool to have a tracker macro.
maybe not just one that has only the name but the whole definition (name+args).


masken
(MM club member)
2007-02-20 10:10 PM
Re: See if an UDF is loaded?

\:\)

Well for me it's become clear when I've started working with KiXforms.

I know it might be bad structure to call UDF's from within UDF's, but that is what I have today. I want to keep my UDF's as standalone UDF's and that they work when called (ie, call their own dependencies), but that becomes hard when you're calling several of them from a form for example, and they have the same dependencies.

Reconstructing them so that the main script calls all dependencies is of course doable, but then one would still lack a good way to know why an UDF failed. If you could check within the UDF if a dependency is loaded or not, you could return a relevant errorcode and message saying this too \:\)

I really like the idea of a function that reports loaded functions. Perhaps IsDeclared() could work so that if IsDeclared() is called without a name, it returns an array with all loaded vars and functions, and if called with a name, returns if loaded or not, like:

Syntax: IsDeclared("string", type)

Parameters:

string (optional)
The variable or function you want to know the status of. If left out, all declared variables and/or functions are returned.

type (optional)
1 = variable
2 = function

?


LonkeroAdministrator
(KiX Master Guru)
2007-02-20 10:15 PM
Re: See if an UDF is loaded?

hmm...
so, the first point of improvement would be to make the parser tell where is the dublicate defined.
hmm, so, you load the UDF's on the fly with call lines?
I mean, included udf's can't be scripted away.


Richard H.Administrator
(KiX Supporter)
2007-02-21 11:03 AM
Re: See if an UDF is loaded?

 Originally Posted By: masken
I really like the idea of a function that reports loaded functions. Perhaps IsDeclared() could work so that if IsDeclared() is called without a name, it returns an array with all loaded vars and functions, and if called with a name, returns if loaded or not


There have been a few occassions when I'm doing something a bit tricky and I've wished that Ruud exposed the internal structures of KiXtart through automation objects, even if they were read only. Some of these have been addressed now with built-ins, but even so, I'd love to be able to code like:
 Code:
$oKix=GetObject("KiXtart.Interpreter")
 
If Not $oKix.Features("IsDeclared()")
   "Sorry, this script requires IsDeclared() to work correctly"+@CRLF
   Exit 1
EndIf
 
For Each $oVariable in $oKix.Globals
   "Global variable "+$oVariable.Name+" = '"$oVariable.Value+@CRLF
Next
 
For Each $oVariable in $oKix.Locals
   "Local variable "+$oVariable.Name+" = '"$oVariable.Value+@CRLF
Next
 
For Each $oUDF in $oKix.UDF
   "Defined UDFs "+$oUDF.Name+@CRLF
   For Each $oParameter in $oUDF.Parameters
      ; Note, parameter will only have a value when inside the actual UDF.
      "Parameter: "+$oParameter.Name+" value='"+$oParameter.Value+"' "+IIf($oParameter.Optional,"(optional)","(mandatory)")+@CRLF
   Next
Next
 
For Each $oFD in $oKix.FileDescriptors
   "File descriptor "+$oFD.Name+" is "
   If $oFD.mode>=0
      "in use, mode is "+$oFD.mode+" file path is '"+$oFD.Path
   Else
      "not in use."
   EndIf
   @CRLF
Next
 
"Currently executing line # "+$oKix.CurrentLine+@CRLF
 
myFunction()
 
Function myFunction()
   Debug("Testing return stack enumeration")
EndFunction
 
Function Debug($s)
   "DEBUG: "+$s"+@CRLF
   "   Return Stack:"+@CRLF
   For Each $oReturn in $oKix.ReturnStack
      "   Calling entity: "+$oReturnStack.name+", line #"+$oReturnStack.line"+@CRLF
   Next
EndFunction


masken
(MM club member)
2007-03-15 11:07 PM
Re: See if an UDF is loaded?

I'd like a ruudimentary comment on this ;\)

Les
(KiX Master)
2007-03-15 11:58 PM
Re: See if an UDF is loaded?

Looks to me you gotten more than just rudimentary comments. ;\)

masken
(MM club member)
2008-02-29 05:41 PM
Re: See if an UDF is loaded?

Where is that Ruud?

NTDOCAdministrator
(KiX Master)
2008-02-29 06:01 PM
Re: See if an UDF is loaded?

He actually just got back from a trip to the USA and is busy with many projects at work. Though he is also working on updates for KiXtart to support Unicode.

LonkeroAdministrator
(KiX Master Guru)
2008-03-16 11:20 PM
Re: See if an UDF is loaded?

hope he would just fix the bugs in 4.60 before trying to make new stuff.