Implement function.toString operation (#4752)

May increase the memory consumtpion heavily.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-08-31 13:37:25 +02:00
committed by GitHub
parent 1c6b18ecdf
commit 6649940ea6
37 changed files with 1002 additions and 192 deletions
@@ -0,0 +1,188 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
function check() {}
if (check.toString() !== "function () { /* ecmascript */ }")
{
var a, b, o, c, f;
/* Function statements. */
a = 6; function f1(a,b=1,c) { return a /* comment */ + b + c } b = 7
assert(f1.toString() === "function f1(a,b=1,c) { return a /* comment */ + b + c }")
a = 6; function * f2(a,b,c=1) {
function x() {}
} b = 7
assert(f2.toString() === "function * f2(a,b,c=1) {\n function x() {}\n }")
a = 6;async function f3 ( a , b , c ) { } b = 7
assert(f3.toString() === "async function f3 ( a , b , c ) { }")
a = 6;async/**/function*f4(a,b,c){} b = 7
assert(f4.toString() === "async/**/function*f4(a,b,c){}")
/* Object initializers. */
o = {f(a) { return a }}
assert(o.f.toString() === "f(a) { return a }")
o = {f:function(a){/**/}}
assert(o.f.toString() === "function(a){/**/}")
o = { [function(){ return 'f' }()] (a = function() {}) {} }
assert(o.f.toString() === "[function(){ return 'f' }()] (a = function() {}) {}")
o = {* f(a) {}}
assert(o.f.toString() === "* f(a) {}")
o = {*[function(){ return 'f' }()](a) {}}
assert(o.f.toString() === "*[function(){ return 'f' }()](a) {}")
o = {async/**/f(a) {}}
assert(o.f.toString() === "async/**/f(a) {}")
o = {/**/async [function(){ return 'f' }()](a) {}/**/}
assert(o.f.toString() === "async [function(){ return 'f' }()](a) {}")
o = {a:1,async/**/*/**/f(a) {},b:1}
assert(o.f.toString() === "async/**/*/**/f(a) {}")
o = {async *//
[function(){ return 'f' }()](a) {}/**/}
assert(o.f.toString() === "async *//\n [function(){ return 'f' }()](a) {}")
o = { get a() {return/**/6} }
assert(Object.getOwnPropertyDescriptor(o, "a").get.toString() === "get a() {return/**/6}")
o = { get[function(){ return 'a' }()](){} }
assert(Object.getOwnPropertyDescriptor(o, "a").get.toString() === "get[function(){ return 'a' }()](){}")
o = { set a(v) {/**/} }
assert(Object.getOwnPropertyDescriptor(o, "a").set.toString() === "set a(v) {/**/}")
o = {/**/set/**/[function(){ return 'a' }()]/**/(v) {/**/}/**/}
assert(Object.getOwnPropertyDescriptor(o, "a").set.toString() === "set/**/[function(){ return 'a' }()]/**/(v) {/**/}")
/* Class static functions. */
c = class { static/**/f() {}/**/ }
assert(c.f.toString() === "f() {}")
c = class { static[function(){ return 'f' }()]() {} }
assert(c.f.toString() === "[function(){ return 'f' }()]() {}")
c = class { static *f() {} }
assert(c.f.toString() === "*f() {}")
c = class {/**/static * [function(){ return 'f' }()](a=6){}/**/}
assert(c.f.toString() === "* [function(){ return 'f' }()](a=6){}")
c = class { static/**/async f() {} }
assert(c.f.toString() === "async f() {}")
c = class {static async[function(){ return 'f' }()]() {/**/}}
assert(c.f.toString() === "async[function(){ return 'f' }()]() {/**/}")
c = class { static async*f() {}}
assert(c.f.toString() === "async*f() {}")
c = class { static async*/**/[function(){ return 'f' }()](){} }
assert(c.f.toString() === "async*/**/[function(){ return 'f' }()](){}")
c = class { static/**/get/**/a() {}}
assert(Object.getOwnPropertyDescriptor(c, "a").get.toString() === "get/**/a() {}")
c = class {static set a(v){}}
assert(Object.getOwnPropertyDescriptor(c, "a").set.toString() === "set a(v){}")
c = class { static get[function(){ return 'a' }()](){} }
assert(Object.getOwnPropertyDescriptor(c, "a").get.toString() === "get[function(){ return 'a' }()](){}")
c = class { static set[function(){ return 'a' }()](v){}//
}
assert(Object.getOwnPropertyDescriptor(c, "a").set.toString() === "set[function(){ return 'a' }()](v){}")
/* Class functions. */
o = Object.getPrototypeOf(new class {/**/f() {}/**/})
assert(o.f.toString() === "f() {}")
o = Object.getPrototypeOf(new class {[function(){ return 'f' }()](){}})
assert(o.f.toString() === "[function(){ return 'f' }()](){}")
o = Object.getPrototypeOf(new class { * /**/ f() {} })
assert(o.f.toString() === "* /**/ f() {}")
o = Object.getPrototypeOf(new class { * [function(){ return 'f' }()]/**/(){} })
assert(o.f.toString() === "* [function(){ return 'f' }()]/**/(){}")
o = Object.getPrototypeOf(new class {async f() {}})
assert(o.f.toString() === "async f() {}")
o = Object.getPrototypeOf(new class {async[function(){ return 'f' }()](){}})
assert(o.f.toString() === "async[function(){ return 'f' }()](){}")
o = Object.getPrototypeOf(new class {/**/get/**/a() {}/**/})
assert(Object.getOwnPropertyDescriptor(o, "a").get.toString() === "get/**/a() {}")
o = Object.getPrototypeOf(new class { set a(v){} })
assert(Object.getOwnPropertyDescriptor(o, "a").set.toString() === "set a(v){}")
o = Object.getPrototypeOf(new class {/**/get/**/[function(){ return 'a' }()]() {}/**/})
assert(Object.getOwnPropertyDescriptor(o, "a").get.toString() === "get/**/[function(){ return 'a' }()]() {}")
o = Object.getPrototypeOf(new class { set/**/[function(){ return 'a' }()]( v ){} })
assert(Object.getOwnPropertyDescriptor(o, "a").set.toString() === "set/**/[function(){ return 'a' }()]( v ){}")
/* Function creators. */
f = Function("a,b", "return a + b")
assert(f.toString() === "function anonymous(a,b\n) {\nreturn a + b\n}")
f = Function("", "")
assert(f.toString() === "function anonymous(\n) {\n\n}")
f = function*(){}.constructor("a,b", "c", "yield a; return b + c")
assert(f.toString() === "function* anonymous(a,b,c\n) {\nyield a; return b + c\n}")
f = async function(){}.constructor("a", "return a + 'x'")
assert(f.toString() === "async function anonymous(a\n) {\nreturn a + 'x'\n}")
f = async function*(){}.constructor("a=3", "return a")
assert(f.toString() === "async function* anonymous(a=3\n) {\nreturn a\n}")
f = Function("a = function(x) { return x + 3 }", "return a")
assert(f().toString() === "function(x) { return x + 3 }")
f = Function("return function(x) { return x + 3 }")
assert(f().toString() === "function(x) { return x + 3 }")
/* Arrow functions. */
f = x => x + 1/**/
assert(f.toString() === "x => x + 1")
f =x => { return x + 1 }/**/
assert(f.toString() === "x => { return x + 1 }")
f = (/**/) => 'x' //
+ y
assert(f.toString() === "(/**/) => 'x' //\n + y")
f = async x => x + 1
assert(f.toString() === "async x => x + 1")
f =/**/async (x,/**/y)=>/**/null
assert(f.toString() === "async (x,/**/y)=>/**/null")
}
+3 -1
View File
@@ -149,7 +149,9 @@ new delete_test ();
function binary_test_1 () {
/*/ new.target is converted to string */
assert ((new.target + 1) === "function(){/* ecmascript */}1");
var str = (new.target + 1);
assert (str.substring(0, 8) === "function"
&& str.substring(str.length - 2, str.length) === "}1");
}
function binary_test_2 () { assert (isNaN (new.target - 3)); }
function binary_test_3 () { assert (isNaN (new.target * 2)); }
@@ -14,4 +14,6 @@
var str = 'for (let i=0; i<(eval("1; function x() { }; 2;")); x - i++) { x += delete x;}'
assert(eval(str) === 'function(){/* ecmascript */}true');
var e = eval(str)
assert(e === 'function x() { }true'
|| e === 'function () { /* ecmascript */ }true');
+16 -5
View File
@@ -12,16 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.
assert (Math.cos.toString() === "function(){/* ecmascript */}");
assert (Math.cos.toString() === "function () { [native code] }");
var has_toString = none.toString() != "function () { /* ecmascript */ }"
function check(f, expected)
{
assert (f.toString() === (has_toString ? expected : "function () { /* ecmascript */ }"))
}
function none() { return 1; }
assert (none.toString() === "function(){/* ecmascript */}");
check (none, "function none() { return 1; }")
assert (none.bind({}).toString() === "function () { [native code] }")
function single(b) { return 1; }
assert (single.toString() === "function(){/* ecmascript */}");
check (single, "function single(b) { return 1; }")
assert (single.bind({}).toString() === "function () { [native code] }")
function multiple(a,b) { return 1; }
assert (multiple.toString() === "function(){/* ecmascript */}");
check (multiple, "function multiple(a,b) { return 1; }")
assert (multiple.bind({}).toString() === "function () { [native code] }")
function lots(a,b,c,d,e,f,g,h,i,j,k) { return 1; }
assert (lots.toString() === "function(){/* ecmascript */}");
check (lots, "function lots(a,b,c,d,e,f,g,h,i,j,k) { return 1; }")
assert (lots.bind({}).toString() === "function () { [native code] }")
-61
View File
@@ -28,60 +28,9 @@
<test id="built-ins/BigInt/asUintN/length.js"><reason></reason></test>
<test id="built-ins/BigInt/asUintN/name.js"><reason></reason></test>
<test id="built-ins/BigInt/asUintN/order-of-steps.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/AsyncFunction.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/Function.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/GeneratorFunction.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/arrow-function.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-arrow-function.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-function-declaration.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-function-expression.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-method-class-expression-static.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-method-class-expression.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-method-class-statement-static.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-method-class-statement.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-method-object.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/bound-function.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/built-in-function-object.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/class-declaration-complex-heritage.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/class-declaration-explicit-ctor.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/class-declaration-implicit-ctor.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/class-expression-explicit-ctor.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/class-expression-implicit-ctor.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/function-declaration-non-simple-parameter-list.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/function-declaration.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/function-expression.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/generator-function-declaration.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/generator-function-expression.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/generator-method.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/getter-class-expression-static.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/getter-class-expression.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/getter-class-statement-static.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/getter-class-statement.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/getter-object.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/line-terminator-normalisation-CR-LF.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/line-terminator-normalisation-CR.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/line-terminator-normalisation-LF.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/method-class-expression-static.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/method-class-expression.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/method-class-statement-static.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/method-class-statement.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/method-computed-property-name.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/method-object.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/proxy-arrow-function.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/proxy-async-function.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/proxy-async-method-definition.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/proxy-bound-function.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/proxy-class.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/proxy-function-expression.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/proxy-generator-function.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/proxy-method-definition.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/setter-class-expression-static.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/setter-class-expression.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/setter-class-statement-static.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/setter-class-statement.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/setter-object.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/symbol-named-builtins.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/unicode.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/well-known-intrinsic-object-functions.js"><reason></reason></test>
<test id="built-ins/GeneratorFunction/instance-yield-expr-in-param.js"><reason></reason></test>
<test id="built-ins/GeneratorPrototype/return/from-state-completed.js"><reason></reason></test>
@@ -6678,16 +6627,6 @@
<test id="built-ins/AsyncGeneratorPrototype/throw/this-val-not-object.js"><reason></reason></test>
<test id="built-ins/FinalizationRegistry/prototype/cleanupSome/return-undefined-with-gc.js"><reason></reason></test>
<test id="built-ins/FinalizationRegistry/prototype/cleanupSome/return-undefined.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/AsyncGenerator.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-generator-declaration.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-generator-expression.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-generator-method-class-expression-static.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-generator-method-class-expression.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-generator-method-class-statement-static.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-generator-method-class-statement.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/async-generator-method-object.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/proxy-async-generator-function.js"><reason></reason></test>
<test id="built-ins/Function/prototype/toString/proxy-async-generator-method-definition.js"><reason></reason></test>
<test id="language/arguments-object/cls-decl-async-private-gen-meth-args-trailing-comma-multiple.js"><reason></reason></test>
<test id="language/arguments-object/cls-decl-async-private-gen-meth-args-trailing-comma-null.js"><reason></reason></test>
<test id="language/arguments-object/cls-decl-async-private-gen-meth-args-trailing-comma-single-args.js"><reason></reason></test>