solve-engine 2.34.4 → 2.34.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/{chunk-VI7YREQQ.js → chunk-4RQROFRZ.js} +3 -3
  2. package/dist/{chunk-VI7YREQQ.js.map → chunk-4RQROFRZ.js.map} +1 -1
  3. package/dist/{chunk-DJRGEWPI.js → chunk-6Q3C57ER.js} +2 -2
  4. package/dist/{chunk-DJRGEWPI.js.map → chunk-6Q3C57ER.js.map} +1 -1
  5. package/dist/{chunk-ESNTBS6Z.js → chunk-ERPPBLSR.js} +3 -3
  6. package/dist/{chunk-ESNTBS6Z.js.map → chunk-ERPPBLSR.js.map} +1 -1
  7. package/dist/chunk-HLZRNZEU.js +2 -0
  8. package/dist/chunk-HLZRNZEU.js.map +1 -0
  9. package/dist/{chunk-T7NEEJYP.cjs → chunk-KTAICPWB.cjs} +3 -3
  10. package/dist/{chunk-T7NEEJYP.cjs.map → chunk-KTAICPWB.cjs.map} +1 -1
  11. package/dist/chunk-LKPCB7UK.js +3 -0
  12. package/dist/chunk-LKPCB7UK.js.map +1 -0
  13. package/dist/{chunk-PDD5YQBD.cjs → chunk-LYLRGYEC.cjs} +4 -4
  14. package/dist/{chunk-PDD5YQBD.cjs.map → chunk-LYLRGYEC.cjs.map} +1 -1
  15. package/dist/{chunk-XWDABNQX.cjs → chunk-MV3UPVZX.cjs} +2 -2
  16. package/dist/{chunk-XWDABNQX.cjs.map → chunk-MV3UPVZX.cjs.map} +1 -1
  17. package/dist/chunk-SZ3QZ7N7.cjs +3 -0
  18. package/dist/chunk-SZ3QZ7N7.cjs.map +1 -0
  19. package/dist/chunk-W4XLVZ6R.cjs +2 -0
  20. package/dist/chunk-W4XLVZ6R.cjs.map +1 -0
  21. package/dist/constants.cjs +1 -1
  22. package/dist/constants.js +1 -1
  23. package/dist/engine.cjs +1 -1
  24. package/dist/engine.js +1 -1
  25. package/dist/engine.worker.cjs +1 -1
  26. package/dist/engine.worker.js +1 -1
  27. package/dist/format.cjs +1 -1
  28. package/dist/format.js +1 -1
  29. package/dist/index.cjs +1 -1
  30. package/dist/index.js +1 -1
  31. package/dist/testing.cjs +2 -2
  32. package/dist/testing.js +1 -1
  33. package/dist/utilities.cjs +1 -1
  34. package/dist/utilities.js +1 -1
  35. package/dist/worker.cjs +2 -2
  36. package/dist/worker.js +1 -1
  37. package/package.json +1 -1
  38. package/dist/chunk-IAXRJ5II.cjs +0 -3
  39. package/dist/chunk-IAXRJ5II.cjs.map +0 -1
  40. package/dist/chunk-R2SZFJTT.cjs +0 -2
  41. package/dist/chunk-R2SZFJTT.cjs.map +0 -1
  42. package/dist/chunk-TZ3Q6GOC.js +0 -3
  43. package/dist/chunk-TZ3Q6GOC.js.map +0 -1
  44. package/dist/chunk-XEIDV5JP.js +0 -2
  45. package/dist/chunk-XEIDV5JP.js.map +0 -1
