Sign in Join

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

Mailing lists / fpc-devel / 2014-04

Pascal to Javascript - Call for Help

10 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

Michael Van Canneyt

Hello,

After many years of dreaming of an open source version of Morfik Appsbuilder, (and more recently Smart Mobile Studio), I've finally committed a first version of the pascal-to-javascript conversion engine.

The unit and the associated unit tests are in

packages/pastojs

and a sample converter program in

utils/pas2js

This is a limited initial commit. It will convert plain pascal to javascript, almost verbatim. It will not yet convert everything, objects are not yet supported, for instance, neither are var arguments. Scope is also an issue. In fact, it can convert things that do not need a lot of context - plain syntactical conversion, if you want.

However the engine is IMHO ready for the more serious work. At least it shows the ideas underpinning the project.

This is potentially a big project, which I cannot handle alone (FPC is a circle of volunteers, after all). Therefor I'd like to invite people to have a look at the code, give remarks, submit patches.

That said:

There will also be an effort to let the Free Pascal compiler itself output Javascript (using asm.js or plain javascript), to which I will - to the best of my abilities
- also contribute.

Why 2 efforts ? The goals of these efforts are not the same:

- The goal of the compiler is to compile all of pascal. This will result in unreadable javascript, simulating heap memory etc. It will be more comparable to assembler than to hand-written javascript.

- The goal of this project is to generate understandable Javascript. The idea is not to convert all existing code to work in the browser (depends on the code), but rather to be able to program the browser in Object Pascal.

As a consequence, not all pascal constructs will be supported (pointers, direct memory access jump to mind).

