[Pas2js] pas2js WebAssembly SQLite demos

Michael Van Canneyt michael at freepascal.org
Mon Feb 23 22:00:07 CET 2026



On Fri, 20 Feb 2026, Pierce Ng via Pas2js wrote:

> Hi all,
>
> I've put up two demos of using WebAssembly SQLite from pas2js.
>
> - https://samadhiweb.com/demo/demo1.html
> - https://samadhiweb.com/demo/demo2.html
>
> Demo 1 loads and runs WebAssembly SQLite in the browser's main UI thread
> and stores data in the browser local storage. Demo 2 runs in a worker
> thread and stores data in the browser's OPFS.
>
> One quirk I encountered in demo 2 is that I've had to initialize SQLite
> like this:
>
>  asm
>    importScripts('/dist/sqlite3.js');
>  end;

Importscript exists in webworker unit ?
   Self_.ImportScripts();

>  await(sqlite3InitModule._then(function(sqlite3: JSValue): JSValue
>    begin
>      asm
>        self.sqlite3 = sqlite3
>      end;
>    end, Nil));
>
> I tried the below initially, but I got self.sqlite3 not defined later in
> the code, so looks like self in the _then function is different from the
> self outside of it. I also tried globalThis instead of self, same
> unsuccessful outcome.
>
>  asm
>    importScripts('/dist/sqlite3.js');
>    globalThis.sqlite3InitModule().then(function(sqlite3){
>      self.sqlite3 = sqlite3
>    });
>  end;

See above, Self_ exists in webworker. It cannot be 'self' since that is a reserved word
in pas2js.

>
> I noticed that there is an importScripts procedure declared in Pascal,
> but didn't figure out how to use it.

You need simply the webworker unit in your uses clause ? 
I use it myself, for instance in the new https://live.freepascal.org/

>
> I'll publish my demos as a code repo somewhere somewhen. Meanwhile,
> thanks to Javascript source maps, all Pascal code is accessible in the
> browser debugger.
>
> Next demo, unlikely any time soon, will probably be importing an
> existing SQLite database into the browser.

Well, If you're OK with that, I would love to convert it to an 
example in https://live.freepascal.org/ :-)

Michael.


More information about the Pas2js mailing list