Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
JavaScript
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384export function parseLearningBlock(code) { let value; try { value = JSON.parse(code); } catch { return null; } if (!value || typeof value !== "object" || Array.isArray(value)) return null; if (["note", "definition", "example", "warning"].includes(value.type)) { if (typeof value.content !== "string" || value.content.length > 30000) return null; if ( value.title !== undefined && (typeof value.title !== "string" || value.title.length > 200) ) return null; return { type: value.type, title: value.title || value.type[0].toUpperCase() + value.type.slice(1), content: value.content, }; } if (value.type !== "address-translation") return null; const { pageSize = 4096, mappings = [5, 2, 7, 8, 1, 6], address = 12384, } = value; if ( !Number.isSafeInteger(pageSize) || pageSize < 16 || pageSize > 65536 || pageSize & (pageSize - 1) ) return null; if ( !Array.isArray(mappings) || !mappings.length || mappings.length > 16 || mappings.some( (n) => n !== null && (!Number.isInteger(n) || n < 0 || n > 65535), ) ) return null; if ( !Number.isSafeInteger(address) || address < 0 || address >= pageSize * mappings.length ) return null; if ( value.caption !== undefined && (typeof value.caption !== "string" || value.caption.length > 1000) ) return null; return { type: value.type, pageSize, mappings, address, caption: value.caption || "A page-table lookup changes the page number. The offset stays the same.", };}
export function translateAddress(address, pageSize, mappings) { if ( !Number.isSafeInteger(address) || address < 0 || address >= pageSize * mappings.length ) return null; const page = Math.floor(address / pageSize), offset = address % pageSize, frame = mappings[page]; return { page, offset, frame, physical: frame === null ? null : frame * pageSize + offset, };}