From 3c5548034c4a066917cd364561360c685852304b Mon Sep 17 00:00:00 2001 From: extremeheat Date: Wed, 6 Aug 2025 03:42:37 -0400 Subject: [PATCH] compiler: Allow writing code to access sizing methods This allows for custom types to implement length prefixed containers (used in bedrock-protocol and protodef-protobuf) --- src/compiler.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/compiler.js b/src/compiler.js index 9c8f057..4d5f171 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -62,6 +62,7 @@ class CompiledProtodef { this.sizeOfCtx = sizeOfCtx this.writeCtx = writeCtx this.readCtx = readCtx + this.setVariable('sizeOfCtx', sizeOfCtx) } read (buffer, cursor, type) { @@ -361,6 +362,12 @@ class WriteCompiler extends Compiler { if (args.length > 0) return '(' + code + `)(${value}, buffer, ${offsetExpr}, ` + args.map(name => this.getField(name)).join(', ') + ')' return '(' + code + `)(${value}, buffer, ${offsetExpr})` } + + callTypeSize (value, type, args = []) { + const code = `ctx.sizeOfCtx['${type}']` + if (args.length > 0) return '(' + code + `)(${value}, ` + args.map(name => this.getField(name)).join(', ') + ')' + return '(' + code + `)(${value})` + } } class SizeOfCompiler extends Compiler {