Throw error when javascript variable is undefined

Is there an easy way to ensure that variables are all defined before running the script? Like… cancel the script if any variable is undefined, throw an error or something.

The script code needs to run before any errors can be caught. (The Juce JavaScriptEngine is not a compiler, the code is interpreted and executed as it runs.) If a variable was left undefined, you can check for that in your C++ code by using var::isUndefined after the script has run. Also JavaScriptEngine::execute returns a Result, which will tell you if there was an error parsing or executing the script code.

It’s just that I have hundreds of “setMethod” functions and not sure how to go about checking undefined in all of them. I’d have to write that if statement over and over. Maybe there’s a better way.

I might have to modify the juce javascript code. Is there one place in the code that evaluates variables that I can throw in a check for undefined?

How do I make this throw an error, where in the javascript code could I put a check?

newVariable = variable + undefined; // and other operators
newVariable += undefined; // and other operators

I figured out how to throw an undefined error for a few other situations by adding a check in Javascript::RootObject::Assignment:

    struct Assignment  : public Expression
        Assignment (const CodeLocation& l, ExpPtr& dest, ExpPtr& source) noexcept : Expression (l), target (dest), newValue (source) {}

        var getResult (const Scope& s) const override
            var value (newValue->getResult (s));

			if (value.isUndefined())
				location.throwError("Assigning an undefined variable.");

            target->assign (s, value);
            return value;

        ExpPtr target, newValue;