This functions seems to (partially?) duplicate ContextifyScript::Run(). Try to factor out the common functionality.