More JavaScriptCore

Once again doing little JavaScriptCore to execises my imagination.

use framework "Foundation"
use framework "JavaScriptCore"
use scripting additions

set funcNumberJS to "function triple(number) {
	return number * 3;
}
triple(5)
"

-- its evaluateScript:"let text = 'John Doe';text" itsType:"string"
-- its evaluateScript:"const car = {type:'Fiat', model:'500', color:'white'};car.type" itsType:"string"

-- its evaluateScript:"[1,2,3]" itsType:"array"
-- its evaluateScript:"1+2+3" itsType:"number"
-- its evaluateScript:"const car = {type:'Fiat', model:'500', color:'white'};car" itsType:"object"
-- its evaluateScript:"new Date('2015-03-25')" itsType:"date"
-- its evaluateScript:"Boolean(10 > 8)" itsType:"boolean"

-- its evaluateScript:funcNumberJS itsType:"number"

-- its contextEvaluateScript:"function triple(number){return number * 3;};var threeTimesFive = triple(5)" itsObject:"threeTimesFive"

-- its contextEvaluateScript:"function triple(number){return number * 3;}" itsObject:"triple" itsArguments:{"9"}

on contextEvaluateScript:jsScript itsObject:theObject itsArguments:theArguments
	set context to current application's JSContext's alloc()'s init()
	context's evaluateScript:jsScript
	set theObject to context's objectForKeyedSubscript:theObject
	return (theObject's callWithArguments:theArguments)'s toInt32()
end contextEvaluateScript:itsObject:itsArguments:

on contextEvaluateScript:jsScript itsObject:theObject
	set context to current application's JSContext's alloc()'s init()
	context's evaluateScript:jsScript
	return (context's objectForKeyedSubscript:theObject)'s toInt32()
end contextEvaluateScript:itsObject:

on evaluateScript:jsScript itsType:jsType
	set context to current application's JSContext's alloc()'s init()
	if jsType is "string" then
		return (context's evaluateScript:jsScript)'s toString() as string
	else if jsType is "array" then
		return (context's evaluateScript:jsScript)'s toArray() as list
	else if jsType is "number" then
		return (context's evaluateScript:jsScript)'s toNumber() as real
	else if jsType is "object" then
		return (context's evaluateScript:jsScript)'s toDictionary() as record
	else if jsType is "date" then
		return (context's evaluateScript:jsScript)'s toDate() as date
	else if jsType is "boolean" then
		return (context's evaluateScript:jsScript)'s toBool() as boolean
	end if
end evaluateScript:itsType: