Skip to content

Enable Wasm plugin initialization or Wasm plugin state transistion. #3313

@Myriad-Dreamin

Description

@Myriad-Dreamin

Description

Some plugin needs initialization, otherwise they feel painful on becoming stateless:

  • jogs plugin has to retrieve a bytecode (some fixed js library) argument per call.
  • mitex plugin has to retrieve a list of TeX commands/packages data per call.
  • typst.wasm plugin has to retrieve user-specified font data read from filesystem and the fonts often very large, in scale of MB.

From observation, the initialization takes up many CPU time. And, they are initialized once and serves stateless call multiple times. Therefore, it should be great if we provide a functionality to initialize them.

Possible Design 1:

Extend current protocol with an additional init function so that we can initialize it on creating an instance.

#let mathjax-js-with-fonts = plugin("quickjs.wasm", init: (
  ("load_bytecode", mathjax-bytecode),
  ("call", "mathjaxLoadMathFonts", math-fonts),
))
#let other-js = plugin("quickjs.wasm", init: (
  ("load_bytecode", other-bytecode),
))
#let typst-with-fonts = plugin("typst.wasm", init: fonts)

Possible Design 2:

Fork/Clone plugin into a sub plugin by calling some plugin exported function.

#let quickjs-engine = plugin("quickjs.wasm")
#let (mathjax-js, _) = plugin.transist(quickjs-engine, "load_bytecode", mathjax-bytecode)
#let (mathjax-js-with-fonts, _) = plugin.transist(mathjax-js, "call", "mathjaxLoadMathFonts", math-fonts)
#let (other-js, _) = plugin.transist(quickjs-engine, "load_bytecode", other-bytecode)
#let typst = plugin("typst.wasm")
#let typst-with-fonts = plugin.transist(typst, "init_compiler", fonts)

Use Case

How will plugins use the API?

quickjs.wasm initializes a new plugin instance with some JS library:

#let quickjs-engine = plugin("quickjs.wasm")
#let mathjax-js = plugin.transist(quickjs-engine, "load_bytecode", mathjax-bytecode)

typst.wasm loads customized huge set of fonts from filesystem:

#let typst = plugin("typst.wasm")
#let fonts = load-from-filesystem()
#let typst = plugin.transist(typst, "init_compiler", fonts)

Both of these two usage will still keep sane number of instances in the comemo cache.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestNew feature or requestscriptingAbout Typst's coding capabilities

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions