Skip to content

Commit be83ff6

Browse files
rerobikadbatyai
authored andcommitted
Update Function.prototype.bind to conform ES6 requirements (#3504)
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent d0e8629 commit be83ff6

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,13 @@ ecma_builtin_function_prototype_object_bind (ecma_object_t *this_arg_obj_p , /**
208208
ecma_length_t arguments_number) /**< number of arguments */
209209
{
210210
/* 4. 11. 18. */
211-
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE);
211+
ecma_object_t *prototype_obj_p;
212+
213+
#if ENABLED (JERRY_ES2015)
214+
prototype_obj_p = ECMA_GET_POINTER (ecma_object_t, this_arg_obj_p->u2.prototype_cp);
215+
#else /* !ENABLED (JERRY_ES2015) */
216+
prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE);
217+
#endif /* ENABLED (JERRY_ES2015) */
212218

213219
ecma_object_t *function_p;
214220
ecma_extended_object_t *ext_function_p;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/* extended class */
16+
(function() {
17+
class C extends Function {}
18+
var c = new C("x", "y", "return this.foo + x + y;").bind({foo : 1}, 2);
19+
assert(c(3) === 6);
20+
assert(c instanceof C);
21+
})();
22+
23+
function boundPrototypeChecker(f, proto) {
24+
Object.setPrototypeOf(f, proto);
25+
26+
var boundFunc = Function.prototype.bind.call(f, null);
27+
assert(Object.getPrototypeOf(boundFunc) === proto);
28+
}
29+
30+
/* generator function */
31+
(function() {
32+
var f = function*(){};
33+
boundPrototypeChecker(f, Function.prototype)
34+
boundPrototypeChecker(f, {})
35+
boundPrototypeChecker(f, null);
36+
})();
37+
38+
/* arrow function */
39+
(function() {
40+
var f = () => 5;
41+
boundPrototypeChecker(f, Function.prototype)
42+
boundPrototypeChecker(f, {})
43+
boundPrototypeChecker(f, null);
44+
})();
45+
46+
/* simple class */
47+
(function() {
48+
class C {};
49+
boundPrototypeChecker(C, Function.prototype)
50+
boundPrototypeChecker(C, {})
51+
boundPrototypeChecker(C, null);
52+
})();
53+
54+
/* subclasses */
55+
(function() {
56+
function boundPrototypeChecker(superclass) {
57+
class C extends superclass {
58+
constructor() {
59+
return Object.create(null);
60+
}
61+
}
62+
var boundF = Function.prototype.bind.call(C, null);
63+
assert(Object.getPrototypeOf(boundF) === Object.getPrototypeOf(C));
64+
}
65+
66+
boundPrototypeChecker(function(){});
67+
boundPrototypeChecker(Array);
68+
boundPrototypeChecker(null);
69+
})();

0 commit comments

Comments
 (0)