|
446 | 446 | allowDropAnywhere: true, |
447 | 447 | canDragDuplicate: true |
448 | 448 | }, |
| 449 | + "---", |
449 | 450 | { |
450 | 451 | opcode: 'forEach', |
451 | 452 | text: 'for each [BYTE] of [BUFFER]', |
|
457 | 458 | } |
458 | 459 | } |
459 | 460 | }, |
460 | | - |
| 461 | + "---", |
| 462 | + { |
| 463 | + opcode: 'readerCurrentIndex', |
| 464 | + text: 'reader index', |
| 465 | + hideFromPalette: true, |
| 466 | + canDragDuplicate: true, |
| 467 | + blockType: BlockType.REPORTER |
| 468 | + }, |
| 469 | + |
| 470 | + { |
| 471 | + opcode: 'reader', |
| 472 | + text: 'reader [BUFFER]', |
| 473 | + blockType: Scratch.BlockType.LOOP, |
| 474 | + hideFromPalette: true, |
| 475 | + arguments: { |
| 476 | + BUFFER: agBuffer.Argument, |
| 477 | + CURRENT: { |
| 478 | + fillIn: 'readerCurrentIndex' |
| 479 | + } |
| 480 | + |
| 481 | + } |
| 482 | + }, |
| 483 | + { |
| 484 | + opcode: 'readerAppend', |
| 485 | + text: 'read [TYPE] value [ENDIAN] for parser', |
| 486 | + blockType: Scratch.BlockType.COMMAND, |
| 487 | + hideFromPalette: true, |
| 488 | + arguments: { |
| 489 | + TYPE:{ |
| 490 | + menu:'DATATYPES', |
| 491 | + type: ArgumentType.STRING |
| 492 | + }, |
| 493 | + ENDIAN: { |
| 494 | + type: ArgumentType.BOOLEAN |
| 495 | + } |
| 496 | + }, |
| 497 | + }, |
| 498 | + { |
| 499 | + opcode: 'readerAppendBuffer', |
| 500 | + text: 'read next [BYTES] for parser', |
| 501 | + blockType: Scratch.BlockType.COMMAND, |
| 502 | + hideFromPalette: true, |
| 503 | + arguments: { |
| 504 | + BYTES: {type: ArgumentType.NUMBER} |
| 505 | + }, |
| 506 | + }, |
461 | 507 | { |
462 | 508 | blockType: BlockType.LABEL, |
463 | 509 | text: "Visual Blocks" |
|
506 | 552 | substack: generator.descendSubstack(block, 'SUBSTACK') |
507 | 553 | } |
508 | 554 | }, |
| 555 | + reader: (generator, block) => { |
| 556 | + generator.script.yields = true |
| 557 | + return { |
| 558 | + kind: 'input', |
| 559 | + substack: generator.descendSubstack(block, 'SUBSTACK') |
| 560 | + } |
| 561 | + }, |
509 | 562 | }, |
510 | 563 | js: { |
511 | 564 | builder: (node, compiler, imports) => { |
|
520 | 573 | const stackSource = compiler.source; |
521 | 574 | compiler.source = originalSource; |
522 | 575 | return new imports.TypedInput(stackSource, imports.TYPE_UNKNOWN); |
| 576 | + }, |
| 577 | + reader: (node, compiler, imports) => { |
| 578 | + const originalSource = compiler.source; |
| 579 | + compiler.source = 'vm.jwArray.Type.toArray(yield* (function*() {'; |
| 580 | + compiler.source += `thread._agBufferReaderIndex ??= [];` |
| 581 | + compiler.source += `thread._agBufferReaderIndex.push({index:0,array:[],buffer:null});` |
| 582 | + //compiler.source += `try{} catch (err) {throw new Error("fancy error catch: " + err)};` |
| 583 | + compiler.descendStack(node.substack, new imports.Frame(false, undefined, true)); |
| 584 | + compiler.source += `return thread._agBufferReaderIndex.pop().array;` |
| 585 | + compiler.source += '})())'; |
| 586 | + const stackSource = compiler.source; |
| 587 | + compiler.source = originalSource; |
| 588 | + return new imports.TypedInput(stackSource, imports.TYPE_UNKNOWN); |
523 | 589 | } |
| 590 | + |
524 | 591 | } |
525 | 592 | }; |
526 | 593 | } |
|
556 | 623 | } |
557 | 624 | builderAppendBuffer({VALUE}, util) { |
558 | 625 | if ((VALUE == undefined || VALUE == null) && !agBuffer.disableErrorHandling) return; |
559 | | - VALUE = new ArrayBuffer(VALUE) |
| 626 | + VALUE = new ArrayBufferType(VALUE) |
560 | 627 | let bi = util.thread._agBufferBuilderIndex ?? [] |
561 | 628 | if (bi[bi.length-1]) { |
562 | 629 | let buffer = bi[bi.length-1] |
|
811 | 878 |
|
812 | 879 | util.startBranch(1, true); |
813 | 880 | } |
| 881 | + readerCurrentIndex({}, util) { |
| 882 | + let bi = util.thread._agBufferReaderIndex ?? [] |
| 883 | + return bi[bi.length-1] ? Cast.toNumber(bi[bi.length-1].index) : 0 |
| 884 | + } |
814 | 885 |
|
| 886 | + reader() { |
| 887 | + return 'noop' |
| 888 | + } |
| 889 | + |
| 890 | + readerAppend({TYPE,ENDIAN = false}, util) { |
| 891 | + let bi = util.thread._agBufferReaderIndex ?? [] |
| 892 | + if (bi[bi.length-1]) { |
| 893 | + let builder = bi[bi.length-1] |
| 894 | + builder.array.push() |
| 895 | + |
| 896 | + } |
| 897 | + } |
| 898 | + readerAppendBuffer({VALUE}, util) { |
| 899 | + if ((VALUE == undefined || VALUE == null) && !agBuffer.disableErrorHandling) return; |
| 900 | + VALUE = new ArrayBufferType(VALUE) |
| 901 | + let bi = util.thread._agBufferReaderIndex ?? [] |
| 902 | + if (bi[bi.length-1]) { |
| 903 | + let buffer = bi[bi.length-1] |
| 904 | + let oldBufferLen = buffer.arrayBuffer.byteLength |
| 905 | + let newBuffer; |
| 906 | + bi[bi.length-1] = newBuffer = new ArrayBufferType(buffer.arrayBuffer.transfer(oldBufferLen + VALUE.arrayBuffer.byteLength)) |
| 907 | + this.writeSubBuffer({INDEX: oldBufferLen, SUBBUFFER: VALUE, BUFFER: newBuffer}) |
| 908 | + } |
| 909 | + } |
815 | 910 | } |
816 | 911 |
|
817 | 912 | vm.agBuffer = agBuffer |
|
0 commit comments