Doesn't instantiated script objects (fully) support AppleScriptObjC?

As the title describes, somehow I cannot run ASObjC code from instantiated script objects. I may have forgotten something quite obvious, or didn’t get the memo that this isn’t possible with ASObjC. Does anyone know why the example below throws an error on NSMakeRect?

use AppleScript version "2.5"
use framework "AppKit"

set instance to createInstanceOfX()
instance's doSomething()

on createInstanceOfX()
	script x
		on init()
			return me
		end init
		
		on doSomething()
			current application's NSMakeRect(0.0, 0.0, 500.0, 80.0)
		end doSomething
	end script
	x's init()
end createInstanceOfX

I’m not sure why, but FWIW you can make it work in a script library. Change it like this:

use AppleScript version "2.5"
use framework "Foundation"
use framework "AppKit"

script forInheritance
	-- required for inheritance
end script

on createInstanceOfX()
	script
		property parent : forInheritance
		on init()
			return me
		end init
		
		on doSomething()
			current application's NSMakeRect(0.0, 0.0, 500.0, 80.0)
		end doSomething
	end script
	set x to result
	x's init()
end createInstanceOfX

Save is as a library and call it like this:

use thisLib : script "TestLibx" version "1.0"
use scripting additions

set instance to thisLib's createInstanceOfX()
set x to instance's doSomething()

I can get it to work without having to turn it into a library by either putting the use framework “Foundation” statement in the script object itself, but not in the main script:

use AppleScript version "2.4"

set instance to createInstanceOfX()
instance's doSomething()

on createInstanceOfX()
	script
		use framework "Foundation"
		
		on init()
			return me
		end init
		
		on doSomething()
			current application's NSMakeRect(0.0, 0.0, 500.0, 80.0)
		end doSomething
	end script
	return result's init()
end createInstanceOfX

… or by using an inheritance script object instantiated at run time:

use AppleScript version "2.4"
use framework "Foundation"

global forInheritance
script
end script
set forInheritance to result

set instance to createInstanceOfX()
instance's doSomething()

on createInstanceOfX()
	script
		property parent : forInheritance
		
		on init()
			return me
		end init
		
		on doSomething()
			current application's NSMakeRect(0.0, 0.0, 500.0, 80.0)
		end doSomething
	end script
	return result's init()
end createInstanceOfX

Perplexingly, the inheritance script object can itself be returned by a handler and everything will work, but the inheriting script object has to inherit from it, not directly from the main script. I suspect that the problem with the original script is some artifact of the compilation process.

use AppleScript version "2.4"
use framework "Foundation"

set forInheritance to createInheritanceObject()

set instance to createInstanceOfX(forInheritance)
instance's doSomething()

on createInheritanceObject()
	script
	end script
	return result
end createInheritanceObject

on createInstanceOfX(forInheritance)
	script
		property parent : forInheritance
		
		on init()
			return me
		end init
		
		on doSomething()
			current application's NSMakeRect(0.0, 0.0, 500.0, 80.0)
		end doSomething
	end script
	return result's init()
end createInstanceOfX

Thank you Shane and Nigel. Nigel’s last example will do the trick for me but I cannot think of any logic reason for it why it behaves like this. Just to clarify to make sure that I understand this well:

It’s not possible to use AppleScriptObjC both in the root level script object and the child script object at the same time. However to make AppleScriptObjC work in different script objects we have to put an (anonymous) script object in between. Then you can use AppleScriptObjC in the root level script object and the child child script object.

Thanks again!

Yes, it looks like it has to be created at run time, but something also needs to exist at compile time for the parent property to compile.

Another thing which works is to make sure the script object uses main script’s instance of current application:

use AppleScript version "2.4"
use framework "Foundation"

global currentApp
set currentApp to current application

set instance to createInstanceOfX()
instance's doSomething()

on createInstanceOfX()
	script
		on init()
			return me
		end init
		
		on doSomething()
			currentApp's NSMakeRect(0.0, 0.0, 500.0, 80.0)
		end doSomething
	end script
	return result's init()
end createInstanceOfX