Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1300 Support ES2019 Function.toString() #1537

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -463,9 +463,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
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,7 @@ String decompile(int indent, EnumSet<DecompilerFlag> flags) {
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("[native code");
sb.append(justbody ? "]\n" : "] }\n");
return sb.toString();
}
Expand Down
16 changes: 0 additions & 16 deletions rhino/src/main/java/org/mozilla/javascript/NativeJavaMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.concurrent.CopyOnWriteArrayList;

/**
Expand Down Expand Up @@ -86,21 +85,6 @@ static String scriptSignature(Object[] values) {
return sig.toString();
}

@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("/*\n");
sb.append(toString());
sb.append(justbody ? "*/\n" : "*/}\n");
return sb.toString();
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand Down
10 changes: 7 additions & 3 deletions rhino/src/main/java/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3528,6 +3528,7 @@ private ObjectLiteral objectLiteral() throws IOException {
// So we check a whitelist of tokens to check if we're at the
// first case. (Because of keywords, the second case may be
// many tokens.)
int functionSourceStart = pname.getPosition();
int peeked = peekToken();
if (peeked != Token.COMMA && peeked != Token.COLON && peeked != Token.RC) {
if (peeked == Token.LP) {
Expand All @@ -3550,7 +3551,8 @@ private ObjectLiteral objectLiteral() throws IOException {
propertyName = null;
} else {
propertyName = ts.getString();
ObjectProperty objectProp = methodDefinition(ppos, pname, entryKind);
ObjectProperty objectProp =
methodDefinition(ppos, pname, entryKind, functionSourceStart);
pname.setJsDocNode(jsdocNode);
elems.add(objectProp);
}
Expand Down Expand Up @@ -3685,9 +3687,11 @@ private ObjectProperty plainProperty(AstNode property, int ptt) throws IOExcepti
return pn;
}

private ObjectProperty methodDefinition(int pos, AstNode propName, int entryKind)
throws IOException {
private ObjectProperty methodDefinition(
int pos, AstNode propName, int entryKind, int functionSourceStart) throws IOException {
FunctionNode fn = function(FunctionNode.FUNCTION_EXPRESSION);
fn.setRawSourceStart(functionSourceStart);

// We've already parsed the function name, so fn should be anonymous.
Name name = fn.getFunctionName();
if (name != null && name.length() != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,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 @@ -85,9 +83,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
@@ -0,0 +1,29 @@
package org.mozilla.javascript.tests;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;

public class NativeJavaMethodTest {

@Test
public void toStringOfJS() throws Exception {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject scope = cx.initStandardObjects();

final String expected = "function valueOf() {\n\t[native code]\n}\n";
final String script = "java.math.BigInteger.valueOf.toString()";

final String result =
(String) cx.evaluateString(scope, script, "myScript", 1, null);

assertEquals(expected, result);

return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,23 @@ private Scriptable buildScope(Context cx, Test262Case testCase, int optLevel)
try (Reader reader = new FileReader(harnessPath)) {
String script = Kit.readReader(reader);

if ("nativeFunctionMatcher.js".equalsIgnoreCase(harnessFile)) {
// If block-scoped const is implemented, please delete this
// code.
script =
script.replace(
"const c = source[pos];",
"let c = source[pos];");
script =
script.replace(
"const end = source.indexOf('*/', pos);",
"let end = source.indexOf('*/', pos);");
script =
script.replace(
"const c = source[end];",
"let c = source[end];");
}

return cx.compileString(script, harnessPath, 1, null);
} catch (IOException ioe) {
throw new RuntimeException(
Expand Down
2 changes: 1 addition & 1 deletion tests/testsrc/doctests/array.every.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 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
4 changes: 2 additions & 2 deletions tests/testsrc/doctests/array.filter.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
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
4 changes: 2 additions & 2 deletions tests/testsrc/doctests/array.find.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
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
4 changes: 2 additions & 2 deletions tests/testsrc/doctests/array.findIndex.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
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
4 changes: 2 additions & 2 deletions tests/testsrc/doctests/array.forEach.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// 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
2 changes: 1 addition & 1 deletion tests/testsrc/doctests/array.isarray.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 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
4 changes: 2 additions & 2 deletions tests/testsrc/doctests/array.map.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// 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
4 changes: 2 additions & 2 deletions tests/testsrc/doctests/array.reduce.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
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
4 changes: 2 additions & 2 deletions tests/testsrc/doctests/array.reduceRight.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
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
4 changes: 2 additions & 2 deletions tests/testsrc/doctests/array.some.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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 +14,7 @@ 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
2 changes: 1 addition & 1 deletion tests/testsrc/doctests/date.toisostring.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
2 changes: 1 addition & 1 deletion tests/testsrc/doctests/date.tojson.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
2 changes: 1 addition & 1 deletion tests/testsrc/doctests/function.bind.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
2 changes: 1 addition & 1 deletion tests/testsrc/doctests/object.create.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
2 changes: 1 addition & 1 deletion tests/testsrc/doctests/object.defineproperties.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
2 changes: 1 addition & 1 deletion tests/testsrc/doctests/object.defineproperty.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
4 changes: 2 additions & 2 deletions tests/testsrc/doctests/object.extensible.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
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
2 changes: 1 addition & 1 deletion tests/testsrc/doctests/object.freeze.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
Loading
Loading