From e08f7e04c5576a9f12df880ea18e520fdf636e4f Mon Sep 17 00:00:00 2001 From: "duncan.macgregor" Date: Fri, 21 Feb 2025 10:32:01 +0000 Subject: [PATCH] Make built-in function source spec compliant. --- .../org/mozilla/javascript/BaseFunction.java | 4 +--- .../mozilla/javascript/IdFunctionObject.java | 24 ------------------- .../javascript/tests/LookupSetterTest.java | 8 ++----- .../javascript/tests/es6/NativeProxyTest.java | 2 +- tests/testsrc/doctests/array.every.doctest | 4 +++- tests/testsrc/doctests/array.filter.doctest | 8 +++++-- tests/testsrc/doctests/array.find.doctest | 8 +++++-- .../testsrc/doctests/array.findIndex.doctest | 8 +++++-- tests/testsrc/doctests/array.forEach.doctest | 8 +++++-- tests/testsrc/doctests/array.isarray.doctest | 4 +++- tests/testsrc/doctests/array.map.doctest | 8 +++++-- tests/testsrc/doctests/array.reduce.doctest | 8 +++++-- .../doctests/array.reduceRight.doctest | 8 +++++-- tests/testsrc/doctests/array.some.doctest | 8 +++++-- .../testsrc/doctests/date.toisostring.doctest | 4 +++- tests/testsrc/doctests/date.tojson.doctest | 4 +++- tests/testsrc/doctests/function.bind.doctest | 4 +++- tests/testsrc/doctests/object.create.doctest | 4 +++- .../doctests/object.defineproperties.doctest | 4 +++- .../doctests/object.defineproperty.doctest | 4 +++- .../doctests/object.extensible.doctest | 8 +++++-- tests/testsrc/doctests/object.freeze.doctest | 4 +++- .../object.getownpropertydescriptor.doctest | 4 +++- .../object.getownpropertynames.doctest | 4 +++- .../doctests/object.getprototypeof.doctest | 4 +++- .../testsrc/doctests/object.isfrozen.doctest | 4 +++- .../testsrc/doctests/object.issealed.doctest | 4 +++- tests/testsrc/doctests/object.keys.doctest | 4 +++- tests/testsrc/doctests/object.seal.doctest | 4 +++- tests/testsrc/doctests/parseint.doctest | 2 +- tests/testsrc/doctests/string.trim.doctest | 4 +++- 31 files changed, 110 insertions(+), 70 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/BaseFunction.java b/rhino/src/main/java/org/mozilla/javascript/BaseFunction.java index c35d9281c1..97c716c5f4 100644 --- a/rhino/src/main/java/org/mozilla/javascript/BaseFunction.java +++ b/rhino/src/main/java/org/mozilla/javascript/BaseFunction.java @@ -505,9 +505,7 @@ String decompile(int indent, EnumSet flags) { sb.append(getFunctionName()); sb.append("() {\n\t"); } - sb.append("[native code, arity="); - sb.append(getArity()); - sb.append("]\n"); + sb.append("[native code]\n"); if (!justbody) { sb.append("}\n"); } diff --git a/rhino/src/main/java/org/mozilla/javascript/IdFunctionObject.java b/rhino/src/main/java/org/mozilla/javascript/IdFunctionObject.java index 0417c0ed54..cd3f8a605a 100644 --- a/rhino/src/main/java/org/mozilla/javascript/IdFunctionObject.java +++ b/rhino/src/main/java/org/mozilla/javascript/IdFunctionObject.java @@ -8,8 +8,6 @@ package org.mozilla.javascript; -import java.util.EnumSet; - public class IdFunctionObject extends BaseFunction { private static final long serialVersionUID = -5332312783643935019L; @@ -97,28 +95,6 @@ public Scriptable createObject(Context cx, Scriptable scope) { throw ScriptRuntime.typeErrorById("msg.not.ctor", functionName); } - @Override - String decompile(int indent, EnumSet flags) { - StringBuilder sb = new StringBuilder(); - boolean justbody = flags.contains(DecompilerFlag.ONLY_BODY); - if (!justbody) { - sb.append("function "); - sb.append(getFunctionName()); - sb.append("() { "); - } - sb.append("[native code for "); - if (idcall instanceof Scriptable) { - Scriptable sobj = (Scriptable) idcall; - sb.append(sobj.getClassName()); - sb.append('.'); - } - sb.append(getFunctionName()); - sb.append(", arity="); - sb.append(getArity()); - sb.append(justbody ? "]\n" : "] }\n"); - return sb.toString(); - } - @Override public int getArity() { return arity; diff --git a/rhino/src/test/java/org/mozilla/javascript/tests/LookupSetterTest.java b/rhino/src/test/java/org/mozilla/javascript/tests/LookupSetterTest.java index e72735a697..faad37a764 100644 --- a/rhino/src/test/java/org/mozilla/javascript/tests/LookupSetterTest.java +++ b/rhino/src/test/java/org/mozilla/javascript/tests/LookupSetterTest.java @@ -45,9 +45,7 @@ public void callLookedUpGetter() throws Exception { @Test public void lookedUpGetter_toString() throws Exception { - test( - "function s() {\n\t[native code, arity=0]\n}\n", - "new Foo().__lookupGetter__('s').toString()"); + test("function s() {\n\t[native code]\n}\n", "new Foo().__lookupGetter__('s').toString()"); } @Test @@ -86,9 +84,7 @@ public void callLookedUpSetter() throws Exception { @Test public void lookedUpSetter_toString() throws Exception { - test( - "function s() {\n\t[native code, arity=0]\n}\n", - "new Foo().__lookupSetter__('s').toString()"); + test("function s() {\n\t[native code]\n}\n", "new Foo().__lookupSetter__('s').toString()"); } @Test diff --git a/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeProxyTest.java b/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeProxyTest.java index b188a83ec5..224381403d 100644 --- a/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeProxyTest.java +++ b/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeProxyTest.java @@ -8,7 +8,7 @@ public class NativeProxyTest { @Test public void testToString() { Utils.assertWithAllModes_ES6( - "function Proxy() {\n\t[native code, arity=2]\n}\n", "Proxy.toString()"); + "function Proxy() {\n\t[native code]\n}\n", "Proxy.toString()"); Utils.assertWithAllModes_ES6( "[object Object]", "Object.prototype.toString.call(new Proxy({}, {}))"); diff --git a/tests/testsrc/doctests/array.every.doctest b/tests/testsrc/doctests/array.every.doctest index a4e63a4b3d..e2ed32902a 100644 --- a/tests/testsrc/doctests/array.every.doctest +++ b/tests/testsrc/doctests/array.every.doctest @@ -3,7 +3,9 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. js> Array.every; -function every() { [native code for Array.every, arity=1] } +function every() { + [native code] +} js> function isSmall(n) { return n < 10; }; diff --git a/tests/testsrc/doctests/array.filter.doctest b/tests/testsrc/doctests/array.filter.doctest index 33bdaf30ac..57391ae66f 100644 --- a/tests/testsrc/doctests/array.filter.doctest +++ b/tests/testsrc/doctests/array.filter.doctest @@ -5,13 +5,17 @@ js> function isSmall(n) { return n < 10; }; js> [1, 13, 4, 16, 42].filter; -function filter() { [native code for Array.filter, arity=1] } +function filter() { + [native code] +} js> "" + [1, 13, 4, 16, 42].filter(isSmall); 1,4 js> Array.filter; -function filter() { [native code for Array.filter, arity=1] } +function filter() { + [native code] +} js> "" + Array.filter([1, 13, 4, 16, 42], isSmall); 1,4 diff --git a/tests/testsrc/doctests/array.find.doctest b/tests/testsrc/doctests/array.find.doctest index b5e9a5486d..2fff0297cb 100644 --- a/tests/testsrc/doctests/array.find.doctest +++ b/tests/testsrc/doctests/array.find.doctest @@ -5,13 +5,17 @@ js> function isSmall(n) { return n < 10; }; js> [13, 4, 17].find; -function find() { [native code for Array.find, arity=1] } +function find() { + [native code] +} js> [13, 4, 17].find(isSmall); 4 js> Array.find; -function find() { [native code for Array.find, arity=1] } +function find() { + [native code] +} js> res = ''; js> Array.find([13, 4, 17], isSmall); diff --git a/tests/testsrc/doctests/array.findIndex.doctest b/tests/testsrc/doctests/array.findIndex.doctest index ae1e004ef1..8cfa335885 100644 --- a/tests/testsrc/doctests/array.findIndex.doctest +++ b/tests/testsrc/doctests/array.findIndex.doctest @@ -5,13 +5,17 @@ js> function isSmall(n) { return n < 10; }; js> [13, 4, 17].findIndex; -function findIndex() { [native code for Array.findIndex, arity=1] } +function findIndex() { + [native code] +} js> [13, 4, 17].findIndex(isSmall); 1 js> Array.findIndex; -function findIndex() { [native code for Array.findIndex, arity=1] } +function findIndex() { + [native code] +} js> Array.findIndex([13, 4, 17], isSmall); 1 diff --git a/tests/testsrc/doctests/array.forEach.doctest b/tests/testsrc/doctests/array.forEach.doctest index 38414e2f16..33d6929b4b 100644 --- a/tests/testsrc/doctests/array.forEach.doctest +++ b/tests/testsrc/doctests/array.forEach.doctest @@ -3,7 +3,9 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. js> [1, 13, 4].forEach; -function forEach() { [native code for Array.forEach, arity=1] } +function forEach() { + [native code] +} js> res = ''; js> [1, 13, 4].forEach(function(elem) { res += elem }); @@ -11,7 +13,9 @@ js> res 1134 js> Array.forEach; -function forEach() { [native code for Array.forEach, arity=1] } +function forEach() { + [native code] +} js> res = ''; js> Array.forEach([1, 13, 4], function(elem) { res += elem }); diff --git a/tests/testsrc/doctests/array.isarray.doctest b/tests/testsrc/doctests/array.isarray.doctest index 41a260870a..7aa5080969 100644 --- a/tests/testsrc/doctests/array.isarray.doctest +++ b/tests/testsrc/doctests/array.isarray.doctest @@ -3,7 +3,9 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. js> Array.isArray; -function isArray() { [native code for Array.isArray, arity=1] } +function isArray() { + [native code] +} js> Array.isArray() false diff --git a/tests/testsrc/doctests/array.map.doctest b/tests/testsrc/doctests/array.map.doctest index 20172ee613..c6cb14c578 100644 --- a/tests/testsrc/doctests/array.map.doctest +++ b/tests/testsrc/doctests/array.map.doctest @@ -3,13 +3,17 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. js> [9, 16, 25].map; -function map() { [native code for Array.map, arity=1] } +function map() { + [native code] +} js> "" + [9, 16, 25].map(Math.sqrt); 3,4,5 js> Array.map; -function map() { [native code for Array.map, arity=1] } +function map() { + [native code] +} js> "" + Array.map([9, 16, 25], Math.sqrt); 3,4,5 diff --git a/tests/testsrc/doctests/array.reduce.doctest b/tests/testsrc/doctests/array.reduce.doctest index e9755d4d1a..4cff80731f 100644 --- a/tests/testsrc/doctests/array.reduce.doctest +++ b/tests/testsrc/doctests/array.reduce.doctest @@ -5,13 +5,17 @@ js> function sum(acc, val) { return acc + val; }; js> [1, 4, 9, 16].reduce; -function reduce() { [native code for Array.reduce, arity=1] } +function reduce() { + [native code] +} js> [1, 4, 9, 16].reduce(sum); 30 js> Array.reduce; -function reduce() { [native code for Array.reduce, arity=1] } +function reduce() { + [native code] +} js> Array.reduce([1, 4, 9, 16], sum); 30 diff --git a/tests/testsrc/doctests/array.reduceRight.doctest b/tests/testsrc/doctests/array.reduceRight.doctest index c359ef1468..a8315ffe79 100644 --- a/tests/testsrc/doctests/array.reduceRight.doctest +++ b/tests/testsrc/doctests/array.reduceRight.doctest @@ -5,13 +5,17 @@ js> function diff(acc, val) { return acc - val; }; js> [1, 4, 9, 16].reduceRight; -function reduceRight() { [native code for Array.reduceRight, arity=1] } +function reduceRight() { + [native code] +} js> [1, 4, 9, 16].reduceRight(diff); 2 js> Array.reduceRight; -function reduceRight() { [native code for Array.reduceRight, arity=1] } +function reduceRight() { + [native code] +} js> Array.reduceRight([1, 4, 9, 16], diff); 2 diff --git a/tests/testsrc/doctests/array.some.doctest b/tests/testsrc/doctests/array.some.doctest index b7aa15e7bc..6fc7e90338 100644 --- a/tests/testsrc/doctests/array.some.doctest +++ b/tests/testsrc/doctests/array.some.doctest @@ -5,7 +5,9 @@ js> function isSmall(n) { return n < 10; }; js> [1, 4, 9, 16].some; -function some() { [native code for Array.some, arity=1] } +function some() { + [native code] +} js> [1, 4, 9, 16].some(isSmall); true @@ -14,7 +16,9 @@ js> [19, 42].some(isSmall); false js> Array.some; -function some() { [native code for Array.some, arity=1] } +function some() { + [native code] +} js> Array.some([1, 4, 9, 16], isSmall); true diff --git a/tests/testsrc/doctests/date.toisostring.doctest b/tests/testsrc/doctests/date.toisostring.doctest index 7d885fdcf1..6197b386b4 100644 --- a/tests/testsrc/doctests/date.toisostring.doctest +++ b/tests/testsrc/doctests/date.toisostring.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Date.prototype.toISOString; -function toISOString() { [native code for Date.toISOString, arity=0] } +function toISOString() { + [native code] +} js> expectError(function() { > new Date(Infinity).toISOString() diff --git a/tests/testsrc/doctests/date.tojson.doctest b/tests/testsrc/doctests/date.tojson.doctest index 38d47cf634..1cc9a1af58 100644 --- a/tests/testsrc/doctests/date.tojson.doctest +++ b/tests/testsrc/doctests/date.tojson.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Date.prototype.toJSON; -function toJSON() { [native code for Date.toJSON, arity=1] } +function toJSON() { + [native code] +} js> Date.prototype.toJSON.call({ > valueOf: function() { return Infinity; } diff --git a/tests/testsrc/doctests/function.bind.doctest b/tests/testsrc/doctests/function.bind.doctest index 01740ab9f5..b52ed11c7b 100644 --- a/tests/testsrc/doctests/function.bind.doctest +++ b/tests/testsrc/doctests/function.bind.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Function.prototype.bind -function bind() { [native code for Function.bind, arity=1] } +function bind() { + [native code] +} js> expectTypeError(function() { > Function.prototype.bind.call({}) diff --git a/tests/testsrc/doctests/object.create.doctest b/tests/testsrc/doctests/object.create.doctest index 47aae108ee..9b8c64e3f0 100644 --- a/tests/testsrc/doctests/object.create.doctest +++ b/tests/testsrc/doctests/object.create.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.create; -function create() { [native code for Object.create, arity=2] } +function create() { + [native code] +} js> expectTypeError(function() { Object.create() }); js> [undefined, true, 1, 'hello'].forEach(function(value) { diff --git a/tests/testsrc/doctests/object.defineproperties.doctest b/tests/testsrc/doctests/object.defineproperties.doctest index b8e6111ccf..9dc7783d01 100644 --- a/tests/testsrc/doctests/object.defineproperties.doctest +++ b/tests/testsrc/doctests/object.defineproperties.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.defineProperties -function defineProperties() { [native code for Object.defineProperties, arity=2] } +function defineProperties() { + [native code] +} js> expectTypeError(function() { Object.defineProperties() }); js> expectTypeError(function() { Object.defineProperties({}) }); diff --git a/tests/testsrc/doctests/object.defineproperty.doctest b/tests/testsrc/doctests/object.defineproperty.doctest index 9c97db78fa..99ca62d33c 100644 --- a/tests/testsrc/doctests/object.defineproperty.doctest +++ b/tests/testsrc/doctests/object.defineproperty.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.defineProperty -function defineProperty() { [native code for Object.defineProperty, arity=3] } +function defineProperty() { + [native code] +} js> expectTypeError(function() { Object.defineProperty() }); js> expectTypeError(function() { Object.defineProperty({}) }); diff --git a/tests/testsrc/doctests/object.extensible.doctest b/tests/testsrc/doctests/object.extensible.doctest index 4807256577..ff302e5141 100644 --- a/tests/testsrc/doctests/object.extensible.doctest +++ b/tests/testsrc/doctests/object.extensible.doctest @@ -5,14 +5,18 @@ js> load('testsrc/doctests/util.js'); js> Object.isExtensible; -function isExtensible() { [native code for Object.isExtensible, arity=1] } +function isExtensible() { + [native code] +} js> expectTypeError(function() { Object.isExtensible() }); js> [undefined, null, true, 1, 'hello'].forEach(function(value) { > expectTypeError(function() { Object.isExtensible(value) }) > }) js> Object.preventExtensions; -function preventExtensions() { [native code for Object.preventExtensions, arity=1] } +function preventExtensions() { + [native code] +} js> expectTypeError(function() { Object.preventExtensions() }); js> [undefined, null, true, 1, 'hello'].forEach(function(value) { > expectTypeError(function() { Object.preventExtensions(value) }) diff --git a/tests/testsrc/doctests/object.freeze.doctest b/tests/testsrc/doctests/object.freeze.doctest index 672a4f4455..b23159c091 100644 --- a/tests/testsrc/doctests/object.freeze.doctest +++ b/tests/testsrc/doctests/object.freeze.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.freeze; -function freeze() { [native code for Object.freeze, arity=1] } +function freeze() { + [native code] +} js> [undefined, null, true, 1, 'hello'].forEach(function(value) { > expectTypeError(function() { Object.freeze(value) }) diff --git a/tests/testsrc/doctests/object.getownpropertydescriptor.doctest b/tests/testsrc/doctests/object.getownpropertydescriptor.doctest index 9baa50c103..13c66b1a2d 100644 --- a/tests/testsrc/doctests/object.getownpropertydescriptor.doctest +++ b/tests/testsrc/doctests/object.getownpropertydescriptor.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.getOwnPropertyDescriptor; -function getOwnPropertyDescriptor() { [native code for Object.getOwnPropertyDescriptor, arity=2] } +function getOwnPropertyDescriptor() { + [native code] +} js> expectTypeError(function() { Object.getOwnPropertyDescriptor() }) diff --git a/tests/testsrc/doctests/object.getownpropertynames.doctest b/tests/testsrc/doctests/object.getownpropertynames.doctest index 0b7b0d7426..decf7150f3 100644 --- a/tests/testsrc/doctests/object.getownpropertynames.doctest +++ b/tests/testsrc/doctests/object.getownpropertynames.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.getOwnPropertyNames; -function getOwnPropertyNames() { [native code for Object.getOwnPropertyNames, arity=1] } +function getOwnPropertyNames() { + [native code] +} js> expectTypeError(function() { Object.getOwnPropertyNames() }) js> [undefined, null, true, 1, 'hello'].forEach(function(value) { diff --git a/tests/testsrc/doctests/object.getprototypeof.doctest b/tests/testsrc/doctests/object.getprototypeof.doctest index 511d2df120..53bed05bfb 100644 --- a/tests/testsrc/doctests/object.getprototypeof.doctest +++ b/tests/testsrc/doctests/object.getprototypeof.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.getPrototypeOf; -function getPrototypeOf() { [native code for Object.getPrototypeOf, arity=1] } +function getPrototypeOf() { + [native code] +} js> expectTypeError(function() { Object.getPrototypeOf() }) js> [undefined, null, true, 1, 'hello'].forEach(function(value) { diff --git a/tests/testsrc/doctests/object.isfrozen.doctest b/tests/testsrc/doctests/object.isfrozen.doctest index fce13bb597..103b910db7 100644 --- a/tests/testsrc/doctests/object.isfrozen.doctest +++ b/tests/testsrc/doctests/object.isfrozen.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.isFrozen -function isFrozen() { [native code for Object.isFrozen, arity=1] } +function isFrozen() { + [native code] +} js> expectTypeError(function() { Object.isFrozen() }); js> [undefined, null, true, 1, 'hello'].forEach(function(value) { diff --git a/tests/testsrc/doctests/object.issealed.doctest b/tests/testsrc/doctests/object.issealed.doctest index 2bdb3188a8..2a31856d49 100644 --- a/tests/testsrc/doctests/object.issealed.doctest +++ b/tests/testsrc/doctests/object.issealed.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.isSealed -function isSealed() { [native code for Object.isSealed, arity=1] } +function isSealed() { + [native code] +} js> expectTypeError(function() { Object.isSealed() }); js> [undefined, null, true, 1, 'hello'].forEach(function(value) { diff --git a/tests/testsrc/doctests/object.keys.doctest b/tests/testsrc/doctests/object.keys.doctest index 9cbd501bf7..72fb37b078 100644 --- a/tests/testsrc/doctests/object.keys.doctest +++ b/tests/testsrc/doctests/object.keys.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.keys; -function keys() { [native code for Object.keys, arity=1] } +function keys() { + [native code] +} js> expectTypeError(function() { Object.keys() }) js> [undefined, null, true, 1, 'hello'].forEach(function(value) { diff --git a/tests/testsrc/doctests/object.seal.doctest b/tests/testsrc/doctests/object.seal.doctest index eaa1adefec..6effb20d41 100644 --- a/tests/testsrc/doctests/object.seal.doctest +++ b/tests/testsrc/doctests/object.seal.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> Object.seal; -function seal() { [native code for Object.seal, arity=1] } +function seal() { + [native code] +} js> [undefined, null, true, 1, 'hello'].forEach(function(value) { > expectTypeError(function() { Object.seal(value) }) diff --git a/tests/testsrc/doctests/parseint.doctest b/tests/testsrc/doctests/parseint.doctest index 805e5b1280..7bda9e442f 100644 --- a/tests/testsrc/doctests/parseint.doctest +++ b/tests/testsrc/doctests/parseint.doctest @@ -4,7 +4,7 @@ js> parseInt function parseInt() { - [native code, arity=2] + [native code] } js> parseInt("0"); 0 diff --git a/tests/testsrc/doctests/string.trim.doctest b/tests/testsrc/doctests/string.trim.doctest index 4c043e3eb3..2b434dd632 100644 --- a/tests/testsrc/doctests/string.trim.doctest +++ b/tests/testsrc/doctests/string.trim.doctest @@ -5,7 +5,9 @@ js> load('testsrc/doctests/util.js'); js> String.prototype.trim; -function trim() { [native code for String.trim, arity=0] } +function trim() { + [native code] +} js> String.prototype.trim.call({toString: function() { return "a" }}); a