Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Wasm: Implement checked behaviors. #5002

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 8 commits into from
Aug 30, 2024
Merged

Conversation

sjrd
Copy link
Member

@sjrd sjrd commented Jun 24, 2024

Based on #4993 but otherwise ready to review. The first commit belonging to this PR is "Wasm: Implement checked asInstanceOfs.".

@sjrd sjrd force-pushed the wasm-checked-behaviors branch 4 times, most recently from 3e2bdcf to 8f607a2 Compare June 26, 2024 20:15
@sjrd sjrd force-pushed the wasm-checked-behaviors branch 2 times, most recently from cd80489 to 12f2093 Compare August 5, 2024 11:27
@sjrd sjrd force-pushed the wasm-checked-behaviors branch 3 times, most recently from 69200de to b06100a Compare August 12, 2024 14:13
@sjrd sjrd changed the title CI ONLY Wasm: Implement checked behaviors. Wasm: Implement checked behaviors. Aug 12, 2024
@sjrd
Copy link
Member Author

sjrd commented Aug 12, 2024

This is still based on #4993 but otherwise it's now ready for review.

/cc @tanishiking

@sjrd sjrd marked this pull request as ready for review August 12, 2024 14:15
@sjrd sjrd requested a review from gzm0 August 12, 2024 14:15
Copy link
Contributor

@tanishiking tanishiking left a comment

Choose a reason for hiding this comment

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

Mostly nitpicks, looks good from my side

ThrowableArgConsructorName)
},

instantiateClass(ClassClass, AnyArgConstructorName),
Copy link
Contributor

Choose a reason for hiding this comment

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

duplicated

// TODO: only emit this when required by checked behaviors
jsValueDescription: ((x) =>
(typeof x === 'number')
? ((x === 0 && 1 / x < 0) ? "number(-0)" : ("number(" + x + ")"))
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit]
Object.is(x, -0) might be a bit clearer?

*/

fb += wa.LocalGet(objParam)
fb += wa.Call(genFunctionID.typeTest(DoubleRef))
Copy link
Contributor

Choose a reason for hiding this comment

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

[note]
We reach here if

  • (1) the objParam is JS number
  • (2) the objParam is neither JS number nor an instance of java.lang.Number

typeTest(DoubleRef) will be true for (1) because it calls the tD: (x) => typeof x === 'number'.

fb += LocalGet(indexParam)
fb += LocalGet(strParam)
fb += Call(genFunctionID.stringLength)
fb += I32GeU // unsigned comparison makes negative values of index larger than the length
Copy link
Contributor

Choose a reason for hiding this comment

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

In an extreme scenario, when index=-2^31 and str.length=2^31 + 1, it will be evaluated to false.

Should we care about this corner case?

i32.const -2147483648
i32.const 2147483649
i32.ge_u
;; false

Ah, that never happens in the JS runtimes:

In V8 (used by Chrome and Node), the maximum length is 2^29 - 24 (~1GiB). On 32-bit systems, the maximum length is 2^28 - 16 (~512MiB).
In Firefox, the maximum length is 2^30 - 2 (~2GiB). Before Firefox 65, the maximum length was 2^28 - 1 (~512MiB).
In Safari, the maximum length is 2^31 - 1 (~4GiB).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length

Comment on lines +741 to +748
// check ongoing initialization
if (semantics.moduleInit != CheckedBehavior.Unchecked) {
val initFlagID = genGlobalID.forModuleInitFlag(className)

// if being initialized
fb += wa.GlobalGet(initFlagID)
fb.ifThen() {
if (semantics.moduleInit == CheckedBehavior.Compliant) {
Copy link
Contributor

Choose a reason for hiding this comment

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

[note]
We reach this point when the globalInstance for the class is not yet available. If semantics.moduleInit is set to Compliant, module access will have no effect because it assumes the module has already been initialized by storeModules.

If semantics.moduleInit is set to Fatal, throw an exception.

* block resultType $noNPELabel
* block $npeLabel
* body
* br $npeLabel
Copy link
Contributor

Choose a reason for hiding this comment

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

br $nonNPELabel ?

private def genCheckNonNull(): Unit = {
if (semantics.nullPointers != CheckedBehavior.Unchecked)
private def genCheckNonNullFor(tree: Tree): Unit = {
if (semantics.nullPointers != CheckedBehavior.Unchecked && nullabilityLevelOf(tree) >= Nullable)
Copy link
Contributor

Choose a reason for hiding this comment

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

[note]
We can ignore NonNullableMaybeWasmNullable here, because unlike genAsNonNullOrNPE we don't expect to have (wasm) non-null value on the stack after this null check.

@sjrd sjrd force-pushed the wasm-checked-behaviors branch 2 times, most recently from 589e4c9 to 8be61eb Compare August 25, 2024 09:21
@sjrd
Copy link
Member Author

sjrd commented Aug 25, 2024

Rebased.

@sjrd sjrd force-pushed the wasm-checked-behaviors branch from 8be61eb to aa58b4d Compare August 26, 2024 08:58
@sjrd
Copy link
Member Author

sjrd commented Aug 26, 2024

Rebased on top of the non-nullable reference types.

@sjrd sjrd force-pushed the wasm-checked-behaviors branch 2 times, most recently from e3eba31 to 066a11e Compare August 26, 2024 13:28
@sjrd sjrd force-pushed the wasm-checked-behaviors branch from 066a11e to 906276c Compare August 29, 2024 21:39
@sjrd sjrd requested a review from gzm0 August 29, 2024 21:42
@sjrd sjrd merged commit b23528a into scala-js:main Aug 30, 2024
3 checks passed
@sjrd sjrd deleted the wasm-checked-behaviors branch August 30, 2024 07:23
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