1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-25 22:53:16 +00:00

add checks for & _ destructuring pattern

This commit adds three checks to ensure & rest patterns are valid:
1. When checking for '& ensure the value is a symbol before unwrapping
2. Make sure '& is followed by a value
3. Make sure the value following '& is a symbol
This commit is contained in:
Ian Shehadeh 2022-01-19 13:55:05 -05:00
parent db631097b1
commit a8e49d084b

View File

@ -155,7 +155,17 @@ static int destructure(JanetCompiler *c,
JanetSlot nextright = janetc_farslot(c);
Janet subval = values[i];
if (!janet_cstrcmp(janet_unwrap_symbol(subval), "&")) {
if (janet_type(subval) == JANET_SYMBOL && !janet_cstrcmp(janet_unwrap_symbol(subval), "&")) {
if (i + 1 >= len) {
janetc_cerror(c, "expected symbol following '& in destructuring pattern");
return 1;
}
if (janet_type(values[i + 1]) != JANET_SYMBOL) {
janetc_error(c, janet_formatc("expected symbol following '& in destructuring pattern, found %q", values[i + 1]));
return 1;
}
JanetSlot argi = janetc_farslot(c);
JanetSlot arg = janetc_farslot(c);
JanetSlot len = janetc_farslot(c);