Ignore:
Timestamp:
Dec 25, 2016, 10:35:07 PM (8 years ago)
Author:
Yusuke Suzuki
Message:

Propagate the source origin as much as possible
https://bugs.webkit.org/show_bug.cgi?id=166348

Reviewed by Darin Adler.

JSTests:

  • stress/source-origin.js: Added.

(shouldBe):

Source/JavaScriptCore:

This patch introduces CallFrame::callerSourceOrigin, SourceOrigin class
and SourceProvider::m_sourceOrigin. CallFrame::callerSourceOrigin returns
an appropriate SourceOrigin if possible. If we cannot find the appropriate
one, we just return null SourceOrigin.

This paves the way for implementing the module dynamic-import[1].
When the import operator is evaluated, it will resolve the module
specifier with this propagated source origin of the caller function.

To support import operator inside the dynamic code generation
functions (like eval, new Function, indirect call to eval),
we need to propagate the caller's source origin to the generated
source code.

We do not use sourceURL for that purpose. This is because we
would like to keep sourceURL for eval / new Function null.
This sourceURL will be used for the stack dump for errors with line/column
numbers. Dumping the caller's sourceURL with line/column numbers are
meaningless. So we would like to keep it null while we would like
to propagate SourceOrigin for dynamic imports.

[1]: https://github.com/tc39/proposal-dynamic-import

  • API/JSBase.cpp:

(JSEvaluateScript):
(JSCheckScriptSyntax):

  • API/JSObjectRef.cpp:

(JSObjectMakeFunction):

  • API/JSScriptRef.cpp:

(OpaqueJSScript::create):
(OpaqueJSScript::vm):
(OpaqueJSScript::OpaqueJSScript):
(parseScript):

  • JavaScriptCore.xcodeproj/project.pbxproj:
  • Scripts/builtins/builtins_templates.py:
  • Scripts/tests/builtins/expected/WebCore-AnotherGuardedInternalBuiltin-Separate.js-result:
  • Scripts/tests/builtins/expected/WebCore-ArbitraryConditionalGuard-Separate.js-result:
  • Scripts/tests/builtins/expected/WebCore-GuardedBuiltin-Separate.js-result:
  • Scripts/tests/builtins/expected/WebCore-GuardedInternalBuiltin-Separate.js-result:
  • Scripts/tests/builtins/expected/WebCore-UnguardedBuiltin-Separate.js-result:
  • Scripts/tests/builtins/expected/WebCore-xmlCasingTest-Separate.js-result:
  • builtins/BuiltinExecutables.cpp:

(JSC::BuiltinExecutables::BuiltinExecutables):
(JSC::BuiltinExecutables::createDefaultConstructor):

  • debugger/DebuggerCallFrame.cpp:

(JSC::DebuggerCallFrame::evaluateWithScopeExtension):

  • inspector/InjectedScriptManager.cpp:

(Inspector::InjectedScriptManager::createInjectedScript):

  • inspector/JSInjectedScriptHost.cpp:

(Inspector::JSInjectedScriptHost::evaluateWithScopeExtension):

  • inspector/agents/InspectorRuntimeAgent.cpp:

(Inspector::InspectorRuntimeAgent::parse):

  • interpreter/CallFrame.cpp:

(JSC::CallFrame::callerSourceOrigin):

  • interpreter/CallFrame.h:
  • interpreter/Interpreter.cpp:

(JSC::eval):

  • jsc.cpp:

(jscSource):
(GlobalObject::finishCreation):
(extractDirectoryName):
(currentWorkingDirectory):
(GlobalObject::moduleLoaderResolve):
(functionRunString):
(functionLoadString):
(functionCallerSourceOrigin):
(functionCreateBuiltin):
(functionCheckModuleSyntax):
(runInteractive):

  • parser/SourceCode.h:

(JSC::makeSource):

  • parser/SourceProvider.cpp:

(JSC::SourceProvider::SourceProvider):

  • parser/SourceProvider.h:

(JSC::SourceProvider::sourceOrigin):
(JSC::StringSourceProvider::create):
(JSC::StringSourceProvider::StringSourceProvider):
(JSC::WebAssemblySourceProvider::create):
(JSC::WebAssemblySourceProvider::WebAssemblySourceProvider):

  • runtime/FunctionConstructor.cpp:

(JSC::constructFunction):
(JSC::constructFunctionSkippingEvalEnabledCheck):

  • runtime/FunctionConstructor.h:
  • runtime/JSGlobalObjectFunctions.cpp:

(JSC::globalFuncEval):

  • runtime/ModuleLoaderPrototype.cpp:

(JSC::moduleLoaderPrototypeParseModule):

  • runtime/ScriptExecutable.h:

(JSC::ScriptExecutable::sourceOrigin):

  • runtime/SourceOrigin.h: Added.

(JSC::SourceOrigin::SourceOrigin):
(JSC::SourceOrigin::string):
(JSC::SourceOrigin::isNull):

  • tools/FunctionOverrides.cpp:

(JSC::initializeOverrideInfo):

Source/WebCore:

  • bindings/js/CachedScriptSourceProvider.h:

(WebCore::CachedScriptSourceProvider::CachedScriptSourceProvider):

  • bindings/js/JSLazyEventListener.cpp:

(WebCore::JSLazyEventListener::initializeJSFunction):

  • bindings/js/ScriptSourceCode.h:

(WebCore::ScriptSourceCode::ScriptSourceCode):

  • bridge/NP_jsobject.cpp:

(_NPN_Evaluate):

  • bridge/objc/WebScriptObject.mm:

