Commit 1d962d08 authored by barraclough@apple.com's avatar barraclough@apple.com

Clarify var/const/function declaration

https://bugs.webkit.org/show_bug.cgi?id=120144

Reviewed by Sam Weinig.

Add methods to JSGlobalObject to declare vars, consts, and functions.

* runtime/Executable.cpp:
(JSC::ProgramExecutable::initializeGlobalProperties):
* runtime/Executable.h:
    - Moved declaration code to JSGlobalObject
* runtime/JSGlobalObject.cpp:
(JSC::JSGlobalObject::addGlobalVar):
    - internal implementation of addVar, addConst, addFunction
* runtime/JSGlobalObject.h:
(JSC::JSGlobalObject::addVar):
(JSC::JSGlobalObject::addConst):
(JSC::JSGlobalObject::addFunction):
    - Added methods to declare vars, consts, and functions



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@154434 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent af6cc59c
2013-08-21 Gavin Barraclough <barraclough@apple.com>
Clarify var/const/function declaration
https://bugs.webkit.org/show_bug.cgi?id=120144
Reviewed by Sam Weinig.
Add methods to JSGlobalObject to declare vars, consts, and functions.
* runtime/Executable.cpp:
(JSC::ProgramExecutable::initializeGlobalProperties):
* runtime/Executable.h:
- Moved declaration code to JSGlobalObject
* runtime/JSGlobalObject.cpp:
(JSC::JSGlobalObject::addGlobalVar):
- internal implementation of addVar, addConst, addFunction
* runtime/JSGlobalObject.h:
(JSC::JSGlobalObject::addVar):
(JSC::JSGlobalObject::addConst):
(JSC::JSGlobalObject::addFunction):
- Added methods to declare vars, consts, and functions
2013-08-21 Yi Shen <max.hong.shen@gmail.com> 2013-08-21 Yi Shen <max.hong.shen@gmail.com>
https://bugs.webkit.org/show_bug.cgi?id=119900 https://bugs.webkit.org/show_bug.cgi?id=119900
......
...@@ -371,24 +371,6 @@ void ProgramExecutable::unlinkCalls() ...@@ -371,24 +371,6 @@ void ProgramExecutable::unlinkCalls()
#endif #endif
} }
int ProgramExecutable::addGlobalVar(JSGlobalObject* globalObject, const Identifier& ident, ConstantMode constantMode, FunctionMode functionMode)
{
// Try to share the symbolTable if possible
SharedSymbolTable* symbolTable = globalObject->symbolTable();
UNUSED_PARAM(functionMode);
ConcurrentJITLocker locker(symbolTable->m_lock);
int index = symbolTable->size(locker);
SymbolTableEntry newEntry(index, (constantMode == IsConstant) ? ReadOnly : 0);
if (functionMode == IsFunctionToSpecialize)
newEntry.attemptToWatch();
SymbolTable::Map::AddResult result = symbolTable->add(locker, ident.impl(), newEntry);
if (!result.isNewEntry) {
result.iterator->value.notifyWrite();
index = result.iterator->value.getIndex();
}
return index;
}
JSObject* ProgramExecutable::initializeGlobalProperties(VM& vm, CallFrame* callFrame, JSScope* scope) JSObject* ProgramExecutable::initializeGlobalProperties(VM& vm, CallFrame* callFrame, JSScope* scope)
{ {
RELEASE_ASSERT(scope); RELEASE_ASSERT(scope);
...@@ -415,20 +397,16 @@ JSObject* ProgramExecutable::initializeGlobalProperties(VM& vm, CallFrame* callF ...@@ -415,20 +397,16 @@ JSObject* ProgramExecutable::initializeGlobalProperties(VM& vm, CallFrame* callF
CallFrame* globalExec = globalObject->globalExec(); CallFrame* globalExec = globalObject->globalExec();
for (size_t i = 0; i < functionDeclarations.size(); ++i) { for (size_t i = 0; i < functionDeclarations.size(); ++i) {
bool propertyDidExist = globalObject->removeDirect(vm, functionDeclarations[i].first); // Newly declared functions overwrite existing properties.
UnlinkedFunctionExecutable* unlinkedFunctionExecutable = functionDeclarations[i].second.get(); UnlinkedFunctionExecutable* unlinkedFunctionExecutable = functionDeclarations[i].second.get();
JSValue value = JSFunction::create(globalExec, unlinkedFunctionExecutable->link(vm, m_source, lineNo(), 0), scope); JSValue value = JSFunction::create(globalExec, unlinkedFunctionExecutable->link(vm, m_source, lineNo(), 0), scope);
int index = addGlobalVar(globalObject, functionDeclarations[i].first, IsVariable, globalObject->addFunction(callFrame, functionDeclarations[i].first, value);
!propertyDidExist ? IsFunctionToSpecialize : NotFunctionOrNotSpecializable);
globalObject->registerAt(index).set(vm, globalObject, value);
} }
for (size_t i = 0; i < variableDeclarations.size(); ++i) { for (size_t i = 0; i < variableDeclarations.size(); ++i) {
if (globalObject->hasProperty(globalExec, variableDeclarations[i].first)) if (variableDeclarations[i].second & DeclarationStacks::IsConstant)
continue; globalObject->addConst(callFrame, variableDeclarations[i].first);
addGlobalVar(globalObject, variableDeclarations[i].first, else
(variableDeclarations[i].second & DeclarationStacks::IsConstant) ? IsConstant : IsVariable, globalObject->addVar(callFrame, variableDeclarations[i].first);
NotFunctionOrNotSpecializable);
} }
return 0; return 0;
} }
......
...@@ -561,10 +561,6 @@ namespace JSC { ...@@ -561,10 +561,6 @@ namespace JSC {
ProgramExecutable(ExecState*, const SourceCode&); ProgramExecutable(ExecState*, const SourceCode&);
enum ConstantMode { IsConstant, IsVariable };
enum FunctionMode { IsFunctionToSpecialize, NotFunctionOrNotSpecializable };
int addGlobalVar(JSGlobalObject*, const Identifier&, ConstantMode, FunctionMode);
JSObject* compileInternal(ExecState*, JSScope*, JITCode::JITType, CompilationResult* = 0, unsigned bytecodeIndex = UINT_MAX); JSObject* compileInternal(ExecState*, JSScope*, JITCode::JITType, CompilationResult* = 0, unsigned bytecodeIndex = UINT_MAX);
static void visitChildren(JSCell*, SlotVisitor&); static void visitChildren(JSCell*, SlotVisitor&);
......
...@@ -196,6 +196,20 @@ bool JSGlobalObject::defineOwnProperty(JSObject* object, ExecState* exec, Proper ...@@ -196,6 +196,20 @@ bool JSGlobalObject::defineOwnProperty(JSObject* object, ExecState* exec, Proper
return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow); return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow);
} }
int JSGlobalObject::addGlobalVar(const Identifier& ident, ConstantMode constantMode, FunctionMode functionMode)
{
ConcurrentJITLocker locker(symbolTable()->m_lock);
int index = symbolTable()->size(locker);
SymbolTableEntry newEntry(index, (constantMode == IsConstant) ? ReadOnly : 0);
if (functionMode == IsFunctionToSpecialize)
newEntry.attemptToWatch();
SymbolTable::Map::AddResult result = symbolTable()->add(locker, ident.impl(), newEntry);
if (!result.isNewEntry) {
result.iterator->value.notifyWrite();
index = result.iterator->value.getIndex();
}
return index;
}
static inline JSObject* lastInPrototypeChain(JSObject* object) static inline JSObject* lastInPrototypeChain(JSObject* object)
{ {
......
...@@ -239,6 +239,10 @@ protected: ...@@ -239,6 +239,10 @@ protected:
init(thisValue); init(thisValue);
} }
enum ConstantMode { IsConstant, IsVariable };
enum FunctionMode { IsFunctionToSpecialize, NotFunctionOrNotSpecializable };
int addGlobalVar(const Identifier&, ConstantMode, FunctionMode);
public: public:
JS_EXPORT_PRIVATE ~JSGlobalObject(); JS_EXPORT_PRIVATE ~JSGlobalObject();
JS_EXPORT_PRIVATE static void destroy(JSCell*); JS_EXPORT_PRIVATE static void destroy(JSCell*);
...@@ -261,6 +265,23 @@ public: ...@@ -261,6 +265,23 @@ public:
// lookups prior to initializing the properties // lookups prior to initializing the properties
bool symbolTableHasProperty(PropertyName); bool symbolTableHasProperty(PropertyName);
void addVar(ExecState* exec, const Identifier& propertyName)
{
if (!hasProperty(exec, propertyName))
addGlobalVar(propertyName, IsVariable, NotFunctionOrNotSpecializable);
}
void addConst(ExecState* exec, const Identifier& propertyName)
{
if (!hasProperty(exec, propertyName))
addGlobalVar(propertyName, IsConstant, NotFunctionOrNotSpecializable);
}
void addFunction(ExecState* exec, const Identifier& propertyName, JSValue value)
{
bool propertyDidExist = removeDirect(exec->vm(), propertyName); // Newly declared functions overwrite existing properties.
int index = addGlobalVar(propertyName, IsVariable, !propertyDidExist ? IsFunctionToSpecialize : NotFunctionOrNotSpecializable);
registerAt(index).set(exec->vm(), this, value);
}
// The following accessors return pristine values, even if a script // The following accessors return pristine values, even if a script
// replaces the global object's associated property. // replaces the global object's associated property.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment