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

feat(stdlib): Convert runtime printing utils to @unsafe #1135

Merged
merged 1 commit into from
Mar 6, 2022

Conversation

ospencer
Copy link
Member

@ospencer ospencer commented Feb 7, 2022

The major thing to notice in this PR is that runtime/numberUtils.gr is no longer --compilation-mode=runtime. This means that it uses regular allocated Grain memory, which means that string literals, etc. are allowed to be used. Ideally, malloc.gr/gc.gr/memory.gr will be the only files in runtime mode.

@ospencer ospencer requested a review from a team February 7, 2022 05:19
@ospencer ospencer self-assigned this Feb 7, 2022
let decimalCount32Dummy = (n: WasmI32) => 0n
let utoa32BufferedDummy = (a: WasmI32, b: WasmI32, c: WasmI32) => void

// When these boxes are backpatched, the reference count of each function will
Copy link
Member

@peblair peblair Feb 22, 2022

Choose a reason for hiding this comment

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

Should we maybe emit a warning or something when using boxes in --compilation-mode=runtime? It seems like this could bite us

(not for this PR)

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe? The plan is for only malloc.gr and gc.gr to use runtime mode, so it might not be necessary. But it's not really just a problem for boxes; it's for anything that gets allocated in runtime mode that gets mutated by things outside of the runtime.

I actually thought about a couple of solutions for this:

  • The fake reference count of runtime objects is 1; could just set it to 0xefffffff. While this would work, I'd be worried about some large program that could decRef enough that the problem would occur, and it'd be a hell of a time debugging that.
  • Could alter free to never free anything in runtime land. Actually fairly easy to implement with lowish perf overhead, though I dislike that the reference counts for runtime values would be essentially random. That's more of an aesthetic problem though since the count doesn't matter at all.
  • Disallow mutable boxes/arrays/record fields in runtime mode. That'd completely prevent this (which is handy) though of course we couldn't use those constructs (which isn't a big deal since we don't need them, but it'd prevent doing backrefs for debugging like this, etc.).

But also maybe this comment is fine 🤷

Up to you if you want to make an issue for it.

Copy link
Member

Choose a reason for hiding this comment

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

You know me, I'm always happy with issues where we can have a longer discussion!

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@peblair peblair left a comment

Choose a reason for hiding this comment

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

One minor comment, but this looks 🔥

stdlib/runtime/gc.gr Outdated Show resolved Hide resolved
@ospencer ospencer force-pushed the oscar/unsafe-string branch 2 times, most recently from 0d68959 to ee6fce8 Compare February 27, 2022 19:42
Copy link
Member

@marcusroberts marcusroberts left a comment

Choose a reason for hiding this comment

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

Nice to see real Grain code back!

Copy link
Member

@phated phated left a comment

Choose a reason for hiding this comment

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

🎉 Awesome!

@@ -936,101 +968,108 @@ export let utoa32Buffered = (buf, value, radix) => {
}
}

@unsafe
export let utoa32 = (value, radix) => {
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
throw Exception.InvalidArgument(
"toString() radix argument must be between 2 and 36"
)
}
let str = if (WasmI32.eqz(value)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you just remove let str here and at the end where you return it?

}

@unsafe
export let utoa64 = (value, radix) => {
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
throw Exception.InvalidArgument(
"toString() radix argument must be between 2 and 36"
)
}
let str = if (WasmI64.eqz(value)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this str variable?

let decimalCount32Dummy = (n: WasmI32) => 0n
let utoa32BufferedDummy = (a: WasmI32, b: WasmI32, c: WasmI32) => void

// When these boxes are backpatched, the reference count of each function will
Copy link
Member

Choose a reason for hiding this comment

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

You know me, I'm always happy with issues where we can have a longer discussion!

Comment on lines +40 to +41
[],
[...](String, StringList),
Copy link
Member

Choose a reason for hiding this comment

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

This is 🔥 Love it

Copy link
Member

Choose a reason for hiding this comment

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

Did we have an issue about this somewhere that should be closed by this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably #552 but that's not quite ready to be closed.

Base automatically changed from oscar/no-closure-globals to main March 6, 2022 03:08
@ospencer ospencer merged commit 403e1d2 into main Mar 6, 2022
@ospencer ospencer deleted the oscar/unsafe-string branch March 6, 2022 04:30
@github-actions github-actions bot mentioned this pull request May 16, 2022
@github-actions github-actions bot mentioned this pull request May 31, 2022
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.

4 participants