Skip to content

Commit 1080af5

Browse files
fix bugs and work on a new feature
1 parent 4a6d90c commit 1080af5

1 file changed

Lines changed: 97 additions & 2 deletions

File tree

static/extensions/AndrewGaming587/agBuffer.js

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@
446446
allowDropAnywhere: true,
447447
canDragDuplicate: true
448448
},
449+
"---",
449450
{
450451
opcode: 'forEach',
451452
text: 'for each [BYTE] of [BUFFER]',
@@ -457,7 +458,52 @@
457458
}
458459
}
459460
},
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+
},
461507
{
462508
blockType: BlockType.LABEL,
463509
text: "Visual Blocks"
@@ -506,6 +552,13 @@
506552
substack: generator.descendSubstack(block, 'SUBSTACK')
507553
}
508554
},
555+
reader: (generator, block) => {
556+
generator.script.yields = true
557+
return {
558+
kind: 'input',
559+
substack: generator.descendSubstack(block, 'SUBSTACK')
560+
}
561+
},
509562
},
510563
js: {
511564
builder: (node, compiler, imports) => {
@@ -520,7 +573,21 @@
520573
const stackSource = compiler.source;
521574
compiler.source = originalSource;
522575
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);
523589
}
590+
524591
}
525592
};
526593
}
@@ -556,7 +623,7 @@
556623
}
557624
builderAppendBuffer({VALUE}, util) {
558625
if ((VALUE == undefined || VALUE == null) && !agBuffer.disableErrorHandling) return;
559-
VALUE = new ArrayBuffer(VALUE)
626+
VALUE = new ArrayBufferType(VALUE)
560627
let bi = util.thread._agBufferBuilderIndex ?? []
561628
if (bi[bi.length-1]) {
562629
let buffer = bi[bi.length-1]
@@ -811,7 +878,35 @@
811878

812879
util.startBranch(1, true);
813880
}
881+
readerCurrentIndex({}, util) {
882+
let bi = util.thread._agBufferReaderIndex ?? []
883+
return bi[bi.length-1] ? Cast.toNumber(bi[bi.length-1].index) : 0
884+
}
814885

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+
}
815910
}
816911

817912
vm.agBuffer = agBuffer

0 commit comments

Comments
 (0)