@@ -0,0 +1,2 @@
1
+ function e(t,n,r){return t.toLocaleString(n,{useGrouping:false,maximumFractionDigits:r,minimumFractionDigits:r})}var o=3,s=1e-4,a=6;function g(t,n,r="en-US"){if(!Number.isFinite(t)||t===0||Number(t.toFixed(n))!==0)return;let i=Number(t.toPrecision(o));if(Math.abs(i)>=s){let u=-Math.floor(Math.log10(Math.abs(i))),m=Math.min(a,u+o-1);return i.toLocaleString(r,{useGrouping:false,minimumFractionDigits:0,maximumFractionDigits:m})}return i.toExponential(o-1).replace(/\.?0+e/,"e")}function f(t,n=2,r=false,i="en-US"){return Number.isInteger(t)?r?t.toLocaleString(i,{minimumFractionDigits:0,maximumFractionDigits:0}):e(Math.trunc(t),i,0):r?t.toLocaleString(i,{maximumFractionDigits:n,minimumFractionDigits:n}):e(t,i,n)}export{g as a,f as b};//# sourceMappingURL=chunk-HLZRNZEU.js.map
2
+ //# sourceMappingURL=chunk-HLZRNZEU.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utilities/Number.ts"],"names":["removeThousandsSeparators","value","locale","decimalPlaces","SIGNIFICANT_DIGITS","EXPONENT_FORM_BELOW","MAX_PLAIN_DECIMALS","tooSmallToPrintText","numberLocale","rounded","leadingZeros","places","autoFormatIntegerOrFloat","number","includeThousandSeparators"],"mappings":"AAoBA,SAASA,EACRC,CAAAA,CACAC,CAAAA,CACAC,CAAAA,CACC,CACD,OAAOF,CAAAA,CAAM,cAAA,CAAeC,CAAAA,CAAQ,CACnC,YAAa,KAAA,CACb,qBAAA,CAAuBC,CAAAA,CACvB,qBAAA,CAAuBA,CACxB,CAAC,CACF,CAGA,IAAMC,EAAqB,CAAA,CASrBC,CAAAA,CAAsB,IAAA,CAGtBC,CAAAA,CAAqB,EAsBpB,SAASC,CAAAA,CACfN,CAAAA,CACAE,CAAAA,CACAK,EAAuB,OAAA,CACF,CAErB,GADI,CAAC,OAAO,QAAA,CAASP,CAAK,CAAA,EAAKA,CAAAA,GAAU,GACrC,MAAA,CAAOA,CAAAA,CAAM,OAAA,CAAQE,CAAa,CAAC,CAAA,GAAM,CAAA,CAAG,OAEhD,IAAMM,EAAU,MAAA,CAAOR,CAAAA,CAAM,WAAA,CAAYG,CAAkB,CAAC,CAAA,CAC5D,GAAI,IAAA,CAAK,GAAA,CAAIK,CAAO,CAAA,EAAKJ,CAAAA,CAAqB,CAI7C,IAAMK,EAAe,CAAC,IAAA,CAAK,KAAA,CAAM,IAAA,CAAK,MAAM,IAAA,CAAK,GAAA,CAAID,CAAO,CAAC,CAAC,EACxDE,CAAAA,CAAS,IAAA,CAAK,GAAA,CAAIL,CAAAA,CAAoBI,EAAeN,CAAAA,CAAqB,CAAC,CAAA,CACjF,OAAOK,EAAQ,cAAA,CAAeD,CAAAA,CAAc,CAC3C,WAAA,CAAa,MACb,qBAAA,CAAuB,CAAA,CACvB,qBAAA,CAAuBG,CACxB,CAAC,CACF,CAEA,OAAOF,CAAAA,CAAQ,cAAcL,CAAAA,CAAqB,CAAC,CAAA,CAAE,OAAA,CAAQ,SAAU,GAAG,CAC3E,CAcO,SAASQ,EACfC,CAAAA,CACAV,CAAAA,CAAwB,EACxBW,CAAAA,CAAqC,KAAA,CACrCN,EAAuB,OAAA,CACtB,CACD,OAAI,MAAA,CAAO,UAAUK,CAAM,CAAA,CACtBC,CAAAA,CAEID,CAAAA,CAAO,eAAeL,CAAAA,CAAc,CAC1C,qBAAA,CAAuB,CAAA,CACvB,sBAAuB,CACxB,CAAC,CAAA,CAGKR,CAAAA,CAA0B,KAAK,KAAA,CAAMa,CAAM,CAAA,CAAGL,CAAAA,CAAc,CAAC,CAAA,CAIjEM,CAAAA,CAEID,CAAAA,CAAO,cAAA,CAAeL,EAAc,CAC1C,qBAAA,CAAuBL,CAAAA,CACvB,qBAAA,CAAuBA,CACxB,CAAC,CAAA,CAKKH,EAA0Ba,CAAAA,CAAQL,CAAAA,CAAcL,CAAa,CACrE","file":"chunk-HLZRNZEU.js","sourcesContent":["/**\n * Render `value` in `locale` with `decimalPlaces` fractional digits and no\n * digit grouping, leaving the locale's own decimal separator alone.\n *\n * Asks `Intl` not to group in the first place rather than formatting with\n * grouping and then deleting a character afterwards. Deleting was wrong twice\n * over. It used `String.replace` with a string pattern, which removes one\n * occurrence, so \"1,234,567\" came back as \"1234,567\" and the surviving comma\n * read as a decimal point. And it chose the character to delete from a\n * two-case switch, \"de-DE\" or a comma for everything else, when a comma is the\n * DECIMAL separator in French, Spanish, Italian, Portuguese and every other\n * comma-decimal locale: deleting it turned 1.5 into \"150\", a hundred times the\n * number, while leaving French's actual group separator (a narrow no-break\n * space) in place. `numberResult.decimalSeparatorLocale` is an unvalidated\n * host string, so no locale can be assumed.\n *\n * An unusable locale tag still throws the `RangeError` `toLocaleString` has\n * always thrown for one, since swallowing it would hide the host's typo behind\n * silently different output.\n */\nfunction removeThousandsSeparators(\n\tvalue: number,\n\tlocale: string,\n\tdecimalPlaces: number\n) {\n\treturn value.toLocaleString(locale, {\n\t\tuseGrouping: false,\n\t\tmaximumFractionDigits: decimalPlaces,\n\t\tminimumFractionDigits: decimalPlaces,\n\t});\n}\n\n/** How many digits of a too-small value are worth showing: enough to read it, not enough to imply precision. */\nconst SIGNIFICANT_DIGITS = 3;\n\n/**\n * Below this magnitude the plain decimal form stops being readable and the\n * exponent form takes over.\n *\n * At 1e-4 three significant digits still fit in six decimal places, which is\n * about as many zeros as a reader will count without losing their place.\n */\nconst EXPONENT_FORM_BELOW = 1e-4;\n\n/** The most decimal places the plain form will spend showing a small value. */\nconst MAX_PLAIN_DECIMALS = 6;\n\n/**\n * A reading for a value that is not zero but would print as one.\n *\n * Two decimal places is the right budget for almost everything the engine\n * answers, and wrong for the answers that live below it: `1 Hz in MHz` printed\n * `0.00 MHz`, which a reader cannot tell from a real zero, and `1 byte in GB`\n * printed `0.00 GB`. Both are correct conversions with nothing left of them.\n *\n * So a magnitude that rounds away is shown to three significant digits instead,\n * as a decimal while the zeros are still countable and in exponent form once\n * they are not: `0.001`, and `1e-6`. Three digits is enough to read the value\n * and few enough not to imply a precision the conversion does not have.\n *\n * Returns undefined when the ordinary rendering already shows something, which\n * is every other value the engine formats, so callers keep the output they had.\n *\n * @param value - The magnitude about to be rendered.\n * @param decimalPlaces - The budget it would be rendered with.\n * @param numberLocale - `Intl` locale, for the decimal separator of the plain form.\n */\nexport function tooSmallToPrintText(\n\tvalue: number,\n\tdecimalPlaces: number,\n\tnumberLocale: string = \"en-US\"\n): string | undefined {\n\tif (!Number.isFinite(value) || value === 0) return undefined;\n\tif (Number(value.toFixed(decimalPlaces)) !== 0) return undefined;\n\n\tconst rounded = Number(value.toPrecision(SIGNIFICANT_DIGITS));\n\tif (Math.abs(rounded) >= EXPONENT_FORM_BELOW) {\n\t\t// The zeros before the first digit, plus the digits themselves. Trailing\n\t\t// zeros are trimmed by asking for no minimum, so `0.001` does not read as\n\t\t// `0.00100` and imply three measured digits.\n\t\tconst leadingZeros = -Math.floor(Math.log10(Math.abs(rounded)));\n\t\tconst places = Math.min(MAX_PLAIN_DECIMALS, leadingZeros + SIGNIFICANT_DIGITS - 1);\n\t\treturn rounded.toLocaleString(numberLocale, {\n\t\t\tuseGrouping: false,\n\t\t\tminimumFractionDigits: 0,\n\t\t\tmaximumFractionDigits: places,\n\t\t});\n\t}\n\t// `1.00e-6` says nothing `1e-6` does not, so the mantissa's trailing zeros go.\n\treturn rounded.toExponential(SIGNIFICANT_DIGITS - 1).replace(/\\.?0+e/, \"e\");\n}\n\n/**\n * Format `number` for display, branching on whether it's a whole number:\n * integers are always rendered with zero decimal places (never padded to\n * `decimalPlaces`), while non-integers are rendered with up to\n * `decimalPlaces` fractional digits. `includeThousandSeparators` controls\n * whether groups are separated (e.g. `\"1,234\"`) per `numberLocale`.\n *\n * @param number - The value to format.\n * @param decimalPlaces - Max fractional digits for non-integer values (default 2).\n * @param includeThousandSeparators - Whether to group digits (default false).\n * @param numberLocale - `Intl`/`toLocaleString` locale to format with (default \"en-US\").\n */\nexport function autoFormatIntegerOrFloat(\n\tnumber: number,\n\tdecimalPlaces: number = 2,\n\tincludeThousandSeparators: boolean = false,\n\tnumberLocale: string = \"en-US\"\n) {\n\tif (Number.isInteger(number)) {\n\t\tif (includeThousandSeparators) {\n\t\t\t// We can return the format early as we don't need to strip thousands\n\t\t\treturn number.toLocaleString(numberLocale, {\n\t\t\t\tminimumFractionDigits: 0,\n\t\t\t\tmaximumFractionDigits: 0,\n\t\t\t});\n\t\t}\n\n\t\treturn removeThousandsSeparators(Math.trunc(number), numberLocale, 0);\n\t}\n\n\t// Decimal\n\tif (includeThousandSeparators) {\n\t\t// We can return the format early as we don't need to strip thousands\n\t\treturn number.toLocaleString(numberLocale, {\n\t\t\tmaximumFractionDigits: decimalPlaces,\n\t\t\tminimumFractionDigits: decimalPlaces,\n\t\t});\n\t}\n\n\t// number.toFixed(decimalPlaces)\n\n\treturn removeThousandsSeparators(number, numberLocale, decimalPlaces);\n}\n"]}
@@ -1,3 +1,3 @@
1
- 'use strict';var chunkEFJCC6FJ_cjs=require('./chunk-EFJCC6FJ.cjs'),chunkPDD5YQBD_cjs=require('./chunk-PDD5YQBD.cjs'),chunkUURM7P3P_cjs=require('./chunk-UURM7P3P.cjs'),chunkJ4Y4MAX4_cjs=require('./chunk-J4Y4MAX4.cjs'),chunkNH74IT7B_cjs=require('./chunk-NH74IT7B.cjs'),chunkXWDABNQX_cjs=require('./chunk-XWDABNQX.cjs'),chunk3MG7VIPL_cjs=require('./chunk-3MG7VIPL.cjs'),chunk6SYU4MH7_cjs=require('./chunk-6SYU4MH7.cjs');var P=class{constructor(){this.root=null;}get length(){return this.root?this.root.size:0}get isEmpty(){return this.root===null}getAt(e){if(!(e<0||e>=(this.root?.size??0)))return this.nodeAt(this.root,e).lineId}insertAt(e,n){let[t,r]=I(this.root,e);this.root=S(S(t,te(n)),r);}deleteAt(e){if(e<0||e>=(this.root?.size??0))return;let[n,t]=I(this.root,e),[r,s]=I(t,1),i=r?r.lineId:void 0;return this.root=S(n,s),i}spliceAt(e,n,t){let r=Math.max(0,e),s=Math.min(Math.max(0,n),(this.root?.size??0)-r),[i,a]=I(this.root,r),[l,o]=I(a,s),c=ne(l),d=B(t,0,t.length);return this.root=S(S(i,d),o),c}replaceAll(e){this.root=B(e,0,e.length);}getRange(e,n){let t=Math.max(0,e),r=Math.min((this.root?.size??0)-1,n);if(t>r)return [];let s=[];return this.collectRange(this.root,t,r,0,s),s}clear(){this.root=null;}*[Symbol.iterator](){yield*this.inOrder(this.root);}*inOrder(e){e&&(yield*this.inOrder(e.left),yield e.lineId,yield*this.inOrder(e.right));}nodeAt(e,n){let t=e.left?e.left.size:0;return n<t?this.nodeAt(e.left,n):n===t?e:this.nodeAt(e.right,n-t-1)}collectRange(e,n,t,r,s){if(!e)return;let i=e.left?e.left.size:0,a=r+i;a>n&&e.left&&this.collectRange(e.left,n,t,r,s),a>=n&&a<=t&&s.push(e.lineId),a<t&&e.right&&this.collectRange(e.right,n,t,a+1,s);}};function te(u){return {lineId:u,size:1,priority:Math.random(),left:null,right:null}}function T(u){u.size=1+(u.left?u.left.size:0)+(u.right?u.right.size:0);}function S(u,e){return u?e?u.priority>e.priority?(u.right=S(u.right,e),T(u),u):(e.left=S(u,e.left),T(e),e):u:e}function I(u,e){if(!u)return [null,null];let n=u.left?u.left.size:0;if(e<=n){let[t,r]=I(u.left,e);return u.left=r,T(u),[t,u]}else {let[t,r]=I(u.right,e-n-1);return u.right=t,T(u),[u,r]}}function ne(u){let e=[];return F(u,e),e}function F(u,e){u&&(F(u.left,e),e.push(u.lineId),F(u.right,e));}function B(u,e,n){if(e>=n)return null;let t=Math.floor((e+n)/2),r=re(t);return {lineId:u[t],size:n-e,priority:r,left:B(u,e,t),right:B(u,t+1,n)}}function re(u){let e=u;return e=(e>>16^e)*73244475,e=(e>>16^e)*73244475,e=e>>16^e,e/4294967295}var R=class{constructor(e=chunkXWDABNQX_cjs.b.performance.maxDocumentLines){this.lines=new Map;this.orderTree=new P;this.nextLineId=1;this._positionCache=null;this.dirtyLineIds=new Set;this.maxLines=e;}setDocument(e){if(chunkUURM7P3P_cjs.b(e,this.maxLines)>this.maxLines)throw chunk6SYU4MH7_cjs.c.execution("DOCUMENT_TOO_LARGE",`This document has more than ${this.maxLines.toLocaleString("en-US")} lines, which is the most the engine will hold at once`,{maxLines:this.maxLines});this.lines.clear(),this.orderTree.clear(),this._positionCache=null,this.dirtyLineIds.clear(),this.nextLineId=1;let t=e.split(`
2
- `),r=new Array(t.length);for(let s=0;s<t.length;s++){let i=this.nextLineId++;r[s]=i,this.lines.set(i,{lineId:i,textHash:chunkEFJCC6FJ_cjs.a(t[s]),text:t[s],expressions:[],bytecodes:[],reads:[],writes:[],results:[],result:null,dirty:true,isVariableDef:false,isEmpty:t[s].trim().length===0,inlineSolveCount:0});}this.orderTree.replaceAll(r);for(let s of r)this.dirtyLineIds.add(s);}applyChanges(e){let n=[],t=[],r=[...e].sort((s,i)=>i.startLine-s.startLine);for(let s of r){let i=s.startLine-1,a=[];for(let o of s.insertLines){let c=this.nextLineId++;a.push(c),n.push(c),this.lines.set(c,{lineId:c,textHash:chunkEFJCC6FJ_cjs.a(o),text:o,expressions:[],bytecodes:[],reads:[],writes:[],results:[],result:null,dirty:true,isVariableDef:false,isEmpty:o.trim().length===0,inlineSolveCount:0});}for(let o of a)this.dirtyLineIds.add(o);let l=this.orderTree.spliceAt(i,s.deleteCount,a);for(let o of l)t.push(o),this.lines.delete(o),this.dirtyLineIds.delete(o);}return this._positionCache=null,{inserted:n,removed:t}}insertLines(e,n){let t={startLine:e,deleteCount:0,insertLines:n};return this.applyChanges([t]).inserted}deleteLines(e,n){let t={startLine:e,deleteCount:n-e+1,insertLines:[]};return this.applyChanges([t]).removed}editLine(e,n){let t=this.getLineAt(e);if(!t)return false;let r=chunkEFJCC6FJ_cjs.a(n);return r===t.textHash?false:(t.text=n,t.textHash=r,t.expressions=[],t.bytecodes=[],t.results=[],t.result=null,t.inlineSolveCount=0,t.dirty=true,this.dirtyLineIds.add(t.lineId),t.isEmpty=n.trim().length===0,true)}getLineAt(e){let n=e-1,t=this.orderTree.getAt(n);if(t!==void 0)return this.lines.get(t)}getLinePosition(e){if(this._positionCache)return this._positionCache.get(e)??-1;this._positionCache=new Map;let n=1;for(let t of this.orderTree)this._positionCache.set(t,n++);return this._positionCache.get(e)??-1}getVisibleLines(e,n){let t=this.orderTree.getRange(e-1,n-1),r=[];for(let s of t){let i=this.lines.get(s);i&&r.push(i);}return r}getAllLines(){return this.getVisibleLines(1,this.lineCount)}getLineById(e){return this.lines.get(e)}getDirtyLines(){let e=[];for(let n of this.lines.values())n.dirty&&e.push(n);return e}hasAnyDirtyLineBefore(e){for(let n of this.dirtyLineIds){let t=this.getLinePosition(n);if(t>=1&&t<e)return true}return false}hasAnyDirtyVariableDefLineBefore(e){for(let n of this.dirtyLineIds){let t=this.lines.get(n);if(!t||!t.isVariableDef)continue;let r=this.getLinePosition(n);if(r>=1&&r<e)return true}return false}get dirtyCount(){return this.dirtyLineIds.size}isBytecodeValid(e,n){let t=this.lines.get(e);return t!==void 0&&t.textHash===n}markClean(e){let n=this.lines.get(e);n&&(n.dirty=false,this.dirtyLineIds.delete(e));}markDirtyByLineNumber(e){let n=this.getLineAt(e);n&&(n.dirty=true,this.dirtyLineIds.add(n.lineId));}markDirty(e){let n=this.lines.get(e);n&&(n.dirty=true,this.dirtyLineIds.add(e));}invalidateAll(){for(let e of this.lines.values())e.dirty=true;this.dirtyLineIds=new Set(this.lines.keys());}updateLineResult(e,n,t,r,s,i,a,l=0){let o=this.lines.get(e);o&&(o.results=n,o.result=n[0]?.[0]??null,o.bytecodes=t,o.expressions=r,o.reads=s,o.writes=i,o.isVariableDef=a,o.inlineSolveCount=l,o.dirty=false,this.dirtyLineIds.delete(e));}updateLineCompiled(e,n,t,r,s,i,a=0){let l=this.lines.get(e);l&&(l.expressions=n,l.bytecodes=t,l.reads=r,l.writes=s,l.isVariableDef=i,l.inlineSolveCount=a);}get lineCount(){return this.orderTree.length}get isEmpty(){return this.orderTree.isEmpty}*[Symbol.iterator](){for(let e of this.orderTree){let n=this.lines.get(e);n&&(yield n);}}clear(){this.lines.clear(),this.orderTree.clear(),this._positionCache=null,this.nextLineId=1;}toJSON(){return {lineCount:this.lineCount,lines:this.getAllLines().map(e=>({lineId:e.lineId,text:e.text.substring(0,80),textHash:e.textHash,dirty:e.dirty,isVariableDef:e.isVariableDef,isEmpty:e.isEmpty,hasBytecode:e.bytecodes.length>0,hasResult:e.results.length>0,inlineSolveCount:e.inlineSolveCount,reads:e.reads,writes:e.writes}))}}};var A=class{constructor(){this.worker=null;this.nextId=1;this.pending=new Map;}ensureWorker(){if(this.worker)return this.worker;let e=chunkPDD5YQBD_cjs.h();if(e===null)throw new Error("no engine worker factory registered, so compilation stays on the main thread");return this.worker=e(),this.worker.onmessage=n=>{let t=n.data;if(!t||t.type!=="COMPILE_RESULT")return;let r=this.pending.get(t.id);if(!r)return;this.pending.delete(t.id);let s=[];for(let i of t.results)s.push(this.reconstructResult(i));r.resolve(s);},this.worker.onerror=n=>{for(let[t,r]of this.pending)r.reject(new Error(`Compilation worker error: ${n.message}`)),this.pending.delete(t);},this.worker}async compileBatch(e){if(e.length===0)return [];let n=this.ensureWorker(),t=this.nextId++;return new Promise((r,s)=>{this.pending.set(t,{resolve:r,reject:s}),n.postMessage({type:"COMPILE_BATCH",id:t,items:e});})}storeResults(e,n){let t=new Map;for(let i of e){if(i.error)continue;let a=n.getLineById(i.lineId);if(!a||a.textHash!==i.compiledAgainstHash)continue;let l=t.get(i.lineId);l||(l={bytecodes:[],reads:new Set,writes:new Set,isVariableDef:false,expressions:a.expressions.length>0?[...a.expressions]:[],textHash:i.compiledAgainstHash},t.set(i.lineId,l)),l.bytecodes.push(i.program);for(let o of i.reads)l.reads.add(o);for(let o of i.writes)l.writes.add(o);i.isVariableDef&&(l.isVariableDef=true);}let s=0;for(let[i,a]of t){if(a.bytecodes.length===0)continue;let o=n.getLineById(i)?.inlineSolveCount??0;n.updateLineCompiled(i,a.expressions,a.bytecodes,[...a.reads],[...a.writes],a.isVariableDef,o),s+=a.bytecodes.length;}return s}terminate(){this.worker&&(this.worker.terminate(),this.worker=null,this.pending.clear());}get isActive(){return this.worker!==null}reconstructResult(e){if(e.error)return {lineId:e.lineId,compiledAgainstHash:e.compiledAgainstHash,program:{opcodes:new Uint8Array(0),numbers:new Float64Array(0),strings:[],hasAsync:false},reads:[],writes:[],isVariableDef:false,error:e.error};let n=e.opcodesLength>0?new Uint8Array(e.opcodesBuffer,0,e.opcodesLength):new Uint8Array(0),t=e.numbersLength>0?new Float64Array(e.numbersBuffer,0,e.numbersLength):new Float64Array(0),r={opcodes:n,numbers:t,strings:e.strings,hasAsync:false};return {lineId:e.lineId,compiledAgainstHash:e.compiledAgainstHash,program:r,reads:e.reads,writes:e.writes,isVariableDef:e.isVariableDef,error:null}}};var U=128,k=3,J=6,Q=2,X=k,H=class u{constructor(){this.lastViewportStart=null;this.savedDirection=null;this.pageAccess=new Map;this.accessSeq=0;}static pageForLine(e){return Math.floor((e-1)/U)}static pageRange(e,n){return {startLine:e*U+1,endLine:Math.min((e+1)*U,n)}}detectDirection(e){return this.lastViewportStart===null?null:e.startLine>this.lastViewportStart?"down":e.startLine<this.lastViewportStart?"up":null}maintainAfterEval(e,n){this.savedDirection=this.detectDirection(e),this.lastViewportStart=e.startLine;let t=n.lineCount,r=u.pageForLine(e.startLine),s=u.pageForLine(e.endLine),i=u.pageForLine(t),a=Math.max(0,r-k),l=Math.min(i,s+k),o=Math.max(0,r-J),c=Math.min(i,s+J);for(let h=a;h<=l;h++)this.touchPage(h);for(let h=o;h<=c;h++)h>=a&&h<=l||this.evictPageResults(h,n,t);let d=o-1,p=Math.max(0,d-X+1);for(let h=p;h<=d;h++)this.evictPageBytecode(h,n,t);let f=c+1,g=Math.min(i,f+X-1);for(let h=f;h<=g;h++)this.evictPageBytecode(h,n,t);}getPreloadTargets(e,n){let t=this.savedDirection;if(!t)return [];let r=[],s=u.pageForLine(e.endLine),i=u.pageForLine(e.startLine),a=u.pageForLine(n.lineCount),l=[];if(t==="down"){let o=s+k+1,c=Math.min(o+Q-1,a);for(let d=o;d<=c&&d<=a;d++)l.push(d);}else {let o=i-k-1;for(let c=o;c>o-Q&&c>=0;c--)l.push(c);}for(let o of l){let c=u.pageRange(o,n.lineCount);for(let d=c.startLine;d<=c.endLine;d++){let p=n.getLineAt(d);if(p&&p.dirty&&!(p.bytecodes.length>0&&!p.isVariableDef)&&!p.isEmpty)if(p.expressions.length>0)for(let f of p.expressions)f.trim()&&r.push({lineId:p.lineId,expression:f,textHash:p.textHash});else {let f=chunk3MG7VIPL_cjs.c.findInlineSolves(p.text);if(f.length>0)for(let g of f)g.expression.trim()&&r.push({lineId:p.lineId,expression:g.expression,textHash:p.textHash});else {let g=p.text.trim();g&&r.push({lineId:p.lineId,expression:g,textHash:p.textHash});}}}}return r}clear(){this.pageAccess.clear(),this.accessSeq=0,this.lastViewportStart=null,this.savedDirection=null;}touchPage(e){this.pageAccess.set(e,++this.accessSeq);}evictPageResults(e,n,t){let r=u.pageRange(e,t);for(let s=r.startLine;s<=r.endLine;s++){let i=n.getLineAt(s);i&&!i.isVariableDef&&i.results.length>0&&(i.results=[],i.result=null);}}evictPageBytecode(e,n,t){let r=u.pageRange(e,t);for(let s=r.startLine;s<=r.endLine;s++){let i=n.getLineAt(s);!i||i.isVariableDef||(i.bytecodes.length>0||i.results.length>0)&&(i.bytecodes=[],i.results=[],i.result=null,n.markDirty(i.lineId));}}};var Y=(r=>(r[r.Tier1=1]="Tier1",r[r.Tier2=2]="Tier2",r[r.Tier3=3]="Tier3",r[r.Skipped=0]="Skipped",r))(Y||{}),w=class{constructor(e,n,t){this.compilationWorker=null;this.globalUnsubscribe=null;this.doc=e,this.engine=n,this.dag=n.getDag(),this.checkpointer=t??null,this.pageManager=new H,this.engine.setDocumentModel(this.doc),this.globalUnsubscribe=chunkJ4Y4MAX4_cjs.K.subscribe(r=>{for(let s of this.dag.getAffectedLines(chunkJ4Y4MAX4_cjs.L(r)))this.doc.markDirtyByLineNumber(s);});}evaluate(e,n){this.engine.setKeystrokeSignal(n??null),chunkNH74IT7B_cjs.h();try{this.reseedAccumulators();let t=[],r=new Map,s={tier1:0,tier2:0,tier3:0,skipped:0},i=this.doc.lineCount,a=Math.min(e.endLine,i);for(let l=1;l<=a;l++){let o=this.doc.getLineAt(l);if(!o){s.skipped++;continue}let c=l>=e.startLine&&l<=e.endLine,d=this.evaluateSingleLine(o,l,c);t.push(d),d.tier===1?s.tier1++:d.tier===2?s.tier2++:d.tier===3?s.tier3++:s.skipped++,d.results&&c&&r.set(l,d.results.flat());}return this.pageManager.maintainAfterEval(e,this.doc),{lines:t,resultMap:r,tierCounts:s}}finally{this.engine.setKeystrokeSignal(null),chunkNH74IT7B_cjs.i();}}backgroundCompile(e){let n=[],t=this.doc.lineCount,r=e.endLine+1;for(let s=r;s<=t;s++){let i=this.doc.getLineAt(s);if(!i||!i.dirty||i.bytecodes.length>0&&!i.isVariableDef)continue;let a=this.evaluateSingleLine(i,s,false);n.push(a);}return n}dispatchBackgroundCompiles(e){let n=this.collectInvisibleCompileTargets(e);n.length!==0&&(this.compilationWorker||(this.compilationWorker=new A),this.compilationWorker.compileBatch(n).then(t=>{this.compilationWorker.storeResults(t,this.doc);}).catch(t=>{}));}terminateWorker(){this.compilationWorker&&(this.compilationWorker.terminate(),this.compilationWorker=null),this.globalUnsubscribe&&(this.globalUnsubscribe(),this.globalUnsubscribe=null);}getDoc(){return this.doc}evaluateAll(e){let n={startLine:1,endLine:this.doc.lineCount};return this.evaluate(n,e)}setViewport(e,n){if(this.engine.setKeystrokeSignal(n??null),e.startLine>1&&this.hasDirtyLinesBefore(e.startLine))return this.checkpointer?.clear(),this.evaluate(e,n);this.pageManager.maintainAfterEval(e,this.doc),this.preloadNextPages(e),this.restoreTo(e.startLine-1),chunkNH74IT7B_cjs.h();try{return this.collectEvalResults(e.startLine,e.endLine)}finally{this.engine.setKeystrokeSignal(null),chunkNH74IT7B_cjs.i();}}applyTransaction(e){let n=new Set;for(let s of e)for(let i=0;i<s.deleteCount;i++){let a=s.startLine+i,l=this.dag.getWrites(a);for(let o of l)n.add(o);this.dag.removeLine(a),this.engine.getLineCache().removeAllForLine(a);}let t=new Set;for(let s of n){let i=this.dag.getAffectedLines(s);for(let a of i){let l=this.doc.getLineAt(a);l&&t.add(l.lineId);}}let r=this.doc.applyChanges(e);this.checkpointer?.clear();for(let s of t)this.doc.markDirty(s);return this.dag.clear(),{inserted:r.inserted,removed:r.removed}}reseedAccumulators(){let e=this.engine.resetAccumulators();if(e.size===0)return;let n=this.doc.lineCount;for(let t=1;t<=n;t++){let r=this.dag.getWrites(t);if(r.size!==0){for(let s of r)if(e.has(s)){this.doc.markDirtyByLineNumber(t);break}}}}collectEvalResults(e,n){let t=[],r=new Map,s={tier1:0,tier2:0,tier3:0,skipped:0},i=this.doc.lineCount,a=Math.min(n,i);for(let l=e;l<=a;l++){let o=this.doc.getLineAt(l);if(!o)continue;let c=this.evaluateSingleLine(o,l,true);t.push(c),c.tier===1?s.tier1++:c.tier===2?s.tier2++:c.tier===3?s.tier3++:s.skipped++,c.results&&r.set(l,c.results.flat());}return {lines:t,resultMap:r,tierCounts:s}}hasDirtyLinesBefore(e){return this.doc.hasAnyDirtyVariableDefLineBefore(e)}evaluateSingleLine(e,n,t){let r={lineId:e.lineId,lineNumber:n};if(e.isEmpty||chunkPDD5YQBD_cjs.m(e.text))return e.isEmpty=true,this.doc.markClean(e.lineId),{...r,tier:0,result:null,error:null};let{expressions:s,inlineSolveCount:i}=this.extractExpressions(e);return s.length===0?(e.isEmpty=true,this.doc.markClean(e.lineId),{...r,tier:0,result:null,error:null}):e.dirty?t?this.evaluateTier1(e,n,s,i,r):e.bytecodes.length>0&&e.bytecodes.length===s.length&&!e.isVariableDef?{...r,tier:0,result:null,error:null}:this.evaluateTier3(e,n,s,i,r):t&&e.bytecodes.length>0?this.evaluateTier2(e,n,r):{...r,tier:0,result:null,error:null}}evaluateTier1(e,n,t,r,s){let i=[],a=[],l=new Set,o=new Set,c=false,d=null,p=null,f=false,g=false;for(let y of t){if(!y.trim())continue;let b=null,L;try{let m=this.engine.evaluateLine(n,y);b=[m],d=m,L=this.engine.getLineCache().get(n,y);}catch(m){let x=m instanceof Error?m.message:String(m);p||(p=x),f=true,b=null;}if(b&&b.some(m=>m.type===12)&&(g=true),b?i.push(b):i.push([chunkNH74IT7B_cjs.P("eval_failed",p??"unknown error")]),L){a.push(L.bytecode);for(let m of L.readVariables)l.add(m);L.writeVariable&&(o.add(L.writeVariable),c=true);}else try{let{program:m,reads:x,writes:E}=this.engine.compileExpression(y);a.push(m);for(let C of x)l.add(C);for(let C of E)o.add(C);E.length>0&&(c=!0);}catch(m){if(a.push({opcodes:new Uint8Array(0),numbers:new Float64Array(0),strings:[],hasAsync:false}),m instanceof chunk6SYU4MH7_cjs.b&&m.context){let x=m.context.reads,E=m.context.writes;if(Array.isArray(x))for(let C of x)l.add(C);if(Array.isArray(E))for(let C of E)o.add(C);}}}let h=[...l],v=[...o];return f||g?(this.doc.updateLineCompiled(e.lineId,t,a,h,v,c,r),e.results=i,e.result=i[0]?.[0]??null,e.inlineSolveCount=r,e.expressions=t):this.doc.updateLineResult(e.lineId,i,a,t,h,v,c,r),this.dag.registerLine(n,h,v),this.checkpointer&&v.length>0&&!f&&!g&&this.checkpointer.snapshot(n,e.lineId,v),{...s,tier:1,result:d,results:i,error:p}}evaluateTier2(e,n,t){if(e.bytecodes.length===0)return {...t,tier:0,result:null,error:null};let r=[],s=null,i=null,a=false;for(let l of e.bytecodes)if(l.opcodes.length!==0)try{let o=this.engine.executeCached(l,n);s=o,r.push([o]);}catch(o){let c=o instanceof Error?o.message:String(o);i||(i=c),a=true,r.push([chunkNH74IT7B_cjs.P("exec_failed",c)]);}return this.dag.registerLine(n,e.reads,e.writes),e.results=r,e.result=r[0]?.[0]??null,a&&this.doc.markDirty(e.lineId),{...t,tier:2,result:s,results:r,error:i}}evaluateTier3(e,n,t,r,s){let i=[],a=new Set,l=new Set,o=false,c=null,d=null,p=false,f=false;for(let v of t)if(v.trim())try{let{program:y,reads:b,writes:L}=this.engine.compileExpression(v);i.push(y);for(let m of b)a.add(m);for(let m of L)l.add(m);L.length>0&&(o=!0),L.length>0&&y.opcodes.length>0&&(c=this.engine.executeCached(y,n),c&&c.type===12&&(f=!0));}catch(y){let b=y instanceof Error?y.message:String(y);d||(d=b),p=true,i.push({opcodes:new Uint8Array(0),numbers:new Float64Array(0),strings:[],hasAsync:false});}let g=[...a],h=[...l];return this.doc.updateLineCompiled(e.lineId,t,i,g,h,o,r),this.dag.registerLine(n,g,h),o&&c&&!p&&!f&&(e.results=[[c]],e.result=c,this.doc.markClean(e.lineId),this.checkpointer&&this.checkpointer.snapshot(n,e.lineId,h)),{...s,tier:3,result:c,results:o&&c&&!p&&!f?[[c]]:void 0,error:d}}restoreTo(e){this.checkpointer&&this.checkpointer.restoreTo(e);}getCheckpointer(){return this.checkpointer}getPageManager(){return this.pageManager}preloadNextPages(e){let n=this.pageManager.getPreloadTargets(e,this.doc);n.length!==0&&(this.compilationWorker||(this.compilationWorker=new A),this.compilationWorker.compileBatch(n).then(t=>{this.compilationWorker.storeResults(t,this.doc);}).catch(t=>{}));}collectInvisibleCompileTargets(e){let n=[],t=this.doc.lineCount,r=e.endLine+1;for(let s=r;s<=t;s++){let i=this.doc.getLineAt(s);if(!i||!i.dirty||i.bytecodes.length>0&&!i.isVariableDef||i.isEmpty||chunkPDD5YQBD_cjs.m(i.text))continue;let{expressions:a}=this.extractExpressions(i);if(a.length!==0)for(let l of a)l.trim()&&n.push({lineId:i.lineId,expression:l,textHash:i.textHash});}return n}extractExpressions(e){if(e.expressions.length>0)return {expressions:e.expressions,inlineSolveCount:e.inlineSolveCount};let n=e.text.trim();if(n.length===0)return {expressions:[],inlineSolveCount:0};let t=chunk3MG7VIPL_cjs.c.findInlineSolves(e.text);return t.length>0?{expressions:t.map(r=>r.expression),inlineSolveCount:t.length}:{expressions:[n],inlineSolveCount:0}}};function ee(u){return typeof u.unit=="string"?u.unit:String(u.value)}function ie(u,e,n={inputType:"markdown"}){let t=u.getDocumentModel(),r=new R;r.setDocument(e);let s=new w(r,u);try{let i=r.lineCount;s.evaluate({startLine:1,endLine:i});let a=[],l=[],o=0;for(let c=1;c<=i;c++){let d=r.getLineAt(c),p=d.text,f=o,g=o+p.length;o=g+1;let h=d.inlineSolveCount>0,v=[],y=null,b=null;d.isEmpty||(h?v=chunkPDD5YQBD_cjs.n(p,c).map((L,m)=>{let x=d.results[m]?.[0]??null;if(x&&x.type===13){let E=ee(x);return l.push(`Line ${c}: ${E}`),{...L,result:null,error:E}}return {...L,result:x,error:null}}):(y=d.expressions[0]??null,b=d.result??null,b&&b.type===13&&l.push(`Line ${c}: ${ee(b)}`))),a.push({lineNumber:c,text:p,startPosition:f,endPosition:g,isEmpty:d.isEmpty,hasInlineSolves:h,inlineSolves:v,expression:y,error:null,result:b});}return {lines:a,totalLines:i,errors:l}}finally{s.terminateWorker(),u.setDocumentModel(t);}}exports.a=R;exports.b=Y;exports.c=w;exports.d=ie;//# sourceMappingURL=chunk-T7NEEJYP.cjs.map
3
- //# sourceMappingURL=chunk-T7NEEJYP.cjs.map
1
+ 'use strict';var chunkEFJCC6FJ_cjs=require('./chunk-EFJCC6FJ.cjs'),chunkLYLRGYEC_cjs=require('./chunk-LYLRGYEC.cjs'),chunkUURM7P3P_cjs=require('./chunk-UURM7P3P.cjs'),chunkJ4Y4MAX4_cjs=require('./chunk-J4Y4MAX4.cjs'),chunkNH74IT7B_cjs=require('./chunk-NH74IT7B.cjs'),chunkMV3UPVZX_cjs=require('./chunk-MV3UPVZX.cjs'),chunk3MG7VIPL_cjs=require('./chunk-3MG7VIPL.cjs'),chunk6SYU4MH7_cjs=require('./chunk-6SYU4MH7.cjs');var P=class{constructor(){this.root=null;}get length(){return this.root?this.root.size:0}get isEmpty(){return this.root===null}getAt(e){if(!(e<0||e>=(this.root?.size??0)))return this.nodeAt(this.root,e).lineId}insertAt(e,n){let[t,r]=I(this.root,e);this.root=S(S(t,te(n)),r);}deleteAt(e){if(e<0||e>=(this.root?.size??0))return;let[n,t]=I(this.root,e),[r,s]=I(t,1),i=r?r.lineId:void 0;return this.root=S(n,s),i}spliceAt(e,n,t){let r=Math.max(0,e),s=Math.min(Math.max(0,n),(this.root?.size??0)-r),[i,a]=I(this.root,r),[l,o]=I(a,s),c=ne(l),d=B(t,0,t.length);return this.root=S(S(i,d),o),c}replaceAll(e){this.root=B(e,0,e.length);}getRange(e,n){let t=Math.max(0,e),r=Math.min((this.root?.size??0)-1,n);if(t>r)return [];let s=[];return this.collectRange(this.root,t,r,0,s),s}clear(){this.root=null;}*[Symbol.iterator](){yield*this.inOrder(this.root);}*inOrder(e){e&&(yield*this.inOrder(e.left),yield e.lineId,yield*this.inOrder(e.right));}nodeAt(e,n){let t=e.left?e.left.size:0;return n<t?this.nodeAt(e.left,n):n===t?e:this.nodeAt(e.right,n-t-1)}collectRange(e,n,t,r,s){if(!e)return;let i=e.left?e.left.size:0,a=r+i;a>n&&e.left&&this.collectRange(e.left,n,t,r,s),a>=n&&a<=t&&s.push(e.lineId),a<t&&e.right&&this.collectRange(e.right,n,t,a+1,s);}};function te(u){return {lineId:u,size:1,priority:Math.random(),left:null,right:null}}function T(u){u.size=1+(u.left?u.left.size:0)+(u.right?u.right.size:0);}function S(u,e){return u?e?u.priority>e.priority?(u.right=S(u.right,e),T(u),u):(e.left=S(u,e.left),T(e),e):u:e}function I(u,e){if(!u)return [null,null];let n=u.left?u.left.size:0;if(e<=n){let[t,r]=I(u.left,e);return u.left=r,T(u),[t,u]}else {let[t,r]=I(u.right,e-n-1);return u.right=t,T(u),[u,r]}}function ne(u){let e=[];return F(u,e),e}function F(u,e){u&&(F(u.left,e),e.push(u.lineId),F(u.right,e));}function B(u,e,n){if(e>=n)return null;let t=Math.floor((e+n)/2),r=re(t);return {lineId:u[t],size:n-e,priority:r,left:B(u,e,t),right:B(u,t+1,n)}}function re(u){let e=u;return e=(e>>16^e)*73244475,e=(e>>16^e)*73244475,e=e>>16^e,e/4294967295}var R=class{constructor(e=chunkMV3UPVZX_cjs.b.performance.maxDocumentLines){this.lines=new Map;this.orderTree=new P;this.nextLineId=1;this._positionCache=null;this.dirtyLineIds=new Set;this.maxLines=e;}setDocument(e){if(chunkUURM7P3P_cjs.b(e,this.maxLines)>this.maxLines)throw chunk6SYU4MH7_cjs.c.execution("DOCUMENT_TOO_LARGE",`This document has more than ${this.maxLines.toLocaleString("en-US")} lines, which is the most the engine will hold at once`,{maxLines:this.maxLines});this.lines.clear(),this.orderTree.clear(),this._positionCache=null,this.dirtyLineIds.clear(),this.nextLineId=1;let t=e.split(`
2
+ `),r=new Array(t.length);for(let s=0;s<t.length;s++){let i=this.nextLineId++;r[s]=i,this.lines.set(i,{lineId:i,textHash:chunkEFJCC6FJ_cjs.a(t[s]),text:t[s],expressions:[],bytecodes:[],reads:[],writes:[],results:[],result:null,dirty:true,isVariableDef:false,isEmpty:t[s].trim().length===0,inlineSolveCount:0});}this.orderTree.replaceAll(r);for(let s of r)this.dirtyLineIds.add(s);}applyChanges(e){let n=[],t=[],r=[...e].sort((s,i)=>i.startLine-s.startLine);for(let s of r){let i=s.startLine-1,a=[];for(let o of s.insertLines){let c=this.nextLineId++;a.push(c),n.push(c),this.lines.set(c,{lineId:c,textHash:chunkEFJCC6FJ_cjs.a(o),text:o,expressions:[],bytecodes:[],reads:[],writes:[],results:[],result:null,dirty:true,isVariableDef:false,isEmpty:o.trim().length===0,inlineSolveCount:0});}for(let o of a)this.dirtyLineIds.add(o);let l=this.orderTree.spliceAt(i,s.deleteCount,a);for(let o of l)t.push(o),this.lines.delete(o),this.dirtyLineIds.delete(o);}return this._positionCache=null,{inserted:n,removed:t}}insertLines(e,n){let t={startLine:e,deleteCount:0,insertLines:n};return this.applyChanges([t]).inserted}deleteLines(e,n){let t={startLine:e,deleteCount:n-e+1,insertLines:[]};return this.applyChanges([t]).removed}editLine(e,n){let t=this.getLineAt(e);if(!t)return false;let r=chunkEFJCC6FJ_cjs.a(n);return r===t.textHash?false:(t.text=n,t.textHash=r,t.expressions=[],t.bytecodes=[],t.results=[],t.result=null,t.inlineSolveCount=0,t.dirty=true,this.dirtyLineIds.add(t.lineId),t.isEmpty=n.trim().length===0,true)}getLineAt(e){let n=e-1,t=this.orderTree.getAt(n);if(t!==void 0)return this.lines.get(t)}getLinePosition(e){if(this._positionCache)return this._positionCache.get(e)??-1;this._positionCache=new Map;let n=1;for(let t of this.orderTree)this._positionCache.set(t,n++);return this._positionCache.get(e)??-1}getVisibleLines(e,n){let t=this.orderTree.getRange(e-1,n-1),r=[];for(let s of t){let i=this.lines.get(s);i&&r.push(i);}return r}getAllLines(){return this.getVisibleLines(1,this.lineCount)}getLineById(e){return this.lines.get(e)}getDirtyLines(){let e=[];for(let n of this.lines.values())n.dirty&&e.push(n);return e}hasAnyDirtyLineBefore(e){for(let n of this.dirtyLineIds){let t=this.getLinePosition(n);if(t>=1&&t<e)return true}return false}hasAnyDirtyVariableDefLineBefore(e){for(let n of this.dirtyLineIds){let t=this.lines.get(n);if(!t||!t.isVariableDef)continue;let r=this.getLinePosition(n);if(r>=1&&r<e)return true}return false}get dirtyCount(){return this.dirtyLineIds.size}isBytecodeValid(e,n){let t=this.lines.get(e);return t!==void 0&&t.textHash===n}markClean(e){let n=this.lines.get(e);n&&(n.dirty=false,this.dirtyLineIds.delete(e));}markDirtyByLineNumber(e){let n=this.getLineAt(e);n&&(n.dirty=true,this.dirtyLineIds.add(n.lineId));}markDirty(e){let n=this.lines.get(e);n&&(n.dirty=true,this.dirtyLineIds.add(e));}invalidateAll(){for(let e of this.lines.values())e.dirty=true;this.dirtyLineIds=new Set(this.lines.keys());}updateLineResult(e,n,t,r,s,i,a,l=0){let o=this.lines.get(e);o&&(o.results=n,o.result=n[0]?.[0]??null,o.bytecodes=t,o.expressions=r,o.reads=s,o.writes=i,o.isVariableDef=a,o.inlineSolveCount=l,o.dirty=false,this.dirtyLineIds.delete(e));}updateLineCompiled(e,n,t,r,s,i,a=0){let l=this.lines.get(e);l&&(l.expressions=n,l.bytecodes=t,l.reads=r,l.writes=s,l.isVariableDef=i,l.inlineSolveCount=a);}get lineCount(){return this.orderTree.length}get isEmpty(){return this.orderTree.isEmpty}*[Symbol.iterator](){for(let e of this.orderTree){let n=this.lines.get(e);n&&(yield n);}}clear(){this.lines.clear(),this.orderTree.clear(),this._positionCache=null,this.nextLineId=1;}toJSON(){return {lineCount:this.lineCount,lines:this.getAllLines().map(e=>({lineId:e.lineId,text:e.text.substring(0,80),textHash:e.textHash,dirty:e.dirty,isVariableDef:e.isVariableDef,isEmpty:e.isEmpty,hasBytecode:e.bytecodes.length>0,hasResult:e.results.length>0,inlineSolveCount:e.inlineSolveCount,reads:e.reads,writes:e.writes}))}}};var A=class{constructor(){this.worker=null;this.nextId=1;this.pending=new Map;}ensureWorker(){if(this.worker)return this.worker;let e=chunkLYLRGYEC_cjs.h();if(e===null)throw new Error("no engine worker factory registered, so compilation stays on the main thread");return this.worker=e(),this.worker.onmessage=n=>{let t=n.data;if(!t||t.type!=="COMPILE_RESULT")return;let r=this.pending.get(t.id);if(!r)return;this.pending.delete(t.id);let s=[];for(let i of t.results)s.push(this.reconstructResult(i));r.resolve(s);},this.worker.onerror=n=>{for(let[t,r]of this.pending)r.reject(new Error(`Compilation worker error: ${n.message}`)),this.pending.delete(t);},this.worker}async compileBatch(e){if(e.length===0)return [];let n=this.ensureWorker(),t=this.nextId++;return new Promise((r,s)=>{this.pending.set(t,{resolve:r,reject:s}),n.postMessage({type:"COMPILE_BATCH",id:t,items:e});})}storeResults(e,n){let t=new Map;for(let i of e){if(i.error)continue;let a=n.getLineById(i.lineId);if(!a||a.textHash!==i.compiledAgainstHash)continue;let l=t.get(i.lineId);l||(l={bytecodes:[],reads:new Set,writes:new Set,isVariableDef:false,expressions:a.expressions.length>0?[...a.expressions]:[],textHash:i.compiledAgainstHash},t.set(i.lineId,l)),l.bytecodes.push(i.program);for(let o of i.reads)l.reads.add(o);for(let o of i.writes)l.writes.add(o);i.isVariableDef&&(l.isVariableDef=true);}let s=0;for(let[i,a]of t){if(a.bytecodes.length===0)continue;let o=n.getLineById(i)?.inlineSolveCount??0;n.updateLineCompiled(i,a.expressions,a.bytecodes,[...a.reads],[...a.writes],a.isVariableDef,o),s+=a.bytecodes.length;}return s}terminate(){this.worker&&(this.worker.terminate(),this.worker=null,this.pending.clear());}get isActive(){return this.worker!==null}reconstructResult(e){if(e.error)return {lineId:e.lineId,compiledAgainstHash:e.compiledAgainstHash,program:{opcodes:new Uint8Array(0),numbers:new Float64Array(0),strings:[],hasAsync:false},reads:[],writes:[],isVariableDef:false,error:e.error};let n=e.opcodesLength>0?new Uint8Array(e.opcodesBuffer,0,e.opcodesLength):new Uint8Array(0),t=e.numbersLength>0?new Float64Array(e.numbersBuffer,0,e.numbersLength):new Float64Array(0),r={opcodes:n,numbers:t,strings:e.strings,hasAsync:false};return {lineId:e.lineId,compiledAgainstHash:e.compiledAgainstHash,program:r,reads:e.reads,writes:e.writes,isVariableDef:e.isVariableDef,error:null}}};var U=128,k=3,J=6,Q=2,X=k,H=class u{constructor(){this.lastViewportStart=null;this.savedDirection=null;this.pageAccess=new Map;this.accessSeq=0;}static pageForLine(e){return Math.floor((e-1)/U)}static pageRange(e,n){return {startLine:e*U+1,endLine:Math.min((e+1)*U,n)}}detectDirection(e){return this.lastViewportStart===null?null:e.startLine>this.lastViewportStart?"down":e.startLine<this.lastViewportStart?"up":null}maintainAfterEval(e,n){this.savedDirection=this.detectDirection(e),this.lastViewportStart=e.startLine;let t=n.lineCount,r=u.pageForLine(e.startLine),s=u.pageForLine(e.endLine),i=u.pageForLine(t),a=Math.max(0,r-k),l=Math.min(i,s+k),o=Math.max(0,r-J),c=Math.min(i,s+J);for(let h=a;h<=l;h++)this.touchPage(h);for(let h=o;h<=c;h++)h>=a&&h<=l||this.evictPageResults(h,n,t);let d=o-1,p=Math.max(0,d-X+1);for(let h=p;h<=d;h++)this.evictPageBytecode(h,n,t);let f=c+1,g=Math.min(i,f+X-1);for(let h=f;h<=g;h++)this.evictPageBytecode(h,n,t);}getPreloadTargets(e,n){let t=this.savedDirection;if(!t)return [];let r=[],s=u.pageForLine(e.endLine),i=u.pageForLine(e.startLine),a=u.pageForLine(n.lineCount),l=[];if(t==="down"){let o=s+k+1,c=Math.min(o+Q-1,a);for(let d=o;d<=c&&d<=a;d++)l.push(d);}else {let o=i-k-1;for(let c=o;c>o-Q&&c>=0;c--)l.push(c);}for(let o of l){let c=u.pageRange(o,n.lineCount);for(let d=c.startLine;d<=c.endLine;d++){let p=n.getLineAt(d);if(p&&p.dirty&&!(p.bytecodes.length>0&&!p.isVariableDef)&&!p.isEmpty)if(p.expressions.length>0)for(let f of p.expressions)f.trim()&&r.push({lineId:p.lineId,expression:f,textHash:p.textHash});else {let f=chunk3MG7VIPL_cjs.c.findInlineSolves(p.text);if(f.length>0)for(let g of f)g.expression.trim()&&r.push({lineId:p.lineId,expression:g.expression,textHash:p.textHash});else {let g=p.text.trim();g&&r.push({lineId:p.lineId,expression:g,textHash:p.textHash});}}}}return r}clear(){this.pageAccess.clear(),this.accessSeq=0,this.lastViewportStart=null,this.savedDirection=null;}touchPage(e){this.pageAccess.set(e,++this.accessSeq);}evictPageResults(e,n,t){let r=u.pageRange(e,t);for(let s=r.startLine;s<=r.endLine;s++){let i=n.getLineAt(s);i&&!i.isVariableDef&&i.results.length>0&&(i.results=[],i.result=null);}}evictPageBytecode(e,n,t){let r=u.pageRange(e,t);for(let s=r.startLine;s<=r.endLine;s++){let i=n.getLineAt(s);!i||i.isVariableDef||(i.bytecodes.length>0||i.results.length>0)&&(i.bytecodes=[],i.results=[],i.result=null,n.markDirty(i.lineId));}}};var Y=(r=>(r[r.Tier1=1]="Tier1",r[r.Tier2=2]="Tier2",r[r.Tier3=3]="Tier3",r[r.Skipped=0]="Skipped",r))(Y||{}),w=class{constructor(e,n,t){this.compilationWorker=null;this.globalUnsubscribe=null;this.doc=e,this.engine=n,this.dag=n.getDag(),this.checkpointer=t??null,this.pageManager=new H,this.engine.setDocumentModel(this.doc),this.globalUnsubscribe=chunkJ4Y4MAX4_cjs.K.subscribe(r=>{for(let s of this.dag.getAffectedLines(chunkJ4Y4MAX4_cjs.L(r)))this.doc.markDirtyByLineNumber(s);});}evaluate(e,n){this.engine.setKeystrokeSignal(n??null),chunkNH74IT7B_cjs.h();try{this.reseedAccumulators();let t=[],r=new Map,s={tier1:0,tier2:0,tier3:0,skipped:0},i=this.doc.lineCount,a=Math.min(e.endLine,i);for(let l=1;l<=a;l++){let o=this.doc.getLineAt(l);if(!o){s.skipped++;continue}let c=l>=e.startLine&&l<=e.endLine,d=this.evaluateSingleLine(o,l,c);t.push(d),d.tier===1?s.tier1++:d.tier===2?s.tier2++:d.tier===3?s.tier3++:s.skipped++,d.results&&c&&r.set(l,d.results.flat());}return this.pageManager.maintainAfterEval(e,this.doc),{lines:t,resultMap:r,tierCounts:s}}finally{this.engine.setKeystrokeSignal(null),chunkNH74IT7B_cjs.i();}}backgroundCompile(e){let n=[],t=this.doc.lineCount,r=e.endLine+1;for(let s=r;s<=t;s++){let i=this.doc.getLineAt(s);if(!i||!i.dirty||i.bytecodes.length>0&&!i.isVariableDef)continue;let a=this.evaluateSingleLine(i,s,false);n.push(a);}return n}dispatchBackgroundCompiles(e){let n=this.collectInvisibleCompileTargets(e);n.length!==0&&(this.compilationWorker||(this.compilationWorker=new A),this.compilationWorker.compileBatch(n).then(t=>{this.compilationWorker.storeResults(t,this.doc);}).catch(t=>{}));}terminateWorker(){this.compilationWorker&&(this.compilationWorker.terminate(),this.compilationWorker=null),this.globalUnsubscribe&&(this.globalUnsubscribe(),this.globalUnsubscribe=null);}getDoc(){return this.doc}evaluateAll(e){let n={startLine:1,endLine:this.doc.lineCount};return this.evaluate(n,e)}setViewport(e,n){if(this.engine.setKeystrokeSignal(n??null),e.startLine>1&&this.hasDirtyLinesBefore(e.startLine))return this.checkpointer?.clear(),this.evaluate(e,n);this.pageManager.maintainAfterEval(e,this.doc),this.preloadNextPages(e),this.restoreTo(e.startLine-1),chunkNH74IT7B_cjs.h();try{return this.collectEvalResults(e.startLine,e.endLine)}finally{this.engine.setKeystrokeSignal(null),chunkNH74IT7B_cjs.i();}}applyTransaction(e){let n=new Set;for(let s of e)for(let i=0;i<s.deleteCount;i++){let a=s.startLine+i,l=this.dag.getWrites(a);for(let o of l)n.add(o);this.dag.removeLine(a),this.engine.getLineCache().removeAllForLine(a);}let t=new Set;for(let s of n){let i=this.dag.getAffectedLines(s);for(let a of i){let l=this.doc.getLineAt(a);l&&t.add(l.lineId);}}let r=this.doc.applyChanges(e);this.checkpointer?.clear();for(let s of t)this.doc.markDirty(s);return this.dag.clear(),{inserted:r.inserted,removed:r.removed}}reseedAccumulators(){let e=this.engine.resetAccumulators();if(e.size===0)return;let n=this.doc.lineCount;for(let t=1;t<=n;t++){let r=this.dag.getWrites(t);if(r.size!==0){for(let s of r)if(e.has(s)){this.doc.markDirtyByLineNumber(t);break}}}}collectEvalResults(e,n){let t=[],r=new Map,s={tier1:0,tier2:0,tier3:0,skipped:0},i=this.doc.lineCount,a=Math.min(n,i);for(let l=e;l<=a;l++){let o=this.doc.getLineAt(l);if(!o)continue;let c=this.evaluateSingleLine(o,l,true);t.push(c),c.tier===1?s.tier1++:c.tier===2?s.tier2++:c.tier===3?s.tier3++:s.skipped++,c.results&&r.set(l,c.results.flat());}return {lines:t,resultMap:r,tierCounts:s}}hasDirtyLinesBefore(e){return this.doc.hasAnyDirtyVariableDefLineBefore(e)}evaluateSingleLine(e,n,t){let r={lineId:e.lineId,lineNumber:n};if(e.isEmpty||chunkLYLRGYEC_cjs.m(e.text))return e.isEmpty=true,this.doc.markClean(e.lineId),{...r,tier:0,result:null,error:null};let{expressions:s,inlineSolveCount:i}=this.extractExpressions(e);return s.length===0?(e.isEmpty=true,this.doc.markClean(e.lineId),{...r,tier:0,result:null,error:null}):e.dirty?t?this.evaluateTier1(e,n,s,i,r):e.bytecodes.length>0&&e.bytecodes.length===s.length&&!e.isVariableDef?{...r,tier:0,result:null,error:null}:this.evaluateTier3(e,n,s,i,r):t&&e.bytecodes.length>0?this.evaluateTier2(e,n,r):{...r,tier:0,result:null,error:null}}evaluateTier1(e,n,t,r,s){let i=[],a=[],l=new Set,o=new Set,c=false,d=null,p=null,f=false,g=false;for(let y of t){if(!y.trim())continue;let b=null,L;try{let m=this.engine.evaluateLine(n,y);b=[m],d=m,L=this.engine.getLineCache().get(n,y);}catch(m){let x=m instanceof Error?m.message:String(m);p||(p=x),f=true,b=null;}if(b&&b.some(m=>m.type===12)&&(g=true),b?i.push(b):i.push([chunkNH74IT7B_cjs.P("eval_failed",p??"unknown error")]),L){a.push(L.bytecode);for(let m of L.readVariables)l.add(m);L.writeVariable&&(o.add(L.writeVariable),c=true);}else try{let{program:m,reads:x,writes:E}=this.engine.compileExpression(y);a.push(m);for(let C of x)l.add(C);for(let C of E)o.add(C);E.length>0&&(c=!0);}catch(m){if(a.push({opcodes:new Uint8Array(0),numbers:new Float64Array(0),strings:[],hasAsync:false}),m instanceof chunk6SYU4MH7_cjs.b&&m.context){let x=m.context.reads,E=m.context.writes;if(Array.isArray(x))for(let C of x)l.add(C);if(Array.isArray(E))for(let C of E)o.add(C);}}}let h=[...l],v=[...o];return f||g?(this.doc.updateLineCompiled(e.lineId,t,a,h,v,c,r),e.results=i,e.result=i[0]?.[0]??null,e.inlineSolveCount=r,e.expressions=t):this.doc.updateLineResult(e.lineId,i,a,t,h,v,c,r),this.dag.registerLine(n,h,v),this.checkpointer&&v.length>0&&!f&&!g&&this.checkpointer.snapshot(n,e.lineId,v),{...s,tier:1,result:d,results:i,error:p}}evaluateTier2(e,n,t){if(e.bytecodes.length===0)return {...t,tier:0,result:null,error:null};let r=[],s=null,i=null,a=false;for(let l of e.bytecodes)if(l.opcodes.length!==0)try{let o=this.engine.executeCached(l,n);s=o,r.push([o]);}catch(o){let c=o instanceof Error?o.message:String(o);i||(i=c),a=true,r.push([chunkNH74IT7B_cjs.P("exec_failed",c)]);}return this.dag.registerLine(n,e.reads,e.writes),e.results=r,e.result=r[0]?.[0]??null,a&&this.doc.markDirty(e.lineId),{...t,tier:2,result:s,results:r,error:i}}evaluateTier3(e,n,t,r,s){let i=[],a=new Set,l=new Set,o=false,c=null,d=null,p=false,f=false;for(let v of t)if(v.trim())try{let{program:y,reads:b,writes:L}=this.engine.compileExpression(v);i.push(y);for(let m of b)a.add(m);for(let m of L)l.add(m);L.length>0&&(o=!0),L.length>0&&y.opcodes.length>0&&(c=this.engine.executeCached(y,n),c&&c.type===12&&(f=!0));}catch(y){let b=y instanceof Error?y.message:String(y);d||(d=b),p=true,i.push({opcodes:new Uint8Array(0),numbers:new Float64Array(0),strings:[],hasAsync:false});}let g=[...a],h=[...l];return this.doc.updateLineCompiled(e.lineId,t,i,g,h,o,r),this.dag.registerLine(n,g,h),o&&c&&!p&&!f&&(e.results=[[c]],e.result=c,this.doc.markClean(e.lineId),this.checkpointer&&this.checkpointer.snapshot(n,e.lineId,h)),{...s,tier:3,result:c,results:o&&c&&!p&&!f?[[c]]:void 0,error:d}}restoreTo(e){this.checkpointer&&this.checkpointer.restoreTo(e);}getCheckpointer(){return this.checkpointer}getPageManager(){return this.pageManager}preloadNextPages(e){let n=this.pageManager.getPreloadTargets(e,this.doc);n.length!==0&&(this.compilationWorker||(this.compilationWorker=new A),this.compilationWorker.compileBatch(n).then(t=>{this.compilationWorker.storeResults(t,this.doc);}).catch(t=>{}));}collectInvisibleCompileTargets(e){let n=[],t=this.doc.lineCount,r=e.endLine+1;for(let s=r;s<=t;s++){let i=this.doc.getLineAt(s);if(!i||!i.dirty||i.bytecodes.length>0&&!i.isVariableDef||i.isEmpty||chunkLYLRGYEC_cjs.m(i.text))continue;let{expressions:a}=this.extractExpressions(i);if(a.length!==0)for(let l of a)l.trim()&&n.push({lineId:i.lineId,expression:l,textHash:i.textHash});}return n}extractExpressions(e){if(e.expressions.length>0)return {expressions:e.expressions,inlineSolveCount:e.inlineSolveCount};let n=e.text.trim();if(n.length===0)return {expressions:[],inlineSolveCount:0};let t=chunk3MG7VIPL_cjs.c.findInlineSolves(e.text);return t.length>0?{expressions:t.map(r=>r.expression),inlineSolveCount:t.length}:{expressions:[n],inlineSolveCount:0}}};function ee(u){return typeof u.unit=="string"?u.unit:String(u.value)}function ie(u,e,n={inputType:"markdown"}){let t=u.getDocumentModel(),r=new R;r.setDocument(e);let s=new w(r,u);try{let i=r.lineCount;s.evaluate({startLine:1,endLine:i});let a=[],l=[],o=0;for(let c=1;c<=i;c++){let d=r.getLineAt(c),p=d.text,f=o,g=o+p.length;o=g+1;let h=d.inlineSolveCount>0,v=[],y=null,b=null;d.isEmpty||(h?v=chunkLYLRGYEC_cjs.n(p,c).map((L,m)=>{let x=d.results[m]?.[0]??null;if(x&&x.type===13){let E=ee(x);return l.push(`Line ${c}: ${E}`),{...L,result:null,error:E}}return {...L,result:x,error:null}}):(y=d.expressions[0]??null,b=d.result??null,b&&b.type===13&&l.push(`Line ${c}: ${ee(b)}`))),a.push({lineNumber:c,text:p,startPosition:f,endPosition:g,isEmpty:d.isEmpty,hasInlineSolves:h,inlineSolves:v,expression:y,error:null,result:b});}return {lines:a,totalLines:i,errors:l}}finally{s.terminateWorker(),u.setDocumentModel(t);}}exports.a=R;exports.b=Y;exports.c=w;exports.d=ie;//# sourceMappingURL=chunk-KTAICPWB.cjs.map
3
+ //# sourceMappingURL=chunk-KTAICPWB.cjs.map