pastojs: allow typecast extclass to extclass in mode delphi

This commit is contained in:
mattias 2019-03-06 21:12:24 +00:00
parent 0e7de6af01
commit cd086a2bfd
3 changed files with 49 additions and 1 deletions

View File

@ -4977,6 +4977,14 @@ begin
or IsExternalClass_Name(ToClass,'Object') then
// TJSFunction(@Proc) or TJSFunction(ProcVar)
exit(cExact);
end
else if FromTypeEl.ClassType=TPasClassType then
begin
if TPasClassType(FromTypeEl).IsExternal
and (msDelphi in CurrentParser.CurrentModeswitches)
and not (bsObjectChecks in CurrentParser.Scanner.CurrentBoolSwitches) then
// ExtClass(ExtClass) -> allow in mode delphi and no objectchecks
exit(cAliasExact); // $mode delphi
end;
end;
end;

View File

@ -588,6 +588,7 @@ type
Procedure TestExternalClass_TypeCastToJSObject;
Procedure TestExternalClass_TypeCastStringToExternalString;
Procedure TestExternalClass_TypeCastToJSFunction;
Procedure TestExternalClass_TypeCastDelphiUnrelated;
Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
Procedure TestExternalClass_BracketAccessor;
Procedure TestExternalClass_BracketAccessor_Call;
@ -16377,6 +16378,43 @@ begin
'']));
end;
procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
begin
StartProgram(false);
Add([
'{$mode delphi}',
'{$modeswitch externalclass}',
'type',
' TJSObject = class external name ''Object'' end;',
' TJSWindow = class external name ''Window''(TJSObject)',
' procedure Open;',
' end;',
' TJSEventTarget = class external name ''Event''(TJSObject)',
' procedure Execute;',
' end;',
'procedure Fly;',
'var',
' w: TJSWindow;',
' e: TJSEventTarget;',
'begin',
' w:=TJSWindow(e);',
' e:=TJSEventTarget(w);',
'end;',
'begin']);
ConvertProgram;
CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
LinesToStr([ // statements
'this.Fly = function () {',
' var w = null;',
' var e = null;',
' w = e;',
' e = w;',
'};',
'']),
LinesToStr([ // $mod.$main
'']));
end;
procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
begin
StartProgram(false);

View File

@ -2692,7 +2692,9 @@ End.
call <i>aJSString.fromCharCode()</i>.</li>
<li>An external class can descend from another external class.</li>
<li>Since class types are JS objects it is possible to typecast a class type
to the JS Object, e.g. TJSObject(TObject)</li>
to the JS Object, e.g. <i>TJSObject(TObject)</i>.
Note that you cannot typecast directly to a <i>TJSObject</i> descendant
in $mode objfpc. You can use <i>TJSWindow(TJSObject(ExtClassInstance))</i>.</li>
<li>You can typecast function addresses and function references to JS
function, e.g. <i>TJSFunction(@SomeProc)</i>, <i>TJSFunction(OnClick)</i>.
Keep in mind that typecasting a method address creates a function wrapper