Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2026-02

pas2js WebAssembly SQLite demos

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Pierce Ng

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;
  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;

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

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.

Pierce

Michael Van Canneyt
to Pierce Ng
Pierce Ng wrote 17 lines
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();
Show quoted text 18 lines
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/

Show quoted text 7 lines
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.

Pierce Ng
to Michael Van Canneyt
Michael Van Canneyt wrote
Importscript exists in webworker unit ? Self_.ImportScripts();

Not in my installation. Ah, I'm not running the latest stuff. :-)

This is my pas2js:

commit 9ac46614dc8252a44b21346f967dd7d2a87bfeba (HEAD, tag: release_3_2_0, origin/fixes_3_2) Author: mattias
Date: Sat Jul 26 09:49:10 2025 +0200

rtl: less hints

Built with this version of FPC:

commit 5d768f725c6748ddacef547fe74e6e8163347c4e (HEAD -> main, origin/main, origin/HEAD) Author: Micha?l Van Canneyt
Date: Thu Jan 8 15:50:39 2026 +0100

* Remove packed from TXYZReferenceWhite and put Illuminant at the end

Show quoted text 9 lines
> 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 meant doing it all in Javascript wrapped in an asm block, so 'self' and 'globalThis' were Javascript symbols.

Show quoted text 9 lines
> 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/ :-)

Sure, please, go ahead.

Pierce