Another matter is that the treshold for understanding the compiler is rather high (I myself don't claim to understand the compiler fully). Whereas I believe that understanding this project should be within reach of everyone that knows object pascal (I may be wrong in this belief, of course ;) )

Both efforts will use the same backend for writing javascript, so the javascript writer and the javascript syntax tree have been kept low-key, meaning that they can be compiled to depend almost exclusively on the system unit (no classes unit, to name but one).

Feel free to discuss here or mail me in private with more questions.

Michael.

Reply by email

hinstance
to Michael Van Canneyt

I can already see what for this tool could be possibly used. For example, one could write ObjectPascal code in Lazarus and then convert it to JavaScript when needed. The advantage is that one could then use Lazarus IDE code refactoring features like code completion, renaming identifiers, searching for identifier usages. These features could be especially useful: renaming functions, renaming variables and code completion. I currently edit my JavaScript sources in notepad. I know that there are IDEs for JavaScript, however, I currently don't use any. Using Object Pascal for JavaScript could be particularly suitable if one develops a web application where server side is implemented in FreePascal while client side web pages contain JavaScript & interact with server.
I will try ur tool on my project soon and report then the results; as soon as I have time.

05.04.2014 13:48, Michael Van Canneyt пишет:

Show quoted text 70 lines
Hello, After many years of dreaming of an open source version of Morfik Appsbuilder, (and more recently Smart Mobile Studio), I've finally committed a first version of the pascal-to-javascript conversion engine. The unit and the associated unit tests are in packages/pastojs and a sample converter program in utils/pas2js This is a limited initial commit. It will convert plain pascal to javascript, almost verbatim. It will not yet convert everything, objects are not yet supported, for instance, neither are var arguments. Scope is also an issue. In fact, it can convert things that do not need a lot of context - plain syntactical conversion, if you want. However the engine is IMHO ready for the more serious work. At least it shows the ideas underpinning the project. This is potentially a big project, which I cannot handle alone (FPC is a circle of volunteers, after all). Therefor I'd like to invite people to have a look at the code, give remarks, submit patches. That said: There will also be an effort to let the Free Pascal compiler itself output Javascript (using asm.js or plain javascript), to which I will - to the best of my abilities - also contribute. Why 2 efforts ? The goals of these efforts are not the same: - The goal of the compiler is to compile all of pascal. This will result in unreadable javascript, simulating heap memory etc. It will be more comparable to assembler than to hand-written javascript. - The goal of this project is to generate understandable Javascript. The idea is not to convert all existing code to work in the browser (depends on the code), but rather to be able to program the browser in Object Pascal. As a consequence, not all pascal constructs will be supported (pointers, direct memory access jump to mind). Another matter is that the treshold for understanding the compiler is rather high (I myself don't claim to understand the compiler fully). Whereas I believe that understanding this project should be within reach of everyone that knows object pascal (I may be wrong in this belief, of course ;) ) Both efforts will use the same backend for writing javascript, so the javascript writer and the javascript syntax tree have been kept low-key, meaning that they can be compiled to depend almost exclusively on the system unit (no classes unit, to name but one). Feel free to discuss here or mail me in private with more questions. Michael. _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Mark Morgan Lloyd
Michael Van Canneyt wrote 6 lines
Hello, After many years of dreaming of an open source version of Morfik Appsbuilder, (and more recently Smart Mobile Studio), I've finally committed a first version of the pascal-to-javascript conversion engine.

:-)

As a consequence, not all pascal constructs will be supported (pointers, direct memory access jump to mind).

I've spent time over the last few months converting some Javascript to Pascal. The thing that really stood out in the original code was that bitwise manipulation of integers (specifically, integers of the order of 48 bits) was poor, resulting in gross overuse of multiply and mod operators.

As a side effect of this (unless, of course, everything works first time), I'm hoping to put time into some comparison utilities that- if things work out- will be smart enough to locate corresponding statements in two different languages. If my recollection of the algorithms is correct, central to doing this is that the relative size of lines stays more or less the same, i.e. the longest lines in the Javascript translate to the longest lines in Pascal (and, obviously, vice versa).

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Marcos Douglas
to Michael Van Canneyt: After many years of dreaming of an open source version of Morfik Appsbuilder, (and more…
Michael Van Canneyt wrote 67 lines
Hello, After many years of dreaming of an open source version of Morfik Appsbuilder, (and more recently Smart Mobile Studio), I've finally committed a first version of the pascal-to-javascript conversion engine. The unit and the associated unit tests are in packages/pastojs and a sample converter program in utils/pas2js This is a limited initial commit. It will convert plain pascal to javascript, almost verbatim. It will not yet convert everything, objects are not yet supported, for instance, neither are var arguments. Scope is also an issue. In fact, it can convert things that do not need a lot of context - plain syntactical conversion, if you want. However the engine is IMHO ready for the more serious work. At least it shows the ideas underpinning the project. This is potentially a big project, which I cannot handle alone (FPC is a circle of volunteers, after all). Therefor I'd like to invite people to have a look at the code, give remarks, submit patches. That said: There will also be an effort to let the Free Pascal compiler itself output Javascript (using asm.js or plain javascript), to which I will - to the best of my abilities - also contribute. Why 2 efforts ? The goals of these efforts are not the same: - The goal of the compiler is to compile all of pascal. This will result in unreadable javascript, simulating heap memory etc. It will be more comparable to assembler than to hand-written javascript. - The goal of this project is to generate understandable Javascript. The idea is not to convert all existing code to work in the browser (depends on the code), but rather to be able to program the browser in Object Pascal. As a consequence, not all pascal constructs will be supported (pointers, direct memory access jump to mind). Another matter is that the treshold for understanding the compiler is rather high (I myself don't claim to understand the compiler fully). Whereas I believe that understanding this project should be within reach of everyone that knows object pascal (I may be wrong in this belief, of course ;) ) Both efforts will use the same backend for writing javascript, so the javascript writer and the javascript syntax tree have been kept low-key, meaning that they can be compiled to depend almost exclusively on the system unit (no classes unit, to name but one). Feel free to discuss here or mail me in private with more questions. Michael.

Hi Michael,

A first little patch.
In line 1101 -- Function TPasToJSConverter.ConvertRepeatStatement -- you need to put a "raise"

===code===
  except
    FreeAndNil(B);
    FreeAndNil(C);
    raise; <<<<
  end;
===code===

Regards,
Marcos Douglas

Reply by email

Michael Schnell
to Michael Van Canneyt: After many years of dreaming of an open source version of Morfik Appsbuilder, (and more…
Michael Van Canneyt wrote
I've finally committed a first version of the pascal-to-javascript conversion engine.

GREAT !
Thanks !

One additional side-chain thought:

I understand that the goal is to allow to compile Pascal programs to be able to run inside a Browser.

After SilverLight / Moonlight / Delphi .NET seems to be a rather dead end, this makes a lot of sense.

OTOH, AFAIK, it is possible (or standard) that Java Byte Code can be run in a sandbox in a Browser. Thus the Pascal to Java-Byte-Code compiler (famous for allowing to create Android APPs) might be able to create code that runs in that environment.

-Michael

Reply by email

Michael Schnell
to Michael Schnell

Another slightly related question: did anybody try to use the fpc to Java-Byte-Code compiler to create "server-site Java" CGI applications (to get around the arch-dependance of native compiled fpc programs) ?

-Michael

Reply by email

Michael Van Canneyt
to Michael Schnell: GREAT ! Thanks ! One additional side-chain thought: I understand that the goal is to…
Michael Schnell wrote 19 lines
On 04/05/2014 11:48 AM, Michael Van Canneyt wrote: > I've finally committed a first version of the pascal-to-javascript > conversion engine. > GREAT ! Thanks ! One additional side-chain thought: I understand that the goal is to allow to compile Pascal programs to be able to run inside a Browser. After SilverLight / Moonlight / Delphi .NET seems to be a rather dead end, this makes a lot of sense. OTOH, AFAIK, it is possible (or standard) that Java Byte Code can be run in a sandbox in a Browser. Thus the Pascal to Java-Byte-Code compiler (famous for allowing to create Android APPs) might be able to create code that runs in that environment.

I don't want Java. Just plain HTML and Javascript, standards-compliant. But I see no reason why you would not be able to use the jvm to do what you want.

Michael.

Reply by email

Michael Schnell
to Michael Van Canneyt
Michael Van Canneyt wrote
I don't want Java. Just plain HTML and Javascript, standards-compliant.

I see. This does make sense.

But I see no reason why you would not be able to use the jvm to do what you want.

I have no idea how to integrate Java Byte Code in a HTML based web "page" in order to run it in such a sandbox . I suppose this is doable, but I have no time to research this right now.

I just thought this might be a viable alternative to the "unreadable javascript" effort variant you mentioned.

-Michael

Reply by email

Michael Van Canneyt
to Marcos Douglas: On Sat, Apr 5, 2014 at 6:48 AM, Michael Van Canneyt A first little patch. In line 1101 --…
Marcos Douglas wrote 16 lines
> > Michael. Hi Michael, A first little patch. In line 1101 -- Function TPasToJSConverter.ConvertRepeatStatement -- you need to put a "raise" ===code=== except FreeAndNil(B); FreeAndNil(C); raise; <<<< end; ===code===

Thank you. There were several other locations where the raise was missing, I fixed them as well.

Michael.

Reply by email

Marcos Douglas
to Michael Van Canneyt
Michael Van Canneyt wrote 25 lines
On Sat, 5 Apr 2014, Marcos Douglas wrote: >> >> Michael. > > > Hi Michael, > > A first little patch. > In line 1101 -- Function TPasToJSConverter.ConvertRepeatStatement -- > you need to put a "raise" > > ===code=== > except > FreeAndNil(B); > FreeAndNil(C); > raise; <<<< > end; > ===code=== Thank you. There were several other locations where the raise was missing, I fixed them as well.

Inspiration, maybe:
http://blog.synopse.info/post/2014/04/07/JavaScript-support-in-mORMot-via-SpiderMonkey

Marcos Douglas

Reply by email