(-[WebScriptObject evaluateWebScript:]):

Source/WebKit/mac:

  • Plugins/Hosted/NetscapePluginInstanceProxy.mm:

(WebKit::NetscapePluginInstanceProxy::evaluate):

Source/WebKit/win:

  • Plugins/PluginPackage.cpp:

(WebCore::NPN_Evaluate):
(WebCore::makeSource): Deleted.

Source/WebKit2:

  • WebProcess/Plugins/Netscape/NPRuntimeObjectMap.cpp:

(WebKit::NPRuntimeObjectMap::evaluate):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/API/JSScriptRef.cpp

    r209627 r210149  
    4242struct OpaqueJSScript : public SourceProvider {
    4343public:
    44     static WTF::RefPtr<OpaqueJSScript> create(VM* vm, const String& url, int startingLineNumber, const String& source)
     44    static WTF::RefPtr<OpaqueJSScript> create(VM& vm, const SourceOrigin& sourceOrigin, const String& url, int startingLineNumber, const String& source)
    4545    {
    46         return WTF::adoptRef(*new OpaqueJSScript(vm, url, startingLineNumber, source));
     46        return WTF::adoptRef(*new OpaqueJSScript(vm, sourceOrigin, url, startingLineNumber, source));
    4747    }
    4848
     
    5757    }
    5858
    59     VM* vm() const { return m_vm; }
     59    VM& vm() const { return m_vm; }
    6060
    6161private:
    62     OpaqueJSScript(VM* vm, const String& url, int startingLineNumber, const String& source)
    63         : SourceProvider(url, TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber()), SourceProviderSourceType::Program)
     62    OpaqueJSScript(VM& vm, const SourceOrigin& sourceOrigin, const String& url, int startingLineNumber, const String& source)
     63        : SourceProvider(sourceOrigin, url, TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber()), SourceProviderSourceType::Program)
    6464        , m_vm(vm)
    6565        , m_source(source.isNull() ? *StringImpl::empty() : *source.impl())
     
    6969    virtual ~OpaqueJSScript() { }
    7070
    71     VM* m_vm;
     71    VM& m_vm;
    7272    Ref<StringImpl> m_source;
    7373};
    7474
    75 static bool parseScript(VM* vm, const SourceCode& source, ParserError& error)
     75static bool parseScript(VM& vm, const SourceCode& source, ParserError& error)
    7676{
    7777    return !!JSC::parse<JSC::ProgramNode>(
    78         vm, source, Identifier(), JSParserBuiltinMode::NotBuiltin,
     78        &vm, source, Identifier(), JSParserBuiltinMode::NotBuiltin,
    7979        JSParserStrictMode::NotStrict, JSParserScriptMode::Classic, SourceParseMode::ProgramMode, SuperBinding::NotNeeded,
    8080        error);
     
    8585JSScriptRef JSScriptCreateReferencingImmortalASCIIText(JSContextGroupRef contextGroup, JSStringRef url, int startingLineNumber, const char* source, size_t length, JSStringRef* errorMessage, int* errorLine)
    8686{
    87     VM* vm = toJS(contextGroup);
    88     JSLockHolder locker(vm);
     87    auto& vm = *toJS(contextGroup);
     88    JSLockHolder locker(&vm);
    8989    for (size_t i = 0; i < length; i++) {
    9090        if (!isASCII(source[i]))
     
    9494    startingLineNumber = std::max(1, startingLineNumber);
    9595
    96     auto result = OpaqueJSScript::create(vm, url ? url->string() : String(), startingLineNumber, String(StringImpl::createFromLiteral(source, length)));
     96    auto sourceURLString = url ? url->string() : String();
     97    auto result = OpaqueJSScript::create(vm, SourceOrigin { sourceURLString }, sourceURLString, startingLineNumber, String(StringImpl::createFromLiteral(source, length)));
    9798
    9899    ParserError error;
     
    110111JSScriptRef JSScriptCreateFromString(JSContextGroupRef contextGroup, JSStringRef url, int startingLineNumber, JSStringRef source, JSStringRef* errorMessage, int* errorLine)
    111112{
    112     VM* vm = toJS(contextGroup);
    113     JSLockHolder locker(vm);
     113    auto& vm = *toJS(contextGroup);
     114    JSLockHolder locker(&vm);
    114115
    115116    startingLineNumber = std::max(1, startingLineNumber);
    116117
    117     auto result = OpaqueJSScript::create(vm, url ? url->string() : String(), startingLineNumber, source->string());
     118    auto sourceURLString = url ? url->string() : String();
     119    auto result = OpaqueJSScript::create(vm, SourceOrigin { sourceURLString }, sourceURLString, startingLineNumber, source->string());
    118120
    119121    ParserError error;
     
    131133void JSScriptRetain(JSScriptRef script)
    132134{
    133     JSLockHolder locker(script->vm());
     135    JSLockHolder locker(&script->vm());
    134136    script->ref();
    135137}
     
    137139void JSScriptRelease(JSScriptRef script)
    138140{
    139     JSLockHolder locker(script->vm());
     141    JSLockHolder locker(&script->vm());
    140142    script->deref();
    141143}
     
    145147    ExecState* exec = toJS(context);
    146148    JSLockHolder locker(exec);
    147     if (script->vm() != &exec->vm()) {
     149    if (&script->vm() != &exec->vm()) {
    148150        RELEASE_ASSERT_NOT_REACHED();
    149151        return 0;
Note: See TracChangeset for help on using the changeset viewer.