mirror of
https://gitlab.com/freepascal.org/fpc/pas2js.git
synced 2025-08-24 07:00:05 +02:00
pastojs: mark NewInstance function as used
This commit is contained in:
parent
b2ce235ff0
commit
01daeca427
@ -13,8 +13,13 @@
|
|||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
|
|
||||||
Abstract:
|
Abstract:
|
||||||
Extends the FCL Pascal use analyzer for the language subset of pas2js.
|
Extends the FCL Pascal use analyzer for the language subset of pas2js.
|
||||||
|
|
||||||
|
Works:
|
||||||
|
- Array of Const marks function System.VarRecs()
|
||||||
|
- TPascalDescendantOfExt.Create marks class method NewInstance
|
||||||
|
|
||||||
}
|
}
|
||||||
unit Pas2jsUseAnalyzer;
|
unit Pas2jsUseAnalyzer;
|
||||||
|
|
||||||
@ -35,6 +40,7 @@ type
|
|||||||
TPas2JSAnalyzer = class(TPasAnalyzer)
|
TPas2JSAnalyzer = class(TPasAnalyzer)
|
||||||
public
|
public
|
||||||
procedure UseExpr(El: TPasExpr); override;
|
procedure UseExpr(El: TPasExpr); override;
|
||||||
|
procedure UseConstructor(Proc: TPasConstructor; PosEl: TPasElement); virtual;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
@ -86,11 +92,35 @@ begin
|
|||||||
Ref:=TResolvedReference(El.CustomData);
|
Ref:=TResolvedReference(El.CustomData);
|
||||||
Decl:=Ref.Declaration;
|
Decl:=Ref.Declaration;
|
||||||
if Decl is TPasProcedure then
|
if Decl is TPasProcedure then
|
||||||
CheckArgs(TPasProcedure(Decl).ProcType.Args)
|
begin
|
||||||
|
CheckArgs(TPasProcedure(Decl).ProcType.Args);
|
||||||
|
if Decl.ClassType=TPasConstructor then
|
||||||
|
UseConstructor(TPasConstructor(Decl),El);
|
||||||
|
end
|
||||||
else if Decl.ClassType=TPasProperty then
|
else if Decl.ClassType=TPasProperty then
|
||||||
CheckArgs(Resolver.GetPasPropertyArgs(TPasProperty(Decl)));
|
CheckArgs(Resolver.GetPasPropertyArgs(TPasProperty(Decl)));
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TPas2JSAnalyzer.UseConstructor(Proc: TPasConstructor;
|
||||||
|
PosEl: TPasElement);
|
||||||
|
var
|
||||||
|
ClassScope: TPas2JSClassScope;
|
||||||
|
begin
|
||||||
|
if Proc.Parent.ClassType=TPasClassType then
|
||||||
|
begin
|
||||||
|
ClassScope:=TPasClassType(Proc.Parent).CustomData as TPas2JSClassScope;
|
||||||
|
repeat
|
||||||
|
if ClassScope.NewInstanceFunction<>nil then
|
||||||
|
begin
|
||||||
|
UseProcedure(ClassScope.NewInstanceFunction);
|
||||||
|
break;
|
||||||
|
end;
|
||||||
|
ClassScope:=ClassScope.AncestorScope as TPas2JSClassScope;
|
||||||
|
until ClassScope=nil;
|
||||||
|
end;
|
||||||
|
if PosEl=nil then ;
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -76,6 +76,7 @@ type
|
|||||||
procedure TestWPO_Class_OmitPropertyGetter2;
|
procedure TestWPO_Class_OmitPropertyGetter2;
|
||||||
procedure TestWPO_Class_OmitPropertySetter1;
|
procedure TestWPO_Class_OmitPropertySetter1;
|
||||||
procedure TestWPO_Class_OmitPropertySetter2;
|
procedure TestWPO_Class_OmitPropertySetter2;
|
||||||
|
procedure TestWPO_Class_KeepNewInstance;
|
||||||
procedure TestWPO_CallInherited;
|
procedure TestWPO_CallInherited;
|
||||||
procedure TestWPO_UseUnit;
|
procedure TestWPO_UseUnit;
|
||||||
procedure TestWPO_ArrayOfConst_Use;
|
procedure TestWPO_ArrayOfConst_Use;
|
||||||
@ -724,6 +725,56 @@ begin
|
|||||||
'']));
|
'']));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TTestOptimizations.TestWPO_Class_KeepNewInstance;
|
||||||
|
begin
|
||||||
|
StartProgram(false);
|
||||||
|
Add([
|
||||||
|
'{$modeswitch externalclass}',
|
||||||
|
'type',
|
||||||
|
' TExt = class external name ''Object''',
|
||||||
|
' end;',
|
||||||
|
' TBird = class(TExt)',
|
||||||
|
' protected',
|
||||||
|
' class function NewInstance(fnname: string; const paramarray): TBird; virtual;',
|
||||||
|
' public',
|
||||||
|
' constructor Create;',
|
||||||
|
' end;',
|
||||||
|
'class function TBird.NewInstance(fnname: string; const paramarray): TBird;',
|
||||||
|
'begin',
|
||||||
|
' asm',
|
||||||
|
' Result = Object.create();',
|
||||||
|
' end;',
|
||||||
|
'end;',
|
||||||
|
'constructor TBird.Create;',
|
||||||
|
'begin',
|
||||||
|
' inherited;',
|
||||||
|
'end;',
|
||||||
|
'begin',
|
||||||
|
' TBird.Create;',
|
||||||
|
'']);
|
||||||
|
ConvertProgram;
|
||||||
|
CheckSource('TestWPO_Class_KeepNewInstance',
|
||||||
|
LinesToStr([
|
||||||
|
'rtl.createClassExt($mod, "TBird", Object, "NewInstance", function () {',
|
||||||
|
' this.$init = function () {',
|
||||||
|
' };',
|
||||||
|
' this.$final = function () {',
|
||||||
|
' };',
|
||||||
|
' this.NewInstance = function (fnname, paramarray) {',
|
||||||
|
' var Result = null;',
|
||||||
|
' Result = Object.create();',
|
||||||
|
' return Result;',
|
||||||
|
' };',
|
||||||
|
' this.Create = function () {',
|
||||||
|
' return this;',
|
||||||
|
' };',
|
||||||
|
'});',
|
||||||
|
'']),
|
||||||
|
LinesToStr([
|
||||||
|
'$mod.TBird.$create("Create");',
|
||||||
|
'']));
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TTestOptimizations.TestWPO_CallInherited;
|
procedure TTestOptimizations.TestWPO_CallInherited;
|
||||||
begin
|
begin
|
||||||
StartProgram(false);
|
StartProgram(false);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<CONFIG>
|
<CONFIG>
|
||||||
<ProjectOptions BuildModesCount="1">
|
<ProjectOptions>
|
||||||
<Version Value="12"/>
|
<Version Value="12"/>
|
||||||
<General>
|
<General>
|
||||||
<Flags>
|
<Flags>
|
||||||
@ -17,8 +17,8 @@
|
|||||||
<i18n>
|
<i18n>
|
||||||
<EnableI18N LFM="False"/>
|
<EnableI18N LFM="False"/>
|
||||||
</i18n>
|
</i18n>
|
||||||
<BuildModes>
|
<BuildModes Count="1">
|
||||||
<Item1 Name="Default" Default="True"/>
|
<Item1 Name="default" Default="True"/>
|
||||||
</BuildModes>
|
</BuildModes>
|
||||||
<PublishOptions>
|
<PublishOptions>
|
||||||
<Version Value="2"/>
|
<Version Value="2"/>
|
||||||
|
Loading…
Reference in New Issue
Block a user