List of scripts: is that possible or buggy?

In a script of mine I was trying to create a list of scripts, as follows:


property aList : {}

-- Returns a script object
on makeScript(valueForX)
	script
		property x : valueForX
		on foo()
			x + 1
		end foo
	end script
end makeScript

set refList to a reference to aList
repeat with i from 1 to 100
	set md to makeScript(i)
	copy md to the end of refList
end repeat
set aList to {} -- Reset persistent aList before the script ends

For some reason, when I run the above script in AppleScript Editor or with osascript, the process seems to enter an endless loop and it starts eating more and more memory, so that I have to kill it. Can anybody reproduce such behaviour? If so, is that a bug? If I change the number of iterations from 100 to a low value (say, 2) the script works correctly.

Thanks in advance.

Hi, druido.

Same behaviour here. Change the ‘copy’ line to ‘set’:


property aList : {}

-- Returns a script object
on makeScript(valueForX)
	script
		property x : valueForX
		on foo()
			x + 1
		end foo
	end script
end makeScript

set refList to a reference to aList
repeat with i from 1 to 100
	set md to makeScript(i)
	set the end of refList to md -- NB. not 'copy'
end repeat
set aList to {} -- Reset persistent aList before the script ends

‘Copy’ duplicates compound objects. I believe script objects are particularly complex items to duplicate, but I don’t know the exact details.

Thanks, that fixes the problem! So, now I can compare the performance against the alternative approach:


on makeStruct(valueForX)
	{x:valueForX}
end makeStruct

on foo(aStruct)
	(aStruct's x) + 1
end foo