[Pas2js] @pointer/reference inside ASM block - missing feature
warleyalex
warleyalex at yahoo.com.br
Sat Jun 9 16:04:25 CEST 2018
I came across an issue when using the ASM block/section: "you have to
initialize the variables reference" before you use that reference iniside
the ASM block.
for instance:
---------------------
var
s: string;
Begin
s = 'Hello World!';
ASM
console.log(s);
End;
---------------------
transpiles to:
---------------------
var $mod = this;
this.s = '';
$mod.$main = function(){
$mod.s = "Hello World!";
console.log(s);
};
---------------------
Pas2JS does not support @ prefix with the ASM Block:
----------------------------------------------------
Since the pas2js compiler does neither parse, nor check the syntax of the
JS, it does not understand the prefix@ inside the ASM section, for instance:
-------------
function getSomeData: JSValue;
begin
asm
/* return a data structure */
@Result = {first:"hello",second:"hello2"}
end;
end;
------------
You'll get "unexpected token ILLEGAL".
The @ prefix can be used inside the ASM block, as such, the compiler needs
to be able to quickly figure out if you are using a JS based variable or an
Object Pascal variable. So all Object Pascal variables have to be prefixed
with the @ character.
and JSValue serves as a universal data reference. JSValue are special
however, the JSValue type can just used to transport data from one method to
another.
I think the @ pointer could be used at all the standard types are there:
string, boolean, float, integer and so on. You also have records, interfaces
and enumerations to play with. It really is a neat system once you get the
hang of it.
When you use the @ prefix inside the ASM block, the pas2js should initialize
variables types/functions beforehand.
Example: this code compile but does not work. (see no @ prefix in ASM)
---------------------------------------------------------------------
procedure ProcessMessage;
function doTask(aResponse: JSValue): JSValue;
begin
console.log( aResponse );
end;
function _doTask0(aResponse: JSValue): JSValue;
begin
result := wait(1000);
end;
function _doTask1(aResponse: JSValue): JSValue;
begin
console.log('hi, after 10 secs');
result := wait(500);
end;
function _doTask2(aResponse: JSValue): JSValue;
begin
console.log('hi after 5 more secs');
result := wait(200);
end;
function _doTask3(aResponse: JSValue): JSValue;
begin
console.log('hi after 2 more secs');
result := wait(0);
end;
begin
ASM
Promise.resolve()
.then(doTask.bind(null, 'teste'))
.then(_doTask0)
.then(_doTask1)
.then(_doTask2)
.then(_doTask3);
END;
end;
---------------------------------------------------------------------
the pas2js will emit:
-------------------
function ProcessMessage() {
Promise.resolve()
.then(doTask.bind(null, 'teste'))
.then(_doTask0)
.then(_doTask1)
.then(_doTask2)
.then(_doTask3);
};
--------------------------------------
See, the compiler will not emit some methods, smart linking has omitted some
wanted methods: doTask0, _doTask0, _doTask1, _doTask2, _doTask3.
An workaround would be to create a dummy variable to point to the methods;
var
res: TJPromiseCallback_fn;
{...}
{ dummy variable will allow to smart linking emit the required
methods }
res:= @doTask;
res:= @_doTask0;
res:= @_doTask1;
res:= @_doTask2;
res:= @_doTask3;
--------------------------
We could use the @prefix here, pas2js could auto initialize the
variable/functions reference. e.g.
ASM
Promise.resolve()
.then((@doTask).bind(null,'teste'))
.then(@_doTask0)
.then(@_doTask1)
.then(@_doTask2)
.then(@_doTask3);
END;
--
Sent from: http://pas2js.38893.n8.nabble.com/
More information about the Pas2js
mailing list