19 Commits
Author SHA1 Message Date
calixteman 5258159433 Merge pull request #22438 from b84330808/fix-ps-idiv-exp-simplifications
Fix the `x 1 idiv` and `x -1 exp` simplifications in `PSStackToTree`
2026-11-07 14:14:54 +01:00
b84330808 3a326e3a80 Fix the x 1 idiv and x -1 exp simplifications in PSStackToTree
`x 1 idiv` was simplified to `x`, so the Wasm and JS compilers returned a
non-integer x unchanged (e.g. 7.5), whereas the interpreter truncates it
(7). It's now simplified to `truncate(x)`.

`x -1 exp` was rewritten as `1 x div`, which gives 0 for x = 0 since a
division by zero returns 0, whereas the interpreter computes Infinity,
which is then clipped to the range. This rewrite is removed.
2026-11-04 23:52:32 +09:00
Victor Hugo 0aaac4482c Keep the stacks aligned on their top in PSStackToTree
When the two paths of a stack-growing `if`, or the two branches of an
`ifelse`, end with different stack depths, the shorter stack was padded
with zeros on its top. The following operators then used these zeros
instead of the actual values: for example the tint transform in
issue5470.pdf is only correct when its guard fires, i.e. for a tint of 1,
and any other tint is rendered in a darker red instead of red.

Pad the bottom of the shorter stack instead. And when a function leaves
more values on the stack than it has outputs, use the ones at the top, as
the interpreter does, instead of the ones at the bottom.
2026-11-02 13:47:26 -03:00
Jonas Jenwald 2d4949c1e7 Advance the position in the PostScript Lexer on invalid operators/numbers (bug 2086012)
Without this parsing hangs and the document never renders, since *the same* character will be re-parsed indefinitely.
2026-10-28 18:26:15 +01:00
calixteman 9815e4eadd Limit PostScript expression and procedure depth
Cap expression depth to bound recursive comparison, sharing analysis and
code generation. The JS builder interprets deeper expressions. Reject
excessive procedure nesting during parsing.
2026-10-18 20:36:41 +02:00
calixteman 66a86c06d0 Bound PostScript stack-to-tree conversion
Stack-growing `if` forks and comparisons of distinct DAGs can take
exponential work. Limit instruction and comparison work and fork depth;
the JS builder uses the interpreter when conversion exceeds a limit.
2026-10-18 20:36:41 +02:00
calixteman 85698f0e4f Distinguish PostScript booleans from numbers
Preserve boolean flags through stack operations so `0 not` returns -1
and booleans compare unequal to numbers.
2026-10-17 21:19:43 +02:00
Calixte Denizet 9e56fe1cf3 Avoid integer conversion traps in PostScript Wasm
Match JS `x | 0`, including for constant-folded cvi.
2026-10-14 22:51:26 +02:00
Calixte Denizet 3d4e2f2710 Fix the PostScript Wasm compiler for a mod by a constant zero
`OP.drop` wasn't defined, so the dividend was followed by a 0x00 byte
(`unreachable`) and the function trapped when called, dropping the rest of
the page. Since the result is 0 whatever the dividend is, just emit 0.
2026-10-13 18:10:25 +02:00
Jayesh Bhade a163deb6eb Fix the two MathClamp calls the prefer-math-clamp fixer mis-ordered
The rule rewrites `Math.max(C, Math.min(A, B))` to `MathClamp(A, C, B)`, always
taking the inner call's first operand as the value. `Math.min` is commutative,
so the value is just as often the second one, and then the fixer swaps it with
the upper bound: the result is `Math.min(C, v)`, which no longer applies the
lower bound at all. Both call sites the fixer rewrote hit that case, so the
max-outer patterns are now reported without a fix.

Reporting them still needs its own message. `Math.max(C, Math.min(A, B))` is
only `MathClamp(A, C, B)` when `C <= B`, and the rule can't know whether that
holds, so telling the reader to "use MathClamp" is wrong advice. The message
states the condition instead, and `useClamp` is left to the min-outer patterns,
which are exact.

