Skip to content

Commit

Permalink
Make built-in function source spec compliant.
Browse files Browse the repository at this point in the history
  • Loading branch information
aardvark179 committed Feb 24, 2025
1 parent 94ff59e commit e08f7e0
Show file tree
Hide file tree
Showing 31 changed files with 110 additions and 70 deletions.
4 changes: 1 addition & 3 deletions rhino/src/main/java/org/mozilla/javascript/BaseFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,7 @@ String decompile(int indent, EnumSet<DecompilerFlag> 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");
}
Expand Down
24 changes: 0 additions & 24 deletions rhino/src/main/java/org/mozilla/javascript/IdFunctionObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

package org.mozilla.javascript;

import java.util.EnumSet;

public class IdFunctionObject extends BaseFunction {
private static final long serialVersionUID = -5332312783643935019L;

Expand Down Expand Up @@ -97,28 +95,6 @@ public Scriptable createObject(Context cx, Scriptable scope) {
throw ScriptRuntime.typeErrorById("msg.not.ctor", functionName);
}

@Override
String decompile(int indent, EnumSet<DecompilerFlag> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({}, {}))");
Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/array.every.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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; };

Expand Down
8 changes: 6 additions & 2 deletions tests/testsrc/doctests/array.filter.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions tests/testsrc/doctests/array.find.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions tests/testsrc/doctests/array.findIndex.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions tests/testsrc/doctests/array.forEach.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
// 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 });
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 });
Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/array.isarray.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions tests/testsrc/doctests/array.map.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions tests/testsrc/doctests/array.reduce.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions tests/testsrc/doctests/array.reduceRight.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions tests/testsrc/doctests/array.some.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/date.toisostring.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/date.tojson.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/function.bind.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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({})
Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/object.create.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/object.defineproperties.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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({}) });
Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/object.defineproperty.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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({}) });
Expand Down
8 changes: 6 additions & 2 deletions tests/testsrc/doctests/object.extensible.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/object.freeze.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() })

Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/object.getownpropertynames.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion tests/testsrc/doctests/object.getprototypeof.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit e08f7e0

Please sign in to comment.