THPDFQuickJS.InstallReplayProvider
Unit: HPDFQuickJS
Declaration
function InstallReplayProvider(Callback: THPDFQJSReplayCallback;
Opaque: Pointer): Boolean;
THPDFQJSReplayCallback = function(Opaque: Pointer; EntryID, Field: Integer;
Buffer: Pointer; Capacity: NativeUInt; out Length: NativeUInt): Integer; cdecl;
Behavior and ownership
Installs the optional HPDFQJS_InstallReplayProvider callback and returns True on success; False means unavailable support, an invalid callback, a busy engine or installation failure
The callback and Opaque storage must remain valid for the engine lifetime. A replacement provider must preserve the same immutable logical-ID namespace. One engine is not reentrant: do not call Eval, EvalCached or destroy it from the callback, and do not allow native exceptions to escape
The required bridge ABI and existing retired-registry callbacks remain unchanged. The replay provider has independent callback storage and does not replace either retired-registry mode
Two-pass UTF-8 protocol
Field 0 requests the recorded script, including its recorded clock and random-seed prefix; field 1 requests the recorded result. Each logical ID and field must resolve to immutable raw UTF-8 bytes
| Callback result | Meaning |
|---|---|
| Positive | The requested field is available and Length reports its exact byte count |
| Zero | The logical ID is unavailable |
| Negative | A host or storage failure occurred |
The first call has Buffer=nil and Capacity=0 and queries the exact length without copying payload. The second call supplies a bridge-owned buffer with capacity equal to that queried length; the callback copies the bytes and reports exactly the same length
A successful zero-length field is an empty string, not a missing record. Its second call still has a nonnil dummy buffer and zero capacity, so the provider can distinguish query from copy
Do not retain the supplied buffer or free it through the host allocator. Copy exactly the explicit-length bytes, including embedded NUL, without adding or requiring a terminator. The bridge creates the JS string and frees its context-owned intermediate buffer on every path
Negative copy results, copy-time absence and length disagreements are infrastructure failures. Callback work should remain bounded and return promptly; the bridge cannot preempt a blocking host I/O operation
Installed native function and limits
Advanced engine hosts receive globalThis.__hpdfReplayNative(entryID, field) with an immutable, nonenumerable own data property replayFields=true. The raw function returns a string for success and undefined for an unavailable ID, and never evaluates the returned source
IDs must be primitive integral Numbers from 0 through 2147483647, with negative zero accepted as zero; field must be primitive Number 0 or 1. Invalid arguments throw without coercion, user valueOf calls or provider access
The engine constructor's actual THPDFQuickJSLimits.MaxScriptBytes applies to the complete fetched script field, even when the outer Eval text is only a short getter call. Set the intended limit before creating the engine; result fields are not subject to the script-byte cap and remain subject to VM and host storage budgets
Representation, script-size, allocation, host I/O and copy failures latch a top-level infrastructure error. Eval and EvalCached cannot report success when JavaScript catches one of these fetch errors, including a catch inside completion-value toString; resource failures report qjsResourceError
The fetch path retains the original top-level deadline through completion conversion and checks it around callback and string-construction work without restarting the clock or interrupt counter; an expired fetch reports qjsTimeLimit
Ordinary missing IDs and invalid caller arguments do not latch that infrastructure failure. They remain ordinary JS outcomes that application code may handle; this guard does not change ordinary nonfetch completion-conversion limits
XFA Session integration
The XFA Session captures the native function during bootstrap and removes its temporary raw global before replaying user history. It supplies __hpdfReplay.script(id) and result(id) to its generated fragments while preserving caller-level indirect eval, recorded Date and Math.random inputs, result checks and public reference lifetime
A Session detects optional support once and preserves that mode and its module lease through rebuilding. Older bridges retain the eager entry-based implementation; an advertised provider that fails to install produces an error rather than a silent downgrade
Related APIs
SupportsReplayProvider, Eval, Native replay provider and Session replay field residency