KSP-KOS / KOS

Fully programmable autopilot mod for KSP. Originally By Nivekk
Other
697 stars 230 forks source link

Calling a delegate made by putting @ on a built-in suffix caused internal error. #2773

Open stevemeacham opened 4 years ago

stevemeacham commented 4 years ago

Internal Error. Contact the kOS developers with the phrase 'impossible FromPrimitiveWithAssert(kOS.Safe.Encapsulation.Suffixes.DelegateSuffixResult) was attempted'.

I would provide output.log if I knew where it is. There was a stderr.log created in KSP_ROOT at or about the time this happened, but it is zero bytes.

Here is the Kerboscript code that produced this. It's the PARAMETER statement for an anonymous function:

PARAMETER c is Lex(), t IS O:new(), a IS t:ADD@.

Here is the offending source file. It was already "compressed" when it was being run, so you get to see the ugly but compact version:

// oolib.ks - see oolib.md
// Copyright © 2020 Steve Meacham and contributors
// Lic: GPL-3.0-or-later

PARAMETER nsr. nsr:CLEAR.

LOCAL O IS Lex(
"new", {
PARAMETER t IS Lex(). t:CLEAR.
RETURN t.
}).

LOCAL M IS Lex(
"new", {
PARAMETER c is Lex(), t IS O:new(), a IS t:ADD@.
a("put", {PARAMETER k,v. SET c[k] TO v. RETURN t.}).
a("del", {PARAMETER k. c:REMOVE(k). RETURN t.}).
a("get", {PARAMETER k. RETURN c[k].}).
a("all", {RETURN c.}).
RETURN t.
}).

M:new(Lex("oo", M:new(Lex("Object", O, "Map", M))), O:new(nsr)).
Screen Shot 2020-10-19 at 9 01 34 PM
nuggreat commented 4 years ago

A bit of poking and I think the problem is the interaction between the built in suffix function :ADD() and the @ as this code also throws the same error

LOCAL munLatLng IS MUN:GEOPOSITIONLATLNG@.
PRINT munLatLng(0,0).

where as trying the same thing with a lexicon

LOCAL l IS LEX("a",{PARAMETER a. RETURN a.}).
LOCAL la is l:a@.
PRINT la("a").

works just fine.

So a temporary work around would be to wrap the suffix function in your own function.

stevemeacham commented 4 years ago

Great detective work! Thank you.

The workaround works, of course, but is too verbose for me to use in this instance. I'll just call Lexicon:ADD() directly in the four places I was using the delegate.

I can use functional programming techniques with delegates, closures, and so on, in so many ways, I can't complain that one minor use case doesn't work and has workarounds.

Dunbaratu commented 4 years ago

@stevemeacham Thank you for reporting this. @nuggreat Thanks for diagnosing this and reducing it to a minimal example.

I'm going to change the title to something that makes it easier for me to see what it's about when I see it in the issue list.