Fix "toSource is not a function" on V8
TypeError: data.toSource is not a function
ReferenceError: uneval is not defined
Why it happens
toSource() and uneval() were Mozilla extensions that printed an object as source code, e.g. ({a:1}). Rhino-era scripts used them to log objects or save them to properties. V8 never had them.
The fix
// Rhino
Logger.log(data.toSource());
PropertiesService.getScriptProperties().setProperty('cache', uneval(rows));
// V8
Logger.log(JSON.stringify(data));
PropertiesService.getScriptProperties().setProperty('cache', JSON.stringify(rows));
Gotchas
- Values saved with
unevalwere read back witheval. Switch the reader toJSON.parseat the same time, and convert existing stored values once. JSON.stringifydrops functions andundefinedand turnsDateinto an ISO string. For a function's source useString(fn).
How V8Shift handles it
x.toSource() → JSON.stringify(x) and uneval( → JSON.stringify( automatically, each marked V8Shift: review.