SC: new command loadFunction()

Features wanted...
Post Reply
Filehero
Posts: 2644
Joined: 27 Feb 2012 18:50
Location: Windows 10 Pro x64

SC: new command loadFunction()

Post by Filehero »

Hi Don,

since the arrival of User Functions my script repository is more and more build around functions. During development this is starting to become cumbersome because to simply call a function I always need a dedicated adapter (script). Therefore I propose to introduce a new command loadFunction.

Code: Select all

loadFunction

  Loads a function from the given resource file.

Syntax

  loadFunction resource, functionName, parameters

resource: [Required] The path of a script/multi-script file declaring the named user function.

functionName: [Required] The name of the function to be loaded.

parameters: WELL, STILL CLUELESS ABOUT (something like a string of |-separated named parameters or an array of parameters)

return: the function's return value or NOTHING
What do you think?


Filehero

highend
Posts: 13317
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC: new command loadFunction()

Post by highend »

I'd suggest to split any (non-related) multi function scripts into single
function scripts if you need access to only a single one.

Or you could write a function, that picks that function code, puts it in a temp .xyi script
and include that one :mrgreen:

While I was just doing this... include_once is executed before all other stuff :blackstorm:

@Don: Can you allow us to use include / include_once dynamically like so:

Code: Select all

    if (IncludeUserFunction("D:\Users\Highend\Tools\XYplorer\Data\Scripts\_functions.xyi", "regexEscape")) {
        text regexEscape("file [01].txt");
    }


function IncludeUserFunction($file, $functionName) {
    $result  = false;
    $tmpFile = "%TEMP%\$functionName.xyi";

    $content  = readfile($file);
    $function = regexmatches($content, "^function $functionName\([\s\S]+?^}");
    if ($function) {
        writefile($tmpFile, $function, , "utf8bom");
        include_once $tmpFile;
        // Probably delete the tmp file afterwards...
        $result = true;
    }
    return $result;
}
One of my scripts helped you out? Please donate via Paypal

Filehero
Posts: 2644
Joined: 27 Feb 2012 18:50
Location: Windows 10 Pro x64

Re: SC: new command loadFunction()

Post by Filehero »

highend wrote:I'd suggest to split any (non-related) multi function scripts into single
function scripts if you need access to only a single one.
This is exactly what I don't want to do anymore. Some lib files have 50 functions already. What's the benefit of adding another 50 wrapper scripts? Maintenance nightmare!

All Trumpism aside:
Let's say there is a two line function wakeupNAS() which is is called by some other functions, and all of them are contained in nas_lib.xyi. Now I want to expose this core function on the gui as well. Of course, I could
(1) add a script caption/label entry to the library script
(2) create a new dedicated script file wrapping the function

I would like to avoid
(1) because it is a library and therefore agnostic of any usage context including GUI exposure
(2) unnecessary "bloat"

Yes, in the end it's mainly a matter of style. I simply prefer functions over scripts, the first offer straight variability at call time, the latter don't.



/e: (3) I would like to avoid parsing/mangling/writing temp files as well. It is a working way, of course. But to me it is a bulky workaround for what could be easily(?) offered natively.

Anyway, thanks a lot, highend! It's always a pleasure to read your code proposals, your creativity is stunning und ich ziehe meinen Hut in tiefer Verbeugung!

highend
Posts: 13317
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC: new command loadFunction()

Post by highend »

/e: (3) I would like to avoid parsing/mangling/writing temp files as well. It is a working way, of course. But to me it is a bulky workaround for what could be easily(?) offered natively.
The question is, if this can be offered at all. XY scripting's design was taken from PHP which only knows about include and require and both take
a full file, not parts of it. Which scripting languages do support this feature (at all)? The Microsoft compiler (Visual Studio) can do this (only import
the minimal set of functions from a library instead of the complete one) resulting in much smaller (statically compiled) .exe than most other compilers. Just wanted to state that this is probably not easy to achieve...
One of my scripts helped you out? Please donate via Paypal

Filehero
Posts: 2644
Joined: 27 Feb 2012 18:50
Location: Windows 10 Pro x64

Re: SC: new command loadFunction()

Post by Filehero »

highend wrote:The question is, if this can be offered at all.... Just wanted to state that this is probably not easy to achieve...
Good points. I have no clue about how the XYplorer's script engine is working internally and what restrictions are imposed.

In my naive view, I thought this command could just like be implemented as a variation of the existing SC load. It would have to load the entire resource file and resolve all dependencies and then "just" inject a generic adapter script in memory which than calls the function just like a static one existing before load would do. All existing runtime mechanisms would still apply.

Code: Select all

include "lib_a.xyi";
include "lib_b.xyi";

"GenericAdapterScript"                <-- "Injected" by loadFunction(...)
  function_a("param_1", "param_2");   <-- to call function_a

function function_a($param_1, $param_2) {
  // code
}
Yes, it could be naive (but the issue is interesting). I will experiment with your "workaround", thanks again for that. :)

Cheers,
Filehero

highend
Posts: 13317
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC: new command loadFunction()

Post by highend »

I will experiment with your "workaround"
It won't work atm

Ofc this could be done the other way round, using self("script") in conjunction with a modified IncludeUserFunction()
that creates a new temp script which you execute from the main one. This would work now (without any changes from
Don) and would only create a single temp file... Sure, not really elegant though :ninja:
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60567
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: SC: new command loadFunction()

Post by admin »

Hmm, not sure if this will happen too soon. My plans for 2018 focus more on the medium power users...

highend
Posts: 13317
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC: new command loadFunction()

Post by highend »

@Don: Can you allow us to use include / include_once dynamically like so:
viewtopic.php?p=155273#p155273

Can this be done without some serious effort? It would at least allow something useable atm...
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60567
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: SC: new command loadFunction()

Post by admin »

I don't see a possibility for dynamically loading functions (without some very serious effort).

Filehero
Posts: 2644
Joined: 27 Feb 2012 18:50
Location: Windows 10 Pro x64

Re: SC: new command loadFunction()

Post by Filehero »

Thanks for taking your time and investigations. :tup:

Post Reply