#!/usr/bin/env python3 """Generate bounded typed values for the standard hosted Kryon widget ABI.""" from pathlib import Path import re root = Path(__file__).resolve().parent.parent modules = ['session', 'geometry', 'drawing_props', 'text_props', 'image_props', 'button_props', 'control_props', 'text_input', 'text_input_props', 'scroll', 'scroll_props', 'semantic', 'text_align', 'checkbox_props', 'toggle_props', 'tab_bar_props', 'box_props'] schema = {} for path in [*(root / 'build/packages/kryon/src/ui' / (m + '.zi') for m in modules), root / 'src/cells/diary_ui_types.zi', root / 'src/cells/inbe_ui_types.zi', root / 'src/cells/lists_ui_types.zi', root / 'src/cells/lumi_ui_types.zi', root / 'src/cells/lumi_types.zi']: for name, body in re.findall(r'(\w+)\s*::\s*struct\s*\{([^{}]*)\}', path.read_text()): schema[name] = [(n, re.sub(r'\s+', '', t.split('//')[0])) for n, t in re.findall(r'^\s*(\w+)\s*:\s*([^\n;]+)', body, re.M)] roots = ['TextProps','ImageProps','ButtonProps','TextAreaProps','TextAreaResult', 'ScrollProps','ScrollResult','DiaryUiFrame','Session','InbeUiFrame', 'CheckboxProps','CheckboxValueResult','ToggleProps','ToggleValueResult', 'TextFieldProps','TextFieldResult','Tab','TabBarProps','TabBarResult', 'BoxProps','ListsUiFrame','ListsUiRow','LumiUiFrame','LumiChatRow','LumiTask','LumiAction','LumiTools','LumiDonation'] needed = [] def discover(t): if t.startswith('[]'): discover(t[2:]); return if t in schema and t not in needed: for _, s in schema[t]: discover(s) needed.append(t) for t in roots: discover(t) lines = ['// Generated by scripts/generate-widget-codecs.py; bounded public widget values.', '#import "bundle_host"', '#import "byte_text_linux"', '#import, file "value_codec.zi";', '#import, file "diary_ui_types.zi";', '#import, file "inbe_ui_types.zi";', '#import, file "lists_ui_types.zi";', '#import, file "lumi_ui_types.zi";', '#import, file "lumi_types.zi";', *(f'#import "{m}"' for m in modules), 'using HostValueKind;', ''] def scalar(t): return 'HostString' if t == 'string' else 'HostReal' if t.startswith('float') else 'HostUnsigned' if t.startswith('u') else 'HostInteger' for t in needed: lines += [f'Widget{t}Codec :: struct {{', f' fields: [{len(schema[t])}]HostField'] for n,s in schema[t]: if s in needed: lines += [f' {n}: Widget{s}Codec'] if s.startswith('[]'): b=s[2:] lines += [f' {n}_values: [1024]HostValue', f' {n}_codecs: [1024]Widget{b}Codec', f' {n}_decoded: [1024]{b}'] lines += ['}', '', f'WidgetEncode{t} :: (data: {t}, codec: *Widget{t}Codec) -> HostValue {{', ' value: HostValue', ' value.kind = cast(s32)HostRecord', f' value.type_name = ModuleTextData("{t}")', ' value.fields = *codec.fields[0]', f' value.field_count = {len(schema[t])}'] for i,(n,s) in enumerate(schema[t]): v=f'codec.fields[{i}].value'; lines += [f' codec.fields[{i}].name = ModuleTextData("{n}")'] if s in needed: lines += [f' {v} = WidgetEncode{s}(data.{n}, *codec.{n})'] elif s.startswith('[]'): b=s[2:] lines += [f' {v}.kind = cast(s32)HostSlice', f' {v}.type_name = ModuleTextData("{s}")', f' {v}.length = cast(usize)data.{n}.count', f' if {v}.length > cast(usize)1024 {{ {v}.length = cast(usize)1024 }}', f' {v}.elements = *codec.{n}_values[0]', ' {', ' index: usize = 0', f' while index < {v}.length {{', f' codec.{n}_values[index] = WidgetEncode{b}(data.{n}[index], *codec.{n}_codecs[index])', ' index += cast(usize)1', ' }', ' }'] elif s=='string': lines += [f' {v} = HostValue.{{.kind = cast(s32)HostString, .type_name = ModuleTextData("string"), .data = ModuleTextData(data.{n}), .length = cast(usize)data.{n}.count}}'] elif s.startswith('float'): lines += [f' {v} = HostValue.{{.kind = cast(s32)HostReal, .type_name = ModuleTextData("{s}"), .real = cast(float64)data.{n}}}'] else: lines += [f' {v} = ModuleScalar("{s}", ' + (f'ifx data.{n} then 1 else 0)' if s=='bool' else f'cast(s64)data.{n})')] lines += [' return value', '}', '', f'WidgetDecode{t} :: (value: HostValue, codec: *Widget{t}Codec) -> {t} {{', f' data: {t}'] for i,(n,s) in enumerate(schema[t]): v=f'value.fields[{i}].value' if s in needed: lines += [f' data.{n} = WidgetDecode{s}({v}, *codec.{n})'] elif s.startswith('[]'): b=s[2:] lines += [' {', ' index: usize = 0', f' while index < {v}.length {{', f' codec.{n}_decoded[index] = WidgetDecode{b}({v}.elements[index], *codec.{n}_codecs[index])', ' index += cast(usize)1', ' }', ' }', f' // Slice storage is attached by the rendering effect after decoding.'] elif s=='string': lines += [f' data.{n} = TextFromBytes(BytesFromPointer({v}.data, cast(u64){v}.length))'] elif s=='bool': lines += [f' data.{n} = {v}.integer != 0'] else: lines += [f' data.{n} = cast({s}){v}.' + ('real' if s.startswith('float') else 'bits' if s.startswith('u') else 'integer')] lines += [' return data', '}', '', f'WidgetValid{t} :: (value: HostValue) -> bool {{', f' if value.kind != cast(s32)HostRecord || TextFromCString(value.type_name) != "{t}" ||', f' value.field_count != {len(schema[t])} || value.fields == null {{ return false }}'] for i,(n,s) in enumerate(schema[t]): v=f'value.fields[{i}].value' lines += [f' if TextFromCString(value.fields[{i}].name) != "{n}" {{ return false }}'] if s in needed: lines += [f' if !WidgetValid{s}({v}) {{ return false }}'] elif s.startswith('[]'): b=s[2:]; lines += [f' if {v}.kind != cast(s32)HostSlice || TextFromCString({v}.type_name) != "{s}" ||', f' {v}.length > cast(usize)1024 || ({v}.length > cast(usize)0 && {v}.elements == null) {{ return false }}', ' {', ' index: usize = 0', f' while index < {v}.length {{', f' if !WidgetValid{b}({v}.elements[index]) {{ return false }}', ' index += cast(usize)1', ' }', ' }'] else: lines += [f' if !ModuleScalarValid({v}, "{s}", cast(s32){scalar(s)}) {{ return false }}'] lines += [' return true', '}', ''] (root / 'src/cells/widget_codec.zi').write_text('\n'.join(lines))