|
@@ -21,9 +21,20 @@ unit jswriter;
|
|
interface
|
|
interface
|
|
|
|
|
|
uses
|
|
uses
|
|
|
|
+ {$ifdef pas2js}
|
|
|
|
+ JS,
|
|
|
|
+ {$endif}
|
|
SysUtils, jstoken, jsbase, jstree;
|
|
SysUtils, jstoken, jsbase, jstree;
|
|
|
|
|
|
Type
|
|
Type
|
|
|
|
+ {$ifdef pas2js}
|
|
|
|
+ TJSWriterString = UnicodeString;
|
|
|
|
+ TJSWriterChar = WideChar;
|
|
|
|
+ {$else}
|
|
|
|
+ TJSWriterString = AnsiString;
|
|
|
|
+ TJSWriterChar = AnsiChar;
|
|
|
|
+ {$endif}
|
|
|
|
+
|
|
TTextWriter = class;
|
|
TTextWriter = class;
|
|
|
|
|
|
TTextWriterWriting = procedure(Sender: TTextWriter) of object;
|
|
TTextWriterWriting = procedure(Sender: TTextWriter) of object;
|
|
@@ -37,33 +48,38 @@ Type
|
|
FCurColumn: integer;
|
|
FCurColumn: integer;
|
|
FOnWriting: TTextWriterWriting;
|
|
FOnWriting: TTextWriterWriting;
|
|
protected
|
|
protected
|
|
- Function DoWrite(Const S : AnsiString) : Integer; virtual; abstract;
|
|
|
|
|
|
+ Function DoWrite(Const S : TJSWriterString) : Integer; virtual; abstract;
|
|
|
|
+ {$ifdef fpc}
|
|
Function DoWrite(Const S : UnicodeString) : Integer; virtual; abstract;
|
|
Function DoWrite(Const S : UnicodeString) : Integer; virtual; abstract;
|
|
|
|
+ {$endif}
|
|
procedure SetCurElement(const AValue: TJSElement); virtual;
|
|
procedure SetCurElement(const AValue: TJSElement); virtual;
|
|
Procedure Writing; virtual; // called before adding new characters
|
|
Procedure Writing; virtual; // called before adding new characters
|
|
Public
|
|
Public
|
|
// All functions return the number of bytes copied to output stream.
|
|
// All functions return the number of bytes copied to output stream.
|
|
constructor Create;
|
|
constructor Create;
|
|
|
|
+ {$ifdef fpc}
|
|
Function Write(Const S : UnicodeString) : Integer;
|
|
Function Write(Const S : UnicodeString) : Integer;
|
|
- Function Write(Const S : AnsiString) : Integer;
|
|
|
|
- Function WriteLn(Const S : AnsiString) : Integer;
|
|
|
|
- Function Write(Const Fmt : AnsiString; Args : Array of const) : Integer;
|
|
|
|
- Function WriteLn(Const Fmt : AnsiString; Args : Array of const) : Integer;
|
|
|
|
- Function Write(Const Args : Array of const) : Integer;
|
|
|
|
- Function WriteLn(Const Args : Array of const) : Integer;
|
|
|
|
|
|
+ {$endif}
|
|
|
|
+ Function Write(Const S : TJSWriterString) : Integer;
|
|
|
|
+ Function WriteLn(Const S : TJSWriterString) : Integer;
|
|
|
|
+ Function Write(Const Fmt : TJSWriterString; Args : Array of {$ifdef pas2js}jsvalue{$else}const{$endif}) : Integer;
|
|
|
|
+ Function WriteLn(Const Fmt : TJSWriterString; Args : Array of {$ifdef pas2js}jsvalue{$else}const{$endif}) : Integer;
|
|
|
|
+ Function Write(Const Args : Array of {$ifdef pas2js}jsvalue{$else}const{$endif}) : Integer;
|
|
|
|
+ Function WriteLn(Const Args : Array of {$ifdef pas2js}jsvalue{$else}const{$endif}) : Integer;
|
|
Property CurLine: integer read FCurLine write FCurLine;
|
|
Property CurLine: integer read FCurLine write FCurLine;
|
|
Property CurColumn: integer read FCurColumn write FCurColumn;// char index, not codepoint
|
|
Property CurColumn: integer read FCurColumn write FCurColumn;// char index, not codepoint
|
|
Property CurElement: TJSElement read FCurElement write SetCurElement;
|
|
Property CurElement: TJSElement read FCurElement write SetCurElement;
|
|
Property OnWriting: TTextWriterWriting read FOnWriting write FOnWriting;
|
|
Property OnWriting: TTextWriterWriting read FOnWriting write FOnWriting;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+ {$ifdef fpc}
|
|
{ TFileWriter }
|
|
{ TFileWriter }
|
|
|
|
|
|
TFileWriter = Class(TTextWriter)
|
|
TFileWriter = Class(TTextWriter)
|
|
Protected
|
|
Protected
|
|
FFile : Text;
|
|
FFile : Text;
|
|
FFileName : String;
|
|
FFileName : String;
|
|
- Function DoWrite(Const S : AnsiString) : Integer; override;
|
|
|
|
|
|
+ Function DoWrite(Const S : TJSWriterString) : Integer; override;
|
|
Function DoWrite(Const S : UnicodeString) : Integer; override;
|
|
Function DoWrite(Const S : UnicodeString) : Integer; override;
|
|
Public
|
|
Public
|
|
Constructor Create(Const AFileNAme : String);
|
|
Constructor Create(Const AFileNAme : String);
|
|
@@ -72,32 +88,45 @@ Type
|
|
Procedure Close;
|
|
Procedure Close;
|
|
Property FileName : String Read FFileName;
|
|
Property FileName : String Read FFileName;
|
|
end;
|
|
end;
|
|
|
|
+ {$endif}
|
|
|
|
|
|
{ TBufferWriter }
|
|
{ TBufferWriter }
|
|
|
|
|
|
- TBytes = Array of byte;
|
|
|
|
TBufferWriter = Class(TTextWriter)
|
|
TBufferWriter = Class(TTextWriter)
|
|
|
|
+ private type
|
|
|
|
+ TBuffer = Array of {$ifdef fpc}byte{$else}string{$endif};
|
|
private
|
|
private
|
|
FBufPos,
|
|
FBufPos,
|
|
FCapacity: Cardinal;
|
|
FCapacity: Cardinal;
|
|
- FBuffer : TBytes;
|
|
|
|
- function GetAsAnsistring: AnsiString;
|
|
|
|
|
|
+ FBuffer : TBuffer;
|
|
|
|
+ function GetAsString: TJSWriterString;
|
|
|
|
+ {$ifdef fpc}
|
|
function GetBuffer: Pointer;
|
|
function GetBuffer: Pointer;
|
|
|
|
+ {$endif}
|
|
function GetBufferLength: Integer;
|
|
function GetBufferLength: Integer;
|
|
function GetCapacity: Cardinal;
|
|
function GetCapacity: Cardinal;
|
|
|
|
+ {$ifdef fpc}
|
|
function GetUnicodeString: UnicodeString;
|
|
function GetUnicodeString: UnicodeString;
|
|
|
|
+ {$endif}
|
|
procedure SetCapacity(AValue: Cardinal);
|
|
procedure SetCapacity(AValue: Cardinal);
|
|
Protected
|
|
Protected
|
|
- Function DoWrite(Const S : AnsiString) : integer; override;
|
|
|
|
|
|
+ Function DoWrite(Const S : TJSWriterString) : integer; override;
|
|
|
|
+ {$ifdef fpc}
|
|
Function DoWrite(Const S : UnicodeString) : integer; override;
|
|
Function DoWrite(Const S : UnicodeString) : integer; override;
|
|
|
|
+ {$endif}
|
|
Public
|
|
Public
|
|
- Constructor Create(Const ACapacity : Cardinal);
|
|
|
|
|
|
+ Constructor Create(Const ACapacity : Cardinal); reintroduce;
|
|
|
|
+ {$ifdef fpc}
|
|
Procedure SaveToFile(Const AFileName : String);
|
|
Procedure SaveToFile(Const AFileName : String);
|
|
Property Buffer : Pointer Read GetBuffer;
|
|
Property Buffer : Pointer Read GetBuffer;
|
|
|
|
+ {$endif}
|
|
Property BufferLength : Integer Read GetBufferLength;
|
|
Property BufferLength : Integer Read GetBufferLength;
|
|
Property Capacity : Cardinal Read GetCapacity Write SetCapacity;
|
|
Property Capacity : Cardinal Read GetCapacity Write SetCapacity;
|
|
- Property AsAnsistring : AnsiString Read GetAsAnsistring;
|
|
|
|
|
|
+ Property AsString : TJSWriterString Read GetAsString;
|
|
|
|
+ {$ifdef fpc}
|
|
|
|
+ Property AsAnsiString : AnsiString Read GetAsString; deprecated 'use AsString instead, fpc 3.3.1';
|
|
Property AsUnicodeString : UnicodeString Read GetUnicodeString;
|
|
Property AsUnicodeString : UnicodeString Read GetUnicodeString;
|
|
|
|
+ {$endif}
|
|
end;
|
|
end;
|
|
|
|
|
|
TJSEscapeQuote = (
|
|
TJSEscapeQuote = (
|
|
@@ -109,7 +138,9 @@ Type
|
|
{ TJSWriter }
|
|
{ TJSWriter }
|
|
|
|
|
|
TWriteOption = (woCompact,
|
|
TWriteOption = (woCompact,
|
|
|
|
+ {$ifdef fpc}
|
|
woUseUTF8,
|
|
woUseUTF8,
|
|
|
|
+ {$endif}
|
|
woTabIndent,
|
|
woTabIndent,
|
|
woEmptyStatementAsComment,
|
|
woEmptyStatementAsComment,
|
|
woQuoteElementNames,
|
|
woQuoteElementNames,
|
|
@@ -134,13 +165,17 @@ Type
|
|
procedure SetOptions(AValue: TWriteOptions);
|
|
procedure SetOptions(AValue: TWriteOptions);
|
|
Protected
|
|
Protected
|
|
// Helper routines
|
|
// Helper routines
|
|
- Procedure Error(Const Msg : String);
|
|
|
|
- Procedure Error(Const Fmt : String; Args : Array of const);
|
|
|
|
|
|
+ Procedure Error(Const Msg : TJSWriterString);
|
|
|
|
+ Procedure Error(Const Fmt : TJSWriterString; Args : Array of {$ifdef pas2js}jsvalue{$else}const{$endif});
|
|
Procedure WriteIndent; // inline;
|
|
Procedure WriteIndent; // inline;
|
|
|
|
+ {$ifdef fpc}
|
|
Procedure Write(Const U : UnicodeString);
|
|
Procedure Write(Const U : UnicodeString);
|
|
- Procedure Write(Const S : AnsiString);
|
|
|
|
- Procedure WriteLn(Const S : AnsiString);
|
|
|
|
|
|
+ {$endif}
|
|
|
|
+ Procedure Write(Const S : TJSWriterString);
|
|
|
|
+ Procedure WriteLn(Const S : TJSWriterString);
|
|
|
|
+ {$ifdef fpc}
|
|
Procedure WriteLn(Const U : UnicodeString);
|
|
Procedure WriteLn(Const U : UnicodeString);
|
|
|
|
+ {$endif}
|
|
// one per type of statement
|
|
// one per type of statement
|
|
Procedure WriteValue(V : TJSValue); virtual;
|
|
Procedure WriteValue(V : TJSValue); virtual;
|
|
Procedure WriteRegularExpressionLiteral(El: TJSRegularExpressionLiteral);
|
|
Procedure WriteRegularExpressionLiteral(El: TJSRegularExpressionLiteral);
|
|
@@ -179,7 +214,9 @@ Type
|
|
Public
|
|
Public
|
|
Function EscapeString(const S: TJSString; Quote: TJSEscapeQuote = jseqDouble): TJSString;
|
|
Function EscapeString(const S: TJSString; Quote: TJSEscapeQuote = jseqDouble): TJSString;
|
|
Constructor Create(AWriter : TTextWriter);
|
|
Constructor Create(AWriter : TTextWriter);
|
|
|
|
+ {$ifdef fpc}
|
|
Constructor Create(Const AFileName : String);
|
|
Constructor Create(Const AFileName : String);
|
|
|
|
+ {$endif}
|
|
Destructor Destroy; override;
|
|
Destructor Destroy; override;
|
|
Procedure WriteJS(El : TJSElement);
|
|
Procedure WriteJS(El : TJSElement);
|
|
Procedure Indent;
|
|
Procedure Indent;
|
|
@@ -192,7 +229,9 @@ Type
|
|
end;
|
|
end;
|
|
EJSWriter = Class(Exception);
|
|
EJSWriter = Class(Exception);
|
|
|
|
|
|
|
|
+{$ifdef fpc}
|
|
Function UTF16ToUTF8(const S: UnicodeString): string;
|
|
Function UTF16ToUTF8(const S: UnicodeString): string;
|
|
|
|
+{$endif}
|
|
|
|
|
|
implementation
|
|
implementation
|
|
|
|
|
|
@@ -200,6 +239,7 @@ Resourcestring
|
|
SErrUnknownJSClass = 'Unknown javascript element class : %s';
|
|
SErrUnknownJSClass = 'Unknown javascript element class : %s';
|
|
SErrNilNode = 'Nil node in Javascript';
|
|
SErrNilNode = 'Nil node in Javascript';
|
|
|
|
|
|
|
|
+{$ifdef fpc}
|
|
function HexDump(p: PChar; Count: integer): string;
|
|
function HexDump(p: PChar; Count: integer): string;
|
|
var
|
|
var
|
|
i: Integer;
|
|
i: Integer;
|
|
@@ -216,6 +256,7 @@ begin
|
|
// conversion magic
|
|
// conversion magic
|
|
SetCodePage(RawByteString(Result), CP_ACP, False);
|
|
SetCodePage(RawByteString(Result), CP_ACP, False);
|
|
end;
|
|
end;
|
|
|
|
+{$endif}
|
|
|
|
|
|
{ TBufferWriter }
|
|
{ TBufferWriter }
|
|
|
|
|
|
@@ -224,24 +265,33 @@ begin
|
|
Result:=FBufPos;
|
|
Result:=FBufPos;
|
|
end;
|
|
end;
|
|
|
|
|
|
-function TBufferWriter.GetAsAnsistring: AnsiString;
|
|
|
|
|
|
+function TBufferWriter.GetAsString: TJSWriterString;
|
|
begin
|
|
begin
|
|
|
|
+ {$ifdef pas2js}
|
|
|
|
+ if FBufPos<length(FBuffer) then
|
|
|
|
+ TJSArray(FBuffer).Length:=FBufPos;
|
|
|
|
+ Result:=TJSArray(FBuffer).join('');
|
|
|
|
+ {$else}
|
|
Result:='';
|
|
Result:='';
|
|
SetLength(Result,BufferLength);
|
|
SetLength(Result,BufferLength);
|
|
if (BufferLength>0) then
|
|
if (BufferLength>0) then
|
|
Move(FBuffer[0],Result[1],BufferLength);
|
|
Move(FBuffer[0],Result[1],BufferLength);
|
|
|
|
+ {$endif}
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{$ifdef fpc}
|
|
function TBufferWriter.GetBuffer: Pointer;
|
|
function TBufferWriter.GetBuffer: Pointer;
|
|
begin
|
|
begin
|
|
Result:=Pointer(FBuffer);
|
|
Result:=Pointer(FBuffer);
|
|
end;
|
|
end;
|
|
|
|
+{$endif}
|
|
|
|
|
|
function TBufferWriter.GetCapacity: Cardinal;
|
|
function TBufferWriter.GetCapacity: Cardinal;
|
|
begin
|
|
begin
|
|
Result:=Length(FBuffer);
|
|
Result:=Length(FBuffer);
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{$ifdef fpc}
|
|
function TBufferWriter.GetUnicodeString: UnicodeString;
|
|
function TBufferWriter.GetUnicodeString: UnicodeString;
|
|
|
|
|
|
Var
|
|
Var
|
|
@@ -254,6 +304,7 @@ begin
|
|
if (SL>0) then
|
|
if (SL>0) then
|
|
Move(FBuffer[0],Result[1],SL*SizeOf(UnicodeChar));
|
|
Move(FBuffer[0],Result[1],SL*SizeOf(UnicodeChar));
|
|
end;
|
|
end;
|
|
|
|
+{$endif}
|
|
|
|
|
|
procedure TBufferWriter.SetCapacity(AValue: Cardinal);
|
|
procedure TBufferWriter.SetCapacity(AValue: Cardinal);
|
|
begin
|
|
begin
|
|
@@ -263,13 +314,21 @@ begin
|
|
FBufPos:=Capacity;
|
|
FBufPos:=Capacity;
|
|
end;
|
|
end;
|
|
|
|
|
|
-Function TBufferWriter.DoWrite(Const S: AnsiString): integer;
|
|
|
|
-
|
|
|
|
|
|
+Function TBufferWriter.DoWrite(Const S: TJSWriterString): integer;
|
|
|
|
+{$ifdef pas2js}
|
|
|
|
+begin
|
|
|
|
+ Result:=Length(S)*2;
|
|
|
|
+ if Result=0 then exit;
|
|
|
|
+ TJSArray(FBuffer).push(S);
|
|
|
|
+ inc(FBufPos);
|
|
|
|
+ FCapacity:=FBufPos;
|
|
|
|
+end;
|
|
|
|
+{$else}
|
|
Var
|
|
Var
|
|
DesLen,MinLen : Integer;
|
|
DesLen,MinLen : Integer;
|
|
|
|
|
|
begin
|
|
begin
|
|
- Result:=Length(S)*SizeOf(Char);
|
|
|
|
|
|
+ Result:=Length(S)*SizeOf(TJSWriterChar);
|
|
if Result=0 then exit;
|
|
if Result=0 then exit;
|
|
MinLen:=Result+FBufPos;
|
|
MinLen:=Result+FBufPos;
|
|
If (MinLen>Capacity) then
|
|
If (MinLen>Capacity) then
|
|
@@ -282,7 +341,9 @@ begin
|
|
Move(S[1],FBuffer[FBufPos],Result);
|
|
Move(S[1],FBuffer[FBufPos],Result);
|
|
FBufPos:=FBufPos+Result;
|
|
FBufPos:=FBufPos+Result;
|
|
end;
|
|
end;
|
|
|
|
+{$endif}
|
|
|
|
|
|
|
|
+{$ifdef fpc}
|
|
Function TBufferWriter.DoWrite(Const S: UnicodeString): integer;
|
|
Function TBufferWriter.DoWrite(Const S: UnicodeString): integer;
|
|
|
|
|
|
Var
|
|
Var
|
|
@@ -302,6 +363,7 @@ begin
|
|
Move(S[1],FBuffer[FBufPos],Result);
|
|
Move(S[1],FBuffer[FBufPos],Result);
|
|
FBufPos:=FBufPos+Result;
|
|
FBufPos:=FBufPos+Result;
|
|
end;
|
|
end;
|
|
|
|
+{$endif}
|
|
|
|
|
|
Constructor TBufferWriter.Create(Const ACapacity: Cardinal);
|
|
Constructor TBufferWriter.Create(Const ACapacity: Cardinal);
|
|
begin
|
|
begin
|
|
@@ -309,8 +371,8 @@ begin
|
|
Capacity:=ACapacity;
|
|
Capacity:=ACapacity;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{$ifdef fpc}
|
|
Procedure TBufferWriter.SaveToFile(Const AFileName: String);
|
|
Procedure TBufferWriter.SaveToFile(Const AFileName: String);
|
|
-
|
|
|
|
Var
|
|
Var
|
|
F : File;
|
|
F : File;
|
|
|
|
|
|
@@ -323,6 +385,7 @@ begin
|
|
Close(F);
|
|
Close(F);
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
+{$endif}
|
|
|
|
|
|
{ TJSWriter }
|
|
{ TJSWriter }
|
|
|
|
|
|
@@ -330,7 +393,7 @@ procedure TJSWriter.SetOptions(AValue: TWriteOptions);
|
|
begin
|
|
begin
|
|
if FOptions=AValue then Exit;
|
|
if FOptions=AValue then Exit;
|
|
FOptions:=AValue;
|
|
FOptions:=AValue;
|
|
- If woTabIndent in Foptions then
|
|
|
|
|
|
+ If woTabIndent in FOptions then
|
|
FIndentChar:=#9
|
|
FIndentChar:=#9
|
|
else
|
|
else
|
|
FIndentChar:=' ';
|
|
FIndentChar:=' ';
|
|
@@ -338,15 +401,15 @@ end;
|
|
|
|
|
|
function TJSWriter.GetUseUTF8: Boolean;
|
|
function TJSWriter.GetUseUTF8: Boolean;
|
|
begin
|
|
begin
|
|
- Result:=(woUseUTF8 in Options)
|
|
|
|
|
|
+ Result:={$ifdef pas2js}false{$else}(woUseUTF8 in Options){$endif};
|
|
end;
|
|
end;
|
|
|
|
|
|
-procedure TJSWriter.Error(const Msg: String);
|
|
|
|
|
|
+procedure TJSWriter.Error(const Msg: TJSWriterString);
|
|
begin
|
|
begin
|
|
Raise EJSWriter.Create(Msg);
|
|
Raise EJSWriter.Create(Msg);
|
|
end;
|
|
end;
|
|
|
|
|
|
-procedure TJSWriter.Error(const Fmt: String; Args: array of const);
|
|
|
|
|
|
+procedure TJSWriter.Error(const Fmt: TJSWriterString; Args: array of {$ifdef pas2js}jsvalue{$else}const{$endif});
|
|
begin
|
|
begin
|
|
Raise EJSWriter.CreateFmt(Fmt,Args);
|
|
Raise EJSWriter.CreateFmt(Fmt,Args);
|
|
end;
|
|
end;
|
|
@@ -374,6 +437,7 @@ begin
|
|
FCurIndent:=0;
|
|
FCurIndent:=0;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{$ifdef fpc}
|
|
procedure TJSWriter.Write(const U: UnicodeString);
|
|
procedure TJSWriter.Write(const U: UnicodeString);
|
|
|
|
|
|
Var
|
|
Var
|
|
@@ -394,12 +458,15 @@ begin
|
|
FLastChar:=U[length(U)];
|
|
FLastChar:=U[length(U)];
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
+{$endif}
|
|
|
|
|
|
-procedure TJSWriter.Write(const S: AnsiString);
|
|
|
|
|
|
+procedure TJSWriter.Write(const S: TJSWriterString);
|
|
begin
|
|
begin
|
|
|
|
+ {$ifdef fpc}
|
|
if Not (woUseUTF8 in Options) then
|
|
if Not (woUseUTF8 in Options) then
|
|
Write(UnicodeString(S))
|
|
Write(UnicodeString(S))
|
|
else
|
|
else
|
|
|
|
+ {$endif}
|
|
begin
|
|
begin
|
|
WriteIndent;
|
|
WriteIndent;
|
|
if s='' then exit;
|
|
if s='' then exit;
|
|
@@ -408,11 +475,13 @@ begin
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
-procedure TJSWriter.WriteLn(const S: AnsiString);
|
|
|
|
|
|
+procedure TJSWriter.WriteLn(const S: TJSWriterString);
|
|
begin
|
|
begin
|
|
|
|
+ {$ifdef fpc}
|
|
if Not (woUseUTF8 in Options) then
|
|
if Not (woUseUTF8 in Options) then
|
|
Writeln(UnicodeString(S))
|
|
Writeln(UnicodeString(S))
|
|
else
|
|
else
|
|
|
|
+ {$endif}
|
|
begin
|
|
begin
|
|
WriteIndent;
|
|
WriteIndent;
|
|
Writer.WriteLn(S);
|
|
Writer.WriteLn(S);
|
|
@@ -421,6 +490,7 @@ begin
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{$ifdef fpc}
|
|
procedure TJSWriter.WriteLn(const U: UnicodeString);
|
|
procedure TJSWriter.WriteLn(const U: UnicodeString);
|
|
Var
|
|
Var
|
|
S : String;
|
|
S : String;
|
|
@@ -440,6 +510,7 @@ begin
|
|
FLinePos:=0;
|
|
FLinePos:=0;
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
+{$endif}
|
|
|
|
|
|
function TJSWriter.EscapeString(const S: TJSString; Quote: TJSEscapeQuote
|
|
function TJSWriter.EscapeString(const S: TJSString; Quote: TJSEscapeQuote
|
|
): TJSString;
|
|
): TJSString;
|
|
@@ -728,11 +799,13 @@ begin
|
|
FOptions:=[woUseUTF8];
|
|
FOptions:=[woUseUTF8];
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{$ifdef fpc}
|
|
constructor TJSWriter.Create(const AFileName: String);
|
|
constructor TJSWriter.Create(const AFileName: String);
|
|
begin
|
|
begin
|
|
Create(TFileWriter.Create(AFileName));
|
|
Create(TFileWriter.Create(AFileName));
|
|
FFreeWriter:=True;
|
|
FFreeWriter:=True;
|
|
end;
|
|
end;
|
|
|
|
+{$endif}
|
|
|
|
|
|
destructor TJSWriter.Destroy;
|
|
destructor TJSWriter.Destroy;
|
|
begin
|
|
begin
|
|
@@ -1695,9 +1768,10 @@ begin
|
|
FSkipCurlyBrackets:=False;
|
|
FSkipCurlyBrackets:=False;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{$ifdef fpc}
|
|
{ TFileWriter }
|
|
{ TFileWriter }
|
|
|
|
|
|
-Function TFileWriter.DoWrite(Const S: AnsiString) : Integer;
|
|
|
|
|
|
+Function TFileWriter.DoWrite(Const S: TJSWriterString) : Integer;
|
|
begin
|
|
begin
|
|
Result:=Length(S);
|
|
Result:=Length(S);
|
|
system.Write(FFile,S);
|
|
system.Write(FFile,S);
|
|
@@ -1732,6 +1806,7 @@ Procedure TFileWriter.Close;
|
|
begin
|
|
begin
|
|
system.Close(FFile);
|
|
system.Close(FFile);
|
|
end;
|
|
end;
|
|
|
|
+{$endif}
|
|
|
|
|
|
{ TTextWriter }
|
|
{ TTextWriter }
|
|
|
|
|
|
@@ -1785,7 +1860,7 @@ begin
|
|
until false;
|
|
until false;
|
|
end;
|
|
end;
|
|
|
|
|
|
-function TTextWriter.Write(const S: AnsiString): Integer;
|
|
|
|
|
|
+function TTextWriter.Write(const S: TJSWriterString): Integer;
|
|
var
|
|
var
|
|
p: PChar;
|
|
p: PChar;
|
|
c: Char;
|
|
c: Char;
|
|
@@ -1818,32 +1893,36 @@ begin
|
|
until false;
|
|
until false;
|
|
end;
|
|
end;
|
|
|
|
|
|
-function TTextWriter.WriteLn(const S: AnsiString): Integer;
|
|
|
|
|
|
+function TTextWriter.WriteLn(const S: TJSWriterString): Integer;
|
|
begin
|
|
begin
|
|
Result:=Write(S)+Write(sLineBreak);
|
|
Result:=Write(S)+Write(sLineBreak);
|
|
end;
|
|
end;
|
|
|
|
|
|
-function TTextWriter.Write(const Fmt: AnsiString;
|
|
|
|
- Args: array of const): Integer;
|
|
|
|
|
|
+function TTextWriter.Write(const Fmt: TJSWriterString;
|
|
|
|
+ Args: array of {$ifdef pas2js}jsvalue{$else}const{$endif}): Integer;
|
|
|
|
|
|
begin
|
|
begin
|
|
Result:=Write(Format(Fmt,Args));
|
|
Result:=Write(Format(Fmt,Args));
|
|
end;
|
|
end;
|
|
|
|
|
|
-function TTextWriter.WriteLn(const Fmt: AnsiString;
|
|
|
|
- Args: array of const): Integer;
|
|
|
|
|
|
+function TTextWriter.WriteLn(const Fmt: TJSWriterString;
|
|
|
|
+ Args: array of {$ifdef pas2js}jsvalue{$else}const{$endif}): Integer;
|
|
begin
|
|
begin
|
|
Result:=WriteLn(Format(Fmt,Args));
|
|
Result:=WriteLn(Format(Fmt,Args));
|
|
end;
|
|
end;
|
|
|
|
|
|
-function TTextWriter.Write(const Args: array of const): Integer;
|
|
|
|
|
|
+function TTextWriter.Write(const Args: array of {$ifdef pas2js}jsvalue{$else}const{$endif}): Integer;
|
|
|
|
|
|
Var
|
|
Var
|
|
I : Integer;
|
|
I : Integer;
|
|
|
|
+ {$ifdef pas2js}
|
|
|
|
+ V: jsvalue;
|
|
|
|
+ S: TJSWriterString;
|
|
|
|
+ {$else}
|
|
V : TVarRec;
|
|
V : TVarRec;
|
|
S : String;
|
|
S : String;
|
|
U : UnicodeString;
|
|
U : UnicodeString;
|
|
-
|
|
|
|
|
|
+ {$endif}
|
|
|
|
|
|
begin
|
|
begin
|
|
Result:=0;
|
|
Result:=0;
|
|
@@ -1851,6 +1930,21 @@ begin
|
|
begin
|
|
begin
|
|
V:=Args[i];
|
|
V:=Args[i];
|
|
S:='';
|
|
S:='';
|
|
|
|
+ {$ifdef pas2js}
|
|
|
|
+ case jsTypeOf(V) of
|
|
|
|
+ 'boolean':
|
|
|
|
+ S:=if V then S:='true' else S:='false';
|
|
|
|
+ 'number':
|
|
|
|
+ if isInteger(V) then
|
|
|
|
+ S:=str(NativeInt(V))
|
|
|
|
+ else
|
|
|
|
+ S:=str(Double(V));
|
|
|
|
+ 'string':
|
|
|
|
+ S:=String(V);
|
|
|
|
+ else continue;
|
|
|
|
+ end;
|
|
|
|
+ Result:=Result+Write(S);
|
|
|
|
+ {$else}
|
|
U:='';
|
|
U:='';
|
|
case V.VType of
|
|
case V.VType of
|
|
vtInteger : Str(V.VInteger,S);
|
|
vtInteger : Str(V.VInteger,S);
|
|
@@ -1873,10 +1967,11 @@ begin
|
|
Result:=Result+Write(u)
|
|
Result:=Result+Write(u)
|
|
else if (S<>'') then
|
|
else if (S<>'') then
|
|
Result:=Result+Write(s);
|
|
Result:=Result+Write(s);
|
|
|
|
+ {$endif}
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
-function TTextWriter.WriteLn(const Args: array of const): Integer;
|
|
|
|
|
|
+function TTextWriter.WriteLn(const Args: array of {$ifdef pas2js}jsvalue{$else}const{$endif}): Integer;
|
|
begin
|
|
begin
|
|
Result:=Write(Args)+Writeln('');
|
|
Result:=Write(Args)+Writeln('');
|
|
end;
|
|
end;
|