简体   繁体   中英

Applescript - stack overflow in recursive function

I have an AppleScript script that runs a recursive function that counts instances of inserted plugins. After each plugin that is inserted the function checks the CPU and decides whether there's a CPU overload or not.

If a CPU overload was found, it starts to remove plugins until it reaches a point that is satisfactory. Then it returns the number of instances that was loaded on the computer.

The problem is that I get a stack overflow after a certain amount of runs. Does AppleScript has a limit of internal recursive threads?

on plugin_recurse(mode, plugin_name, component, track_count, instance_count, has_ref, min_instances, last_max)
    try
        log "mode - " & mode
        if mode is "ret" then return {track_count, instance_count, last_max}

        if mode is "add" then
            if (instance_count - (10 * track_count) = 0) then
                create_track(component)
                set track_count to track_count + 1
            end if
            set instance_count to instance_count + 1
            insert_plugin(plugin_name, component, track_count, instance_count)
            if has_ref then
                set CPUover to false
                if min_instances = 1 then
                    set mode to "ret"
                else
                    set min_instances to min_instances - 1
                end if
            else
                set {CPUover, last_max} to check_cpu(last_max)
            end if
            if CPUover then
                set mode to "sub"
            end if
        end if

        if mode is "sub" then
            if instance_count > 1 then
                remove_plugin(plugin_name, component, track_count, instance_count)
                set instance_count to instance_count - 1
                if ((10 * track_count) - instance_count = 10) then
                    remove_track(track_count)
                    set track_count to track_count - 1
                end if
                set {CPUover, last_max} to check_cpu(last_max)
                if not CPUover then
                    set mode to "ret"
                end if
            else
                set mode to "ret"
            end if
        end if
        plugin_recurse(mode, plugin_name, component, track_count, instance_count, has_ref, min_instances, last_max)
    on error err
        error err
    end try
end plugin_recurse

The problem is that I get a stack overflow after a certain amount of runs. Does AppleScript has a limit of internal recursive threads?

Yes, AppleScript is limited to many boundaries. Your error is caused by the limit of depth of handler (functions) calls. On my machine the level limit is set to 577. Such limitations are quite common for OOP languages because the 'virtual machine' that runs your code needs it's limits. Objective-C, for example, has also an limitation in recursion. If you need more, your code is considered as bad coding and you should try using a normal loop. However, I have to admit that 577 is not a very high number compared with other OOP's limits.

For such code, where it's uncertain how many recursion there will be, it's normally better to use loops than recursion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM