mirror of
https://github.com/ccxvii/mujs.git
synced 2026-02-06 18:35:54 +08:00
typeof X when X in not declared should return undefined rather than throw.
This commit is contained in:
11
jscompile.c
11
jscompile.c
@@ -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;
|
||||
|
||||
@@ -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
15
jsrun.c
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user