Change castFromString so it can cast to constant string references

Before this change, constant string references were initialized from temporary stack allocated strings, which resulted in memory bugs.