Changeset 207618 in webkit


Ignore:
Timestamp:
Oct 20, 2016 8:00:43 AM (8 years ago)
Author:
caitp@igalia.com
Message:

[JSC] throw TypeError when constructing dynamically created JSGeneratorFunction
https://bugs.webkit.org/show_bug.cgi?id=163714

Reviewed by Mark Lam.

JSTests:

Add missing test coverage that dynamically created
JSGeneratorFunctions can not be constructed.

  • stress/generator-function-constructor.js:

(shouldThrow):

Source/JavaScriptCore:

According to CreateDynamicFunction() (https://tc39.github.io/ecma262/#sec-createdynamicfunction),
non-normal functions are not constructors. Previously, dynamically created functions would always
be constructible, and so it was possible to evaluate new (function*() {}.constructor()),
and have it return an Iterator object.

This change selects a dynamically created function's ConstructAbility based on its parse mode instead.

  • runtime/CodeCache.cpp:

(JSC::CodeCache::getFunctionExecutableFromGlobalCode):

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/JSTests/ChangeLog

    r207572 r207618  
     12016-10-20  Caitlin Potter  <caitp@igalia.com>
     2
     3        [JSC] throw TypeError when constructing dynamically created JSGeneratorFunction
     4        https://bugs.webkit.org/show_bug.cgi?id=163714
     5
     6        Reviewed by Mark Lam.
     7
     8        Add missing test coverage that dynamically created
     9        JSGeneratorFunctions can not be constructed.
     10
     11        * stress/generator-function-constructor.js:
     12        (shouldThrow):
     13
    1142016-10-19  JF Bastien  <jfbastien@apple.com>
    215
  • trunk/JSTests/stress/generator-function-constructor.js

    r206710 r207618  
    33        throw new Error('bad value: ' + actual);
    44}
     5
     6function shouldThrow(func, errorMessage) {
     7    var errorThrown = false;
     8    var error = null;
     9    try {
     10        func();
     11    } catch (e) {
     12        errorThrown = true;
     13        error = e;
     14    }
     15    if (!errorThrown)
     16        throw new Error('not thrown');
     17    if (String(error) !== errorMessage)
     18        throw new Error(`bad error: ${String(error)}`);
     19}
     20
    521var global = (new Function("return this"))();
    622shouldBe(typeof global.GeneratorFunction, 'undefined');
     
    1127shouldBe(generatorFunctionConstructor("a") instanceof generatorFunctionConstructor, true);
    1228shouldBe(generatorFunctionConstructor("a", "b") instanceof generatorFunctionConstructor, true);
     29
     30// Generator functions created by the GeneratorFunction constructor are not themselves constructors.
     31shouldThrow(() => new (generatorFunctionConstructor()), "TypeError: function is not a constructor (evaluating 'new (generatorFunctionConstructor())')");
  • trunk/Source/JavaScriptCore/ChangeLog

    r207576 r207618  
     12016-10-20  Caitlin Potter  <caitp@igalia.com>
     2
     3        [JSC] throw TypeError when constructing dynamically created JSGeneratorFunction
     4        https://bugs.webkit.org/show_bug.cgi?id=163714
     5
     6        Reviewed by Mark Lam.
     7
     8        According to CreateDynamicFunction() (https://tc39.github.io/ecma262/#sec-createdynamicfunction),
     9        non-normal functions are not constructors. Previously, dynamically created functions would always
     10        be constructible, and so it was possible to evaluate `new  (function*() {}.constructor())`,
     11        and have it return an Iterator object.
     12
     13        This change selects a dynamically created function's ConstructAbility based on its parse mode instead.
     14
     15        * runtime/CodeCache.cpp:
     16        (JSC::CodeCache::getFunctionExecutableFromGlobalCode):
     17
    1182016-10-19  JF Bastien  <jfbastien@apple.com>
    219
  • trunk/Source/JavaScriptCore/runtime/CodeCache.cpp

    r206400 r207618  
    203203    // The Function constructor only has access to global variables, so no variables will be under TDZ.
    204204    VariableEnvironment emptyTDZVariables;
    205     UnlinkedFunctionExecutable* functionExecutable = UnlinkedFunctionExecutable::create(&vm, source, metadata, UnlinkedNormalFunction, ConstructAbility::CanConstruct, JSParserScriptMode::Classic, emptyTDZVariables, DerivedContextType::None);
     205    ConstructAbility constructAbility = constructAbilityForParseMode(metadata->parseMode());
     206    UnlinkedFunctionExecutable* functionExecutable = UnlinkedFunctionExecutable::create(&vm, source, metadata, UnlinkedNormalFunction, constructAbility, JSParserScriptMode::Classic, emptyTDZVariables, DerivedContextType::None);
    206207
    207208    functionExecutable->setSourceURLDirective(source.provider()->sourceURL());
Note: See TracChangeset for help on using the changeset viewer.