A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(eht): marshal a component's POD struct fields, don't silently drop them
yhh2de7edaa2b49
diff --git a/native/host_js/game.js b/native/host_js/game.jsindex 3ddd3a6c7..049443314 100644--- a/native/host_js/game.js+++ b/native/host_js/game.js@@ -20,6 +20,8 @@ function init() {// design resolution and the clear colour, a Camera gives the projection. The// host no longer decides either — it presents what this camera renders.var stage = world.spawn();+ world.insert(stage, ESEngine.Transform, { position: { x: W * 0.5, y: H * 0.5, z: 0 } });+ world.insert(stage, ESEngine.UINode, {});world.insert(stage, ESEngine.Canvas, {designResolution: { x: W, y: H }, scaleMode: 2 /* Expand */,backgroundColor: { r: 0.07, g: 0.08, b: 0.12, a: 1 } });diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex 0f6eb28ea..31c016ea6 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -5,7 +5,7 @@ Symbols: 1335 stable · 17 beta · 1 deprecated## ABI_LAYOUT_HASH — const```-"56d27a450a1ab9c6"+"7454a67d3890bcbc"```## ANCHOR_AXES — constdiff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.mdindex 1b2eb1bc9..6abcd9dfa 100644--- a/sdk/etc/index.native.api.md+++ b/sdk/etc/index.native.api.md@@ -5,7 +5,7 @@ Symbols: 1354 stable · 22 beta · 1 deprecated## ABI_LAYOUT_HASH — const```-"56d27a450a1ab9c6"+"7454a67d3890bcbc"```## ANCHOR_AXES — constdiff --git a/sdk/src/component.generated.ts b/sdk/src/component.generated.tsindex cf7ddd070..51c71156f 100644--- a/sdk/src/component.generated.ts+++ b/sdk/src/component.generated.ts@@ -15,7 +15,7 @@ import type { Dimension, Padding } from './wasm.generated';* getAbiLayoutHash(); BuiltinBridge.connect() compares them and refuses to* run on mismatch, because mismatched offsets read the wrong heap bytes.*/-export const ABI_LAYOUT_HASH = '56d27a450a1ab9c6';+export const ABI_LAYOUT_HASH = '7454a67d3890bcbc';export interface AssetFieldMeta {field: string;diff --git a/sdk/src/ecs/ptrAccessors.generated.ts b/sdk/src/ecs/ptrAccessors.generated.tsindex 5657ff4a6..3fe64e5fb 100644--- a/sdk/src/ecs/ptrAccessors.generated.ts+++ b/sdk/src/ecs/ptrAccessors.generated.ts@@ -344,6 +344,7 @@ export interface FlexContainerPtrData {alignItems: number;alignContent: number;gap: Vec2;+ padding: { left: number; top: number; right: number; bottom: number; };}export function fillFlexContainer(@@ -356,6 +357,7 @@ export function fillFlexContainer(out.alignItems = u8[ptr + 3];out.alignContent = u8[ptr + 4];const gap_ = out.gap; gap_.x = f32[(ptr + 8) >> 2]; gap_.y = f32[((ptr + 8) >> 2) + 1];+ const padding_ = out.padding; padding_.left = f32[(ptr + 16) >> 2]; padding_.top = f32[(ptr + 20) >> 2]; padding_.right = f32[(ptr + 24) >> 2]; padding_.bottom = f32[(ptr + 28) >> 2];}export function writeFlexContainer(@@ -368,6 +370,7 @@ export function writeFlexContainer(u8[ptr + 3] = data.alignItems;u8[ptr + 4] = data.alignContent;f32[(ptr + 8) >> 2] = data.gap.x; f32[((ptr + 8) >> 2) + 1] = data.gap.y;+ f32[(ptr + 16) >> 2] = data.padding.left; f32[(ptr + 20) >> 2] = data.padding.top; f32[(ptr + 24) >> 2] = data.padding.right; f32[(ptr + 28) >> 2] = data.padding.bottom;}export function createFlexContainerData(): FlexContainerPtrData {@@ -378,6 +381,7 @@ export function createFlexContainerData(): FlexContainerPtrData {alignItems: 0,alignContent: 0,gap: { x: 0, y: 0 },+ padding: { left: 0, top: 0, right: 0, bottom: 0 },};}@@ -1392,9 +1396,24 @@ export function createUIMaskData(): UIMaskPtrData {export interface UINodePtrData {position: number;display: number;+ width: { value: number; unit: number; };+ height: { value: number; unit: number; };+ minWidth: { value: number; unit: number; };+ minHeight: { value: number; unit: number; };+ maxWidth: { value: number; unit: number; };+ maxHeight: { value: number; unit: number; };flexGrow: number;flexShrink: number;+ flexBasis: { value: number; unit: number; };alignSelf: number;+ marginLeft: { value: number; unit: number; };+ marginTop: { value: number; unit: number; };+ marginRight: { value: number; unit: number; };+ marginBottom: { value: number; unit: number; };+ insetLeft: { value: number; unit: number; };+ insetTop: { value: number; unit: number; };+ insetRight: { value: number; unit: number; };+ insetBottom: { value: number; unit: number; };}export function fillUINode(@@ -1403,9 +1422,24 @@ export function fillUINode(): void {out.position = u8[ptr];out.display = u8[ptr + 1];+ const width_ = out.width; width_.value = f32[(ptr + 4) >> 2]; width_.unit = u8[ptr + 8];+ const height_ = out.height; height_.value = f32[(ptr + 12) >> 2]; height_.unit = u8[ptr + 16];+ const minWidth_ = out.minWidth; minWidth_.value = f32[(ptr + 20) >> 2]; minWidth_.unit = u8[ptr + 24];+ const minHeight_ = out.minHeight; minHeight_.value = f32[(ptr + 28) >> 2]; minHeight_.unit = u8[ptr + 32];+ const maxWidth_ = out.maxWidth; maxWidth_.value = f32[(ptr + 36) >> 2]; maxWidth_.unit = u8[ptr + 40];+ const maxHeight_ = out.maxHeight; maxHeight_.value = f32[(ptr + 44) >> 2]; maxHeight_.unit = u8[ptr + 48];out.flexGrow = f32[(ptr + 52) >> 2];out.flexShrink = f32[(ptr + 56) >> 2];+ const flexBasis_ = out.flexBasis; flexBasis_.value = f32[(ptr + 60) >> 2]; flexBasis_.unit = u8[ptr + 64];out.alignSelf = u8[ptr + 68];+ const marginLeft_ = out.marginLeft; marginLeft_.value = f32[(ptr + 72) >> 2]; marginLeft_.unit = u8[ptr + 76];+ const marginTop_ = out.marginTop; marginTop_.value = f32[(ptr + 80) >> 2]; marginTop_.unit = u8[ptr + 84];+ const marginRight_ = out.marginRight; marginRight_.value = f32[(ptr + 88) >> 2]; marginRight_.unit = u8[ptr + 92];+ const marginBottom_ = out.marginBottom; marginBottom_.value = f32[(ptr + 96) >> 2]; marginBottom_.unit = u8[ptr + 100];+ const insetLeft_ = out.insetLeft; insetLeft_.value = f32[(ptr + 104) >> 2]; insetLeft_.unit = u8[ptr + 108];+ const insetTop_ = out.insetTop; insetTop_.value = f32[(ptr + 112) >> 2]; insetTop_.unit = u8[ptr + 116];+ const insetRight_ = out.insetRight; insetRight_.value = f32[(ptr + 120) >> 2]; insetRight_.unit = u8[ptr + 124];+ const insetBottom_ = out.insetBottom; insetBottom_.value = f32[(ptr + 128) >> 2]; insetBottom_.unit = u8[ptr + 132];}export function writeUINode(@@ -1414,18 +1448,48 @@ export function writeUINode(): void {u8[ptr] = data.position;u8[ptr + 1] = data.display;+ f32[(ptr + 4) >> 2] = data.width.value; u8[ptr + 8] = data.width.unit;+ f32[(ptr + 12) >> 2] = data.height.value; u8[ptr + 16] = data.height.unit;+ f32[(ptr + 20) >> 2] = data.minWidth.value; u8[ptr + 24] = data.minWidth.unit;+ f32[(ptr + 28) >> 2] = data.minHeight.value; u8[ptr + 32] = data.minHeight.unit;+ f32[(ptr + 36) >> 2] = data.maxWidth.value; u8[ptr + 40] = data.maxWidth.unit;+ f32[(ptr + 44) >> 2] = data.maxHeight.value; u8[ptr + 48] = data.maxHeight.unit;f32[(ptr + 52) >> 2] = data.flexGrow;f32[(ptr + 56) >> 2] = data.flexShrink;+ f32[(ptr + 60) >> 2] = data.flexBasis.value; u8[ptr + 64] = data.flexBasis.unit;u8[ptr + 68] = data.alignSelf;+ f32[(ptr + 72) >> 2] = data.marginLeft.value; u8[ptr + 76] = data.marginLeft.unit;+ f32[(ptr + 80) >> 2] = data.marginTop.value; u8[ptr + 84] = data.marginTop.unit;+ f32[(ptr + 88) >> 2] = data.marginRight.value; u8[ptr + 92] = data.marginRight.unit;+ f32[(ptr + 96) >> 2] = data.marginBottom.value; u8[ptr + 100] = data.marginBottom.unit;+ f32[(ptr + 104) >> 2] = data.insetLeft.value; u8[ptr + 108] = data.insetLeft.unit;+ f32[(ptr + 112) >> 2] = data.insetTop.value; u8[ptr + 116] = data.insetTop.unit;+ f32[(ptr + 120) >> 2] = data.insetRight.value; u8[ptr + 124] = data.insetRight.unit;+ f32[(ptr + 128) >> 2] = data.insetBottom.value; u8[ptr + 132] = data.insetBottom.unit;}export function createUINodeData(): UINodePtrData {return {position: 0,display: 0,+ width: { value: 0, unit: 0 },+ height: { value: 0, unit: 0 },+ minWidth: { value: 0, unit: 0 },+ minHeight: { value: 0, unit: 0 },+ maxWidth: { value: 0, unit: 0 },+ maxHeight: { value: 0, unit: 0 },flexGrow: 0,flexShrink: 0,+ flexBasis: { value: 0, unit: 0 },alignSelf: 0,+ marginLeft: { value: 0, unit: 0 },+ marginTop: { value: 0, unit: 0 },+ marginRight: { value: 0, unit: 0 },+ marginBottom: { value: 0, unit: 0 },+ insetLeft: { value: 0, unit: 0 },+ insetTop: { value: 0, unit: 0 },+ insetRight: { value: 0, unit: 0 },+ insetBottom: { value: 0, unit: 0 },};}diff --git a/sdk/src/ptrLayouts.generated.ts b/sdk/src/ptrLayouts.generated.tsindex 069db5879..d78f0cecf 100644--- a/sdk/src/ptrLayouts.generated.ts+++ b/sdk/src/ptrLayouts.generated.ts@@ -4,12 +4,21 @@* @details Generated by EHT - DO NOT EDIT*/-type PtrFieldType = 'f32' | 'i32' | 'u32' | 'bool' | 'u8' | 'vec2' | 'vec3' | 'vec4' | 'quat' | 'color';+type PtrFieldType = 'f32' | 'i32' | 'u32' | 'bool' | 'u8' | 'vec2' | 'vec3' | 'vec4' | 'quat' | 'color' | 'struct';++/** A registered POD struct field (Dimension, Padding): its members, at+ * offsets relative to the field. */+interface PtrStructMember {+ readonly name: string;+ readonly type: PtrFieldType;+ readonly offset: number;+}interface PtrFieldDesc {readonly name: string;readonly type: PtrFieldType;readonly offset: number;+ readonly members?: readonly PtrStructMember[];}export interface PtrLayout {@@ -109,6 +118,7 @@ export const PTR_LAYOUTS: Record<string, PtrLayout> = {{ name: 'alignItems', type: 'u8', offset: 3 },{ name: 'alignContent', type: 'u8', offset: 4 },{ name: 'gap', type: 'vec2', offset: 8 },+ { name: 'padding', type: 'struct', offset: 16, members: [{ name: 'left', type: 'f32', offset: 0 }, { name: 'top', type: 'f32', offset: 4 }, { name: 'right', type: 'f32', offset: 8 }, { name: 'bottom', type: 'f32', offset: 12 }] },],},Interactable: {@@ -368,9 +378,24 @@ export const PTR_LAYOUTS: Record<string, PtrLayout> = {fields: [{ name: 'position', type: 'u8', offset: 0 },{ name: 'display', type: 'u8', offset: 1 },+ { name: 'width', type: 'struct', offset: 4, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'height', type: 'struct', offset: 12, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'minWidth', type: 'struct', offset: 20, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'minHeight', type: 'struct', offset: 28, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'maxWidth', type: 'struct', offset: 36, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'maxHeight', type: 'struct', offset: 44, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },{ name: 'flexGrow', type: 'f32', offset: 52 },{ name: 'flexShrink', type: 'f32', offset: 56 },+ { name: 'flexBasis', type: 'struct', offset: 60, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },{ name: 'alignSelf', type: 'u8', offset: 68 },+ { name: 'marginLeft', type: 'struct', offset: 72, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'marginTop', type: 'struct', offset: 80, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'marginRight', type: 'struct', offset: 88, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'marginBottom', type: 'struct', offset: 96, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'insetLeft', type: 'struct', offset: 104, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'insetTop', type: 'struct', offset: 112, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'insetRight', type: 'struct', offset: 120, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },+ { name: 'insetBottom', type: 'struct', offset: 128, members: [{ name: 'value', type: 'f32', offset: 0 }, { name: 'unit', type: 'u8', offset: 4 }] },],},UIVisual: {diff --git a/src/esengine/bindings/WebBindings.generated.cpp b/src/esengine/bindings/WebBindings.generated.cppindex 5aebdd827..e41058837 100644--- a/src/esengine/bindings/WebBindings.generated.cpp+++ b/src/esengine/bindings/WebBindings.generated.cpp@@ -2024,6 +2024,7 @@ static_assert(offsetof(esengine::ecs::FlexContainer, justifyContent) == 2, "ABIstatic_assert(offsetof(esengine::ecs::FlexContainer, alignItems) == 3, "ABI offset drift: esengine::ecs::FlexContainer.alignItems (EHT expected 3)");static_assert(offsetof(esengine::ecs::FlexContainer, alignContent) == 4, "ABI offset drift: esengine::ecs::FlexContainer.alignContent (EHT expected 4)");static_assert(offsetof(esengine::ecs::FlexContainer, gap) == 8, "ABI offset drift: esengine::ecs::FlexContainer.gap (EHT expected 8)");+static_assert(offsetof(esengine::ecs::FlexContainer, padding) == 16, "ABI offset drift: esengine::ecs::FlexContainer.padding (EHT expected 16)");static_assert(offsetof(esengine::ecs::Interactable, enabled) == 0, "ABI offset drift: esengine::ecs::Interactable.enabled (EHT expected 0)");static_assert(offsetof(esengine::ecs::Interactable, blockRaycast) == 1, "ABI offset drift: esengine::ecs::Interactable.blockRaycast (EHT expected 1)");static_assert(offsetof(esengine::ecs::Interactable, raycastTarget) == 2, "ABI offset drift: esengine::ecs::Interactable.raycastTarget (EHT expected 2)");@@ -2198,9 +2199,24 @@ static_assert(offsetof(esengine::ecs::UIMask, enabled) == 0, "ABI offset drift:static_assert(offsetof(esengine::ecs::UIMask, mode) == 1, "ABI offset drift: esengine::ecs::UIMask.mode (EHT expected 1)");static_assert(offsetof(esengine::ecs::UINode, position) == 0, "ABI offset drift: esengine::ecs::UINode.position (EHT expected 0)");static_assert(offsetof(esengine::ecs::UINode, display) == 1, "ABI offset drift: esengine::ecs::UINode.display (EHT expected 1)");+static_assert(offsetof(esengine::ecs::UINode, width) == 4, "ABI offset drift: esengine::ecs::UINode.width (EHT expected 4)");+static_assert(offsetof(esengine::ecs::UINode, height) == 12, "ABI offset drift: esengine::ecs::UINode.height (EHT expected 12)");+static_assert(offsetof(esengine::ecs::UINode, minWidth) == 20, "ABI offset drift: esengine::ecs::UINode.minWidth (EHT expected 20)");+static_assert(offsetof(esengine::ecs::UINode, minHeight) == 28, "ABI offset drift: esengine::ecs::UINode.minHeight (EHT expected 28)");+static_assert(offsetof(esengine::ecs::UINode, maxWidth) == 36, "ABI offset drift: esengine::ecs::UINode.maxWidth (EHT expected 36)");+static_assert(offsetof(esengine::ecs::UINode, maxHeight) == 44, "ABI offset drift: esengine::ecs::UINode.maxHeight (EHT expected 44)");static_assert(offsetof(esengine::ecs::UINode, flexGrow) == 52, "ABI offset drift: esengine::ecs::UINode.flexGrow (EHT expected 52)");static_assert(offsetof(esengine::ecs::UINode, flexShrink) == 56, "ABI offset drift: esengine::ecs::UINode.flexShrink (EHT expected 56)");+static_assert(offsetof(esengine::ecs::UINode, flexBasis) == 60, "ABI offset drift: esengine::ecs::UINode.flexBasis (EHT expected 60)");static_assert(offsetof(esengine::ecs::UINode, alignSelf) == 68, "ABI offset drift: esengine::ecs::UINode.alignSelf (EHT expected 68)");+static_assert(offsetof(esengine::ecs::UINode, marginLeft) == 72, "ABI offset drift: esengine::ecs::UINode.marginLeft (EHT expected 72)");+static_assert(offsetof(esengine::ecs::UINode, marginTop) == 80, "ABI offset drift: esengine::ecs::UINode.marginTop (EHT expected 80)");+static_assert(offsetof(esengine::ecs::UINode, marginRight) == 88, "ABI offset drift: esengine::ecs::UINode.marginRight (EHT expected 88)");+static_assert(offsetof(esengine::ecs::UINode, marginBottom) == 96, "ABI offset drift: esengine::ecs::UINode.marginBottom (EHT expected 96)");+static_assert(offsetof(esengine::ecs::UINode, insetLeft) == 104, "ABI offset drift: esengine::ecs::UINode.insetLeft (EHT expected 104)");+static_assert(offsetof(esengine::ecs::UINode, insetTop) == 112, "ABI offset drift: esengine::ecs::UINode.insetTop (EHT expected 112)");+static_assert(offsetof(esengine::ecs::UINode, insetRight) == 120, "ABI offset drift: esengine::ecs::UINode.insetRight (EHT expected 120)");+static_assert(offsetof(esengine::ecs::UINode, insetBottom) == 128, "ABI offset drift: esengine::ecs::UINode.insetBottom (EHT expected 128)");static_assert(offsetof(esengine::ecs::UIVisual, visualType) == 0, "ABI offset drift: esengine::ecs::UIVisual.visualType (EHT expected 0)");static_assert(offsetof(esengine::ecs::UIVisual, texture) == 4, "ABI offset drift: esengine::ecs::UIVisual.texture (EHT expected 4)");static_assert(offsetof(esengine::ecs::UIVisual, color) == 8, "ABI offset drift: esengine::ecs::UIVisual.color (EHT expected 8)");@@ -2221,7 +2237,7 @@ static_assert(offsetof(esengine::ecs::Velocity, angular) == 12, "ABI offset drif// ABI Hash -- runtime handshake against the SDK bundle// =============================================================================-static const char* kEsAbiLayoutHash = "56d27a450a1ab9c6";+static const char* kEsAbiLayoutHash = "7454a67d3890bcbc";std::string esengineGetAbiLayoutHash() {return std::string(kEsAbiLayoutHash);diff --git a/tools/eht/generators/ptr_layout.py b/tools/eht/generators/ptr_layout.pyindex 59a8e4bae..a7e0f919c 100644--- a/tools/eht/generators/ptr_layout.py+++ b/tools/eht/generators/ptr_layout.py@@ -106,8 +106,32 @@ class PtrLayoutGenerator:return (12, 4)return None+ def _struct_members(self, t: str) -> List[Dict]:+ """Member (name, ptr type, offset-within-struct) of a registered POD+ struct, laid out exactly as _custom_struct_size_align sizes it."""+ members: List[Dict] = []+ offset = 0+ for member_name, member_cpp in self.types.CUSTOM_STRUCTS[t]:+ align = self.TYPE_ALIGNS[member_cpp]+ if offset % align != 0:+ offset += align - (offset % align)+ members.append({+ 'name': member_name,+ 'type': self.CPP_TO_PTR_TYPE[member_cpp],+ 'offset': offset,+ })+ offset += self.TYPE_SIZES[member_cpp]+ return members+def _get_ptr_type(self, cpp_type: str, prop: Property) -> Optional[str]:t = self.types.clean_type(cpp_type)+ # A registered POD struct (Dimension, Padding) is marshalled member by+ # member. Before this it had no accessor at all: on the web that was+ # invisible (embind's object path filled it in), but the native registry+ # IS these accessors, so every such field was silently dropped — a+ # UINode's width never reached the engine on a device.+ if t in self.types.CUSTOM_STRUCTS:+ return 'struct'if t in self.CPP_TO_PTR_TYPE:if t == 'glm::vec4':return 'color' if is_color_field(prop, self.types) else 'vec4'@@ -139,11 +163,21 @@ class PtrLayoutGenerator:ptr_type = self._get_ptr_type(prop.cpp_type, prop)if ptr_type is not None:- fields.append({+ field = {'name': prop.name,'type': ptr_type,'offset': offset,- })+ }+ if ptr_type == 'struct':+ field['members'] = self._struct_members(self.types.clean_type(prop.cpp_type))+ fields.append(field)+ elif sa is not None:+ # Sized (so later offsets stay right) but not marshalled. Say so+ # every run: a field no accessor covers is a field the native+ # registry silently drops.+ print(f" NOTE: {comp.name}.{prop.name}: type '{prop.cpp_type}' has no "+ f"pointer accessor; it does not cross to a native core",+ file=sys.stderr)offset += sizeif fields:@@ -192,12 +226,21 @@ class PtrLayoutGenerator:' * @details Generated by EHT - DO NOT EDIT',' */','',- "type PtrFieldType = 'f32' | 'i32' | 'u32' | 'bool' | 'u8' | 'vec2' | 'vec3' | 'vec4' | 'quat' | 'color';",+ "type PtrFieldType = 'f32' | 'i32' | 'u32' | 'bool' | 'u8' | 'vec2' | 'vec3' | 'vec4' | 'quat' | 'color' | 'struct';",+ '',+ '/** A registered POD struct field (Dimension, Padding): its members, at',+ ' * offsets relative to the field. */',+ 'interface PtrStructMember {',+ ' readonly name: string;',+ ' readonly type: PtrFieldType;',+ ' readonly offset: number;',+ '}','','interface PtrFieldDesc {',' readonly name: string;',' readonly type: PtrFieldType;',' readonly offset: number;',+ ' readonly members?: readonly PtrStructMember[];','}','','export interface PtrLayout {',@@ -213,13 +256,29 @@ class PtrLayoutGenerator:lines.append(f" ptrFn: '{layout['ptrFn']}',")lines.append(' fields: [')for f in layout['fields']:- lines.append(f" {{ name: '{f['name']}', type: '{f['type']}', offset: {f['offset']} }},")+ if f['type'] == 'struct':+ members = ', '.join(+ f"{{ name: '{m['name']}', type: '{m['type']}', offset: {m['offset']} }}"+ for m in f['members'])+ lines.append(f" {{ name: '{f['name']}', type: 'struct', "+ f"offset: {f['offset']}, members: [{members}] }},")+ else:+ lines.append(f" {{ name: '{f['name']}', type: '{f['type']}', offset: {f['offset']} }},")lines.append(' ],')lines.append(' },')lines.extend(['};', ''])return '\n'.join(lines)+ @staticmethod+ def _struct_ts_type(field: Dict) -> str:+ return '{ ' + ' '.join(f'{m["name"]}: number;' for m in field['members']) + ' }'++ def _member_expr(self, m: Dict, field_offset: int) -> tuple:+ """(index expression, byte expression) for a struct member's slot."""+ off = field_offset + m['offset']+ return (f'(ptr + {off}) >> 2' if off else 'ptr >> 2', f'ptr + {off}' if off else 'ptr')+def _ts_type_for(self, ptr_type: str) -> str:return {'f32': 'number', 'i32': 'number', 'u32': 'number',@@ -252,7 +311,7 @@ class PtrLayoutGenerator:# Generate data interfacelines.append(f'export interface {name}PtrData {{')for f in fields:- ts_type = self._ts_type_for(f['type'])+ ts_type = self._struct_ts_type(f) if f['type'] == 'struct' else self._ts_type_for(f['type'])lines.append(f' {f["name"]}: {ts_type};')lines.append('}')lines.append('')@@ -286,6 +345,17 @@ class PtrLayoutGenerator:lines.append(f' const {fn}_ = out.{fn}; {fn}_.x = f32[{idx_expr}]; {fn}_.y = f32[({idx_expr}) + 1]; {fn}_.z = f32[({idx_expr}) + 2]; {fn}_.w = f32[({idx_expr}) + 3];')elif ft == 'color':lines.append(f' const {fn}_ = out.{fn}; {fn}_.r = f32[{idx_expr}]; {fn}_.g = f32[({idx_expr}) + 1]; {fn}_.b = f32[({idx_expr}) + 2]; {fn}_.a = f32[({idx_expr}) + 3];')+ elif ft == 'struct':+ reads = []+ for m in f['members']:+ m_idx, m_byte = self._member_expr(m, byte_off)+ if m['type'] == 'f32':+ reads.append(f'{fn}_.{m["name"]} = f32[{m_idx}];')+ elif m['type'] in ('u8', 'bool'):+ reads.append(f'{fn}_.{m["name"]} = u8[{m_byte}];')+ else:+ reads.append(f'{fn}_.{m["name"]} = u32[{m_idx}];')+ lines.append(f' const {fn}_ = out.{fn}; ' + ' '.join(reads))lines.append('}')lines.append('')@@ -318,6 +388,17 @@ class PtrLayoutGenerator:lines.append(f' f32[{idx_expr}] = data.{fn}.x; f32[({idx_expr}) + 1] = data.{fn}.y; f32[({idx_expr}) + 2] = data.{fn}.z; f32[({idx_expr}) + 3] = data.{fn}.w;')elif ft == 'color':lines.append(f' f32[{idx_expr}] = data.{fn}.r; f32[({idx_expr}) + 1] = data.{fn}.g; f32[({idx_expr}) + 2] = data.{fn}.b; f32[({idx_expr}) + 3] = data.{fn}.a;')+ elif ft == 'struct':+ writes = []+ for m in f['members']:+ m_idx, m_byte = self._member_expr(m, byte_off)+ if m['type'] == 'f32':+ writes.append(f'f32[{m_idx}] = data.{fn}.{m["name"]};')+ elif m['type'] in ('u8', 'bool'):+ writes.append(f'u8[{m_byte}] = data.{fn}.{m["name"]};')+ else:+ writes.append(f'u32[{m_idx}] = data.{fn}.{m["name"]};')+ lines.append(' ' + ' '.join(writes))lines.append('}')lines.append('')@@ -337,6 +418,9 @@ class PtrLayoutGenerator:lines.append(f' {fn}: {{ x: 0, y: 0, z: 0 }},')elif ft in ('vec4', 'quat'):lines.append(f' {fn}: {{ x: 0, y: 0, z: 0, w: 0 }},')+ elif ft == 'struct':+ zeros = ', '.join(f'{m["name"]}: 0' for m in f['members'])+ lines.append(f' {fn}: {{ {zeros} }},')elif ft == 'color':lines.append(f' {fn}: {{ r: 0, g: 0, b: 0, a: 0 }},')lines.append(' };')