diff options
Diffstat (limited to 'prototypes')
| -rw-r--r-- | prototypes/connections/module.c | 8 | ||||
| -rw-r--r-- | prototypes/connections/module.h | 4 | ||||
| -rw-r--r-- | prototypes/connections/test-module.c | 49 |
3 files changed, 56 insertions, 5 deletions
diff --git a/prototypes/connections/module.c b/prototypes/connections/module.c index 5e2b98f..7af1d5c 100644 --- a/prototypes/connections/module.c +++ b/prototypes/connections/module.c @@ -356,8 +356,11 @@ PURE_COLD PureResult pure_module_load(const void *bytes, size_t size, PureModule out->initial[i] = read_double(at + 8); if (!isfinite(out->initial[i])) REFUSE("non-finite initial value", i); } else { - if (read32(at + 12) || read32(at + 8) > UINT16_MAX) REFUSE("state reference", i); + uint32_t flags = read32(at + 12); + if ((flags && (out->state_type[i] != PURE_STATE_CURSOR || flags != PURE_CURSOR_STAYS)) || + read32(at + 8) > UINT16_MAX) REFUSE("state reference", i); out->state_ref[i] = (uint16_t)read32(at + 8); + out->state_flags[i] = (uint8_t)flags; } } for (uint32_t i = 0; i < out->collections; ++i, at += COLLECTION_BYTES) { @@ -648,7 +651,8 @@ static void move_cursors(const PureModule *m, double *values, uint32_t text, siz if (removed) { if (position >= at + removed) position -= (double)removed; else if (position > at) position = (double)at; - } else if (position >= at) position += (double)inserted; + } else if (position > at || (position == at && !(m->state_flags[c] & PURE_CURSOR_STAYS))) + position += (double)inserted; values[c] = position; } } diff --git a/prototypes/connections/module.h b/prototypes/connections/module.h index 16b755c..c27f836 100644 --- a/prototypes/connections/module.h +++ b/prototypes/connections/module.h @@ -46,6 +46,9 @@ typedef enum { PURE_FIELD_U32 = 1, PURE_FIELD_BYTES = 2 } PureFieldType; typedef enum { PURE_STATE_NUM = 0, PURE_STATE_TEXT = 1, PURE_STATE_CURSOR = 2, PURE_STATE_LIST = 3, PURE_STATE_INDEX = 4 } PureStateType; +/* A cursor that stays before text inserted at it (a mark); other cursors go + * after it (a caret). */ +enum { PURE_CURSOR_STAYS = 1 }; /* A table view has one row per item of a list; its columns are number or * text views evaluated for one row at a time. */ typedef enum { PURE_VIEW_NUM = 0, PURE_VIEW_TEXT = 1, PURE_VIEW_TABLE = 2 } PureViewType; @@ -80,6 +83,7 @@ typedef struct { char input_keys[PURE_MODULE_INPUTS][PURE_MODULE_NAME]; uint8_t state_type[PURE_MODULE_STATES]; uint16_t state_ref[PURE_MODULE_STATES]; /* text: constant; cursor: text; list: record; index: list */ + uint8_t state_flags[PURE_MODULE_STATES]; /* cursor: PURE_CURSOR_STAYS */ PureInputShape input[PURE_MODULE_INPUTS]; PureField field[PURE_MODULE_FIELDS]; double initial[PURE_MODULE_STATES]; diff --git a/prototypes/connections/test-module.c b/prototypes/connections/test-module.c index b0666fe..950fa88 100644 --- a/prototypes/connections/test-module.c +++ b/prototypes/connections/test-module.c @@ -33,7 +33,7 @@ typedef struct { uint8_t op; uint32_t arg; double value; } O; /* Text (type 1, ref = constant), cursor (type 2, ref = text state), list * (type 3, ref = record) and index (type 4, ref = list state) states follow * the number states; text views follow the number views. */ -typedef struct { const char *name; uint32_t type, ref; } T; +typedef struct { const char *name; uint32_t type, ref; } T; /* type: the state type, its flags from bit 8 */ /* A record field: a number and its default, or text (text = 1) whose * default is the constant numbered initial. */ typedef struct { const char *name; uint32_t text; double initial; } RF; @@ -108,7 +108,12 @@ static size_t build(unsigned char *out, Spec spec) { p += 40; size_t n = 0; for (size_t i = 0; i < spec.ns; ++i) { p = put32(p, offset[n++]); p = put32(p, 0); p = put_double(p, spec.s[i].initial); } - for (size_t i = 0; i < spec.nt; ++i) { p = put32(p, offset[n++]); p = put32(p, spec.t[i].type); p = put32(p, spec.t[i].ref); p = put32(p, 0); } + for (size_t i = 0; i < spec.nt; ++i) { + p = put32(p, offset[n++]); + p = put32(p, spec.t[i].type & 0xFF); + p = put32(p, spec.t[i].ref); + p = put32(p, spec.t[i].type >> 8); + } for (size_t i = 0; i < spec.nc; ++i) { p = put32(p, offset[n++]); p = put32(p, 1); p = put32(p, 1); } uint32_t field_offsets[64], field_start = 0; size_t nf = 0; @@ -1714,6 +1719,43 @@ static void deferred(void) { deferred_messages(); } +/* Text inserted at a cursor goes before it; at a mark (a cursor that stays), + * after it. Everywhere else, and when text is removed, both move alike. */ +static void marks(void) { + enum { MARK = 2 | PURE_CURSOR_STAYS << 8 }; + static const T textual[] = {{"note", 1, 0}, {"at", 2, 0}, {"mark", MARK, 0}}; + static const char *const constants[] = {"ab"}; + static const I inputs[] = {{"typed", NULL, PURE_KIND_TEXT, false, NULL, 0}, KEY("keep", "KeyK"), KEY("back", "Backspace")}; + static const O code[] = { + {CUL, 1, 0}, {TI, 0, 0}, {TINS, 0, 0}, /* 0 typed: at the cursor */ + {CUL, 1, 0}, {CS, 2, 0}, /* 3 keep: mark = at */ + {CUL, 1, 0}, {C, 0, -1}, {CM, 0, 0}, {CUL, 1, 0}, {TREM, 0, 0}, /* 5 back */ + }; + static const H handlers[] = {{1, 0, 3}, {3, 3, 2}, {5, 5, 5}}; + Spec spec = {NULL, 0, inputs, LEN(inputs), NULL, 0, handlers, LEN(handlers), code, LEN(code), + NULL, 0, textual, 3, constants, 1, NULL, 0, NULL, 0, NULL, 0}; + if (load(spec, &module_a) != PURE_OK) { fprintf(stderr, "%s\n", error); abort(); } + CHECK(module_a.state_flags[2] == PURE_CURSOR_STAYS && !module_a.state_flags[1]); + PureApp app; + CHECK(pure_app_start(&app, &module_a, (PureData){0}) == PURE_OK); + CHECK(pure_app_text(&app, 0, (PureSpan){(const unsigned char *)"x", 1}) == PURE_OK); + CHECK(!strcmp(state_text(&app, 0), "xab") && app.values[1] == 1 && app.values[2] == 0); /* the mark stayed */ + CHECK(pure_app_input(&app, 1, PURE_EDGE_DOWN) == PURE_OK && app.values[2] == 1); + CHECK(pure_app_text(&app, 0, (PureSpan){(const unsigned char *)"yz", 2}) == PURE_OK); + CHECK(!strcmp(state_text(&app, 0), "xyzab") && app.values[1] == 3 && app.values[2] == 1); + CHECK(pure_app_input(&app, 2, PURE_EDGE_DOWN) == PURE_OK && app.values[1] == 2 && app.values[2] == 1); + CHECK(pure_app_input(&app, 2, PURE_EDGE_DOWN) == PURE_OK && app.values[1] == 1 && app.values[2] == 1); /* gathered */ + pure_app_stop(&app); + /* A flag only on a cursor, and only this one. */ + static const T wrong_flag[] = {{"note", 1, 0}, {"at", 2 | 2 << 8, 0}, {"mark", MARK, 0}}; + static const T flagged_text[] = {{"note", 1 | PURE_CURSOR_STAYS << 8, 0}, {"at", 2, 0}, {"mark", 2, 0}}; + Spec bad = spec; + bad.t = wrong_flag; + CHECK(load(bad, &module_b) == PURE_INVALID && strstr(error, "state reference")); + bad.t = flagged_text; + CHECK(load(bad, &module_b) == PURE_INVALID && strstr(error, "state reference")); +} + /* A host points at an item: the index a view shows as its position moves * there, as an event with no handler, saved like a key event; deferred, it * joins the burst and a failed save undoes it with the rest. */ @@ -1751,6 +1793,7 @@ int main(void) { messages(); message_mutations(); texts(); + marks(); text_mutations(); lists(); pointing(); @@ -1760,6 +1803,6 @@ int main(void) { stored_states(); deferred(); printf("{\"sizeof_module\":%zu,\"sizeof_app\":%zu}\n", sizeof(PureModule), sizeof(PureApp)); - puts("{\"module_tests\":\"verified-load-atomic-events-faults-signed-zero-if-else-replace-by-name-rejections-mutations-messages-documents-typed-verification-text-cursors-utf8-views-rollback-replace-lists-indices-undo-migration-joins-tables-columns-pick-slide-stored-equivalence-crashes-frames-messages-migration-deferred-bursts-pointing\"}"); + puts("{\"module_tests\":\"verified-load-atomic-events-faults-signed-zero-if-else-replace-by-name-rejections-mutations-messages-documents-typed-verification-text-cursors-utf8-views-rollback-replace-lists-indices-undo-migration-joins-tables-columns-pick-slide-stored-equivalence-crashes-frames-messages-migration-deferred-bursts-pointing-marks\"}"); return 0; } |
