Attachment #707411: Reflect support for export declarations + tests for bug #568953

View | Details | Raw Unified | Return to bug 568953
Collapse All | Expand All

(-)3ff2a91d7552 (+32 lines)
Added Link Here 
Added Link Here 
1
load(libdir + "asserts.js");
2
3
assertThrowsInstanceOf(function () {
4
    Reflect.parse("export var x;");
5
}, SyntaxError);
6
7
assertThrowsInstanceOf(function () {
8
    Reflect.parse("module 'foo' { export get function() {}; }");
9
}, SyntaxError);
10
11
assertThrowsInstanceOf(function () {
12
    Reflect.parse("module 'foo' { export set function(x) {}; }");
13
}, SyntaxError);
14
15
assertThrowsInstanceOf(function () {
16
    Reflect.parse("module 'foo' { export 42; }");
17
}, SyntaxError);
18
19
function test(string, type) {
20
    var node = Reflect.parse(string);
21
    node = node.body[0].body.body[0];
22
    assertEq(typeof node == "object" && node != null, true);
23
    assertEq(node.type, "ExportStatement");
24
    node = node.expr;
25
    assertEq(typeof node == "object" && node != null, true);
26
    assertEq(node.type, type);
27
};
28
29
test("module 'foo' { export function f() {}; }", "FunctionDeclaration");
30
test("module 'foo' { export var x; }", "VariableDeclaration");
31
test("module 'foo' { export const x; }", "VariableDeclaration");
32
test("module 'foo' { export let x; }", "VariableDeclaration");
(-)a/js/src/jsast.tbl (+1 lines)
Line     Link Here 
 Lines 39-54   ASTDEF(AST_GENERATOR_EXPR, "Gener Link Here 
39
ASTDEF(AST_YIELD_EXPR,            "YieldExpression",               "yieldExpression")
39
ASTDEF(AST_YIELD_EXPR,            "YieldExpression",               "yieldExpression")
40
ASTDEF(AST_LET_EXPR,              "LetExpression",                 "letExpression")
40
ASTDEF(AST_LET_EXPR,              "LetExpression",                 "letExpression")
41
41
42
ASTDEF(AST_EMPTY_STMT,            "EmptyStatement",                 "emptyStatement")
42
ASTDEF(AST_EMPTY_STMT,            "EmptyStatement",                 "emptyStatement")
43
ASTDEF(AST_BLOCK_STMT,            "BlockStatement",                 "blockStatement")
43
ASTDEF(AST_BLOCK_STMT,            "BlockStatement",                 "blockStatement")
44
ASTDEF(AST_EXPR_STMT,             "ExpressionStatement",            "expressionStatement")
44
ASTDEF(AST_EXPR_STMT,             "ExpressionStatement",            "expressionStatement")
45
ASTDEF(AST_LAB_STMT,              "LabeledStatement",               "labeledStatement")
45
ASTDEF(AST_LAB_STMT,              "LabeledStatement",               "labeledStatement")
46
ASTDEF(AST_IF_STMT,               "IfStatement",                    "ifStatement")
46
ASTDEF(AST_IF_STMT,               "IfStatement",                    "ifStatement")
47
ASTDEF(AST_EXPORT_STMT,           "ExportStatement",                "exportStatement")
47
ASTDEF(AST_SWITCH_STMT,           "SwitchStatement",                "switchStatement")
48
ASTDEF(AST_SWITCH_STMT,           "SwitchStatement",                "switchStatement")
48
ASTDEF(AST_WHILE_STMT,            "WhileStatement",                 "whileStatement")
49
ASTDEF(AST_WHILE_STMT,            "WhileStatement",                 "whileStatement")
49
ASTDEF(AST_DO_STMT,               "DoWhileStatement",               "doWhileStatement")
50
ASTDEF(AST_DO_STMT,               "DoWhileStatement",               "doWhileStatement")
50
ASTDEF(AST_FOR_STMT,              "ForStatement",                   "forStatement")
51
ASTDEF(AST_FOR_STMT,              "ForStatement",                   "forStatement")
51
ASTDEF(AST_FOR_IN_STMT,           "ForInStatement",                 "forInStatement")
52
ASTDEF(AST_FOR_IN_STMT,           "ForInStatement",                 "forInStatement")
52
ASTDEF(AST_FOR_OF_STMT,           "ForOfStatement",                 "forOfStatement")
53
ASTDEF(AST_FOR_OF_STMT,           "ForOfStatement",                 "forOfStatement")
53
ASTDEF(AST_BREAK_STMT,            "BreakStatement",                 "breakStatement")
54
ASTDEF(AST_BREAK_STMT,            "BreakStatement",                 "breakStatement")
54
ASTDEF(AST_CONTINUE_STMT,         "ContinueStatement",              "continueStatement")
55
ASTDEF(AST_CONTINUE_STMT,         "ContinueStatement",              "continueStatement")
(-)a/js/src/jsreflect.cpp (+26 lines)
Line     Link Here 
 Lines 531-546   class NodeBuilder Link Here 
531
531
532
    bool withStatement(HandleValue expr, HandleValue stmt, TokenPos *pos, MutableHandleValue dst);
532
    bool withStatement(HandleValue expr, HandleValue stmt, TokenPos *pos, MutableHandleValue dst);
533
533
534
    bool whileStatement(HandleValue test, HandleValue stmt, TokenPos *pos, MutableHandleValue dst);
534
    bool whileStatement(HandleValue test, HandleValue stmt, TokenPos *pos, MutableHandleValue dst);
535
535
536
    bool doWhileStatement(HandleValue stmt, HandleValue test, TokenPos *pos,
536
    bool doWhileStatement(HandleValue stmt, HandleValue test, TokenPos *pos,
537
                          MutableHandleValue dst);
537
                          MutableHandleValue dst);
538
538
539
    bool exportStatement(HandleValue expr, TokenPos *pos, MutableHandleValue dst);
540
539
    bool switchStatement(HandleValue disc, NodeVector &elts, bool lexical, TokenPos *pos,
541
    bool switchStatement(HandleValue disc, NodeVector &elts, bool lexical, TokenPos *pos,
540
                         MutableHandleValue dst);
542
                         MutableHandleValue dst);
541
543
542
    bool tryStatement(HandleValue body, NodeVector &guarded, HandleValue unguarded,
544
    bool tryStatement(HandleValue body, NodeVector &guarded, HandleValue unguarded,
543
                      HandleValue finally, TokenPos *pos, MutableHandleValue dst);
545
                      HandleValue finally, TokenPos *pos, MutableHandleValue dst);
544
546
545
    bool debuggerStatement(TokenPos *pos, MutableHandleValue dst);
547
    bool debuggerStatement(TokenPos *pos, MutableHandleValue dst);
546
548
 Lines 958-973   NodeBuilder::doWhileStatement(HandleValu Link Here 
958
960
959
    return newNode(AST_DO_STMT, pos,
961
    return newNode(AST_DO_STMT, pos,
960
                   "body", stmt,
962
                   "body", stmt,
961
                   "test", test,
963
                   "test", test,
962
                   dst);
964
                   dst);
963
}
965
}
964
966
965
bool
967
bool
968
NodeBuilder::exportStatement(HandleValue expr, TokenPos *pos, MutableHandleValue dst)
969
{
970
    RootedValue cb(cx, callbacks[AST_EXPORT_STMT]);
971
    if (!cb.isNull())
972
        return callback(cb, expr, pos, dst);
973
974
    return newNode(AST_EXPORT_STMT, pos,
975
                   "expr", expr,
976
                   dst);
977
}
978
979
bool
966
NodeBuilder::switchStatement(HandleValue disc, NodeVector &elts, bool lexical, TokenPos *pos,
980
NodeBuilder::switchStatement(HandleValue disc, NodeVector &elts, bool lexical, TokenPos *pos,
967
                             MutableHandleValue dst)
981
                             MutableHandleValue dst)
