ref: b659aa50e16dac8b0013e79ee28ed5853de57452
parent: ca3c7e43416b28d3b58a455e0a4068fa845fdfba
author: Tor Andersson <tor@ccxvii.net>
date: Thu Feb 6 08:56:50 EST 2014
Compile ES5 getters and setters.
--- a/jscompile.c
+++ b/jscompile.c
@@ -192,8 +192,8 @@
{
while (list) {
js_Ast *kv = list->a;
+ js_Ast *prop = kv->a;
if (kv->type == EXP_PROP_VAL) {
- js_Ast *prop = kv->a;
if (prop->type == AST_IDENTIFIER || prop->type == AST_STRING) {
cexp(J, F, kv->b);
emitstring(J, F, OP_INITPROP_S, prop->string);
@@ -211,8 +211,15 @@
jsC_error(J, list, "illegal property name in object initializer");
}
} else {
- // TODO: set/get
- jsC_error(J, kv, "property setters and getters are not implemented");
+ if (prop->type == AST_IDENTIFIER || prop->type == AST_STRING)
+ emitstring(J, F, OP_STRING, prop->string);
+ if (prop->type == AST_NUMBER)
+ emitnumber(J, F, prop->number);
+ emitfunction(J, F, newfun(J, NULL, kv->b, kv->c, 0));
+ if (kv->type == EXP_PROP_GET)
+ emit(J, F, OP_INITGETTER);
+ if (kv->type == EXP_PROP_SET)
+ emit(J, F, OP_INITSETTER);
}
list = list->b;
}
@@ -599,7 +606,7 @@
static int isfun(js_AstType T)
{
- return T == AST_FUNDEC || T == EXP_FUN;
+ return T == AST_FUNDEC || T == EXP_FUN || T == EXP_PROP_GET || T == EXP_PROP_SET;
}
static int matchlabel(js_Ast *node, const char *label)
--- a/jscompile.h
+++ b/jscompile.h
@@ -48,6 +48,9 @@
OP_INITPROP_N, /* <obj> <val> -- <obj> */
OP_INITPROP_S, /* <obj> <val> -- <obj> */
+ OP_INITGETTER, /* <obj> <key> <closure> -- <obj> */
+ OP_INITSETTER, /* <obj> <key> <closure> -- <obj> */
+
OP_GETPROP, /* <obj> <name> -- <value> */
OP_GETPROP_S, /* <obj> -S- <value> */
OP_SETPROP, /* <obj> <name> <value> -- <value> */
--- a/jsdump.c
+++ b/jsdump.c
@@ -197,7 +197,7 @@
ps("get ");
pexpi(d, COMMA, kv->a);
ps("() {\n");
- pstmlist(d, kv->b);
+ pstmlist(d, kv->c);
in(d); ps("}");
break;
case EXP_PROP_SET:
@@ -204,7 +204,7 @@
ps("set ");
pexpi(d, COMMA, kv->a);
ps("(");
- pexpi(d, COMMA, kv->b);
+ pargs(d, kv->b);
ps(") {\n");
pstmlist(d, kv->c);
in(d); ps("}");
@@ -639,6 +639,8 @@
case AST_NUMBER: printf(" %.9g", node->number); break;
case STM_BLOCK: afun = sblock; break;
case AST_FUNDEC: case EXP_FUN: cfun = sblock; break;
+ case EXP_PROP_GET: cfun = sblock; break;
+ case EXP_PROP_SET: cfun = sblock; break;
case STM_SWITCH: bfun = sblock; break;
case STM_CASE: bfun = sblock; break;
case STM_DEFAULT: afun = sblock; break;
--- a/jsparse.c
+++ b/jsparse.c
@@ -259,7 +259,7 @@
expect(J, '(');
expect(J, ')');
body = funbody(J);
- return EXP2(PROP_GET, name, body);
+ return EXP3(PROP_GET, name, NULL, body);
}
if (!strcmp(name->string, "set")) {
name = propname(J);
@@ -267,7 +267,7 @@
arg = identifier(J);
expect(J, ')');
body = funbody(J);
- return EXP3(PROP_SET, name, arg, body);
+ return EXP3(PROP_SET, name, LIST(arg), body);
}
}