0

Purpose: Find undefined variables

Suppose we have a set of variables: varA, varB, varC

we know the following expression is valid:

varA + varB - varC

and the following expression is not valid(because VARC is not in the set of variables):

varA + varB - VARC

I've made it working by:

function validate(comma separated variables)
{
    the literal expression
    return 'PASS'
}

Now I simply run "validate()" without passing in any argument(Yes, it is supported by javascript), if the function returns 'PASS', it means the literal expression is valid. Otherwise it has error.

In this example, the function will not return 'PASS' because we have a typo in "varA + varB - VARC" and "VARC" will be reported by the javascript debugger.

function validate(varA, varB, varC)
{
    varA + varB - VARC
    return 'PASS'
}
validate();  //call the function WITHOUT passing in any argument

---------------------All good so far. I can validate arbitrary expression ! ---------------------

However, if I use javascript objects as a variable in the set, this way does not work. In the following code, 'PASS' is returned (while I'm expecting the debugger report error).

function MyClass()
{
    this.attr1 = null
    this.attr2 = null
}

myClass = new MyClass();

function validate(varA, varB, varC)
{
    varA + varB - varC + myClass.attr3
    return 'PASS'
}

Question 1: How do I find a way to report "myClass.attr3" is not defined?

NOTE: I can not tokenize (actually it is more complicated, has to be a grammar parser) the EXPRESSION and validate each token via "undefine test". I have to validate at the EXPRESSION level.

Edit:

I've tested the code and found that:

varA === undefined
VARC can not be evaluated, exception thrown  //the only case that is helping me
myClass.attr1 === null
myClass.attr3 === undefined
milesma
  • 1,561
  • 1
  • 15
  • 37
  • 1
    "we can not "validate" the expression in a compiling way as C++/Java". In your second example, even if you could validate it in a "compiler" way, it depends on a value is determine at runtime. Are you expecting to be able to "validate" arbitrary pieces of code? What are the rules regarding validity? This is really broad. – Evan Trimboli Aug 17 '17 at 05:36
  • @EvanTrimboli Yes, I'm expecting to be able to "validate" arbitrary piece of literal expression. My validity rule is simple: pickup the undefined variables. – milesma Aug 17 '17 at 05:39
  • What is expected result of `varA + varB - varC + myClass.attr3`? – guest271314 Aug 17 '17 at 05:48
  • How exactly will this validate an arbitrary literal expression? – maazadeeb Aug 17 '17 at 05:49
  • If it evaluates without error then it is by definition is a valid expression. If it does not produce the result you want that's a seperate problem. Attempting validation of arbitrary statements without tokenization is also a bit of a fool's errand. – shians Aug 17 '17 at 05:56
  • I think it would be better to take a step back and explain what the actual problem is. It sounds like you've already decided what the solution is and now you're trying to find a way to do it. – Evan Trimboli Aug 17 '17 at 05:58
  • @milesma The expression at code at Question does not throw an exception – guest271314 Aug 17 '17 at 06:05

4 Answers4

1

the expected result is: validate(...) function throw exception and not returning 'PASS' to the spidermonky engine.

One issue with the approach at Question is that the code within validate function does not throw an exception.

You use + and - operators, which can convert undefined to NaN, which is the result of

varA + varB - varC + myClass.attr3

For example

console.log(undefined + void 0 - undefined + void 0); // NaN

The solution is to re-evaluate and adjust your code if you are expecting an exception to be thrown if an expression includes undefined. The expression at validate() call currently evaluates to NaN, not an exception or undefined.

function myClass() {}

function validate(varA, varB, varC) {
  var x = varA + varB - varC + myClass.attr3;

  console.assert(!isNaN(x), x, [isNaN(x)]);
  return 'PASS'
}

validate()
guest271314
  • 1
  • 15
  • 104
  • 177
  • In order to let this code work, I have to assign argument for varA, varB and varC while calling validate, which means I still can not validate the expression out of arbitrary context. – milesma Aug 17 '17 at 06:37
  • @milesma Not certain what you are trying to achieve. _"What I'm expecting is message "myClass.attr3 is undefined" is returned because attr3 is not defined in MyClass."_ is not the case. The code at Question does not throw an exception. – guest271314 Aug 17 '17 at 06:52
  • I've updated the post, see the Edit section. If the code throw exception, my problem is solved. So "not throwing exception" proved my strategy, wrong (or limited to non object case) – milesma Aug 17 '17 at 06:56
  • @milesma The question is not clear. Not certain why you are expecting an exception given the code that you included at original question? – guest271314 Aug 17 '17 at 06:57
  • This is easy to explain. I can validate " varA + varB - VARC" is not valid and pick up VARC, cool, isn't it? (suppose the expression contains thousands of characters input by the user, I can pickup the token and report and the user change it). I could achieve this by the validate(...) function. – milesma Aug 17 '17 at 07:06
  • The code is valid. You have not created appropriate checks for the types or values that you appear to be expecting. You have not described why you are expecting an exception at `varA + varB - varC + myClass.attr3`? What is the purpose of using `+` and `-` operators? – guest271314 Aug 17 '17 at 07:07
  • + and - is just for demo, could be any JavaScript supported literal expressions. The ideal case is : an exception is thrown whenever myClass.attr3 is used(just like VARC), so I can report to the user that myClass.attr3 is not defined. However, this ideal case does not stand. – milesma Aug 17 '17 at 07:54
  • The demo is not an accurate as to input and expected result. You can check if variable or function parameter is defined. – guest271314 Aug 17 '17 at 08:00
0

The correct way to check if variables are defined is using

if (typeof var === "undefined") {
    // some kind of error
}

as discussed here.

shians
  • 955
  • 1
  • 6
  • 21
  • I understand this one. However, I can not tokenize (actually it is more complicated, has to be a grammar parser) the EXPRESSION and validate each token in your way. I have to validate at the EXPRESSION level. – milesma Aug 17 '17 at 05:43
  • 2
    Then please ask write your question in a way that reflects what you're trying to achieve. – shians Aug 17 '17 at 05:45
0

In order to find out undefined variables and null values there is a way to find it in javascript.
Go [here](Is there a standard function to check for null, undefined, or blank variables in JavaScript?"this text appears when you mouse over")! This will help.

  • I understand this one. However, I can not tokenize (actually it is more complicated, has to be a grammar parser) the EXPRESSION and validate each token in your way. I have to validate at the EXPRESSION level. – milesma Aug 17 '17 at 05:44
0
function MyClass()
{
    this.attr1 = null
    this.attr2 = null
}

myClass = new MyClass();

function validate(varA, varB, varC, myClass_attr1, myClass_attr2)
{
    varA + varB - varC + myClass_attr3
    return 'PASS'
}

Since this code will report myClass_attr3 is not defined, all I need to do is:

  1. flatten myClass to all leaves: myClass.attr1 and myClass.attr2

  2. Then preprocess the expression and set of variables by replacing "." With "_".

  3. Compose the validate function and run

  4. The engine will report because exception is thrown and caught in the engine.

I am doing more test...

milesma
  • 1,561
  • 1
  • 15
  • 37
  • _"Then preprocess the expression and set of variables by replacing "." With "\_""_ Not certain what you are trying to describe? _"The engine will report because exception is thrown"_ Where is an exception thrown at code at Answer? – guest271314 Aug 17 '17 at 08:16
  • The code will run in spidermonkey.You can think like debugging the code in chrome or Firefox, when error happened, the code will stop running and the debugger tell you which line, which position has a kind of error. I am capturing information like that. – milesma Aug 17 '17 at 08:25
  • What is the exception thrown using spidermonkey? – guest271314 Aug 17 '17 at 08:27
  • Uncaught exception with message (n line , m position, xyz is not defined). Again, it is the same as you debug the code. – milesma Aug 17 '17 at 09:11