Skip to content

Commit

Permalink
fix name property for bound functions (see issue mozilla#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jan 30, 2023
1 parent 9032f89 commit 6045893
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/org/mozilla/javascript/BoundFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public int getLength() {
return length;
}

@Override
public String getFunctionName() {
if (targetFunction instanceof BaseFunction) {
return "bound " + ((BaseFunction) targetFunction).getFunctionName();
}
return "";
}

private static Object[] concat(Object[] first, Object[] second) {
Object[] args = new Object[first.length + second.length];
System.arraycopy(first, 0, args, 0, first.length);
Expand Down
43 changes: 43 additions & 0 deletions testsrc/org/mozilla/javascript/tests/es6/BoundFunctionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
* Tests for the Object.getOwnPropertyDescriptor(obj, prop) method
*/
package org.mozilla.javascript.tests.es6;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;

public class BoundFunctionTest {

private Context cx;
private ScriptableObject scope;

@Before
public void setUp() {
cx = Context.enter();
cx.setLanguageVersion(Context.VERSION_ES6);
scope = cx.initSafeStandardObjects(new TopLevel(), false);
}

@After
public void tearDown() {
Context.exit();
}

@Test
public void ctorCallableThis() {
Object result =
cx.evaluateString(
scope, " function foo() {};\n" + " foo.bind({}).name;", "test", 1, null);
assertEquals("bound foo", result);
}
}
3 changes: 1 addition & 2 deletions testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ built-ins/eval 3/9 (33.33%)
name.js
private-identifiers-not-empty.js {unsupported: [class-fields-private]}

built-ins/Function 187/505 (37.03%)
built-ins/Function 186/505 (36.83%)
internals/Call 2/2 (100.0%)
internals/Construct 6/6 (100.0%)
length/S15.3.5.1_A1_T3.js strict
Expand Down Expand Up @@ -532,7 +532,6 @@ built-ins/Function 187/505 (37.03%)
prototype/bind/instance-name.js
prototype/bind/instance-name-chained.js
prototype/bind/instance-name-error.js
prototype/bind/instance-name-non-string.js
prototype/bind/proto-from-ctor-realm.js {unsupported: [Reflect]}
prototype/call/15.3.4.4-1-s.js strict
prototype/call/15.3.4.4-2-s.js strict
Expand Down

0 comments on commit 6045893

Please sign in to comment.