+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);
+ int index = symbolTable->size();
+ SymbolTableEntry newEntry(index, (constantMode == IsConstant) ? ReadOnly : 0);
+ if (functionMode == IsFunctionToSpecialize)
+ newEntry.attemptToWatch();
+ SymbolTable::AddResult result = symbolTable->add(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)
+{
+ RELEASE_ASSERT(scope);
+ JSGlobalObject* globalObject = scope->globalObject();
+ RELEASE_ASSERT(globalObject);
+ ASSERT(&globalObject->vm() == &vm);
+
+ JSObject* exception = 0;
+ UnlinkedProgramCodeBlock* unlinkedCode = globalObject->createProgramCodeBlock(callFrame, this, &exception);
+ if (exception)
+ return exception;
+
+ m_unlinkedProgramCodeBlock.set(vm, this, unlinkedCode);
+
+ BatchedTransitionOptimizer optimizer(vm, globalObject);
+
+ const UnlinkedProgramCodeBlock::VariableDeclations& variableDeclarations = unlinkedCode->variableDeclarations();
+ const UnlinkedProgramCodeBlock::FunctionDeclations& functionDeclarations = unlinkedCode->functionDeclarations();
+
+ size_t newGlobals = variableDeclarations.size() + functionDeclarations.size();
+ if (!newGlobals)
+ return 0;
+ globalObject->addRegisters(newGlobals);
+ CallFrame* globalExec = globalObject->globalExec();
+
+ 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();
+ JSValue value = JSFunction::create(globalExec, unlinkedFunctionExecutable->link(vm, m_source, lineNo(), 0), scope);
+ int index = addGlobalVar(globalObject, functionDeclarations[i].first, IsVariable,
+ !propertyDidExist ? IsFunctionToSpecialize : NotFunctionOrNotSpecializable);
+ globalObject->registerAt(index).set(vm, globalObject, value);
+ }
+
+ for (size_t i = 0; i < variableDeclarations.size(); ++i) {
+ if (globalObject->hasProperty(globalExec, variableDeclarations[i].first))
+ continue;
+ addGlobalVar(globalObject, variableDeclarations[i].first,
+ (variableDeclarations[i].second & DeclarationStacks::IsConstant) ? IsConstant : IsVariable,
+ NotFunctionOrNotSpecializable);
+ }
+ return 0;
+}
+