writes its Apply: a failed basis or prestructural stops it, then its structure and predicates run in turn
(js: NodeCompiler)
| 389 | |
| 390 | /** writes its Apply: a failed basis or prestructural stops it, then its structure and predicates run in turn */ |
| 391 | private jsApply(js: NodeCompiler): void { |
| 392 | const { basis, preStruct, structure } = this |
| 393 | const predicates = this.inner.predicate |
| 394 | if (basis) { |
| 395 | const condition = basis.jsCond(js, "data") |
| 396 | if (this.children.length === 1) { |
| 397 | js.return(`${condition} || !${js.error(basis)}`) |
| 398 | return |
| 399 | } |
| 400 | js.if(`!(${condition})`, () => js.return(`!${js.error(basis)}`)) |
| 401 | } |
| 402 | if (!structure && !predicates) { |
| 403 | for (const node of preStruct) js.check(node) |
| 404 | js.return(true) |
| 405 | return |
| 406 | } |
| 407 | const conditions = preStruct.map(node => node.jsCond(js, "data")) |
| 408 | if (conditions.length === 1) { |
| 409 | js.if(`!(${conditions[0]})`, () => |
| 410 | js.return(`!${js.error(preStruct[0])}`) |
| 411 | ) |
| 412 | } else if (conditions.length) { |
| 413 | js.if(`!(${conditions.map(c => `(${c})`).join(" && ")})`, () => |
| 414 | js.return( |
| 415 | `!(${conditions.map((c, i) => `(!(${c}) && ${js.error(preStruct[i])})`).join(" || ")})` |
| 416 | ) |
| 417 | ) |
| 418 | } |
| 419 | if (!predicates) { |
| 420 | const sequence = this.inlinedSequence |
| 421 | if (sequence) sequence.compile(js) |
| 422 | else js.return(js.inv(structure!)) |
| 423 | return |
| 424 | } |
| 425 | // read only where a later check follows one that can add issues |
| 426 | if (structure || predicates.length > 1) js.initErrCount() |
| 427 | if (structure) { |
| 428 | // transformed only if the predicates allow it |
| 429 | if (structure.tfs) { |
| 430 | js.if(`!${js.allows(structure)}`, () => |
| 431 | js |
| 432 | .if(`!${js.inv(structure)}`, () => js.return(false)) |
| 433 | .if("ctx.errN > errN", () => js.return(true)) |
| 434 | ) |
| 435 | } else { |
| 436 | js.if(`!${js.inv(structure)}`, () => js.return(false)).if( |
| 437 | "ctx.errN > errN", |
| 438 | () => js.return(true) |
| 439 | ) |
| 440 | } |
| 441 | } |
| 442 | // since predicates can be chained, the first to fail stops this node |
| 443 | for (const predicate of predicates.slice(0, -1)) { |
| 444 | js.if(`!${js.inv(predicate)}`, () => js.return(false)).if( |
| 445 | "ctx.errN > errN", |
| 446 | () => js.return(true) |
| 447 | ) |
| 448 | } |