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

Enhancement: Detect/Warn on multilevel cycles #120

Open
Olipro opened this issue Oct 25, 2023 · 4 comments
Open

Enhancement: Detect/Warn on multilevel cycles #120

Olipro opened this issue Oct 25, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@Olipro
Copy link

Olipro commented Oct 25, 2023

This doesn't affect Greybel at all, but it can completely hang a script in-game. It requires a certain amount of cyclic references - simply doing Foo = {}; Foo.__isa = Foo doesn't affect it at all. This is a bug that's been fixed in the first 2023 release of MiniScript, but of course, GreyHack isn't using that.

From in-game testing, the hang seems to occur somewhere inside hash calculation of map keys - I suspect it's somewhere on the order of O(n^2) (or worse) since paring down the example below will hang the game for just a few seconds.

For the purposes of the enhancement, just warning on the detection of any cycles would be a fantastic feature.

The bad code:

Foo = {}
Foo.a = "str"
Foo.b = false
Foo.c = true
Foo.d = false
Foo.id = 0

Foo[0] = new Foo

Foo[1] = new Foo
Foo[1].id = 1
Foo[1].type = "str2"
Foo[1].en_str = "str3"
Foo.x = Foo[1]

Foo[2] = new Foo
Foo[2].id = 2
Foo[2].type = "str4"
Foo[2].en_str = "str5"
Foo.y = Foo[2]

Foo[3] = new Foo
Foo.z = Foo[3]

Foo[4] = new Foo
Foo[4].id = 2
Foo[4].type = "str6"
Foo[4].en_str = "str7"
Foo.a = Foo[4]

Foo[5] = new Foo
Foo[5].id = 2
Foo[5].type = "str6"
Foo[5].en_str = "str7"
Foo.b = Foo[5]

Bar = {}
Bar[new Foo[4]] = 3 //DEAD
@ayecue ayecue added the enhancement New feature or request label Oct 25, 2023
@ayecue
Copy link
Owner

ayecue commented Oct 25, 2023

So your suggestion is to add some kind of warning message when running code with Greybel that has that cyclic pattern?

@Olipro
Copy link
Author

Olipro commented Oct 25, 2023

Running, building, even while editing with a squiggly yellow underline if that's possible :)

Just something that mentions that if you're coding for GreyHack, you have a chance to produce a hanging, unkillable script.

@ayecue
Copy link
Owner

ayecue commented Oct 25, 2023

Well detecting this while building or editing will be pretty hard since I imagine that this requires some quite complex logic. Especially since there is no runtime logic available.

But detecting cyclic data while running the interpreter is a lot more feasible.

@Olipro
Copy link
Author

Olipro commented Oct 25, 2023

Yeah, I suppose you could do a rudimentary check without executing by evaluating the script in the background - but the caveat there is that once any sort of map mutation is occurring in a funcRef, you have to skip over it since the script shouldn't actually execute any operations.

In any case, even just during runtime could prevent folks from shooting themselves in the foot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants