Implement Function.name and Function#toString for ES6 class.
https://bugs.webkit.org/show_bug.cgi?id=155336
Reviewed by Geoffrey Garen.
Source/JavaScriptCore:
The only thing that the ES6 spec says about toString with regards to class
objects is:
"The string representation must have the syntax of a FunctionDeclaration,
FunctionExpression, GeneratorDeclaration, GeneratorExpression, ClassDeclaration,
ClassExpression, ArrowFunction, MethodDefinition, or GeneratorMethod depending
upon the actual characteristics of the object."
Previously, invoking toString() on a class object will return the function
source string of the class' constructor function. This does not conform to the
spec in that the toString string for a class does not have the syntax of a
ClassDeclaration or ClassExpression.
This is now fixed by doing the following:
1. Added "m_classSource" to FunctionExecutable (and correspondingly to
UnlinkedFunctionExecutable, FunctionMetadataNode, and ClassExprNode).
m_classSource is the SourceCode for the code range "class ... { ... }".
Since the class constructor function is the in memory representation of the
class object, only class constructor functions will have its m_classSource
set. m_classSource will be "null" (by default) for all other functions.
This is how we know if a FunctionExecutable is for a class.
Note: FunctionExecutable does not have its own m_classSource. It always gets
it from its UnlinkedFunctionExecutable. This is ok to do because our CodeCache
currently does not cache UnlinkedFunctionExecutables for class constructors.
2. The ClassExprNode now tracks the SourceCode range for the class expression.
This is used to set m_classSource in the UnlinkedFunctionExecutable at
bytecode generation time, and the FunctionExecutable later at bytecode
linking time.
3. Function.prototype.toString() now checks if the function is for a class.
If so, it returns the string for the class source instead of just the
function source for the class constructor.
Note: the class source is static from the time the class was parsed. This
can introduces some weirdness at runtime. Consider the following:
var v1 = class {}
v1.toString(); // yields "class {}".
class c2 extends v1 {}
c2.__proto__ === v1; // yields true i.e. c2 extends v1.
c2.toString(); // yields "class c2 extends v1 {}" which is fine.
v1 = {}; // point v1 to something else now.
c2.__proto__ === v1; // now yields false i.e. c2 no longer extends v1.
// c2 actually extends the class that v1 used to
// point to, but ...
c2.toString(); // still yields "class c2 extends v1 {}" which is no longer true.
It is unclear how we can best implement toString() to avoid this issue.
The above behavior is how Chrome (Version 51.0.2671.0 canary (64-bit))
currently implements toString() of a class, and we do the same in this patch.
In Firefox (45.0), toString() of a class will yield the function source of it
constructor function, which is not better.
In this patch, we also added ES6 compliance for Function.name on class objects:
4. The ClassExprNode now has a m_ecmaName string for tracking the inferred
name of a class according to the ES6 spec. The ASTBuilder now mirrors its
handling of FuncExprNodes to ClassExprNodes in setting the nodes' m_ecmaName
where relevant.
The m_ecmaName is later used to set the m_ecmaName of the FunctionExecutable
of the class constructor, which in turn is used to populate the initial value
of the Function.name property.
5. Also renamed some variable names (/m_metadata/metadata/) to be consistent with
webkit naming convention.
* bytecode/UnlinkedFunctionExecutable.cpp:
(JSC::UnlinkedFunctionExecutable::UnlinkedFunctionExecutable):
* bytecode/UnlinkedFunctionExecutable.h:
* bytecompiler/BytecodeGenerator.cpp:
(JSC::BytecodeGenerator::emitNewArrowFunctionExpression):
(JSC::BytecodeGenerator::emitNewDefaultConstructor):
* bytecompiler/BytecodeGenerator.h:
* bytecompiler/NodesCodegen.cpp:
(JSC::ClassExprNode::emitBytecode):
* parser/ASTBuilder.h:
(JSC::ASTBuilder::createAssignResolve):
(JSC::ASTBuilder::createYield):
(JSC::ASTBuilder::createClassExpr):
(JSC::ASTBuilder::createFunctionExpr):
(JSC::ASTBuilder::createProperty):
(JSC::ASTBuilder::makeAssignNode):
* parser/NodeConstructors.h:
(JSC::FunctionParameters::FunctionParameters):
(JSC::BaseFuncExprNode::BaseFuncExprNode):
(JSC::FuncExprNode::FuncExprNode):
(JSC::FuncDeclNode::FuncDeclNode):
(JSC::ArrowFuncExprNode::ArrowFuncExprNode):
(JSC::ClassDeclNode::ClassDeclNode):
(JSC::ClassExprNode::ClassExprNode):
* parser/Nodes.h:
(JSC::ExpressionNode::isDestructuringNode):
(JSC::ExpressionNode::isFuncExprNode):
(JSC::ExpressionNode::isArrowFuncExprNode):
(JSC::ExpressionNode::isClassExprNode):
(JSC::ExpressionNode::isCommaNode):
(JSC::ExpressionNode::isSimpleArray):
(JSC::ExpressionNode::isAdd):
* parser/Parser.cpp:
(JSC::stringForFunctionMode):
(JSC::Parser<LexerType>::parseFunctionInfo):
(JSC::Parser<LexerType>::parseClass):
* parser/ParserFunctionInfo.h:
* parser/SyntaxChecker.h:
(JSC::SyntaxChecker::createEmptyLetExpression):
(JSC::SyntaxChecker::createYield):
(JSC::SyntaxChecker::createClassExpr):
(JSC::SyntaxChecker::createFunctionExpr):
(JSC::SyntaxChecker::createFunctionMetadata):
(JSC::SyntaxChecker::createArrowFunctionExpr):
* runtime/Executable.cpp:
(JSC::FunctionExecutable::FunctionExecutable):
(JSC::FunctionExecutable::finishCreation):
* runtime/Executable.h:
* runtime/FunctionPrototype.cpp:
(JSC::functionProtoFuncToString):
* tests/es6.yaml:
LayoutTests:
* js/class-syntax-name-expected.txt:
* js/script-tests/class-syntax-name.js:
(shouldBe):
(shouldBeTrue):
- Rebased expected result.
* js/function-toString-vs-name.html:
* js/script-tests/function-toString-vs-name.js:
- Added new tests for class.
* platform/mac/inspector/model/remote-object-expected.txt:
- Rebased expected result.
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@198042 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/JavaScriptCore/ChangeLog b/Source/JavaScriptCore/ChangeLog
index 77d3c4b..65ddc91 100644
--- a/Source/JavaScriptCore/ChangeLog
+++ b/Source/JavaScriptCore/ChangeLog
@@ -1,3 +1,137 @@
+2016-03-11 Mark Lam <mark.lam@apple.com>
+
+ Implement Function.name and Function#toString for ES6 class.
+ https://bugs.webkit.org/show_bug.cgi?id=155336
+
+ Reviewed by Geoffrey Garen.
+
+ The only thing that the ES6 spec says about toString with regards to class
+ objects is:
+
+ "The string representation must have the syntax of a FunctionDeclaration,
+ FunctionExpression, GeneratorDeclaration, GeneratorExpression, ClassDeclaration,
+ ClassExpression, ArrowFunction, MethodDefinition, or GeneratorMethod depending
+ upon the actual characteristics of the object."
+
+ Previously, invoking toString() on a class object will return the function
+ source string of the class' constructor function. This does not conform to the
+ spec in that the toString string for a class does not have the syntax of a
+ ClassDeclaration or ClassExpression.
+
+ This is now fixed by doing the following:
+
+ 1. Added "m_classSource" to FunctionExecutable (and correspondingly to
+ UnlinkedFunctionExecutable, FunctionMetadataNode, and ClassExprNode).
+ m_classSource is the SourceCode for the code range "class ... { ... }".
+
+ Since the class constructor function is the in memory representation of the
+ class object, only class constructor functions will have its m_classSource
+ set. m_classSource will be "null" (by default) for all other functions.
+ This is how we know if a FunctionExecutable is for a class.
+
+ Note: FunctionExecutable does not have its own m_classSource. It always gets
+ it from its UnlinkedFunctionExecutable. This is ok to do because our CodeCache
+ currently does not cache UnlinkedFunctionExecutables for class constructors.
+
+ 2. The ClassExprNode now tracks the SourceCode range for the class expression.
+ This is used to set m_classSource in the UnlinkedFunctionExecutable at
+ bytecode generation time, and the FunctionExecutable later at bytecode
+ linking time.
+
+ 3. Function.prototype.toString() now checks if the function is for a class.
+ If so, it returns the string for the class source instead of just the
+ function source for the class constructor.
+
+ Note: the class source is static from the time the class was parsed. This
+ can introduces some weirdness at runtime. Consider the following:
+
+ var v1 = class {}
+ v1.toString(); // yields "class {}".
+
+ class c2 extends v1 {}
+
+ c2.__proto__ === v1; // yields true i.e. c2 extends v1.
+ c2.toString(); // yields "class c2 extends v1 {}" which is fine.
+
+ v1 = {}; // point v1 to something else now.
+
+ c2.__proto__ === v1; // now yields false i.e. c2 no longer extends v1.
+ // c2 actually extends the class that v1 used to
+ // point to, but ...
+ c2.toString(); // still yields "class c2 extends v1 {}" which is no longer true.
+
+ It is unclear how we can best implement toString() to avoid this issue.
+ The above behavior is how Chrome (Version 51.0.2671.0 canary (64-bit))
+ currently implements toString() of a class, and we do the same in this patch.
+ In Firefox (45.0), toString() of a class will yield the function source of it
+ constructor function, which is not better.
+
+ In this patch, we also added ES6 compliance for Function.name on class objects:
+
+ 4. The ClassExprNode now has a m_ecmaName string for tracking the inferred
+ name of a class according to the ES6 spec. The ASTBuilder now mirrors its
+ handling of FuncExprNodes to ClassExprNodes in setting the nodes' m_ecmaName
+ where relevant.
+
+ The m_ecmaName is later used to set the m_ecmaName of the FunctionExecutable
+ of the class constructor, which in turn is used to populate the initial value
+ of the Function.name property.
+
+ 5. Also renamed some variable names (/m_metadata/metadata/) to be consistent with
+ webkit naming convention.
+
+ * bytecode/UnlinkedFunctionExecutable.cpp:
+ (JSC::UnlinkedFunctionExecutable::UnlinkedFunctionExecutable):
+ * bytecode/UnlinkedFunctionExecutable.h:
+ * bytecompiler/BytecodeGenerator.cpp:
+ (JSC::BytecodeGenerator::emitNewArrowFunctionExpression):
+ (JSC::BytecodeGenerator::emitNewDefaultConstructor):
+ * bytecompiler/BytecodeGenerator.h:
+ * bytecompiler/NodesCodegen.cpp:
+ (JSC::ClassExprNode::emitBytecode):
+ * parser/ASTBuilder.h:
+ (JSC::ASTBuilder::createAssignResolve):
+ (JSC::ASTBuilder::createYield):
+ (JSC::ASTBuilder::createClassExpr):
+ (JSC::ASTBuilder::createFunctionExpr):
+ (JSC::ASTBuilder::createProperty):
+ (JSC::ASTBuilder::makeAssignNode):
+ * parser/NodeConstructors.h:
+ (JSC::FunctionParameters::FunctionParameters):
+ (JSC::BaseFuncExprNode::BaseFuncExprNode):
+ (JSC::FuncExprNode::FuncExprNode):
+ (JSC::FuncDeclNode::FuncDeclNode):
+ (JSC::ArrowFuncExprNode::ArrowFuncExprNode):
+ (JSC::ClassDeclNode::ClassDeclNode):
+ (JSC::ClassExprNode::ClassExprNode):
+ * parser/Nodes.h:
+ (JSC::ExpressionNode::isDestructuringNode):
+ (JSC::ExpressionNode::isFuncExprNode):
+ (JSC::ExpressionNode::isArrowFuncExprNode):
+ (JSC::ExpressionNode::isClassExprNode):
+ (JSC::ExpressionNode::isCommaNode):
+ (JSC::ExpressionNode::isSimpleArray):
+ (JSC::ExpressionNode::isAdd):
+ * parser/Parser.cpp:
+ (JSC::stringForFunctionMode):
+ (JSC::Parser<LexerType>::parseFunctionInfo):
+ (JSC::Parser<LexerType>::parseClass):
+ * parser/ParserFunctionInfo.h:
+ * parser/SyntaxChecker.h:
+ (JSC::SyntaxChecker::createEmptyLetExpression):
+ (JSC::SyntaxChecker::createYield):
+ (JSC::SyntaxChecker::createClassExpr):
+ (JSC::SyntaxChecker::createFunctionExpr):
+ (JSC::SyntaxChecker::createFunctionMetadata):
+ (JSC::SyntaxChecker::createArrowFunctionExpr):
+ * runtime/Executable.cpp:
+ (JSC::FunctionExecutable::FunctionExecutable):
+ (JSC::FunctionExecutable::finishCreation):
+ * runtime/Executable.h:
+ * runtime/FunctionPrototype.cpp:
+ (JSC::functionProtoFuncToString):
+ * tests/es6.yaml:
+
2016-03-11 Commit Queue <commit-queue@webkit.org>
Unreviewed, rolling out r197994.
@@ -579,7 +713,6 @@
(noAssign): Deleted.
(catch): Deleted.
->>>>>>> .r197960
2016-03-08 Skachkov Oleksandr <gskachkov@gmail.com>
How we load new.target in arrow functions is broken
diff --git a/Source/JavaScriptCore/bytecode/UnlinkedFunctionExecutable.cpp b/Source/JavaScriptCore/bytecode/UnlinkedFunctionExecutable.cpp
index d9ca1cf..99baf55 100644
--- a/Source/JavaScriptCore/bytecode/UnlinkedFunctionExecutable.cpp
+++ b/Source/JavaScriptCore/bytecode/UnlinkedFunctionExecutable.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2012, 2013, 2015 Apple Inc. All Rights Reserved.
+ * Copyright (C) 2012-2013, 2015-2016 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -105,6 +105,7 @@
, m_ecmaName(node->ecmaName())
, m_inferredName(node->inferredName())
, m_sourceOverride(WTFMove(sourceOverride))
+ , m_classSource(node->classSource())
{
ASSERT(m_constructorKind == static_cast<unsigned>(node->constructorKind()));
m_parentScopeTDZVariables.swap(parentScopeTDZVariables);
diff --git a/Source/JavaScriptCore/bytecode/UnlinkedFunctionExecutable.h b/Source/JavaScriptCore/bytecode/UnlinkedFunctionExecutable.h
index 30b3c3e..3585480 100644
--- a/Source/JavaScriptCore/bytecode/UnlinkedFunctionExecutable.h
+++ b/Source/JavaScriptCore/bytecode/UnlinkedFunctionExecutable.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2012-2015 Apple Inc. All Rights Reserved.
+ * Copyright (C) 2012-2016 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -76,9 +76,14 @@
const Identifier& name() const { return m_name; }
const Identifier& ecmaName() const { return m_ecmaName; }
+ void setEcmaName(const Identifier& name) { m_ecmaName = name; }
const Identifier& inferredName() const { return m_inferredName; }
unsigned parameterCount() const { return m_parameterCount; };
SourceParseMode parseMode() const { return static_cast<SourceParseMode>(m_sourceParseMode); };
+
+ const SourceCode& classSource() const { return m_classSource; };
+ void setClassSource(const SourceCode& source) { m_classSource = source; };
+
bool isInStrictContext() const { return m_isInStrictContext; }
FunctionMode functionMode() const { return static_cast<FunctionMode>(m_functionMode); }
ConstructorKind constructorKind() const { return static_cast<ConstructorKind>(m_constructorKind); }
@@ -163,6 +168,7 @@
Identifier m_ecmaName;
Identifier m_inferredName;
RefPtr<SourceProvider> m_sourceOverride;
+ SourceCode m_classSource;
VariableEnvironment m_parentScopeTDZVariables;
diff --git a/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp b/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp
index ed1ba0c..703ad43 100644
--- a/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp
+++ b/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008, 2009, 2012-2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2008-2009, 2012-2016 Apple Inc. All rights reserved.
* Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
* Copyright (C) 2012 Igalia, S.L.
*
@@ -2810,10 +2810,13 @@
return dst;
}
-RegisterID* BytecodeGenerator::emitNewDefaultConstructor(RegisterID* dst, ConstructorKind constructorKind, const Identifier& name)
+RegisterID* BytecodeGenerator::emitNewDefaultConstructor(RegisterID* dst, ConstructorKind constructorKind, const Identifier& name,
+ const Identifier& ecmaName, const SourceCode& classSource)
{
UnlinkedFunctionExecutable* executable = m_vm->builtinExecutables()->createDefaultConstructor(constructorKind, name);
executable->setInvalidTypeProfilingOffsets();
+ executable->setEcmaName(ecmaName);
+ executable->setClassSource(classSource);
unsigned index = m_codeBlock->addFunctionExpr(executable);
diff --git a/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.h b/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.h
index fef8d84..dbfd77c 100644
--- a/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.h
+++ b/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008, 2009, 2012-2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2008-2009, 2012-2016 Apple Inc. All rights reserved.
* Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
* Copyright (C) 2012 Igalia, S.L.
*
@@ -518,7 +518,7 @@
RegisterID* emitNewFunction(RegisterID* dst, FunctionMetadataNode*);
RegisterID* emitNewFunctionExpression(RegisterID* dst, FuncExprNode* func);
- RegisterID* emitNewDefaultConstructor(RegisterID* dst, ConstructorKind, const Identifier& name);
+ RegisterID* emitNewDefaultConstructor(RegisterID* dst, ConstructorKind, const Identifier& name, const Identifier& ecmaName, const SourceCode& classSource);
RegisterID* emitNewArrowFunctionExpression(RegisterID*, ArrowFuncExprNode*);
RegisterID* emitNewRegExp(RegisterID* dst, RegExp*);
diff --git a/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp b/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp
index 9b84b00..679651a 100644
--- a/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp
+++ b/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp
@@ -1,7 +1,7 @@
/*
* Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
-* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2012, 2013, 2015 Apple Inc. All rights reserved.
+* Copyright (C) 2003-2009, 2012-2013, 2015-2016 Apple Inc. All rights reserved.
* Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
* Copyright (C) 2007 Maks Orlovich
* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
@@ -3220,11 +3220,16 @@
RefPtr<RegisterID> constructor;
// FIXME: Make the prototype non-configurable & non-writable.
- if (m_constructorExpression)
+ if (m_constructorExpression) {
+ ASSERT(m_constructorExpression->isFuncExprNode());
+ FunctionMetadataNode* metadata = static_cast<FuncExprNode*>(m_constructorExpression)->metadata();
+ metadata->setEcmaName(ecmaName());
+ metadata->setClassSource(m_classSource);
constructor = generator.emitNode(dst, m_constructorExpression);
- else {
+ } else {
constructor = generator.emitNewDefaultConstructor(generator.finalDestination(dst),
- m_classHeritage ? ConstructorKind::Derived : ConstructorKind::Base, m_name);
+ m_classHeritage ? ConstructorKind::Derived : ConstructorKind::Base,
+ m_name, ecmaName(), m_classSource);
}
const auto& propertyNames = generator.propertyNames();
diff --git a/Source/JavaScriptCore/parser/ASTBuilder.h b/Source/JavaScriptCore/parser/ASTBuilder.h
index c24c7e0..e6cda90 100644
--- a/Source/JavaScriptCore/parser/ASTBuilder.h
+++ b/Source/JavaScriptCore/parser/ASTBuilder.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2010, 2013, 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -346,7 +346,8 @@
auto metadata = static_cast<FuncExprNode*>(rhs)->metadata();
metadata->setEcmaName(ident);
metadata->setInferredName(ident);
- }
+ } else if (rhs->isClassExprNode())
+ static_cast<ClassExprNode*>(rhs)->setEcmaName(ident);
AssignResolveNode* node = new (m_parserArena) AssignResolveNode(location, ident, rhs, assignmentContext);
setExceptionLocation(node, start, divot, end);
return node;
@@ -364,10 +365,11 @@
return node;
}
- ClassExprNode* createClassExpr(const JSTokenLocation& location, const Identifier& name, VariableEnvironment& classEnvironment, ExpressionNode* constructor,
+ ClassExprNode* createClassExpr(const JSTokenLocation& location, const ParserClassInfo<ASTBuilder>& classInfo, VariableEnvironment& classEnvironment, ExpressionNode* constructor,
ExpressionNode* parentClass, PropertyListNode* instanceMethods, PropertyListNode* staticMethods)
{
- return new (m_parserArena) ClassExprNode(location, name, classEnvironment, constructor, parentClass, instanceMethods, staticMethods);
+ SourceCode source = m_sourceCode->subExpression(classInfo.startOffset, classInfo.endOffset, classInfo.startLine, classInfo.startColumn);
+ return new (m_parserArena) ClassExprNode(location, *classInfo.className, source, classEnvironment, constructor, parentClass, instanceMethods, staticMethods);
}
ExpressionNode* createFunctionExpr(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& functionInfo)
@@ -442,7 +444,8 @@
auto metadata = static_cast<FuncExprNode*>(node)->metadata();
metadata->setEcmaName(*propertyName);
metadata->setInferredName(*propertyName);
- }
+ } else if (node->isClassExprNode())
+ static_cast<ClassExprNode*>(node)->setEcmaName(*propertyName);
return new (m_parserArena) PropertyNode(*propertyName, node, type, putType, superBinding);
}
PropertyNode* createProperty(VM* vm, ParserArena& parserArena, double propertyName, ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool)
@@ -1301,7 +1304,8 @@
auto metadata = static_cast<FuncExprNode*>(expr)->metadata();
metadata->setEcmaName(resolve->identifier());
metadata->setInferredName(resolve->identifier());
- }
+ } else if (expr->isClassExprNode())
+ static_cast<ClassExprNode*>(expr)->setEcmaName(resolve->identifier());
AssignResolveNode* node = new (m_parserArena) AssignResolveNode(location, resolve->identifier(), expr, AssignmentContext::AssignmentExpression);
setExceptionLocation(node, start, divot, end);
return node;
diff --git a/Source/JavaScriptCore/parser/NodeConstructors.h b/Source/JavaScriptCore/parser/NodeConstructors.h
index 1950f16..c6edf35 100644
--- a/Source/JavaScriptCore/parser/NodeConstructors.h
+++ b/Source/JavaScriptCore/parser/NodeConstructors.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009, 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2009, 2013, 2015 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@@ -890,27 +890,27 @@
}
- inline BaseFuncExprNode::BaseFuncExprNode(const JSTokenLocation& location, const Identifier& ident, FunctionMetadataNode* m_metadata, const SourceCode& source)
+ inline BaseFuncExprNode::BaseFuncExprNode(const JSTokenLocation& location, const Identifier& ident, FunctionMetadataNode* metadata, const SourceCode& source)
: ExpressionNode(location)
- , m_metadata(m_metadata)
+ , m_metadata(metadata)
{
m_metadata->finishParsing(source, ident, FunctionExpression);
}
- inline FuncExprNode::FuncExprNode(const JSTokenLocation& location, const Identifier& ident, FunctionMetadataNode* m_metadata, const SourceCode& source)
- : BaseFuncExprNode(location, ident, m_metadata, source)
+ inline FuncExprNode::FuncExprNode(const JSTokenLocation& location, const Identifier& ident, FunctionMetadataNode* metadata, const SourceCode& source)
+ : BaseFuncExprNode(location, ident, metadata, source)
{
}
- inline FuncDeclNode::FuncDeclNode(const JSTokenLocation& location, const Identifier& ident, FunctionMetadataNode* m_metadata, const SourceCode& source)
+ inline FuncDeclNode::FuncDeclNode(const JSTokenLocation& location, const Identifier& ident, FunctionMetadataNode* metadata, const SourceCode& source)
: StatementNode(location)
- , m_metadata(m_metadata)
+ , m_metadata(metadata)
{
m_metadata->finishParsing(source, ident, FunctionDeclaration);
}
- inline ArrowFuncExprNode::ArrowFuncExprNode(const JSTokenLocation& location, const Identifier& ident, FunctionMetadataNode* m_metadata, const SourceCode& source)
- : BaseFuncExprNode(location, ident, m_metadata, source)
+ inline ArrowFuncExprNode::ArrowFuncExprNode(const JSTokenLocation& location, const Identifier& ident, FunctionMetadataNode* metadata, const SourceCode& source)
+ : BaseFuncExprNode(location, ident, metadata, source)
{
}
@@ -927,10 +927,12 @@
{
}
- inline ClassExprNode::ClassExprNode(const JSTokenLocation& location, const Identifier& name, VariableEnvironment& classEnvironment, ExpressionNode* constructorExpression, ExpressionNode* classHeritage, PropertyListNode* instanceMethods, PropertyListNode* staticMethods)
+ inline ClassExprNode::ClassExprNode(const JSTokenLocation& location, const Identifier& name, const SourceCode& classSource, VariableEnvironment& classEnvironment, ExpressionNode* constructorExpression, ExpressionNode* classHeritage, PropertyListNode* instanceMethods, PropertyListNode* staticMethods)
: ExpressionNode(location)
, VariableEnvironmentNode(classEnvironment)
+ , m_classSource(classSource)
, m_name(name)
+ , m_ecmaName(&name)
, m_constructorExpression(constructorExpression)
, m_classHeritage(classHeritage)
, m_instanceMethods(instanceMethods)
diff --git a/Source/JavaScriptCore/parser/Nodes.h b/Source/JavaScriptCore/parser/Nodes.h
index 852ee86..9fa3f57 100644
--- a/Source/JavaScriptCore/parser/Nodes.h
+++ b/Source/JavaScriptCore/parser/Nodes.h
@@ -1,7 +1,7 @@
/*
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
- * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013, 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2003-2009, 2013, 2015-2016 Apple Inc. All rights reserved.
* Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
* Copyright (C) 2007 Maks Orlovich
* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
@@ -166,6 +166,7 @@
virtual bool isDestructuringNode() const { return false; }
virtual bool isFuncExprNode() const { return false; }
virtual bool isArrowFuncExprNode() const { return false; }
+ virtual bool isClassExprNode() const { return false; }
virtual bool isCommaNode() const { return false; }
virtual bool isSimpleArray() const { return false; }
virtual bool isAdd() const { return false; }
@@ -1844,7 +1845,7 @@
void overrideName(const Identifier& ident) { m_ident = ident; }
const Identifier& ident() { return m_ident; }
- void setEcmaName(const Identifier& ecmaName) { ASSERT(!ecmaName.isNull()); m_ecmaName = ecmaName; }
+ void setEcmaName(const Identifier& ecmaName) { m_ecmaName = ecmaName; }
const Identifier& ecmaName() { return m_ident.isEmpty() ? m_ecmaName : m_ident; }
void setInferredName(const Identifier& inferredName) { ASSERT(!inferredName.isNull()); m_inferredName = inferredName; }
const Identifier& inferredName() { return m_inferredName.isEmpty() ? m_ident : m_inferredName; }
@@ -1862,6 +1863,8 @@
void setEndPosition(JSTextPosition);
const SourceCode& source() const { return m_source; }
+ const SourceCode& classSource() const { return m_classSource; }
+ void setClassSource(const SourceCode& source) { m_classSource = source; }
int startStartOffset() const { return m_startStartOffset; }
bool isInStrictContext() const { return m_isInStrictContext; }
@@ -1888,6 +1891,7 @@
int m_functionNameStart;
int m_parametersStart;
SourceCode m_source;
+ SourceCode m_classSource;
int m_startStartOffset;
unsigned m_parameterCount;
int m_lastLine;
@@ -1974,15 +1978,22 @@
public:
using ParserArenaDeletable::operator new;
- ClassExprNode(const JSTokenLocation&, const Identifier&, VariableEnvironment& classEnvironment, ExpressionNode* constructorExpresssion,
+ ClassExprNode(const JSTokenLocation&, const Identifier&, const SourceCode& classSource,
+ VariableEnvironment& classEnvironment, ExpressionNode* constructorExpresssion,
ExpressionNode* parentClass, PropertyListNode* instanceMethods, PropertyListNode* staticMethods);
const Identifier& name() { return m_name; }
+ const Identifier& ecmaName() { return m_ecmaName ? *m_ecmaName : m_name; }
+ void setEcmaName(const Identifier& name) { m_ecmaName = m_name.isNull() ? &name : &m_name; }
private:
RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
+ bool isClassExprNode() const override { return true; }
+
+ SourceCode m_classSource;
const Identifier& m_name;
+ const Identifier* m_ecmaName;
ExpressionNode* m_constructorExpression;
ExpressionNode* m_classHeritage;
PropertyListNode* m_instanceMethods;
diff --git a/Source/JavaScriptCore/parser/Parser.cpp b/Source/JavaScriptCore/parser/Parser.cpp
index a657909..57f23ab 100644
--- a/Source/JavaScriptCore/parser/Parser.cpp
+++ b/Source/JavaScriptCore/parser/Parser.cpp
@@ -1,7 +1,7 @@
/*
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
- * Copyright (C) 2003, 2006, 2007, 2008, 2009, 2010, 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2003, 2006-2010, 2013, 2016 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@@ -2193,6 +2193,9 @@
{
ASSERT(match(CLASSTOKEN));
JSTokenLocation location(tokenLocation());
+ info.startLine = location.line;
+ info.startColumn = tokenColumn();
+ info.startOffset = location.startOffset;
next();
AutoPopScopeRef classScope(this, pushScope());
@@ -2203,7 +2206,6 @@
const Identifier* className = nullptr;
if (match(IDENT)) {
className = m_token.m_data.ident;
- info.className = className;
next();
failIfTrue(classScope->declareLexicalVariable(className, true) & DeclarationResult::InvalidStrictMode, "'", className->impl(), "' is not a valid class name");
} else if (requirements == FunctionNeedsName) {
@@ -2214,6 +2216,7 @@
} else
className = &m_vm->propertyNames->nullIdentifier;
ASSERT(className);
+ info.className = className;
TreeExpression parentClass = 0;
if (consume(EXTENDS)) {
@@ -2343,9 +2346,10 @@
}
}
+ info.endOffset = tokenLocation().endOffset - 1;
consumeOrFail(CLOSEBRACE, "Expected a closing '}' after a class body");
- auto classExpression = context.createClassExpr(location, *className, classScope->finalizeLexicalEnvironment(), constructor, parentClass, instanceMethods, staticMethods);
+ auto classExpression = context.createClassExpr(location, info, classScope->finalizeLexicalEnvironment(), constructor, parentClass, instanceMethods, staticMethods);
popScope(classScope, TreeBuilder::NeedsFreeVariableInfo);
return classExpression;
}
diff --git a/Source/JavaScriptCore/parser/ParserFunctionInfo.h b/Source/JavaScriptCore/parser/ParserFunctionInfo.h
index 4e8b652..43a3cba 100644
--- a/Source/JavaScriptCore/parser/ParserFunctionInfo.h
+++ b/Source/JavaScriptCore/parser/ParserFunctionInfo.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2015-2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -43,7 +43,11 @@
template <class TreeBuilder>
struct ParserClassInfo {
- const Identifier* className = 0;
+ const Identifier* className { nullptr };
+ unsigned startOffset { 0 };
+ unsigned endOffset { 0 };
+ int startLine { 0 };
+ unsigned startColumn { 0 };
};
}
diff --git a/Source/JavaScriptCore/parser/SyntaxChecker.h b/Source/JavaScriptCore/parser/SyntaxChecker.h
index ea60a4f..37b515d 100644
--- a/Source/JavaScriptCore/parser/SyntaxChecker.h
+++ b/Source/JavaScriptCore/parser/SyntaxChecker.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2010, 2013, 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -182,7 +182,7 @@
ExpressionType createEmptyLetExpression(const JSTokenLocation&, const Identifier&) { return AssignmentExpr; }
ExpressionType createYield(const JSTokenLocation&) { return YieldExpr; }
ExpressionType createYield(const JSTokenLocation&, ExpressionType, bool, int, int, int) { return YieldExpr; }
- ClassExpression createClassExpr(const JSTokenLocation&, const Identifier&, VariableEnvironment&, ExpressionType, ExpressionType, PropertyList, PropertyList) { return ClassExpr; }
+ ClassExpression createClassExpr(const JSTokenLocation&, const ParserClassInfo<SyntaxChecker>&, VariableEnvironment&, ExpressionType, ExpressionType, PropertyList, PropertyList) { return ClassExpr; }
ExpressionType createFunctionExpr(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&) { return FunctionExpr; }
int createFunctionMetadata(const JSTokenLocation&, const JSTokenLocation&, int, int, bool, int, int, int, ConstructorKind, SuperBinding, unsigned, SourceParseMode, bool, InnerArrowFunctionCodeFeatures = NoInnerArrowFunctionFeatures) { return FunctionBodyResult; }
ExpressionType createArrowFunctionExpr(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&) { return FunctionExpr; }
diff --git a/Source/JavaScriptCore/runtime/Executable.h b/Source/JavaScriptCore/runtime/Executable.h
index 5a01fc5..b6adffa 100644
--- a/Source/JavaScriptCore/runtime/Executable.h
+++ b/Source/JavaScriptCore/runtime/Executable.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009, 2010, 2013-2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2009, 2010, 2013-2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -662,6 +662,7 @@
FunctionMode functionMode() { return m_unlinkedExecutable->functionMode(); }
bool isBuiltinFunction() const { return m_unlinkedExecutable->isBuiltinFunction(); }
ConstructAbility constructAbility() const { return m_unlinkedExecutable->constructAbility(); }
+ bool isClass() const { return !classSource().isNull(); }
bool isArrowFunction() const { return parseMode() == SourceParseMode::ArrowFunctionMode; }
bool isGetter() const { return parseMode() == SourceParseMode::GetterMode; }
bool isSetter() const { return parseMode() == SourceParseMode::SetterMode; }
@@ -672,6 +673,7 @@
const Identifier& inferredName() { return m_unlinkedExecutable->inferredName(); }
size_t parameterCount() const { return m_unlinkedExecutable->parameterCount(); } // Excluding 'this'!
SourceParseMode parseMode() const { return m_unlinkedExecutable->parseMode(); }
+ const SourceCode& classSource() const { return m_unlinkedExecutable->classSource(); }
static void visitChildren(JSCell*, SlotVisitor&);
static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
diff --git a/Source/JavaScriptCore/runtime/FunctionPrototype.cpp b/Source/JavaScriptCore/runtime/FunctionPrototype.cpp
index 9140789..bd087ed 100644
--- a/Source/JavaScriptCore/runtime/FunctionPrototype.cpp
+++ b/Source/JavaScriptCore/runtime/FunctionPrototype.cpp
@@ -1,6 +1,6 @@
/*
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
- * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2003-2009, 2015-2016 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -89,7 +89,11 @@
return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(), "() {\n [native code]\n}"));
FunctionExecutable* executable = function->jsExecutable();
-
+ if (executable->isClass()) {
+ StringView classSource = executable->classSource().view();
+ return JSValue::encode(jsString(exec, classSource.toStringWithoutCopying()));
+ }
+
String functionHeader = executable->isArrowFunction() ? "" : "function ";
StringView source = executable->source().provider()->getRange(
diff --git a/Source/JavaScriptCore/tests/es6.yaml b/Source/JavaScriptCore/tests/es6.yaml
index 80b5547..3630374 100644
--- a/Source/JavaScriptCore/tests/es6.yaml
+++ b/Source/JavaScriptCore/tests/es6.yaml
@@ -801,7 +801,7 @@
- path: es6/function_name_property_isnt_writable_is_configurable.js
cmd: runES6 :normal
- path: es6/function_name_property_object_methods_class.js
- cmd: runES6 :fail
+ cmd: runES6 :normal
- path: es6/function_name_property_object_methods_function.js
cmd: runES6 :normal
- path: es6/function_name_property_shorthand_methods_no_lexical_binding.js
@@ -809,7 +809,7 @@
- path: es6/function_name_property_symbol-keyed_methods.js
cmd: runES6 :fail
- path: es6/function_name_property_variables_class.js
- cmd: runES6 :fail
+ cmd: runES6 :normal
- path: es6/function_name_property_variables_function.js
cmd: runES6 :normal
- path: es6/generators_%GeneratorPrototype%.constructor.js