This repository was archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Polyfill: two conformance fixes #27
Open
rwaldron
wants to merge
2
commits into
tc39:master
Choose a base branch
from
rwaldron:ValidateAtomicAccess
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,26 +66,90 @@ | |
| helpers.push(h); | ||
| } | ||
|
|
||
| function ToBigInt64(value) { | ||
| let n = BigInt(value); | ||
| let int64bit = n % (2n ** 64n); | ||
| if (int64bit > (2n ** 63n)) { | ||
| return int64bit - (2n ** 64n); | ||
| } | ||
| return int64bit; | ||
| } | ||
|
|
||
| function ToInteger(value) { | ||
| let number = Number(value); | ||
| if (number !== number) { | ||
| return 0; | ||
| } | ||
| if (number === 0 || !Number.isFinite(number)) { | ||
| return number; | ||
| } | ||
|
|
||
| return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number)); | ||
| } | ||
|
|
||
| function ToLength(value) { | ||
| var len = ToInteger(value); | ||
| if (len <= 0) { | ||
| return 0; | ||
| } | ||
| if (len > Number.MAX_SAFE_INTEGER) { | ||
| return Number.MAX_SAFE_INTEGER; | ||
| } | ||
| return len; | ||
| } | ||
|
|
||
| function ToIndex(value) { | ||
| let index; | ||
| if (value === undefined) { | ||
| index = 0; | ||
| } else { | ||
| let integerIndex = ToInteger(value); | ||
| if (integerIndex < 0) { | ||
| throw new RangeError('Index out of range'); | ||
| } | ||
| index = ToLength(integerIndex); | ||
| if (integerIndex !== index) { | ||
| throw new RangeError('Index out of range'); | ||
| } | ||
| } | ||
| return index; | ||
| } | ||
|
|
||
| function ValidateAtomicAccess(typedArray, requestIndex) { | ||
| let accessIndex; | ||
| try { | ||
| accessIndex = ToIndex(requestIndex); | ||
| } catch (error) { | ||
| if (error instanceof RangeError) { | ||
| throw new RangeError('Invalid atomic access index'); | ||
| } | ||
| } | ||
| let length = typedArray.length; | ||
| if (accessIndex >= length) { | ||
| throw new RangeError('Invalid atomic access index'); | ||
| } | ||
| return accessIndex; | ||
| } | ||
|
|
||
| // Atomics.waitAsync always returns a promise. Throws standard errors | ||
| // for parameter validation. The promise is resolved with a string as from | ||
| // Atomics.wait, or, in the case something went completely wrong, it is | ||
| // rejected with an error string. | ||
|
|
||
| function waitAsync(ia, index_, value_, timeout_) { | ||
| if (typeof ia != "object" || !(ia instanceof Int32Array) || !(ia.buffer instanceof SharedArrayBuffer)) | ||
| throw new TypeError("Expected shared memory"); | ||
| if (typeof ia != "object" || | ||
| (!(ia instanceof Int32Array) && !(ia instanceof BigInt64Array)) || | ||
| !(ia.buffer instanceof SharedArrayBuffer)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And the IsSharedArrayBuffer(ia.buffer)Which is defined using: var SharedArrayBufferProto_getByteLength = Function.prototype.call.bind(
Object.getOwnPropertyDescriptor(
SharedArrayBuffer.prototype,
"byteLength",
).get,
);
function IsSharedArrayBuffer(obj) {
try {
SharedArrayBufferProto_getByteLength(obj);
return true;
} catch (e) {
return false;
}
} |
||
| throw new TypeError("Expected shared memory"); | ||
| } | ||
|
|
||
| // These conversions only approximate the desired semantics but are | ||
| // close enough for the polyfill. | ||
|
|
||
| let index = index_|0; | ||
| let value = value_|0; | ||
| let index = ValidateAtomicAccess(ia, index_); | ||
| let coersion = ia instanceof BigInt64Array ? (v => ToBigInt64(v)) : (v => v|0); | ||
| let value = coersion(value_); | ||
| let timeout = timeout_ === undefined ? Infinity : +timeout_; | ||
|
|
||
| // Range checking for the index. | ||
|
|
||
| ia[index]; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is replaced by |
||
|
|
||
| // Optimization, avoid the helper thread in this common case. | ||
|
|
||
| if (Atomics.load(ia, index) != value) | ||
|
|
@@ -136,4 +200,3 @@ | |
| writable: true, | ||
| }); | ||
| })(); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ia instanceof TypedArrayline should use:Which is defined using:
Refs: https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag