Browse Source

pastojs: test #256+

mattias 4 years ago
parent
commit
8aed6ac7e4

+ 3 - 2
compiler/packages/fcl-passrc/src/pasresolveeval.pas

@@ -923,6 +923,7 @@ end;
 
 function UnicodeStrToCaption(const u: UnicodeString; MaxLength: integer
   ): Unicodestring;
+// encode a string as a Pascal string literal using '' and #
 var
   InLit: boolean;
   Len: integer;
@@ -4158,7 +4159,7 @@ function TResExprEvaluator.EvalPrimitiveExprString(Expr: TPrimitiveExpr
   var
     h: RawByteString;
   begin
-    if ((u>255) or (ForceUTF16)) and (Result.Kind=revkString) then
+    if ((u>255) or ForceUTF16) and (Result.Kind=revkString) then
       begin
       // switch to unicodestring
       h:=TResEvalString(Result).S;
@@ -4285,7 +4286,7 @@ begin
           end;
         if p=StartP then
           RaiseInternalError(20170523123806);
-        AddHash(u,false);
+        AddHash(u,(S[StartP]='0') and (u>0));
         end;
       end;
     '^':

+ 6 - 4
compiler/packages/pastojs/src/fppas2js.pp

@@ -6513,9 +6513,10 @@ function TPas2JSResolver.ExtractPasStringLiteral(El: TPasElement;
   S is a Pascal string literal e.g. 'Line'#10
     ''  empty string
     '''' => "'"
-    #decimal
-    #$hex
+    #decimal   #0..255 is UTF-8 byte, #01..0255 is UTF-16, #256+ is UTF-16
+    #$hex      #$0..$ff is UTF-8 byte, #$01..$0FF is UTF-16, #$100+ is UTF-16
     ^l  l is a letter a-z
+    Invalid UTF-8 sequences give an error
 }
 var
   p, StartP, i, l: integer;
@@ -6543,7 +6544,7 @@ begin
         '''':
           begin
           if p>StartP then
-            Result:=Result+StrToJSString(copy(S,StartP,p-StartP));
+            Result:=Result+StrToJSString(copy(S,StartP,p-StartP)); // todo error on invalid UTF-8 sequence
           inc(p);
           StartP:=p;
           if (p>l) or (S[p]<>'''') then
@@ -6557,10 +6558,11 @@ begin
         end;
       until false;
       if p>StartP then
-        Result:=Result+StrToJSString(copy(S,StartP,p-StartP));
+        Result:=Result+StrToJSString(copy(S,StartP,p-StartP)); // todo error on invalid UTF-8 sequence
       end;
     '#':
       begin
+      // byte or word sequence
       inc(p);
       if p>l then
         RaiseInternalError(20170207155121);

+ 6 - 1
compiler/packages/pastojs/tests/tcmodules.pas

@@ -7467,6 +7467,7 @@ begin
   '  c:=#$DFFF;', // invalid UTF-16
   '  c:=#$FFFF;', // last UCS-2
   '  c:=high(c);', // last UCS-2
+  '  c:=#269;',
   '']);
   ConvertProgram;
   CheckSource('TestCharConst',
@@ -7497,6 +7498,7 @@ begin
     '$mod.c="\uDFFF";',
     '$mod.c="\uFFFF";',
     '$mod.c="\uFFFF";',
+    '$mod.c = "č";',
     '']));
 end;
 
@@ -7607,9 +7609,11 @@ begin
   '  s:=''"''''"'';',
   '  s:=#$20AC;', // euro
   '  s:=#$10437;', // outside BMP
+  //'  s:=#$F0#$90#$90#$B7;', // as UTF-8
   '  s:=default(string);',
   '  s:=concat(s);',
-  '  s:=concat(s,''a'',s)',
+  '  s:=concat(s,''a'',s);',
+  //'  s:=#0250#269;',
   '']);
   ConvertProgram;
   CheckSource('TestStringConst',
@@ -7631,6 +7635,7 @@ begin
     '$mod.s=''"\''"'';',
     '$mod.s="€";',
     '$mod.s="'#$F0#$90#$90#$B7'";',
+    //'$mod.s="'#$F0#$90#$90#$B7'";',
     '$mod.s="";',
     '$mod.s = $mod.s;',
     '$mod.s = $mod.s.concat("a", $mod.s);',