968
{
982
{
969
    RootedValue array(cx);
983
    RootedValue array(cx);
970
    if (!newArray(elts, &array))
984
    if (!newArray(elts, &array))
971
        return false;
985
        return false;
972
986
973
    RootedValue lexicalVal(cx, BooleanValue(lexical));
987
    RootedValue lexicalVal(cx, BooleanValue(lexical));
 Lines 1744-1759   class ASTSerializer Link Here 
1744
        return statement(pn, dst);
1758
        return statement(pn, dst);
1745
    }
1759
    }
1746
1760
1747
    bool forInit(ParseNode *pn, MutableHandleValue dst);
1761
    bool forInit(ParseNode *pn, MutableHandleValue dst);
1748
    bool forOfOrIn(ParseNode *loop, ParseNode *head, HandleValue var, HandleValue stmt,
1762
    bool forOfOrIn(ParseNode *loop, ParseNode *head, HandleValue var, HandleValue stmt,
1749
                   MutableHandleValue dst);
1763
                   MutableHandleValue dst);
1750
    bool statement(ParseNode *pn, MutableHandleValue dst);
1764
    bool statement(ParseNode *pn, MutableHandleValue dst);
1751
    bool blockStatement(ParseNode *pn, MutableHandleValue dst);
1765
    bool blockStatement(ParseNode *pn, MutableHandleValue dst);
