Skip to content

Fix new ArrayBuffer(length) for various input #1959

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

Merged
merged 1 commit into from
Aug 16, 2017
Merged

Fix new ArrayBuffer(length) for various input #1959

merged 1 commit into from
Aug 16, 2017

Conversation

glistening
Copy link
Contributor

Currently new ArrayBuffer(length) does not conform to ES2015.

For example, new ArrayBuffer(length) should not throw RangeError
for length = NaN, undefined, negative number, floating point, and so on.

JerryScript-DCO-1.0-Signed-off-by: Sanggyu Lee [email protected]

@glistening glistening changed the title Fix new ArrayBuffer(length) for variaous input Fix new ArrayBuffer(length) for various input Aug 11, 2017
@glistening
Copy link
Contributor Author

@jiangzidong Could you review this patch?

@jiangzidong
Copy link
Contributor

@glistening I dont think ES2015 allow the length could be NaN, undefined, negative number, floating point.

http://www.ecma-international.org/ecma-262/6.0/#sec-arraybuffer-length see step 5

If SameValueZero(numberLength, byteLength) is false, throw a RangeError exception.

If the length is NaN, undefined, negative number, floating point. they will fail the samevaluezero check

But interesting part is V8 supports NaN, undefined, negative number (-1, 0), floating point. I don't know why.

Correct me if I miss sth in the spec.

@glistening
Copy link
Contributor Author

glistening commented Aug 11, 2017

@jiangzidong Yes, it is hidden in step 3. Please see ToLength in step 3. Please see ToInteger also, which is called from ToLength. It specifies the handling of NaN, negative number, and so on. Test262 checks this inputs also.

@jiangzidong
Copy link
Contributor

@glistening Yes, I know that ToInteger will turn "negative number, NaN and floating number" into non-negative integer. But then it will fail the step5.
for example, var a = new ArrayBuffer(3.4);

  1. Let numberLength be ToNumber(length).

the numberLength is 3.4

  1. Let byteLength be ToLength(numberLength).

the byteLength is 3

  1. If SameValueZero(numberLength, byteLength) is false, throw a RangeError exception.

3 !== 3.4, so it will throw RangeError .

I don't know why test262/v8/spidermonkey don't follow it.

@glistening
Copy link
Contributor Author

glistening commented Aug 11, 2017

@jiangzidong Thank you for correcting me. ES2015 says it should throw.

Test262 and major engines (v8, spidermonkey and JavaScriptCore) seems to follow ES2017, not ES2015. ES2017 uses ToIndex to describe the expected behaviour of ArrayBuffer(length). You can see it in test262 test case comment also.

esid: sec-arraybuffer-length
description: >
The length parameter is converted to a value numeric index value.
info: |

ArrayBuffer( length )

  1. If NewTarget is undefined, throw a TypeError exception.
  2. Let byteLength be ? ToIndex(length).
  3. Return ? AllocateArrayBuffer(NewTarget, byteLength).

ToIndex( value )

  1. If value is undefined, then
    a. Let index be 0.
  2. Else,
    a. Let integerIndex be ? ToInteger(value).
    b. If integerIndex < 0, throw a RangeError exception.
    c. Let index be ! ToLength(integerIndex).
    d. If SameValueZero(integerIndex, index) is false, throw a RangeError exception.
  3. Return index.

I will raise this as Issue to get opinion.

@jiangzidong
Copy link
Contributor

@glistening Wow, I didn't notice that it's ES2017. Thanks for pointing out.

Copy link
Member

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zherczeg
Copy link
Member

(I wouldn't create subsets for such differences, just use the latest standard)

@@ -24,4 +24,5 @@ catch (e)
name = e.name;
}

assert(name === "RangeError");
assert(name === "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is no expection, we dont need to check the name. just

var a = new ArrayBuffer(5.5);
assert(a.byteLength === 5);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jiangzidong I've simplified 24.01.02-006.js ~24.01.02-012.js.


try
{
var a = new ArrayBuffer(-0.9);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also give a case where the length is float and below -1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jiangzidong Now, 24.01.02-012.js covers two more cases that the length is float and below -1.

assert(a.byteLength === 0);

var b = new ArrayBuffer(-1.9);
assert(b.byteLength === 0);
Copy link
Contributor

@jiangzidong jiangzidong Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1.9 and -2.2 should throw error, see ToIndex 2.b

b. If integerIndex < 0, throw a RangeError exception.

Copy link
Contributor Author

@glistening glistening Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jiangzidong Oh, You're right. I will fix right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jiangzidong I've updated PR.

Currently new ArrayBuffer(length) does not conform to ES2017.
Major JS implementations follow ES2017 for ArrayBuffer(length).

For example, new ArrayBuffer(length) should not throw RangeError
for length = NaN, undefined, negative number, floating point, and so on.

JerryScript-DCO-1.0-Signed-off-by: Sanggyu Lee [email protected]
Copy link
Contributor

@jiangzidong jiangzidong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@jiangzidong jiangzidong merged commit c1cff3f into jerryscript-project:master Aug 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants