Recent

Author Topic: Web assembly in Lazarus  (Read 2936 times)

Prakash

  • Full Member
  • ***
  • Posts: 165
Web assembly in Lazarus
« on: September 18, 2023, 08:19:19 pm »
Does any one worked on web assembly in Lazarus.

Please guide me

PierceNg

  • Sr. Member
  • ****
  • Posts: 364
    • SamadhiWeb
Re: Web assembly in Lazarus
« Reply #1 on: September 19, 2023, 04:08:41 am »
For starters, read the wiki pages.

circular

  • Hero Member
  • *****
  • Posts: 4122
    • Personal webpage
Re: Web assembly in Lazarus
« Reply #2 on: September 19, 2023, 07:52:08 am »
Hi Prakash!

Compiling to WebAssembly is an exciting idea.

There is some documentation here: https://wiki.freepascal.org/WebAssembly

For what I understand, the programs can be embedded in a web page with pas2js and it runs within it a WebAssembly compiled. For example:
https://www.freepascal.org/~michael/pas2js-demos/wasienv/canvas/
There are links at the bottom of the page to show the source code.

Regards
Conscience is the debugger of the mind

egsuh

  • Hero Member
  • *****
  • Posts: 1246
Re: Web assembly in Lazarus
« Reply #3 on: October 03, 2023, 03:26:17 pm »
Does it mean to hide codes of jacascript?

PierceNg

  • Sr. Member
  • ****
  • Posts: 364
    • SamadhiWeb
Re: Web assembly in Lazarus
« Reply #4 on: October 04, 2023, 02:54:17 am »
Does it mean to hide codes of jacascript?

WebAssembly (Wasm) is the 'executable' binary format for an abstract machine. The web browser implements this abstract machine allowing it to execute .wasm files. Wasm is designed to be executed at near native speed; well, nearer to native speed than Javascript can be.

Since Wasm is an executable format, programming languages can target it. That's another benefit of Wasm:
Write programs for the web browser in your favourite language.

Actually, modern computers are so fast, and desktop computers typically so idle, people have compiled interpreters (Python, Javascript, etc) into Wasm, then use the web browser's Wasm engine to run the interpreter to run Python/Javascript/whatever scripts. Layers upon layers of compilation/transformation and still fast enough to be useful and/or fun.

egsuh

  • Hero Member
  • *****
  • Posts: 1246
Re: Web assembly in Lazarus
« Reply #5 on: October 05, 2023, 03:45:34 am »
Wow. This is great. I’m more interested in hiding javascript codes so that others cannot see them.

tr_escape

  • Sr. Member
  • ****
  • Posts: 431
  • sector name toys | respect to spectre
    • Github:
Re: Web assembly in Lazarus
« Reply #6 on: October 05, 2023, 07:29:08 am »
Wow. This is great. I’m more interested in hiding javascript codes so that others cannot see them.

Actually users can be see transpilled/compiled codes as javascript by some tools:

https://webassembly.github.io/wabt/demo/wat2wasm/index.html

Main repo:
https://github.com/WebAssembly/wabt/blob/main/docs/decompiler.md

Also there are some attemps to decompile one of them:

https://v8.dev/blog/wasm-decompile

But ofcourse it isn't your pascal codes
« Last Edit: October 05, 2023, 07:38:41 am by tr_escape »

Warfley

  • Hero Member
  • *****
  • Posts: 1486
Re: Web assembly in Lazarus
« Reply #7 on: October 06, 2023, 09:44:32 am »
The thing about WASM is, as great as it sounds, it's actually not as useful for web development as it may appear. The problem is the browser has no APIs towards WASM, meaning you WASM code cannot interact with anything in the browser. It cannot display anything, access the DOM, the web storage, perform webrequests, etc.
All that WASM can do is do raw computations and then call JavaScript functions which can then actually do this sort of work.

So to give an example, lets say you want to color a button green after you clicked it, you would need to first register a JavaScript function to be called when the button is clicked. This JS function then calls the WASM function, which then has the logic to select the green color, and then it would call a javascript function which takes the color as argument and would set the color of the button to that argument.
At this point you replaced 1 javascript function with two functions of JavaScript and one function of WASM with absolutely no benefit

Meaning if you just want to make a website that displays things and interacts with a backend, WASM does not give you any advantage, because for all that functionality you need to use javascript anyway.

WASM is only good if you need to perform a lot of RAW computations, e.g. encoding and decoding video files or something similar. If you do not have such requirements, using plain Pas2JS is probably going to be the better choice for you.
« Last Edit: October 06, 2023, 09:52:10 am by Warfley »

egsuh

  • Hero Member
  • *****
  • Posts: 1246
Re: Web assembly in Lazarus
« Reply #8 on: October 07, 2023, 09:17:14 am »
Quote
meaning you WASM code cannot interact with anything in the browser. It cannot display anything, access the DOM, the web storage, perform webrequests, etc.

If this is the case, WASM has no merit at all for me, because my javascript codes are mostly accessing and controlling DOM.

Fibonacci

  • Sr. Member
  • ****
  • Posts: 330
  • Certified Internal Error Hunter
Re: Web assembly in Lazarus
« Reply #9 on: October 07, 2023, 09:26:59 am »
WASM is for things like this:

https://wavacity.com/

24 MB of WASM

Rendering, processing, computing

Warfley

  • Hero Member
  • *****
  • Posts: 1486
Re: Web assembly in Lazarus
« Reply #10 on: October 07, 2023, 12:50:04 pm »
WASM is for things like this:

https://wavacity.com/

24 MB of WASM

Rendering, processing, computing

Note that this is emscripten, which automatically also generates the required JavaScript to interact with the browser API. So besides the 24 MB of WASM, this also contains a 12k loc JavaScript file

Emscripten is aboslutely great to translate existing C++ programs into WASM and JavaScript, with very little effort. It has automatic translation rules for GUI frameworks like GTK, for filesystem access, etc. which is why it can be used to quite seamlessly port whole C++ applications (like Audacity in this case) to the web.

The FPC WASM capabilities are of course much more limited, you can't expect to put in a whole Lazarus Project and get a fully functional WASM+JS application out.
I think for Pascal you are going to rather do most of the work in Pas2JS manually, and then take the parts that require more performance and put them into a dedicated WASM library, which is then called by the Pas2JS code.

If this is the case, WASM has no merit at all for me, because my javascript codes are mostly accessing and controlling DOM.
As I said, WASM is great in theory, but in practice not that useful for the vast majority of applications. That said I think to have read somewhee that the Webassembly group at W3C is currently working on a future version of WASM, which will not require JavaScript as intermediary anymore, but get direct access to a set of browser APIs.
« Last Edit: October 07, 2023, 12:52:30 pm by Warfley »

Thaddy

  • Hero Member
  • *****
  • Posts: 13959
  • Probably until I exterminate Putin.
Re: Web assembly in Lazarus
« Reply #11 on: October 07, 2023, 05:02:15 pm »
FPC has a native WASI/WASM back end. Just build a cross-compiler for target WASM.... No need to fiddle with other languages.
Specialize a type, not a var.

 

TinyPortal © 2005-2018