+2015-01-29 Ryosuke Niwa <rniwa@webkit.org>
+
+ Implement ES6 class syntax without inheritance support
+ https://bugs.webkit.org/show_bug.cgi?id=140918
+
+ Reviewed by Geoffrey Garen.
+
+ Added two tests for class declarations and class expressions.
+
+ * TestExpectations:
+ * js/class-syntax-declaration-expected.txt: Added.
+ * js/class-syntax-declaration.html: Added.
+ * js/class-syntax-expression-expected.txt: Added.
+ * js/class-syntax-expression.html: Added.
+ * js/script-tests/class-syntax-declaration.js: Added.
+ * js/script-tests/class-syntax-expression.js: Added.
+
2015-01-29 Simon Fraser <simon.fraser@apple.com>
Border-radius clipping on a stacking context causes descendants to not render
webkit.org/b/127860 [ Debug ] js/function-apply-aliased.html [ Skip ]
+# ES6 class syntax hasn't been enabled yet.
+webkit.org/b/140491 js/class-syntax-declaration.html [ Failure ]
+webkit.org/b/140491 js/class-syntax-expression.html [ Failure ]
+
# This test verifies dynamic manipulation of the mroot and msqrt elements.
mathml/roots-removeChild.html [ ImageOnlyFailure ]
--- /dev/null
+PASS constructorCallCount is 0
+PASS A.someStaticMethod() is staticMethodValue
+PASS (new A).someInstanceMethod() is instanceMethodValue
+PASS constructorCallCount is 1
+PASS (new A).someGetter is getterValue
+PASS constructorCallCount is 2
+PASS (new A).someGetter is getterValue
+PASS setterValue is undefined
+PASS (new A).someSetter = 789 did not throw exception.
+PASS setterValue is 789
+PASS (new A).__proto__ is A.prototype
+PASS A.prototype.constructor is A
+PASS class threw exception SyntaxError: Unexpected end of script.
+PASS class X { threw exception SyntaxError: Unexpected end of script.
+PASS class X { ( } threw exception SyntaxError: Unexpected token '('. Expected an indentifier..
+PASS class X {} threw exception SyntaxError: Class declaration without a constructor is not supported yet..
+PASS class X { constructor() {} constructor() {} } threw exception SyntaxError: Cannot declare multiple constructors in a single class..
+PASS class X { constructor() {} static constructor() { return staticMethodValue; } } did not throw exception.
+PASS X.constructor() is staticMethodValue
+PASS class X { constructor() {} static prototype() {} } threw exception SyntaxError: Cannot declare a static method named 'prototype'..
+PASS class X { constructor() {} prototype() { return instanceMethodValue; } } did not throw exception.
+PASS (new X).prototype() is instanceMethodValue
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
--- /dev/null
+<!DOCTYPE html>
+<html>
+<body>
+<script src="../resources/js-test-pre.js"></script>
+<script src="script-tests/class-syntax-declaration.js"></script>
+<script src="../resources/js-test-post.js"></script>
+</body>
+</html>
--- /dev/null
+PASS constructorCallCount is 0
+PASS A.someStaticMethod() is staticMethodValue
+PASS (new A).someInstanceMethod() is instanceMethodValue
+PASS constructorCallCount is 1
+PASS (new A).someGetter is getterValue
+PASS constructorCallCount is 2
+PASS (new A).someGetter is getterValue
+PASS setterValue is undefined
+PASS (new A).someSetter = 789 did not throw exception.
+PASS setterValue is 789
+PASS (new A).__proto__ is A.prototype
+PASS A.prototype.constructor is A
+PASS x = class threw exception SyntaxError: Unexpected end of script.
+PASS x = class { threw exception SyntaxError: Unexpected end of script.
+PASS x = class { ( } threw exception SyntaxError: Unexpected token '('. Expected an indentifier..
+PASS x = class {} threw exception SyntaxError: Class declaration without a constructor is not supported yet..
+PASS x = class { constructor() {} constructor() {} } threw exception SyntaxError: Cannot declare multiple constructors in a single class..
+PASS x = class { constructor() {} static constructor() { return staticMethodValue; } } did not throw exception.
+PASS x.constructor() is staticMethodValue
+PASS x = class { constructor() {} static prototype() {} } threw exception SyntaxError: Cannot declare a static method named 'prototype'..
+PASS x = class { constructor() {} prototype() { return instanceMethodValue; } } did not throw exception.
+PASS (new x).prototype() is instanceMethodValue
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
--- /dev/null
+<!DOCTYPE html>
+<html>
+<body>
+<script src="../resources/js-test-pre.js"></script>
+<script src="script-tests/class-syntax-expression.js"></script>
+<script src="../resources/js-test-post.js"></script>
+</body>
+</html>
--- /dev/null
+var constructorCallCount = 0;
+const staticMethodValue = [1];
+const instanceMethodValue = [2];
+const getterValue = [3];
+var setterValue = undefined;
+class A {
+ constructor() { constructorCallCount++; }
+ static someStaticMethod() { return staticMethodValue; }
+ someInstanceMethod() { return instanceMethodValue; }
+ get someGetter() { return getterValue; }
+ set someSetter(value) { setterValue = value; }
+}
+
+shouldBe("constructorCallCount", "0");
+shouldBe("A.someStaticMethod()", "staticMethodValue");
+shouldBe("(new A).someInstanceMethod()", "instanceMethodValue");
+shouldBe("constructorCallCount", "1");
+shouldBe("(new A).someGetter", "getterValue");
+shouldBe("constructorCallCount", "2");
+shouldBe("(new A).someGetter", "getterValue");
+shouldBe("setterValue", "undefined");
+shouldNotThrow("(new A).someSetter = 789");
+shouldBe("setterValue", "789");
+shouldBe("(new A).__proto__", "A.prototype");
+shouldBe("A.prototype.constructor", "A");
+
+shouldThrow("class", "'SyntaxError: Unexpected end of script'");
+shouldThrow("class X {", "'SyntaxError: Unexpected end of script'");
+shouldThrow("class X { ( }", "'SyntaxError: Unexpected token \\'(\\'. Expected an indentifier.'");
+shouldThrow("class X {}", "'SyntaxError: Class declaration without a constructor is not supported yet.'");
+shouldThrow("class X { constructor() {} constructor() {} }", "'SyntaxError: Cannot declare multiple constructors in a single class.'");
+shouldNotThrow("class X { constructor() {} static constructor() { return staticMethodValue; } }");
+shouldBe("X.constructor()", "staticMethodValue");
+shouldThrow("class X { constructor() {} static prototype() {} }", "'SyntaxError: Cannot declare a static method named \\'prototype\\'.'");
+shouldNotThrow("class X { constructor() {} prototype() { return instanceMethodValue; } }");
+shouldBe("(new X).prototype()", "instanceMethodValue");
+
+var successfullyParsed = true;
--- /dev/null
+var constructorCallCount = 0;
+const staticMethodValue = [1];
+const instanceMethodValue = [2];
+const getterValue = [3];
+var setterValue = undefined;
+var A = class {
+ constructor() { constructorCallCount++; }
+ static someStaticMethod() { return staticMethodValue; }
+ someInstanceMethod() { return instanceMethodValue; }
+ get someGetter() { return getterValue; }
+ set someSetter(value) { setterValue = value; }
+};
+
+shouldBe("constructorCallCount", "0");
+shouldBe("A.someStaticMethod()", "staticMethodValue");
+shouldBe("(new A).someInstanceMethod()", "instanceMethodValue");
+shouldBe("constructorCallCount", "1");
+shouldBe("(new A).someGetter", "getterValue");
+shouldBe("constructorCallCount", "2");
+shouldBe("(new A).someGetter", "getterValue");
+shouldBe("setterValue", "undefined");
+shouldNotThrow("(new A).someSetter = 789");
+shouldBe("setterValue", "789");
+shouldBe("(new A).__proto__", "A.prototype");
+shouldBe("A.prototype.constructor", "A");
+
+shouldThrow("x = class", "'SyntaxError: Unexpected end of script'");
+shouldThrow("x = class {", "'SyntaxError: Unexpected end of script'");
+shouldThrow("x = class { ( }", "'SyntaxError: Unexpected token \\'(\\'. Expected an indentifier.'");
+shouldThrow("x = class {}", "'SyntaxError: Class declaration without a constructor is not supported yet.'");
+shouldThrow("x = class { constructor() {} constructor() {} }", "'SyntaxError: Cannot declare multiple constructors in a single class.'");
+shouldNotThrow("x = class { constructor() {} static constructor() { return staticMethodValue; } }");
+shouldBe("x.constructor()", "staticMethodValue");
+shouldThrow("x = class { constructor() {} static prototype() {} }", "'SyntaxError: Cannot declare a static method named \\'prototype\\'.'");
+shouldNotThrow("x = class { constructor() {} prototype() { return instanceMethodValue; } }");
+shouldBe("(new x).prototype()", "instanceMethodValue");
+
+var successfullyParsed = true;
+2015-01-29 Ryosuke Niwa <rniwa@webkit.org>
+
+ Implement ES6 class syntax without inheritance support
+ https://bugs.webkit.org/show_bug.cgi?id=140918
+
+ Reviewed by Geoffrey Garen.
+
+ Added the most basic support for ES6 class syntax. After this patch, we support basic class definition like:
+ class A {
+ constructor() { }
+ someMethod() { }
+ }
+
+ We'll add the support for "extends" keyword and automatically generating a constructor in follow up patches.
+ We also don't support block scoping of a class declaration.
+
+ We support both class declaration and class expression. A class expression is implemented by the newly added
+ ClassExprNode AST node. A class declaration is implemented by ClassDeclNode, which is a thin wrapper around
+ AssignResolveNode.
+
+ Tests: js/class-syntax-declaration.html
+ js/class-syntax-expression.html
+
+ * bytecompiler/NodesCodegen.cpp:
+ (JSC::ObjectLiteralNode::emitBytecode): Create a new object instead of delegating the work to PropertyListNode.
+ Also fixed the 5-space indentation.
+ (JSC::PropertyListNode::emitBytecode): Don't create a new object now that ObjectLiteralNode does this.
+ (JSC::ClassDeclNode::emitBytecode): Added. Just let the AssignResolveNode node emit the byte code.
+ (JSC::ClassExprNode::emitBytecode): Create the class constructor and add static methods to the constructor by
+ emitting the byte code for PropertyListNode. Add instance methods to the class's prototype object the same way.
+
+ * parser/ASTBuilder.h:
+ (JSC::ASTBuilder::createClassExpr): Added. Creates a ClassExprNode.
+ (JSC::ASTBuilder::createClassDeclStatement): Added. Creates a AssignResolveNode and wraps it by a ClassDeclNode.
+
+ * parser/NodeConstructors.h:
+ (JSC::ClassDeclNode::ClassDeclNode): Added.
+ (JSC::ClassExprNode::ClassExprNode): Added.
+
+ * parser/Nodes.h:
+ (JSC::ClassExprNode): Added.
+ (JSC::ClassDeclNode): Added.
+
+ * parser/Parser.cpp:
+ (JSC::Parser<LexerType>::parseStatement): Added the support for class declaration.
+ (JSC::stringForFunctionMode): Return "method" for MethodMode.
+ (JSC::Parser<LexerType>::parseClassDeclaration): Added. Uses parseClass to create a class expression and wraps
+ it with ClassDeclNode as described above.
+ (JSC::Parser<LexerType>::parseClass): Parses a class expression.
+ (JSC::Parser<LexerType>::parseProperty):
+ (JSC::Parser<LexerType>::parseGetterSetter): Extracted from parseProperty to share the code between parseProperty
+ and parseClass.
+ (JSC::Parser<LexerType>::parsePrimaryExpression): Added the support for class expression.
+
+ * parser/Parser.h:
+ (FunctionParseMode): Added MethodMode.
+
+ * parser/SyntaxChecker.h:
+ (JSC::SyntaxChecker::createClassExpr): Added.
+ (JSC::SyntaxChecker::createClassDeclStatement): Added.
+
2015-01-29 Geoffrey Garen <ggaren@apple.com>
Try to fix the Windows build.
RegisterID* ObjectLiteralNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
- if (!m_list) {
- if (dst == generator.ignoredResult())
- return 0;
- return generator.emitNewObject(generator.finalDestination(dst));
- }
- return generator.emitNode(dst, m_list);
+ if (!m_list) {
+ if (dst == generator.ignoredResult())
+ return 0;
+ return generator.emitNewObject(generator.finalDestination(dst));
+ }
+ RefPtr<RegisterID> newObj = generator.emitNewObject(generator.tempDestination(dst));
+ generator.emitNode(newObj.get(), m_list);
+ return generator.moveToDestinationIfNeeded(dst, newObj.get());
}
// ------------------------------ PropertyListNode -----------------------------
RegisterID* PropertyListNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
- RefPtr<RegisterID> newObj = generator.tempDestination(dst);
-
- generator.emitNewObject(newObj.get());
-
// Fast case: this loop just handles regular value properties.
PropertyListNode* p = this;
for (; p && p->m_node->m_type == PropertyNode::Constant; p = p->m_next)
- emitPutConstantProperty(generator, newObj.get(), *p->m_node);
+ emitPutConstantProperty(generator, dst, *p->m_node);
// Were there any get/set properties?
if (p) {
// Handle regular values.
if (node->m_type == PropertyNode::Constant) {
- emitPutConstantProperty(generator, newObj.get(), *node);
+ emitPutConstantProperty(generator, dst, *node);
continue;
}
}
}
- generator.emitPutGetterSetter(newObj.get(), *node->name(), getterReg.get(), setterReg.get());
+ generator.emitPutGetterSetter(dst, *node->name(), getterReg.get(), setterReg.get());
}
}
- return generator.moveToDestinationIfNeeded(dst, newObj.get());
+ return dst;
}
void PropertyListNode::emitPutConstantProperty(BytecodeGenerator& generator, RegisterID* newObj, PropertyNode& node)
{
return generator.emitNewFunctionExpression(generator.finalDestination(dst), this);
}
+
+#if ENABLE(ES6_CLASS_SYNTAX)
+// ------------------------------ ClassDeclNode ---------------------------------
+
+void ClassDeclNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
+{
+ generator.emitNode(dst, m_classDeclaration);
+}
+
+// ------------------------------ ClassExprNode ---------------------------------
+
+RegisterID* ClassExprNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
+{
+ ASSERT(!m_parentClassExpression);
+
+ RefPtr<RegisterID> constructor = generator.emitNode(dst, m_constructorExpression);
+ RefPtr<RegisterID> prototype = generator.emitGetById(generator.newTemporary(), constructor.get(), generator.propertyNames().prototype);
+
+ if (m_staticMethods)
+ generator.emitNode(constructor.get(), m_staticMethods);
+
+ if (m_instanceMethods)
+ generator.emitNode(prototype.get(), m_instanceMethods);
+
+ return generator.moveToDestinationIfNeeded(dst, constructor.get());
+}
+#endif
// ------------------------------ DeconstructingAssignmentNode -----------------
RegisterID* DeconstructingAssignmentNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
typedef ArgumentListNode* ArgumentsList;
typedef ParameterNode* FormalParameterList;
typedef FunctionBodyNode* FunctionBody;
+#if ENABLE(ES6_CLASS_SYNTAX)
+ typedef ClassExprNode* ClassExpression;
+#endif
typedef StatementNode* Statement;
typedef ClauseListNode* ClauseList;
typedef CaseClauseNode* Clause;
return node;
}
+#if ENABLE(ES6_CLASS_SYNTAX)
+ ClassExprNode* createClassExpr(const JSTokenLocation& location, const Identifier& name, ExpressionNode* constructor,
+ ExpressionNode* parentClass, PropertyListNode* instanceMethods, PropertyListNode* staticMethods)
+ {
+ return new (m_parserArena) ClassExprNode(location, name, constructor, parentClass, instanceMethods, staticMethods);
+ }
+#endif
+
ExpressionNode* createFunctionExpr(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& info, unsigned functionKeywordStart)
{
FuncExprNode* result = new (m_parserArena) FuncExprNode(location, *info.name, info.body,
return decl;
}
+#if ENABLE(ES6_CLASS_SYNTAX)
+ StatementNode* createClassDeclStatement(const JSTokenLocation& location, ClassExprNode* classExpression,
+ const JSTextPosition& classStart, const JSTextPosition& classEnd, unsigned startLine, unsigned endLine)
+ {
+ // FIXME: Use "let" declaration.
+ ExpressionNode* assign = createAssignResolve(location, classExpression->name(), classExpression, classStart, classStart + 1, classEnd);
+ ClassDeclNode* decl = new (m_parserArena) ClassDeclNode(location, assign);
+ decl->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset);
+ return decl;
+ }
+#endif
+
StatementNode* createBlockStatement(const JSTokenLocation& location, JSC::SourceElements* elements, int startLine, int endLine)
{
BlockNode* block = new (m_parserArena) BlockNode(location, elements);
m_body->finishParsing(source, parameter, ident, FunctionDeclaration);
}
+#if ENABLE(ES6_CLASS_SYNTAX)
+ inline ClassDeclNode::ClassDeclNode(const JSTokenLocation& location, ExpressionNode* classDeclaration)
+ : StatementNode(location)
+ , m_classDeclaration(classDeclaration)
+ {
+ }
+
+ inline ClassExprNode::ClassExprNode(const JSTokenLocation& location, const Identifier& name, ExpressionNode* constructorExpression, ExpressionNode* parentClassExpression, PropertyListNode* instanceMethods, PropertyListNode* staticMethods)
+ : ExpressionNode(location)
+ , m_name(name)
+ , m_constructorExpression(constructorExpression)
+ , m_parentClassExpression(parentClassExpression)
+ , m_instanceMethods(instanceMethods)
+ , m_staticMethods(staticMethods)
+ {
+ }
+#endif
+
inline CaseClauseNode::CaseClauseNode(ExpressionNode* expr, SourceElements* statements)
: m_expr(expr)
, m_statements(statements)
FunctionBodyNode* m_body;
};
+#if ENABLE(ES6_CLASS_SYNTAX)
+ class ClassExprNode final : public ExpressionNode {
+ public:
+ ClassExprNode(const JSTokenLocation&, const Identifier&, ExpressionNode* constructorExpresssion,
+ ExpressionNode* parentClass, PropertyListNode* instanceMethods, PropertyListNode* staticMethods);
+
+ const Identifier& name() { return m_name; }
+
+ private:
+ virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
+
+ const Identifier& m_name;
+ ExpressionNode* m_constructorExpression;
+ ExpressionNode* m_parentClassExpression;
+ PropertyListNode* m_instanceMethods;
+ PropertyListNode* m_staticMethods;
+ };
+#endif
+
class DeconstructionPatternNode : public RefCounted<DeconstructionPatternNode> {
WTF_MAKE_NONCOPYABLE(DeconstructionPatternNode);
WTF_MAKE_FAST_ALLOCATED;
FunctionBodyNode* m_body;
};
+#if ENABLE(ES6_CLASS_SYNTAX)
+ class ClassDeclNode final : public StatementNode {
+ public:
+ ClassDeclNode(const JSTokenLocation&, ExpressionNode* classExpression);
+
+ private:
+ virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
+
+ ExpressionNode* m_classDeclaration;
+ };
+#endif
+
class CaseClauseNode : public ParserArenaFreeable {
public:
CaseClauseNode(ExpressionNode*, SourceElements* = 0);
case CONSTTOKEN:
result = parseConstDeclaration(context);
break;
+#if ENABLE(ES6_CLASS_SYNTAX)
+ case CLASSTOKEN:
+ failIfFalse(m_statementDepth == 1, "Class declaration is not allowed in a lexically nested statement");
+ result = parseClassDeclaration(context);
+ break;
+#endif
case FUNCTION:
failIfFalseIfStrict(m_statementDepth == 1, "Strict mode does not allow function declarations in a lexically nested statement");
result = parseFunctionDeclaration(context);
return "setter";
case FunctionMode:
return "function";
+#if ENABLE(ES6_CLASS_SYNTAX)
+ case MethodMode:
+ return "method";
+#endif
}
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
return context.createFuncDeclStatement(location, info, functionKeywordStart);
}
+#if ENABLE(ES6_CLASS_SYNTAX)
+template <typename LexerType>
+template <class TreeBuilder> TreeStatement Parser<LexerType>::parseClassDeclaration(TreeBuilder& context)
+{
+ ASSERT(match(CLASSTOKEN));
+ JSTokenLocation location(tokenLocation());
+ JSTextPosition classStart = tokenStartPosition();
+ unsigned classStartLine = tokenLine();
+
+ TreeClassExpression classExpr = parseClass(context, FunctionNeedsName);
+ failIfFalse(classExpr, "Failed to parse class");
+
+ JSTextPosition classEnd = lastTokenEndPosition();
+ unsigned classEndLine = tokenLine();
+
+ return context.createClassDeclStatement(location, classExpr, classStart, classEnd, classStartLine, classEndLine);
+}
+
+template <typename LexerType>
+template <class TreeBuilder> TreeClassExpression Parser<LexerType>::parseClass(TreeBuilder& context, FunctionRequirements requirements)
+{
+ ASSERT(match(CLASSTOKEN));
+ JSTokenLocation location(tokenLocation());
+ next();
+
+ AutoPopScopeRef classScope(this, pushScope());
+ classScope->setStrictMode();
+
+ const Identifier* className = nullptr;
+ if (match(IDENT)) {
+ className = m_token.m_data.ident;
+ next();
+ failIfFalse(classScope->declareVariable(className), "'", className->impl(), "' is not a valid class name");
+ } else if (requirements == FunctionNeedsName) {
+ semanticFailureDueToKeyword("class name");
+ failDueToUnexpectedToken();
+ } else
+ className = &m_vm->propertyNames->nullIdentifier;
+ ASSERT(className);
+
+ TreeExpression parentClass = 0;
+ if (consume(EXTENDS)) {
+ parentClass = parsePrimaryExpression(context);
+ failIfFalse(parentClass, "Cannot parse the parent class name");
+ failWithMessage("Inheritance is not supported yet");
+ }
+
+ consumeOrFailWithFlags(OPENBRACE, TreeBuilder::DontBuildStrings, "Expected opening '{' at the start of a class body");
+
+ TreeExpression constructor = 0;
+ TreePropertyList staticMethods = 0;
+ TreePropertyList instanceMethods = 0;
+ TreePropertyList instanceMethodsTail = 0;
+ TreePropertyList staticMethodsTail = 0;
+ while (!match(CLOSEBRACE)) {
+ if (match(SEMICOLON))
+ next();
+
+ JSTokenLocation methodLocation(tokenLocation());
+ unsigned methodStart = tokenStart();
+
+ bool isStaticMethod = match(STATICTOKEN);
+ if (isStaticMethod)
+ next();
+
+ matchOrFail(IDENT, "Expected an indentifier");
+
+ const CommonIdentifiers& propertyNames = *m_vm->propertyNames;
+ const Identifier& ident = *m_token.m_data.ident;
+ bool isGetter = ident == propertyNames.get;
+ bool isSetter = ident == propertyNames.set;
+
+ TreeProperty property;
+ const bool alwaysStrictInsideClass = true;
+ if (isGetter || isSetter) {
+ semanticFailIfTrue(isStaticMethod, "Cannot declare a static", stringForFunctionMode(isGetter ? GetterMode : SetterMode));
+ nextExpectIdentifier(LexerFlagsIgnoreReservedWords);
+ property = parseGetterSetter(context, alwaysStrictInsideClass, isGetter ? PropertyNode::Getter : PropertyNode::Setter, methodStart);
+ failIfFalse(property, "Cannot parse this method");
+ } else {
+ ParserFunctionInfo<TreeBuilder> methodInfo;
+ failIfFalse((parseFunctionInfo(context, FunctionNeedsName, FunctionMode, false, methodInfo)), "Cannot parse this method");
+ failIfFalse(methodInfo.name, "method must have a name");
+ failIfFalse(declareVariable(methodInfo.name), "Cannot declare a method named '", methodInfo.name->impl(), "'");
+
+ bool isConstructor = !isStaticMethod && *methodInfo.name == propertyNames.constructor;
+ if (isConstructor)
+ methodInfo.name = className;
+
+ TreeExpression method = context.createFunctionExpr(methodLocation, methodInfo, methodStart);
+ if (isConstructor) {
+ semanticFailIfTrue(constructor, "Cannot declare multiple constructors in a single class");
+ constructor = method;
+ continue;
+ }
+
+ // FIXME: Syntax error when super() is called
+ semanticFailIfTrue(isStaticMethod && *methodInfo.name == propertyNames.prototype,
+ "Cannot declare a static method named 'prototype'");
+ property = context.createProperty(methodInfo.name, method, PropertyNode::Constant, alwaysStrictInsideClass);
+ }
+
+ TreePropertyList& tail = isStaticMethod ? staticMethodsTail : instanceMethodsTail;
+ if (tail)
+ tail = context.createPropertyList(methodLocation, property, tail);
+ else {
+ tail = context.createPropertyList(methodLocation, property);
+ if (isStaticMethod)
+ staticMethods = tail;
+ else
+ instanceMethods = tail;
+ }
+ }
+
+ // FIXME: Create a Miranda function instead.
+ semanticFailIfFalse(constructor, "Class declaration without a constructor is not supported yet");
+
+ consumeOrFail(CLOSEBRACE, "Expected a closing '}' after a class body");
+
+ return context.createClassExpr(location, *className, constructor, parentClass, instanceMethods, staticMethods);
+}
+#endif
+
struct LabelInfo {
LabelInfo(const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end)
: m_ident(ident)
type = PropertyNode::Setter;
else
failWithMessage("Expected a ':' following the property name '", ident->impl(), "'");
- const Identifier* stringPropertyName = 0;
- double numericPropertyName = 0;
- if (m_token.m_type == IDENT || m_token.m_type == STRING)
- stringPropertyName = m_token.m_data.ident;
- else if (m_token.m_type == NUMBER)
- numericPropertyName = m_token.m_data.doubleValue;
- else
- failDueToUnexpectedToken();
- JSTokenLocation location(tokenLocation());
- next();
- ParserFunctionInfo<TreeBuilder> info;
- if (type == PropertyNode::Getter) {
- failIfFalse(match(OPENPAREN), "Expected a parameter list for getter definition");
- failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, GetterMode, false, info)), "Cannot parse getter definition");
- } else {
- failIfFalse(match(OPENPAREN), "Expected a parameter list for setter definition");
- failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SetterMode, false, info)), "Cannot parse setter definition");
- }
- if (stringPropertyName)
- return context.createGetterOrSetterProperty(location, type, complete, stringPropertyName, info, getterOrSetterStartOffset);
- return context.createGetterOrSetterProperty(const_cast<VM*>(m_vm), m_parserArena, location, type, complete, numericPropertyName, info, getterOrSetterStartOffset);
+ return parseGetterSetter(context, complete, type, getterOrSetterStartOffset);
}
case NUMBER: {
double propertyName = m_token.m_data.doubleValue;
}
}
+template <typename LexerType>
+template <class TreeBuilder> TreeProperty Parser<LexerType>::parseGetterSetter(TreeBuilder& context, bool strict, PropertyNode::Type type, unsigned getterOrSetterStartOffset)
+{
+ const Identifier* stringPropertyName = 0;
+ double numericPropertyName = 0;
+ if (m_token.m_type == IDENT || m_token.m_type == STRING)
+ stringPropertyName = m_token.m_data.ident;
+ else if (m_token.m_type == NUMBER)
+ numericPropertyName = m_token.m_data.doubleValue;
+ else
+ failDueToUnexpectedToken();
+ JSTokenLocation location(tokenLocation());
+ next();
+ ParserFunctionInfo<TreeBuilder> info;
+ if (type == PropertyNode::Getter) {
+ failIfFalse(match(OPENPAREN), "Expected a parameter list for getter definition");
+ failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, GetterMode, false, info)), "Cannot parse getter definition");
+ } else {
+ failIfFalse(match(OPENPAREN), "Expected a parameter list for setter definition");
+ failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SetterMode, false, info)), "Cannot parse setter definition");
+ }
+ if (stringPropertyName)
+ return context.createGetterOrSetterProperty(location, type, strict, stringPropertyName, info, getterOrSetterStartOffset);
+ return context.createGetterOrSetterProperty(const_cast<VM*>(m_vm), m_parserArena, location, type, strict, numericPropertyName, info, getterOrSetterStartOffset);
+}
+
template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseObjectLiteral(TreeBuilder& context)
{
failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, FunctionMode, false, info)), "Cannot parse function expression");
return context.createFunctionExpr(location, info, functionKeywordStart);
}
+#if ENABLE(ES6_CLASS_SYNTAX)
+ case CLASSTOKEN:
+ return parseClass(context, FunctionNoRequirements);
+#endif
case OPENBRACE:
if (strictMode())
return parseStrictObjectLiteral(context);
#define TreeArguments typename TreeBuilder::Arguments
#define TreeArgumentsList typename TreeBuilder::ArgumentsList
#define TreeFunctionBody typename TreeBuilder::FunctionBody
+#if ENABLE(ES6_CLASS_SYNTAX)
+#define TreeClassExpression typename TreeBuilder::ClassExpression
+#endif
#define TreeProperty typename TreeBuilder::Property
#define TreePropertyList typename TreeBuilder::PropertyList
#define TreeDeconstructionPattern typename TreeBuilder::DeconstructionPattern
enum SourceElementsMode { CheckForStrictMode, DontCheckForStrictMode };
enum FunctionRequirements { FunctionNoRequirements, FunctionNeedsName };
-enum FunctionParseMode { FunctionMode, GetterMode, SetterMode };
+enum FunctionParseMode {
+ FunctionMode,
+ GetterMode,
+ SetterMode,
+#if ENABLE(ES6_CLASS_SYNTAX)
+ MethodMode
+#endif
+};
enum DeconstructionKind {
DeconstructToVariables,
DeconstructToParameters,
template <class TreeBuilder> TreeSourceElements parseSourceElements(TreeBuilder&, SourceElementsMode);
template <class TreeBuilder> TreeStatement parseStatement(TreeBuilder&, const Identifier*& directive, unsigned* directiveLiteralLength = 0);
+#if ENABLE(ES6_CLASS_SYNTAX)
+ template <class TreeBuilder> TreeStatement parseClassDeclaration(TreeBuilder&);
+#endif
template <class TreeBuilder> TreeStatement parseFunctionDeclaration(TreeBuilder&);
template <class TreeBuilder> TreeStatement parseVarDeclaration(TreeBuilder&);
template <class TreeBuilder> TreeStatement parseConstDeclaration(TreeBuilder&);
enum SpreadMode { AllowSpread, DontAllowSpread };
template <class TreeBuilder> ALWAYS_INLINE TreeArguments parseArguments(TreeBuilder&, SpreadMode);
template <class TreeBuilder> TreeProperty parseProperty(TreeBuilder&, bool strict);
+ template <class TreeBuilder> TreeProperty parseGetterSetter(TreeBuilder&, bool strict, PropertyNode::Type, unsigned getterOrSetterStartOffset);
template <class TreeBuilder> ALWAYS_INLINE TreeFunctionBody parseFunctionBody(TreeBuilder&);
template <class TreeBuilder> ALWAYS_INLINE TreeFormalParameterList parseFormalParameters(TreeBuilder&);
template <class TreeBuilder> TreeExpression parseVarDeclarationList(TreeBuilder&, int& declarations, TreeDeconstructionPattern& lastPattern, TreeExpression& lastInitializer, JSTextPosition& identStart, JSTextPosition& initStart, JSTextPosition& initEnd);
template <class TreeBuilder> NEVER_INLINE TreeDeconstructionPattern tryParseDeconstructionPatternExpression(TreeBuilder&);
template <class TreeBuilder> NEVER_INLINE bool parseFunctionInfo(TreeBuilder&, FunctionRequirements, FunctionParseMode, bool nameIsInContainingScope, ParserFunctionInfo<TreeBuilder>&);
+#if ENABLE(ES6_CLASS_SYNTAX)
+ template <class TreeBuilder> NEVER_INLINE TreeClassExpression parseClass(TreeBuilder&, FunctionRequirements);
+#endif
ALWAYS_INLINE int isBinaryOperator(JSTokenType);
bool allowAutomaticSemicolon();
enum { NoneExpr = 0,
ResolveEvalExpr, ResolveExpr, NumberExpr, StringExpr,
ThisExpr, NullExpr, BoolExpr, RegExpExpr, ObjectLiteralExpr,
- FunctionExpr, BracketExpr, DotExpr, CallExpr,
+ FunctionExpr, ClassExpr, BracketExpr, DotExpr, CallExpr,
NewExpr, PreExpr, PostExpr, UnaryExpr, BinaryExpr,
ConditionalExpr, AssignmentExpr, TypeofExpr,
DeleteExpr, ArrayLiteralExpr, BindingDeconstruction,
typedef int ArgumentsList;
typedef int FormalParameterList;
typedef int FunctionBody;
+#if ENABLE(ES6_CLASS_SYNTAX)
+ typedef int ClassExpression;
+#endif
typedef int Statement;
typedef int ClauseList;
typedef int Clause;
ExpressionType createConditionalExpr(const JSTokenLocation&, ExpressionType, ExpressionType, ExpressionType) { return ConditionalExpr; }
ExpressionType createAssignResolve(const JSTokenLocation&, const Identifier&, ExpressionType, int, int, int) { return AssignmentExpr; }
ExpressionType createEmptyVarExpression(const JSTokenLocation&, const Identifier&) { return AssignmentExpr; }
+#if ENABLE(ES6_CLASS_SYNTAX)
+ ClassExpression createClassExpr(const JSTokenLocation&, const Identifier&, ExpressionType, ExpressionType, PropertyList, PropertyList) { return ClassExpr; }
+#endif
ExpressionType createFunctionExpr(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&, int) { return FunctionExpr; }
int createFunctionBody(const JSTokenLocation&, const JSTokenLocation&, int, int, bool) { return FunctionBodyResult; }
void setFunctionNameStart(int, int) { }
int createClauseList(int) { return ClauseListResult; }
int createClauseList(int, int) { return ClauseListResult; }
int createFuncDeclStatement(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&, int) { return StatementResult; }
- int createClassDeclStatement(const JSTokenLocation&, const Identifier&, ExpressionType, ExpressionType, PropertyList, PropertyList, int, int) { return StatementResult; }
+#if ENABLE(ES6_CLASS_SYNTAX)
+ int createClassDeclStatement(const JSTokenLocation&, ClassExpression,
+ const JSTextPosition&, const JSTextPosition&, int, int) { return StatementResult; }
+#endif
int createBlockStatement(const JSTokenLocation&, int, int, int) { return StatementResult; }
int createExprStatement(const JSTokenLocation&, int, int, int) { return StatementResult; }
int createIfStatement(const JSTokenLocation&, int, int, int, int) { return StatementResult; }