Jump to content

Recommended Posts

  • Moderators

Every now and then (not often) it is worthwhile to check what a compiled function actually looks like.

An easy way to do that is to use the toString method

function test(a:string): string;
begin
  result := 'Da'+a;
end;

Procedure TForm1.ListProc;
begin
  console.log(test('da'));                                    //"Dada"
  asm console.log( (@test).toString() ); end;                 //lists compiled external function 'test'
  asm console.log( (this.ListProc).toString() ); end;         //lists compiled procedure TForm1.ListProc
  asm console.log( (this.@InitializeForm).toString() ); end;  //lists compiled procedure TForm1.InitializeForm
end;

which results in

"Dada"

function test(a) {
   return "Da"+a;
}

function (Self) {
      console.log( test("da") );
      console.log( (test).toString() );
      console.log( (this.ListProc).toString() );
      console.log( (this.InitializeForm).toString() );
   }

function (Self) {
      TW3Form.InitializeForm(Self);
   }

 

Link to post
Share on other sites
  • 4 weeks later...
  • Moderators

When creating webworkers, the choice is to either set up a separate project for each worker (see featured demo's), or create separate text files with the webworkers code.

Another alternative is to create workers from a string  : 

myWorker := createWorker(#"
    onmessage = function(e) {
      console.log(e.data);
    } ");

 

function createWorker(workerFunc: string) : variant;
var newworker: variant;
begin
asm
  const blob = new Blob([workerFunc], {type: 'application/javascript'});
  const url = URL.createObjectURL(blob);
  @newworker = new Worker(url);
end;
  result := newworker;
end;

 

hybrid coding :(

The main advantage though is that creating workers can be done in the main project, no need to switch back and forth

Note : unfortunately I don't see a possibility to use the 'toString()' method here to generate webworker strings from compiled pascal functions.

 

Edited :

Actually it is possible to create webworkers inline/on the fly from compiled procedures without resorting to writing js-code.

As an example the TestFunc procedure below is injected into a new Worker as compiled js. No need to specify an external worker file either.

//create a new variant
  var Dummy : variant := TVariant.CreateObject;  

//give it an anonymous function
  Dummy.TestFunc := lambda (e:variant)

    function test(a: integer): integer;
    begin
      result := inc(a);
    end;

    asm console.log((@e).data); end;
    var a:integer := 1;
    a := test(a);
    asm console.log(@a); end;
  end;

//convert function TestFunc to a string and create worker from it
  var temp: string;
  asm @temp = ((@Dummy).TestFunc).toString(); end;
  WWTry := createWorker( 'onmessage = ' + temp );

the resulting worker code :

onmessage = function (e) {
         var a = 0;

         function test(a) {
            return ++a;
         };

         console.log((e).data);    // 'testing'
         a = 1;
         a = test(a);
         console.log(a);           // '2'
      }

and executing worker :

//execute worker
  WWTry.postMessage('testing');

results as expected in

'testing'
'2'

Link to post
Share on other sites
  • Moderators

Found that after all it actually is possible to create webworkers inline/on the fly from compiled procedures without resorting to writing js-code and without having to specify an external javascript file either.

See edited previous post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...