diff options
Diffstat (limited to 'Source/JavaScriptCore/runtime/Completion.cpp')
-rw-r--r-- | Source/JavaScriptCore/runtime/Completion.cpp | 206 |
1 files changed, 181 insertions, 25 deletions
diff --git a/Source/JavaScriptCore/runtime/Completion.cpp b/Source/JavaScriptCore/runtime/Completion.cpp index 9d7fd1a74..1be8cf8ff 100644 --- a/Source/JavaScriptCore/runtime/Completion.cpp +++ b/Source/JavaScriptCore/runtime/Completion.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, 2007, 2013 Apple Inc. + * Copyright (C) 2003, 2007, 2013, 2016 Apple Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -25,12 +25,21 @@ #include "CallFrame.h" #include "CodeProfiling.h" -#include "Debugger.h" +#include "Exception.h" +#include "IdentifierInlines.h" #include "Interpreter.h" +#include "JSCInlines.h" #include "JSGlobalObject.h" +#include "JSInternalPromise.h" +#include "JSInternalPromiseDeferred.h" #include "JSLock.h" -#include "Operations.h" +#include "JSModuleLoader.h" +#include "JSModuleRecord.h" +#include "JSWithScope.h" +#include "ModuleAnalyzer.h" #include "Parser.h" +#include "ProgramExecutable.h" +#include "ScriptProfilingScope.h" #include <wtf/WTFThreadData.h> namespace JSC { @@ -38,7 +47,7 @@ namespace JSC { bool checkSyntax(ExecState* exec, const SourceCode& source, JSValue* returnedException) { JSLockHolder lock(exec); - RELEASE_ASSERT(exec->vm().identifierTable == wtfThreadData().currentIdentifierTable()); + RELEASE_ASSERT(exec->vm().atomicStringTable() == wtfThreadData().atomicStringTable()); ProgramExecutable* program = ProgramExecutable::create(exec, source); JSObject* error = program->checkSyntax(exec); @@ -54,38 +63,47 @@ bool checkSyntax(ExecState* exec, const SourceCode& source, JSValue* returnedExc bool checkSyntax(VM& vm, const SourceCode& source, ParserError& error) { JSLockHolder lock(vm); - RELEASE_ASSERT(vm.identifierTable == wtfThreadData().currentIdentifierTable()); - RefPtr<ProgramNode> programNode = parse<ProgramNode>(&vm, source, 0, Identifier(), JSParseNormal, JSParseProgramCode, error); - return programNode; + RELEASE_ASSERT(vm.atomicStringTable() == wtfThreadData().atomicStringTable()); + return !!parse<ProgramNode>( + &vm, source, Identifier(), JSParserBuiltinMode::NotBuiltin, + JSParserStrictMode::NotStrict, JSParserScriptMode::Classic, SourceParseMode::ProgramMode, SuperBinding::NotNeeded, error); } -JSValue evaluate(ExecState* exec, const SourceCode& source, JSValue thisValue, JSValue* returnedException) +bool checkModuleSyntax(ExecState* exec, const SourceCode& source, ParserError& error) { - JSLockHolder lock(exec); - RELEASE_ASSERT(exec->vm().identifierTable == wtfThreadData().currentIdentifierTable()); - RELEASE_ASSERT(!exec->vm().isCollectorBusy()); + VM& vm = exec->vm(); + JSLockHolder lock(vm); + RELEASE_ASSERT(vm.atomicStringTable() == wtfThreadData().atomicStringTable()); + std::unique_ptr<ModuleProgramNode> moduleProgramNode = parse<ModuleProgramNode>( + &vm, source, Identifier(), JSParserBuiltinMode::NotBuiltin, + JSParserStrictMode::Strict, JSParserScriptMode::Module, SourceParseMode::ModuleAnalyzeMode, SuperBinding::NotNeeded, error); + if (!moduleProgramNode) + return false; - CodeProfiling profile(source); + PrivateName privateName(PrivateName::Description, "EntryPointModule"); + ModuleAnalyzer moduleAnalyzer(exec, Identifier::fromUid(privateName), source, moduleProgramNode->varDeclarations(), moduleProgramNode->lexicalVariables()); + moduleAnalyzer.analyze(*moduleProgramNode); + return true; +} - ProgramExecutable* program = ProgramExecutable::create(exec, source); - if (!program) { - if (returnedException) - *returnedException = exec->vm().exception(); +JSValue evaluate(ExecState* exec, const SourceCode& source, JSValue thisValue, NakedPtr<Exception>& returnedException) +{ + VM& vm = exec->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + RELEASE_ASSERT(vm.atomicStringTable() == wtfThreadData().atomicStringTable()); + RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread()); - exec->vm().clearException(); - return jsUndefined(); - } + CodeProfiling profile(source); if (!thisValue || thisValue.isUndefinedOrNull()) thisValue = exec->vmEntryGlobalObject(); JSObject* thisObj = jsCast<JSObject*>(thisValue.toThis(exec, NotStrictMode)); - JSValue result = exec->interpreter()->execute(program, exec, thisObj); - - if (exec->hadException()) { - if (returnedException) - *returnedException = exec->exception(); + JSValue result = exec->interpreter()->executeProgram(source, exec, thisObj); - exec->clearException(); + if (scope.exception()) { + returnedException = scope.exception(); + scope.clearException(); return jsUndefined(); } @@ -93,4 +111,142 @@ JSValue evaluate(ExecState* exec, const SourceCode& source, JSValue thisValue, J return result; } +JSValue profiledEvaluate(ExecState* exec, ProfilingReason reason, const SourceCode& source, JSValue thisValue, NakedPtr<Exception>& returnedException) +{ + ScriptProfilingScope profilingScope(exec->vmEntryGlobalObject(), reason); + return evaluate(exec, source, thisValue, returnedException); +} + +JSValue evaluateWithScopeExtension(ExecState* exec, const SourceCode& source, JSObject* scopeExtensionObject, NakedPtr<Exception>& returnedException) +{ + JSGlobalObject* globalObject = exec->vmEntryGlobalObject(); + + if (scopeExtensionObject) { + JSScope* ignoredPreviousScope = globalObject->globalScope(); + globalObject->setGlobalScopeExtension(JSWithScope::create(exec->vm(), globalObject, scopeExtensionObject, ignoredPreviousScope)); + } + + JSValue returnValue = JSC::evaluate(globalObject->globalExec(), source, globalObject, returnedException); + + if (scopeExtensionObject) + globalObject->clearGlobalScopeExtension(); + + return returnValue; +} + +static Symbol* createSymbolForEntryPointModule(VM& vm) +{ + // Generate the unique key for the source-provided module. + PrivateName privateName(PrivateName::Description, "EntryPointModule"); + return Symbol::create(vm, privateName.uid()); +} + +static JSInternalPromise* rejectPromise(ExecState* exec, JSGlobalObject* globalObject) +{ + VM& vm = exec->vm(); + auto scope = DECLARE_CATCH_SCOPE(vm); + ASSERT(scope.exception()); + JSValue exception = scope.exception()->value(); + scope.clearException(); + JSInternalPromiseDeferred* deferred = JSInternalPromiseDeferred::create(exec, globalObject); + deferred->reject(exec, exception); + return deferred->promise(); +} + +static JSInternalPromise* loadAndEvaluateModule(const JSLockHolder&, ExecState* exec, JSGlobalObject* globalObject, JSValue moduleName, JSValue referrer, JSValue scriptFetcher) +{ + return globalObject->moduleLoader()->loadAndEvaluateModule(exec, moduleName, referrer, scriptFetcher); +} + +static JSInternalPromise* loadAndEvaluateModule(const JSLockHolder& lock, ExecState* exec, JSGlobalObject* globalObject, const Identifier& moduleName, JSValue scriptFetcher) +{ + return loadAndEvaluateModule(lock, exec, globalObject, identifierToJSValue(exec->vm(), moduleName), jsUndefined(), scriptFetcher); +} + +JSInternalPromise* loadAndEvaluateModule(ExecState* exec, const String& moduleName, JSValue scriptFetcher) +{ + JSLockHolder lock(exec); + RELEASE_ASSERT(exec->vm().atomicStringTable() == wtfThreadData().atomicStringTable()); + RELEASE_ASSERT(!exec->vm().isCollectorBusyOnCurrentThread()); + + return loadAndEvaluateModule(lock, exec, exec->vmEntryGlobalObject(), Identifier::fromString(exec, moduleName), scriptFetcher); +} + +JSInternalPromise* loadAndEvaluateModule(ExecState* exec, const SourceCode& source, JSValue scriptFetcher) +{ + VM& vm = exec->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_THROW_SCOPE(vm); + RELEASE_ASSERT(vm.atomicStringTable() == wtfThreadData().atomicStringTable()); + RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread()); + + Symbol* key = createSymbolForEntryPointModule(vm); + + JSGlobalObject* globalObject = exec->vmEntryGlobalObject(); + + // Insert the given source code to the ModuleLoader registry as the fetched registry entry. + globalObject->moduleLoader()->provide(exec, key, JSModuleLoader::Status::Fetch, source); + RETURN_IF_EXCEPTION(scope, rejectPromise(exec, globalObject)); + + return loadAndEvaluateModule(lock, exec, globalObject, key, jsUndefined(), scriptFetcher); +} + +static JSInternalPromise* loadModule(const JSLockHolder&, ExecState* exec, JSGlobalObject* globalObject, JSValue moduleName, JSValue referrer, JSValue scriptFetcher) +{ + return globalObject->moduleLoader()->loadModule(exec, moduleName, referrer, scriptFetcher); +} + +static JSInternalPromise* loadModule(const JSLockHolder& lock, ExecState* exec, JSGlobalObject* globalObject, const Identifier& moduleName, JSValue scriptFetcher) +{ + return loadModule(lock, exec, globalObject, identifierToJSValue(exec->vm(), moduleName), jsUndefined(), scriptFetcher); +} + +JSInternalPromise* loadModule(ExecState* exec, const String& moduleName, JSValue scriptFetcher) +{ + JSLockHolder lock(exec); + RELEASE_ASSERT(exec->vm().atomicStringTable() == wtfThreadData().atomicStringTable()); + RELEASE_ASSERT(!exec->vm().isCollectorBusyOnCurrentThread()); + + return loadModule(lock, exec, exec->vmEntryGlobalObject(), Identifier::fromString(exec, moduleName), scriptFetcher); +} + +JSInternalPromise* loadModule(ExecState* exec, const SourceCode& source, JSValue scriptFetcher) +{ + VM& vm = exec->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_THROW_SCOPE(vm); + RELEASE_ASSERT(vm.atomicStringTable() == wtfThreadData().atomicStringTable()); + RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread()); + + Symbol* key = createSymbolForEntryPointModule(vm); + + JSGlobalObject* globalObject = exec->vmEntryGlobalObject(); + + // Insert the given source code to the ModuleLoader registry as the fetched registry entry. + // FIXME: Introduce JSSourceCode object to wrap around this source. + globalObject->moduleLoader()->provide(exec, key, JSModuleLoader::Status::Fetch, source); + RETURN_IF_EXCEPTION(scope, rejectPromise(exec, globalObject)); + + return loadModule(lock, exec, globalObject, key, jsUndefined(), scriptFetcher); +} + +JSValue linkAndEvaluateModule(ExecState* exec, const Identifier& moduleKey, JSValue scriptFetcher) +{ + JSLockHolder lock(exec); + RELEASE_ASSERT(exec->vm().atomicStringTable() == wtfThreadData().atomicStringTable()); + RELEASE_ASSERT(!exec->vm().isCollectorBusyOnCurrentThread()); + + JSGlobalObject* globalObject = exec->vmEntryGlobalObject(); + return globalObject->moduleLoader()->linkAndEvaluateModule(exec, identifierToJSValue(exec->vm(), moduleKey), scriptFetcher); +} + +JSInternalPromise* importModule(ExecState* exec, const Identifier& moduleKey, JSValue scriptFetcher) +{ + JSLockHolder lock(exec); + RELEASE_ASSERT(exec->vm().atomicStringTable() == wtfThreadData().atomicStringTable()); + RELEASE_ASSERT(!exec->vm().isCollectorBusyOnCurrentThread()); + + return exec->vmEntryGlobalObject()->moduleLoader()->requestImportModule(exec, moduleKey, scriptFetcher); +} + } // namespace JSC |