typeof X when X in not declared should return undefined rather than throw.

This commit is contained in:
Tor Andersson
2014-02-24 12:03:59 +01:00
parent 3585abad41
commit 2d28162c2f
4 changed files with 23 additions and 5 deletions

View File

@@ -199,6 +199,15 @@ static void labelto(JF, int inst, int addr)
/* Expressions */
static void ctypeof(JF, js_Ast *exp)
{
if (exp->type == EXP_IDENTIFIER)
emitlocal(J, F, OP_GETLOCAL, OP_HASVAR, exp->string);
else
cexp(J, F, exp);
emit(J, F, OP_TYPEOF);
}
static void cunary(JF, js_Ast *exp, int opcode)
{
cexp(J, F, exp->a);
@@ -525,7 +534,7 @@ static void cexp(JF, js_Ast *exp)
emit(J, F, OP_UNDEF);
break;
case EXP_TYPEOF: cunary(J, F, exp, OP_TYPEOF); break;
case EXP_TYPEOF: ctypeof(J, F, exp->a); break;
case EXP_POS: cunary(J, F, exp, OP_POS); break;
case EXP_NEG: cunary(J, F, exp, OP_NEG); break;
case EXP_BITNOT: cunary(J, F, exp, OP_BITNOT); break;

View File

@@ -42,6 +42,7 @@ enum js_OpCode
OP_INITVAR, /* <value> -S- */
OP_DEFVAR, /* -S- */
OP_HASVAR, /* -S- ( <value> | undefined ) */
OP_GETVAR, /* -S- <value> */
OP_SETVAR, /* <value> -S- <value> */
OP_DELVAR, /* -S- <success> */

15
jsrun.c
View File

@@ -702,7 +702,7 @@ static void js_defvar(js_State *J, const char *name)
jsR_defproperty(J, J->E->variables, name, JS_DONTENUM | JS_DONTCONF, NULL, NULL, NULL);
}
static void js_getvar(js_State *J, const char *name)
static int js_hasvar(js_State *J, const char *name)
{
js_Environment *E = J->E;
do {
@@ -715,11 +715,11 @@ static void js_getvar(js_State *J, const char *name)
} else {
js_pushvalue(J, ref->value);
}
return;
return 1;
}
E = E->outer;
} while (E);
js_referenceerror(J, "%s is not defined", name);
return 0;
}
static void js_setvar(js_State *J, const char *name)
@@ -1075,7 +1075,14 @@ static void jsR_run(js_State *J, js_Function *F)
break;
case OP_GETVAR:
js_getvar(J, ST[*pc++]);
str = ST[*pc++];
if (!js_hasvar(J, str))
js_referenceerror(J, "%s is not defined", str);
break;
case OP_HASVAR:
if (!js_hasvar(J, ST[*pc++]))
js_pushundefined(J);
break;
case OP_SETVAR:

View File

@@ -30,6 +30,7 @@
"dellocal",
"initvar",
"defvar",
"hasvar",
"getvar",
"setvar",
"delvar",