14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci/*
174514f5e3Sopenharmony_ci * @tc.name:bindfunction
184514f5e3Sopenharmony_ci * @tc.desc:test bind function
194514f5e3Sopenharmony_ci * @tc.type: FUNC
204514f5e3Sopenharmony_ci * @tc.require: issueI5NO8G
214514f5e3Sopenharmony_ci */
224514f5e3Sopenharmony_ciconst module = {
234514f5e3Sopenharmony_ci    x: 42,
244514f5e3Sopenharmony_ci    getX: function() {
254514f5e3Sopenharmony_ci        return this.x;
264514f5e3Sopenharmony_ci    }
274514f5e3Sopenharmony_ci};
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_ciconst unboundGetX = module.getX;
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ciconst boundGetX = unboundGetX.bind(module);
324514f5e3Sopenharmony_ciprint(boundGetX()); // expected output: 42
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_cifunction list() {
354514f5e3Sopenharmony_ci    return Array.prototype.slice.call(arguments);
364514f5e3Sopenharmony_ci}
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_cifunction addArguments(arg1, arg2) {
394514f5e3Sopenharmony_ci    return arg1 + arg2;
404514f5e3Sopenharmony_ci}
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ciconst list1 = list(1, 2, 3); // [1, 2, 3]
434514f5e3Sopenharmony_ciprint(list1);
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ciconst result1 = addArguments(1, 2); // 3
464514f5e3Sopenharmony_ciprint(result1);
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci// Create a function with a preset leading argument
494514f5e3Sopenharmony_ciconst leadingThirtysevenList = list.bind(null, 37);
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci// Create a function with a preset first argument.
524514f5e3Sopenharmony_ciconst addThirtySeven = addArguments.bind(null, 37);
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ciconst list2 = leadingThirtysevenList(); // [37]
554514f5e3Sopenharmony_ciprint(list2);
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ciconst list3 = leadingThirtysevenList(1, 2, 3); // [37, 1, 2, 3]
584514f5e3Sopenharmony_ciprint(list3);
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ciconst result2 = addThirtySeven(5); // 37 + 5 = 42
614514f5e3Sopenharmony_ciprint(result2);
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_ciconst result3 = addThirtySeven(5, 10); // 37 + 5 = 42, (the second argument is ignored)
644514f5e3Sopenharmony_ciprint(result3);
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci// TestCase: builtins bind function.
674514f5e3Sopenharmony_cifunction foo(a, b) {
684514f5e3Sopenharmony_ci    return a + b;
694514f5e3Sopenharmony_ci}
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_civar bfoo = foo.bind(undefined, 1);
724514f5e3Sopenharmony_cibfoo(2);
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_civar array = [1];
754514f5e3Sopenharmony_ciarray.forEach(bfoo);
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ciconst v6 = []
784514f5e3Sopenharmony_ciconst v21 = Float32Array.bind(v6, 1, 2);
794514f5e3Sopenharmony_ciprint(v21)
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci// TestCase: bind proxy
824514f5e3Sopenharmony_ciconst proxy = new Proxy(Array.prototype.includes, {});
834514f5e3Sopenharmony_ciconst bind_proxy = proxy.bind();
844514f5e3Sopenharmony_ciprint(bind_proxy.length)
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ciconst arr = []
874514f5e3Sopenharmony_ciconst f = ArrayBuffer.bind();
884514f5e3Sopenharmony_cinew f(...arr);
894514f5e3Sopenharmony_cinew f(1);
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_cifunction f1(...args){
924514f5e3Sopenharmony_ci    print(args)
934514f5e3Sopenharmony_ci    print(args.length)
944514f5e3Sopenharmony_ci}
954514f5e3Sopenharmony_cilet fn=f1.bind(undefined,0,1,2,3,4,5,6,7,8,9);
964514f5e3Sopenharmony_cifn(arr)
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ciBigInt64Array.bind(BigInt64Array,BigInt64Array,BigInt64Array)
994514f5e3Sopenharmony_ciconst t9 = ("round").normalize.bind();
1004514f5e3Sopenharmony_ci
1014514f5e3Sopenharmony_citry {
1024514f5e3Sopenharmony_ci    new t9()
1034514f5e3Sopenharmony_ci} catch(err) {
1044514f5e3Sopenharmony_ci    print(err);
1054514f5e3Sopenharmony_ci}
1064514f5e3Sopenharmony_ci
1074514f5e3Sopenharmony_cifunction testFunc(...args) {
1084514f5e3Sopenharmony_ci    print(this);
1094514f5e3Sopenharmony_ci    print(args);
1104514f5e3Sopenharmony_ci    print(args.length);
1114514f5e3Sopenharmony_ci}
1124514f5e3Sopenharmony_cilet testFuncBound = testFunc.bind(0, 1, 2);
1134514f5e3Sopenharmony_citestFuncBound(3, 4, 5);
1144514f5e3Sopenharmony_ciprint(testFuncBound.name);
1154514f5e3Sopenharmony_ciObject.defineProperty(testFunc, "name", {value: "testFuncChanged"});
1164514f5e3Sopenharmony_citestFuncBound = testFunc.bind();
1174514f5e3Sopenharmony_ciprint(testFuncBound.name);
1184514f5e3Sopenharmony_ci
1194514f5e3Sopenharmony_cifunction func() {}
1204514f5e3Sopenharmony_civar fb = func.bind({});
1214514f5e3Sopenharmony_ciObject.defineProperty(func, 'name', {value: 1});
1224514f5e3Sopenharmony_ciprint('bound func' == fb.name);
123