In `PSStackBasedInterpreter.build` the outputs were therefore never clamped up
to the Range minimum. The interpreter runs whenever `PSStackToTree` can't turn
the program into a tree — a stack-shrinking `if`, or a `copy`/`index`/`roll`
whose operand isn't a constant — and it then let a Type 4 function return e.g.
-1.5 for a component declared as `/Range [0 1]`.

In `SplitView.#clampFirstSize` the first pane's size was never clamped up to
`#minSize`: dragging the resizer past it wrote a negative `flexGrow`.

The existing range-clamping test only checked the upper bound, which is the one
the wrong operand order preserves.
2026-08-08 00:21:33 +05:30
Jonas Jenwald 62d211ee8d Use the toBeTrue() matcher consistently in the unit/font/integration tests
This replaces all `toEqual(true)` and `toBe(true)` occurrences.
2026-07-25 23:52:28 +02:00
Jonas Jenwald 6963b0ef4f Use the toBeFalse() matcher consistently in the unit/integration tests
This replaces all `toEqual(false)` and `toBe(false)` occurrences.
2026-07-25 22:49:03 +02:00
Jonas Jenwald 432d5c57a4 Use the toThrowError() matcher consistently in the unit tests
Currently we mostly use `toThrow()`, which seems intended for things that throw non-Errors (something that we "forbid" with ESLint).
Hence `toThrowError()` seems more appropriate, and it also simplifies things slightly; see https://jasmine.github.io/api/edge/matchers.html#toThrowError
2026-07-25 14:30:55 +02:00
uwezkhan 778aa450d9 Encode i32.const immediates as signed LEB128 in the PostScript Wasm compiler
The Type-4 PostScript -> Wasm compiler emitted i32.const immediates with the
unsigned LEB128 encoder (_emitULEB128). Wasm decodes i32.const as a *signed*
LEB128, so any immediate whose final 7-bit group has bit 0x40 set is
mis-decoded (e.g. 64 -> single byte 0x40 -> read back as -64).

The output-store address for the i-th result is emitted as i32.const (i*8).
For a function with >= 9 outputs the 9th offset is 64, which decodes as -64, so
f64.store targets 0xFFFFFFC0 and traps (memory access out of bounds). The module
still validates and instantiates, so the JS fallback in function.js is not
engaged; the trap only surfaces at render time (the affected shading / tint
transform silently fails to render). The same mis-encoding affects the constant
bitshift amount.

Add a signed-LEB128 emitter (_emitSLEB128) and use it for the three i32.const
immediate sites; local/import/type indices remain unsigned (correct). Add a unit
test covering functions with 9+ outputs, and a reference test rendering a shading
whose colour function has 9 outputs.
2026-07-20 14:56:04 +05:30
Jonas Jenwald ccab310a39 Add an optional parameter in buildPostScriptJsFunction to force use of the PSStackBasedInterpreter code
This way the test-only function `buildPostScriptProgramFunction` can be removed.
2026-04-05 13:52:09 +02:00
calixteman 8c7a5f3500 Add a js fallback for interpreting ps code
It's a basic stack based interpreter.
A wasm version will come soon.
2026-04-01 21:40:45 +02:00
Calixte Denizet 9f3de1edf6 Add an interpreter for optimized ps code
It'll be used as a fallback when wasm is disabled.
And add in the debugger a view for the generated js code and one for the ps code.
2026-03-31 21:00:22 +02:00
Calixte Denizet 63cf35b47f Avoid expressions duplication in the ps AST and use a local instead when compiling to WASM 2026-03-30 16:30:33 +02:00
calixteman 952952c905 [api-minor] Rewrite the ps lexer & parser and add a small Wasm compiler
The main goal is to remove the eval-based interpreter.
In order to have some good performances, the new parser performs some optimizations
on the AST (similar to the ones in the previous implementation),
and the Wasm compiler generates code for the optimized AST.
For now, in case of errors or unsupported features, the Wasm compiler returns null
and the old interpreter is used as a fallback.
Few things are still missing:
 - a wasm-based interpreter using a stack (in case the ps code isn't stack-free);
 - a better js implementation in case of disabled wasm.

 but they will be added in follow-up patches.
2026-03-30 09:22:33 +02:00