|
@@ -168,128 +168,7 @@ type
|
|
|
Property PasDataClass: TPasDataClass read FPasDataClass write FPasDataClass;
|
|
|
end;
|
|
|
|
|
|
-type
|
|
|
- TPas2jsConversionOption = (
|
|
|
- p2jcoUseNativeTypeAliases,
|
|
|
- p2jcoExternalConst
|
|
|
- );
|
|
|
- TPas2jsConversionOptions = Set of TPas2jsConversionOption;
|
|
|
-
|
|
|
-const
|
|
|
- Pas2jsConversionOptionNames: array[TPas2jsConversionOption] of string = (
|
|
|
- 'UseNativeTypeAliases',
|
|
|
- 'ExternalConst'
|
|
|
- );
|
|
|
-
|
|
|
-type
|
|
|
-
|
|
|
- { TWebIDLToPas2js }
|
|
|
-
|
|
|
- TWebIDLToPas2js = class(TBaseWebIDLToPas)
|
|
|
- Private
|
|
|
- FPas2jsOptions: TPas2jsConversionOptions;
|
|
|
- Protected
|
|
|
- Function BaseUnits: String; override;
|
|
|
- // Auxiliary routines
|
|
|
- procedure GetOptions(L: TStrings; Full: boolean); override;
|
|
|
- function GetTypeName(const aTypeName: String; ForTypeDef: Boolean=False
|
|
|
- ): String; override;
|
|
|
- function GetInterfaceDefHead(Intf: TIDLInterfaceDefinition): String;
|
|
|
- override;
|
|
|
- // Code generation routines. Return the number of actually written defs.
|
|
|
- function WritePrivateReadOnlyFields(aList: TIDLDefinitionList): Integer;
|
|
|
- override;
|
|
|
- function WriteProperties(aList: TIDLDefinitionList): Integer; override;
|
|
|
- // Definitions. Return true if a definition was written.
|
|
|
- function WriteConst(aConst: TIDLConstDefinition): Boolean; override;
|
|
|
- function WritePrivateReadOnlyField(aAttr: TIDLAttributeDefinition): Boolean; virtual;
|
|
|
- function WriteReadonlyProperty(aAttr: TIDLAttributeDefinition): Boolean; virtual;
|
|
|
- Public
|
|
|
- constructor Create(TheOwner: TComponent); override;
|
|
|
- Property Pas2jsOptions: TPas2jsConversionOptions Read FPas2jsOptions Write FPas2jsOptions;
|
|
|
- Published
|
|
|
- Property BaseOptions;
|
|
|
- Property ClassPrefix;
|
|
|
- Property ClassSuffix;
|
|
|
- Property DictionaryClassParent;
|
|
|
- Property FieldPrefix;
|
|
|
- Property IncludeImplementationCode;
|
|
|
- Property IncludeInterfaceCode;
|
|
|
- Property InputFileName;
|
|
|
- Property OutputFileName;
|
|
|
- Property TypeAliases;
|
|
|
- Property Verbose;
|
|
|
- Property WebIDLVersion;
|
|
|
- end;
|
|
|
-
|
|
|
-type
|
|
|
- TJOB_JSValueKind = (
|
|
|
- jjvkUndefined,
|
|
|
- jjvkBoolean,
|
|
|
- jjvkDouble,
|
|
|
- jjvkString,
|
|
|
- jjvkObject,
|
|
|
- jivkMethod
|
|
|
- );
|
|
|
- TJOB_JSValueKinds = set of TJOB_JSValueKind;
|
|
|
-
|
|
|
-const
|
|
|
- JOB_JSValueKindNames: array[TJOB_JSValueKind] of string = (
|
|
|
- 'Undefined',
|
|
|
- 'Boolean',
|
|
|
- 'Double',
|
|
|
- 'String',
|
|
|
- 'Object',
|
|
|
- 'Method'
|
|
|
- );
|
|
|
- JOB_JSValueTypeNames: array[TJOB_JSValueKind] of string = (
|
|
|
- 'TJOB_JSValue',
|
|
|
- 'TJOB_JSValueBoolean',
|
|
|
- 'TJOB_JSValueDouble',
|
|
|
- 'TJOB_JSValueString',
|
|
|
- 'TJOB_JSValueObject',
|
|
|
- 'TJOB_JSValueMethod'
|
|
|
- );
|
|
|
-type
|
|
|
-
|
|
|
- { TWebIDLToPasWasmJob }
|
|
|
-
|
|
|
- TWebIDLToPasWasmJob = class(TBaseWebIDLToPas)
|
|
|
- Protected
|
|
|
- function BaseUnits: String; override;
|
|
|
- // Auxiliary routines
|
|
|
- procedure GetOptions(L: TStrings; Full: boolean); override;
|
|
|
- function GetTypeName(const aTypeName: String; ForTypeDef: Boolean=False
|
|
|
- ): String; override;
|
|
|
- // Code generation routines. Return the number of actually written defs.
|
|
|
- function WritePrivateGetters(aList: TIDLDefinitionList): Integer; override;
|
|
|
- function WritePrivateSetters(aList: TIDLDefinitionList): Integer; override;
|
|
|
- function WriteProperties(aList: TIDLDefinitionList): Integer; override;
|
|
|
- // Definitions. Return true if a definition was written.
|
|
|
- function WritePrivateGetter(Attr: TIDLAttributeDefinition): boolean; virtual;
|
|
|
- function WritePrivateSetter(Attr: TIDLAttributeDefinition): boolean; virtual;
|
|
|
- function WriteProperty(Attr: TIDLAttributeDefinition): boolean; virtual;
|
|
|
- Public
|
|
|
- constructor Create(ThOwner: TComponent); override;
|
|
|
- Published
|
|
|
- Property BaseOptions;
|
|
|
- Property ClassPrefix;
|
|
|
- Property ClassSuffix;
|
|
|
- Property DictionaryClassParent;
|
|
|
- Property FieldPrefix;
|
|
|
- Property GetterPrefix;
|
|
|
- Property SetterPrefix;
|
|
|
- Property IncludeImplementationCode;
|
|
|
- Property IncludeInterfaceCode;
|
|
|
- Property InputFileName;
|
|
|
- Property OutputFileName;
|
|
|
- Property TypeAliases;
|
|
|
- Property Verbose;
|
|
|
- Property WebIDLVersion;
|
|
|
- end;
|
|
|
-
|
|
|
function BaseConversionOptionsToStr(Opts: TBaseConversionOptions): string;
|
|
|
-function Pas2jsConversionOptionsToStr(Opts: TPas2jsConversionOptions): string;
|
|
|
|
|
|
implementation
|
|
|
|
|
@@ -308,319 +187,6 @@ begin
|
|
|
Result:='['+Result+']';
|
|
|
end;
|
|
|
|
|
|
-function Pas2jsConversionOptionsToStr(Opts: TPas2jsConversionOptions): string;
|
|
|
-var
|
|
|
- o: TPas2jsConversionOption;
|
|
|
-begin
|
|
|
- Result:='';
|
|
|
- for o in Opts do
|
|
|
- begin
|
|
|
- if Result<>'' then Result:=Result+',';
|
|
|
- Result:=Result+Pas2jsConversionOptionNames[o];
|
|
|
- end;
|
|
|
- Result:='['+Result+']';
|
|
|
-end;
|
|
|
-
|
|
|
-{ TWebIDLToPasWasmJob }
|
|
|
-
|
|
|
-function TWebIDLToPasWasmJob.BaseUnits: String;
|
|
|
-begin
|
|
|
- Result:='SysUtils, JOB_WAsm';
|
|
|
-end;
|
|
|
-
|
|
|
-procedure TWebIDLToPasWasmJob.GetOptions(L: TStrings; Full: boolean);
|
|
|
-begin
|
|
|
- inherited GetOptions(L, Full);
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPasWasmJob.GetTypeName(const aTypeName: String;
|
|
|
- ForTypeDef: Boolean): String;
|
|
|
-begin
|
|
|
- Case aTypeName of
|
|
|
- 'union',
|
|
|
- 'any': Result:=JOB_JSValueTypeNames[jjvkUndefined];
|
|
|
- else
|
|
|
- Result:=inherited GetTypeName(aTypeName,ForTypeDef);
|
|
|
- if (Result=aTypeName) and (LeftStr(Result,length(ClassPrefix))<>ClassPrefix) then
|
|
|
- Result:=ClassPrefix+Result+ClassSuffix;
|
|
|
- end;
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPasWasmJob.WritePrivateGetters(aList: TIDLDefinitionList
|
|
|
- ): Integer;
|
|
|
-var
|
|
|
- D: TIDLDefinition;
|
|
|
-begin
|
|
|
- Result:=0;
|
|
|
- for D in aList do
|
|
|
- if D is TIDLAttributeDefinition then
|
|
|
- if WritePrivateGetter(TIDLAttributeDefinition(D)) then
|
|
|
- inc(Result);
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPasWasmJob.WritePrivateSetters(aList: TIDLDefinitionList
|
|
|
- ): Integer;
|
|
|
-var
|
|
|
- D: TIDLDefinition;
|
|
|
-begin
|
|
|
- Result:=0;
|
|
|
- for D in aList do
|
|
|
- if D is TIDLAttributeDefinition then
|
|
|
- if WritePrivateSetter(TIDLAttributeDefinition(D)) then
|
|
|
- inc(Result);
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPasWasmJob.WriteProperties(aList: TIDLDefinitionList
|
|
|
- ): Integer;
|
|
|
-var
|
|
|
- D: TIDLDefinition;
|
|
|
-begin
|
|
|
- Result:=0;
|
|
|
- for D in aList do
|
|
|
- if D is TIDLAttributeDefinition then
|
|
|
- if WriteProperty(TIDLAttributeDefinition(D)) then
|
|
|
- inc(Result);
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPasWasmJob.WritePrivateGetter(Attr: TIDLAttributeDefinition
|
|
|
- ): boolean;
|
|
|
-var
|
|
|
- FuncName, TypeName, aClassName, Code, ReadFuncName: String;
|
|
|
-begin
|
|
|
- Result:=true;
|
|
|
- if Attr.AttributeType=nil then
|
|
|
- exit;
|
|
|
- FuncName:=GetterPrefix+GetName(Attr);
|
|
|
- TypeName:=GetTypeName(Attr.AttributeType);
|
|
|
- AddLn('Function '+FuncName+': '+TypeName+';');
|
|
|
-
|
|
|
- aClassName:=GetName(Attr.Parent);
|
|
|
-
|
|
|
- case TypeName of
|
|
|
- 'Boolean': ReadFuncName:='ReadJSPropertyBoolean';
|
|
|
- 'ShortInt',
|
|
|
- 'Byte',
|
|
|
- 'SmallInt',
|
|
|
- 'Word',
|
|
|
- 'Integer': ReadFuncName:='ReadJSPropertyLongInt';
|
|
|
- 'LongWord',
|
|
|
- 'Int64',
|
|
|
- 'QWord': ReadFuncName:='ReadJSPropertyInt64';
|
|
|
- 'Single',
|
|
|
- 'Double': ReadFuncName:='ReadJSPropertyDouble';
|
|
|
- 'UnicodeString': ReadFuncName:='ReadJSPropertyUnicodeString';
|
|
|
- 'TJOB_JSValue': ReadFuncName:='ReadJSPropertyValue';
|
|
|
- else
|
|
|
- raise EConvertError.Create('not yet implemented: Getter '+Typename);
|
|
|
- end;
|
|
|
-
|
|
|
- Code:='Function '+aClassName+'.'+FuncName+': '+TypeName+';'+sLineBreak;
|
|
|
- Code:=Code+'begin'+sLineBreak;
|
|
|
- Code:=Code+' Result:='+ReadFuncName+'('''+Attr.Name+''');'+sLineBreak;
|
|
|
- Code:=Code+'end;'+sLineBreak;
|
|
|
-
|
|
|
- FIncludeImplementationCode.Add(Code);
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPasWasmJob.WritePrivateSetter(Attr: TIDLAttributeDefinition
|
|
|
- ): boolean;
|
|
|
-var
|
|
|
- FuncName, TypeName, aClassName, WriteFuncName, Code: String;
|
|
|
-begin
|
|
|
- if aoReadOnly in Attr.Options then
|
|
|
- exit(false);
|
|
|
- if Attr.AttributeType=nil then
|
|
|
- exit;
|
|
|
-
|
|
|
- Result:=true;
|
|
|
- FuncName:=SetterPrefix+GetName(Attr);
|
|
|
- TypeName:=GetTypeName(Attr.AttributeType);
|
|
|
- AddLn('Procedure '+FuncName+'(const aValue: '+TypeName+');');
|
|
|
-
|
|
|
- aClassName:=GetName(Attr.Parent);
|
|
|
-
|
|
|
- case TypeName of
|
|
|
- 'Boolean': WriteFuncName:='WriteJSPropertyBoolean';
|
|
|
- 'ShortInt',
|
|
|
- 'Byte',
|
|
|
- 'SmallInt',
|
|
|
- 'Word',
|
|
|
- 'Integer': WriteFuncName:='WriteJSPropertyLongInt';
|
|
|
- 'LongWord',
|
|
|
- 'Int64',
|
|
|
- 'QWord': WriteFuncName:='WriteJSPropertyDouble';
|
|
|
- 'Single',
|
|
|
- 'Double': WriteFuncName:='WriteJSPropertyDouble';
|
|
|
- 'UnicodeString': WriteFuncName:='WriteJSPropertyUnicodeString';
|
|
|
- 'TJOB_JSValue': WriteFuncName:='WriteJSPropertyValue';
|
|
|
- else
|
|
|
- raise EConvertError.Create('not yet implemented: Setter '+Typename);
|
|
|
- end;
|
|
|
-
|
|
|
- Code:='Procedure '+aClassName+'.'+FuncName+'(const aValue: '+TypeName+');'+sLineBreak;
|
|
|
- Code:=Code+'begin'+sLineBreak;
|
|
|
- Code:=Code+' '+WriteFuncName+'('''+Attr.Name+''',aValue);'+sLineBreak;
|
|
|
- Code:=Code+'end;'+sLineBreak;
|
|
|
-
|
|
|
- FIncludeImplementationCode.Add(Code);
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPasWasmJob.WriteProperty(Attr: TIDLAttributeDefinition
|
|
|
- ): boolean;
|
|
|
-var
|
|
|
- PropName, TypeName, Code: String;
|
|
|
-begin
|
|
|
- if Attr.AttributeType=nil then
|
|
|
- begin
|
|
|
- AddLn('skipping field without type: "'+Attr.Name+'"');
|
|
|
- exit;
|
|
|
- end;
|
|
|
- PropName:=GetName(Attr);
|
|
|
- TypeName:=GetTypeName(Attr.AttributeType);
|
|
|
- Code:='Property '+PropName+': '+TypeName+' read '+GetterPrefix+PropName;
|
|
|
- if not (aoReadOnly in Attr.Options) then
|
|
|
- Code:=Code+' write '+SetterPrefix+PropName;
|
|
|
- AddLn(Code+';');
|
|
|
- Result:=true;
|
|
|
-end;
|
|
|
-
|
|
|
-constructor TWebIDLToPasWasmJob.Create(ThOwner: TComponent);
|
|
|
-begin
|
|
|
- inherited Create(ThOwner);
|
|
|
-end;
|
|
|
-
|
|
|
-{ TWebIDLToPas2js }
|
|
|
-
|
|
|
-function TWebIDLToPas2js.BaseUnits: String;
|
|
|
-begin
|
|
|
- Result:='SysUtils, JS';
|
|
|
-end;
|
|
|
-
|
|
|
-procedure TWebIDLToPas2js.GetOptions(L: TStrings; Full: boolean);
|
|
|
-begin
|
|
|
- inherited GetOptions(L, Full);
|
|
|
- L.Add('Extended Options: '+Pas2jsConversionOptionsToStr(Pas2jsOptions));
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPas2js.GetTypeName(const aTypeName: String;
|
|
|
- ForTypeDef: Boolean): String;
|
|
|
-
|
|
|
- Function UsePascalType(Const aPascalType: string): String;
|
|
|
-
|
|
|
- begin
|
|
|
- if (p2jcoUseNativeTypeAliases in Pas2jsOptions) and ForTypeDef then
|
|
|
- Result:=StringReplace(aTypeName,' ','',[rfReplaceAll])
|
|
|
- else
|
|
|
- Result:=aPascalType;
|
|
|
- end;
|
|
|
-
|
|
|
-begin
|
|
|
- Case aTypeName of
|
|
|
- 'union': Result:='JSValue';
|
|
|
- 'short': Result:=UsePascalType('Integer');
|
|
|
- 'long': Result:=UsePascalType('Integer');
|
|
|
- 'long long': Result:=UsePascalType('NativeInt');
|
|
|
- 'unsigned short': Result:=UsePascalType('Cardinal');
|
|
|
- 'unrestricted float': Result:=UsePascalType('Double');
|
|
|
- 'unrestricted double': Result:=UsePascalType('Double');
|
|
|
- 'unsigned long': Result:=UsePascalType('NativeInt');
|
|
|
- 'unsigned long long': Result:=UsePascalType('NativeInt');
|
|
|
- 'octet': Result:=UsePascalType('Byte');
|
|
|
- 'any': Result:=UsePascalType('JSValue');
|
|
|
- 'float': Result:=UsePascalType('Double');
|
|
|
- 'double': Result:=UsePascalType('Double');
|
|
|
- 'DOMString',
|
|
|
- 'USVString',
|
|
|
- 'ByteString': Result:=UsePascalType('String');
|
|
|
- else
|
|
|
- Result:=inherited GetTypeName(aTypeName,ForTypeDef);
|
|
|
- end;
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPas2js.GetInterfaceDefHead(Intf: TIDLInterfaceDefinition
|
|
|
- ): String;
|
|
|
-begin
|
|
|
- Result:='class external name '+MakePascalString(Intf.Name,True);
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPas2js.WritePrivateReadOnlyFields(aList: TIDLDefinitionList
|
|
|
- ): Integer;
|
|
|
-
|
|
|
-Var
|
|
|
- D: TIDLDefinition;
|
|
|
- A: TIDLAttributeDefinition absolute D;
|
|
|
-
|
|
|
-begin
|
|
|
- Result:=0;
|
|
|
- For D in aList do
|
|
|
- if (D is TIDLAttributeDefinition) then
|
|
|
- if (aoReadOnly in A.Options) then
|
|
|
- if WritePrivateReadOnlyField(A) then
|
|
|
- Inc(Result);
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPas2js.WriteProperties(aList: TIDLDefinitionList): Integer;
|
|
|
-Var
|
|
|
- D: TIDLDefinition;
|
|
|
- A: TIDLAttributeDefinition absolute D;
|
|
|
-begin
|
|
|
- Result:=0;
|
|
|
- For D in aList do
|
|
|
- if (D is TIDLAttributeDefinition) then
|
|
|
- if (aoReadOnly in A.Options) then
|
|
|
- if WriteReadOnlyProperty(A) then
|
|
|
- Inc(Result);
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPas2js.WriteConst(aConst: TIDLConstDefinition): Boolean;
|
|
|
-
|
|
|
-Const
|
|
|
- ConstTypes: Array[TConstType] of String =
|
|
|
- ('Double','NativeInt','Boolean','JSValue','JSValue','JSValue','JSValue','String','JSValue','JSValue');
|
|
|
-Var
|
|
|
- S: String;
|
|
|
-
|
|
|
-begin
|
|
|
- Result:=True;
|
|
|
- // Consts cannot be strings
|
|
|
- if p2jcoExternalConst in Pas2jsOptions then
|
|
|
- begin
|
|
|
- S:=ConstTypes[aConst.ConstType];
|
|
|
- Addln('%s: %s;',[GetName(aConst),S])
|
|
|
- end
|
|
|
- else
|
|
|
- Result:=inherited WriteConst(aConst);
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPas2js.WritePrivateReadOnlyField(
|
|
|
- aAttr: TIDLAttributeDefinition): Boolean;
|
|
|
-begin
|
|
|
- AddLn('%s%s: %s; external name ''%s''; ',[FieldPrefix,GetName(aAttr),GetTypeName(aAttr.AttributeType),aAttr.Name]);
|
|
|
- Result:=true;
|
|
|
-end;
|
|
|
-
|
|
|
-function TWebIDLToPas2js.WriteReadonlyProperty(aAttr: TIDLAttributeDefinition
|
|
|
- ): Boolean;
|
|
|
-
|
|
|
-Var
|
|
|
- TN,N,PN: String;
|
|
|
-
|
|
|
-begin
|
|
|
- Result:=True;
|
|
|
- N:=GetName(aAttr);
|
|
|
- PN:=N;
|
|
|
- TN:=GetTypeName(aAttr.AttributeType);
|
|
|
- if SameText(PN,TN) then
|
|
|
- PN:='_'+PN;
|
|
|
- AddLn('Property %s: %s Read %s%s; ',[PN,TN,FieldPrefix,N]);
|
|
|
-end;
|
|
|
-
|
|
|
-constructor TWebIDLToPas2js.Create(TheOwner: TComponent);
|
|
|
-begin
|
|
|
- inherited Create(TheOwner);
|
|
|
- Switches.Add('modeswitch externalclass');
|
|
|
-end;
|
|
|
-
|
|
|
{ TPasData }
|
|
|
|
|
|
constructor TPasData.Create(APasName: String; const aFile: string; aLine,
|