1766
    bool exportStatement(ParseNode *pn, MutableHandleValue dst);
1752
    bool switchStatement(ParseNode *pn, MutableHandleValue dst);
1767
    bool switchStatement(ParseNode *pn, MutableHandleValue dst);
1753
    bool switchCase(ParseNode *pn, MutableHandleValue dst);
1768
    bool switchCase(ParseNode *pn, MutableHandleValue dst);
1754
    bool tryStatement(ParseNode *pn, MutableHandleValue dst);
1769
    bool tryStatement(ParseNode *pn, MutableHandleValue dst);
1755
    bool catchClause(ParseNode *pn, bool *isGuarded, MutableHandleValue dst);
1770
    bool catchClause(ParseNode *pn, bool *isGuarded, MutableHandleValue dst);
1756
1771
1757
    bool optExpression(ParseNode *pn, MutableHandleValue dst) {
1772
    bool optExpression(ParseNode *pn, MutableHandleValue dst) {
1758
        if (!pn) {
1773
        if (!pn) {
1759
            dst.setMagic(JS_SERIALIZE_NO_NODE);
1774
            dst.setMagic(JS_SERIALIZE_NO_NODE);
 Lines 2139-2154   ASTSerializer::switchCase(ParseNode *pn, Link Here 
2139
    RootedValue expr(cx);
2154
    RootedValue expr(cx);
2140
2155
2141
    return optExpression(pn->pn_left, &expr) &&
2156
    return optExpression(pn->pn_left, &expr) &&
2142
           statements(pn->pn_right, stmts) &&
2157
           statements(pn->pn_right, stmts) &&
2143
           builder.switchCase(expr, stmts, &pn->pn_pos, dst);
2158
           builder.switchCase(expr, stmts, &pn->pn_pos, dst);
2144
}
2159
}
2145
2160
2146
bool
2161
bool
2162
ASTSerializer::exportStatement(ParseNode *pn, MutableHandleValue dst)
2163
{
2164
    RootedValue expr(cx);
2165
    return declaration(pn->pn_kid, &expr)
2166
        && builder.exportStatement(expr, &pn->pn_pos, dst);
2167
}
2168
2169
bool
2147
ASTSerializer::switchStatement(ParseNode *pn, MutableHandleValue dst)
2170
ASTSerializer::switchStatement(ParseNode *pn, MutableHandleValue dst)
2148
{
2171
{
2149
    JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
2172
    JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
2150
    JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
2173
    JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
2151
2174
2152
    RootedValue disc(cx);
2175
    RootedValue disc(cx);
2153
2176
2154
    if (!expression(pn->pn_left, &disc))
2177
    if (!expression(pn->pn_left, &disc))
 Lines 2309-2324   ASTSerializer::statement(ParseNode *pn, Link Here 
2309
        RootedValue test(cx), cons(cx), alt(cx);
2332
        RootedValue test(cx), cons(cx), alt(cx);
2310
2333
2311
        return expression(pn->pn_kid1, &test) &&
2334
        return expression(pn->pn_kid1, &test) &&
2312
               statement(pn->pn_kid2, &cons) &&
2335
               statement(pn->pn_kid2, &cons) &&
2313
               optStatement(pn->pn_kid3, &alt) &&
2336
               optStatement(pn->pn_kid3, &alt) &&
2314
               builder.ifStatement(test, cons, alt, &pn->pn_pos, dst);
2337
               builder.ifStatement(test, cons, alt, &pn->pn_pos, dst);
2315
      }
2338
      }
2316
2339
2340
      case PNK_EXPORT:
2341
        return exportStatement(pn, dst);
2342
2317
      case PNK_SWITCH:
2343
      case PNK_SWITCH:
2318
        return switchStatement(pn, dst);
2344
        return switchStatement(pn, dst);
2319
2345
2320
      case PNK_TRY:
2346
      case PNK_TRY:
2321
        return tryStatement(pn, dst);
2347
        return tryStatement(pn, dst);
2322
2348
2323
      case PNK_WITH:
2349
      case PNK_WITH:
2324
      case PNK_WHILE:
2350
      case PNK_WHILE:

Return to bug 568953