Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

Pas2js Package

Overview

This is a proposal for pas2js packages, working similar to FPC/Delphi packages.

Advantages of packages:

Disadvantages of packages:

Source

A pas2js package has a pascal source starting with keyword package instead of program, and it has an optional requires and contains clause.

Compiling a package

When pas2js compiles a package, it generates a pkgname.ppmd, a pkgname.js and optionally a pkgname.js.map file. A ppmd stands for "pas2js package meta data".

PPMD

The ppmd is a json file and contains the package units including their sources, plus data about specializations (specialized generics) and whatever else is needed.

Specializations

Each pas2js package can have its own specializations - same as in Delphi:

Contains

All units of a package must be listed in the contains clause Contrary to Delphi if a package uses a unit outside a package pas2js creates an error. This means a package can only use packaged units.

Requires

Lists all packages needed by the package.

Using a package

Compiling a program/library with packages:

pas2js gets a new command line option -Fp<pkgname;pkgnam;...> to specify the packages of a program/library and -FP<dir;dir;...> to add directories to the package search path. pas2js searches the ppmd files first in the -FP search paths and then in the unit search paths given by -Fu. It loads all the ppmd recursively. Then it starts the normal compilation, reading all units, searching units first in the packages, then in the unit path.

Then it generates js only for the program sources.

The html needs to load all js files of the packages and the program.

The -Jc option

If -Jc is given when compiling a program with packages pas2js concatenates all the package js and the program into one js (generating a big js.map).

Without -Jc pas2js concatenates all program js, leaving out the packages.

Optimizations

With precompiled package js files pas2js cannot remove unneeded code from the packages, as it does not yet have any post-js optimization. Theoretically since the packages contain all the sources, it could generate a program the current way (not using the package js files).

Dynamic loading

Packages can be loaded after the program was loaded.

Unloading is not supported (yet) as this requires more bookkeeping what was loaded.

Implementation

ToDos


From the Free Pascal wiki, page "Pas2js Package", as it stood on 2023-01-16 after a change by Mattias2, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .