Quellcode durchsuchen

pastojs: nodejs file functions

git-svn-id: trunk@40105 -
Mattias Gaertner vor 6 Jahren
Ursprung
Commit
ba64c44486

+ 13 - 1
packages/pastojs/src/fppas2js.pp

@@ -2238,12 +2238,24 @@ function TPas2JSResolver.GetOverloadAt(const aName: String; Index: integer
   ): TPasIdentifier;
   ): TPasIdentifier;
 var
 var
   i: Integer;
   i: Integer;
+  Scope: TPasIdentifierScope;
 begin
 begin
   Result:=nil;
   Result:=nil;
   for i:=FOverloadScopes.Count-1 downto 0 do
   for i:=FOverloadScopes.Count-1 downto 0 do
     begin
     begin
     // find last added
     // find last added
-    Result:=TPasIdentifierScope(FOverloadScopes[i]).FindLocalIdentifier(aName);
+    Scope:=TPasIdentifierScope(FOverloadScopes[i]);
+    if (Scope.ClassType=TPas2JSSectionScope) and (i<FOverloadScopes.Count-1) then
+      begin
+      // Note: the elevated locals are after the section scope and before the next deeper scope
+
+      // check elevated locals
+      Result:=TPas2JSSectionScope(Scope).FindElevatedLocal(aName);
+      Result:=GetOverloadAt(Result,Index);
+      if Result<>nil then
+        exit;
+      end;
+    Result:=Scope.FindLocalIdentifier(aName);
     Result:=GetOverloadAt(Result,Index);
     Result:=GetOverloadAt(Result,Index);
     if Result<>nil then
     if Result<>nil then
       exit;
       exit;

+ 12 - 15
packages/pastojs/src/pas2jsfilecache.pp

@@ -27,7 +27,7 @@ interface
 uses
 uses
   {$IFDEF Pas2js}
   {$IFDEF Pas2js}
     {$IFDEF NodeJS}
     {$IFDEF NodeJS}
-    NodeJSFS,
+    JS, NodeJSFS,
     {$ENDIF}
     {$ENDIF}
   {$ENDIF}
   {$ENDIF}
   Classes, SysUtils,
   Classes, SysUtils,
@@ -602,18 +602,12 @@ begin
 end;
 end;
 
 
 procedure TPas2jsCachedDirectory.DoReadDir;
 procedure TPas2jsCachedDirectory.DoReadDir;
-{$IFDEF Pas2js}
-{$ELSE}
 var
 var
   Info: TUnicodeSearchRec;
   Info: TUnicodeSearchRec;
-{$ENDIF}
 begin
 begin
   if Assigned(Pool.OnReadDirectory) then
   if Assigned(Pool.OnReadDirectory) then
     if Pool.OnReadDirectory(Self) then exit;
     if Pool.OnReadDirectory(Self) then exit;
 
 
-  {$IFDEF Pas2js}
-  raise Exception.Create('TPas2jsCachedDirectory.DoReadDir TODO');
-  {$ELSE}
   // Note: do not add a 'if not DirectoryExists then exit'.
   // Note: do not add a 'if not DirectoryExists then exit'.
   // This will not work on automounted directories. You must use FindFirst.
   // This will not work on automounted directories. You must use FindFirst.
   if FindFirst(UnicodeString(Path+AllFilesMask),faAnyFile,Info)=0 then
   if FindFirst(UnicodeString(Path+AllFilesMask),faAnyFile,Info)=0 then
@@ -628,7 +622,6 @@ begin
     until FindNext(Info)<>0;
     until FindNext(Info)<>0;
   end;
   end;
   FindClose(Info);
   FindClose(Info);
-  {$ENDIF}
 end;
 end;
 
 
 constructor TPas2jsCachedDirectory.Create(aPath: string;
 constructor TPas2jsCachedDirectory.Create(aPath: string;
@@ -909,12 +902,7 @@ begin
   writeln('TPas2jsCachedDirectory.WriteDebugReport Count=',Count,' Path="',Path,'"');
   writeln('TPas2jsCachedDirectory.WriteDebugReport Count=',Count,' Path="',Path,'"');
   for i:=0 to Count-1 do begin
   for i:=0 to Count-1 do begin
     Entry:=Entries[i];
     Entry:=Entries[i];
-    {$IFDEF Pas2js}
-    writeln(i,' "',Entry.Name,'" Size=',Entry.Size,' Time=',Entry.Time,' Dir=',faDirectory and Entry.Attr>0);
-    raise Exception.Create('TPas2jsCachedDirectory.WriteDebugReport TODO FileDateToDateTime');
-    {$ELSE}
     writeln(i,' "',Entry.Name,'" Size=',Entry.Size,' Time=',DateTimeToStr(FileDateToDateTime(Entry.Time)),' Dir=',faDirectory and Entry.Attr>0);
     writeln(i,' "',Entry.Name,'" Size=',Entry.Size,' Time=',DateTimeToStr(FileDateToDateTime(Entry.Time)),' Dir=',faDirectory and Entry.Attr>0);
-    {$ENDIF}
   end;
   end;
   {AllowWriteln-}
   {AllowWriteln-}
 end;
 end;
@@ -1806,7 +1794,11 @@ begin
     if Result then
     if Result then
       Exit;
       Exit;
     {$IFDEF Pas2js}
     {$IFDEF Pas2js}
-    raise Exception.Create('TPas2jsFilesCache.ReadFile TODO');
+    try
+      Source:=NJS_FS.readFileSync(Filename,new(['encoding','utf8']));
+    except
+      raise EReadError.Create(String(JSExceptValue));
+    end;
     {$ELSE}
     {$ELSE}
     ms:=TMemoryStream.Create;
     ms:=TMemoryStream.Create;
     try
     try
@@ -2192,7 +2184,12 @@ begin
   end else
   end else
   begin
   begin
     {$IFDEF Pas2js}
     {$IFDEF Pas2js}
-    raise Exception.Create('TPas2jsFilesCache.SaveToFile TODO '+Filename);
+    try
+      s:=ms.join('');
+      NJS_FS.writeFileSync(Filename,s,new(['encoding','utf8']));
+    except
+      raise EWriteError.Create(String(JSExceptValue));
+    end;
     {$ELSE}
     {$ELSE}
     try
     try
       ms.SaveToFile(Filename);
       ms.SaveToFile(Filename);

+ 47 - 36
packages/pastojs/src/pas2jsfileutils.pp

@@ -228,16 +228,6 @@ function TryCreateRelativePath(const Filename, BaseDirectory: String;
   - Filename = foo/bar BaseDirectory = bar/foo Result = False (no shared base directory)
   - Filename = foo/bar BaseDirectory = bar/foo Result = False (no shared base directory)
   - Filename = /foo BaseDirectory = bar Result = False (mixed absolute and relative)
   - Filename = /foo BaseDirectory = bar Result = False (mixed absolute and relative)
 }
 }
-{$IFDEF Pas2js}
-begin
-  Result:=false;
-  RelPath:=Filename;
-  if (BaseDirectory='') or (Filename='') then exit;
-  {AllowWriteln}
-  writeln('TryCreateRelativePath ToDo: ',Filename,' Base=',BaseDirectory,' UsePointDirectory=',UsePointDirectory);
-  {AllowWriteln-}
-end;
-{$ELSE}
   function IsNameChar(c: char): boolean; inline;
   function IsNameChar(c: char): boolean; inline;
   begin
   begin
     Result:=(c<>#0) and not (c in AllowDirectorySeparators);
     Result:=(c<>#0) and not (c in AllowDirectorySeparators);
@@ -245,65 +235,75 @@ end;
 
 
 var
 var
   UpDirCount: Integer;
   UpDirCount: Integer;
-  ResultPos: Integer;
   i: Integer;
   i: Integer;
-  FileNameRestLen, SharedDirs: Integer;
-  FileP, BaseP, FileEndP, BaseEndP: PChar;
+  {$IFDEF Pas2js}
+  s: string;
+  {$ELSE}
+  ResultPos, FileNameRestLen: Integer;
+  {$ENDIF}
+  SharedDirs: Integer;
+  FileP, BaseP, FileEndP, BaseEndP, FileL, BaseL: integer;
 begin
 begin
   Result:=false;
   Result:=false;
   RelPath:=Filename;
   RelPath:=Filename;
   if (BaseDirectory='') or (Filename='') then exit;
   if (BaseDirectory='') or (Filename='') then exit;
+  {$IFDEF Windows}
   // check for different windows file drives
   // check for different windows file drives
   if (CompareText(ExtractFileDrive(Filename),
   if (CompareText(ExtractFileDrive(Filename),
                      ExtractFileDrive(BaseDirectory))<>0)
                      ExtractFileDrive(BaseDirectory))<>0)
   then
   then
     exit;
     exit;
+  {$ENDIF}
 
 
-  FileP:=PChar(Filename);
-  BaseP:=PChar(BaseDirectory);
-
-  //writeln('TryCreateRelativePath START File="',FileP,'" Base="',BaseP,'"');
+  FileP:=1;
+  FileL:=length(Filename);
+  BaseP:=1;
+  BaseL:=length(BaseDirectory);
 
 
   // skip matching directories
   // skip matching directories
   SharedDirs:=0;
   SharedDirs:=0;
-  if FileP^ in AllowDirectorySeparators then
+  if Filename[FileP] in AllowDirectorySeparators then
   begin
   begin
-    if not (BaseP^ in AllowDirectorySeparators) then exit;
+    if not (BaseDirectory[BaseP] in AllowDirectorySeparators) then exit;
     repeat
     repeat
-      while FileP^ in AllowDirectorySeparators do inc(FileP);
-      while BaseP^ in AllowDirectorySeparators do inc(BaseP);
-      if (FileP^=#0) or (BaseP^=#0) then break;
-      //writeln('TryCreateRelativePath check match .. File="',FileP,'" Base="',BaseP,'"');
+      while (FileP<=FileL) and (Filename[FileP] in AllowDirectorySeparators) do
+        inc(FileP);
+      while (BaseP<=BaseL) and (BaseDirectory[BaseP] in AllowDirectorySeparators) do
+        inc(BaseP);
+      if (FileP>FileL) or (BaseP>BaseL) then break;
+      //writeln('TryCreateRelativePath check match .. File="',copy(Filename,FileP),'" Base="',copy(BaseDirectory,BaseP),'"');
       FileEndP:=FileP;
       FileEndP:=FileP;
       BaseEndP:=BaseP;
       BaseEndP:=BaseP;
-      while IsNameChar(FileEndP^) do inc(FileEndP);
-      while IsNameChar(BaseEndP^) do inc(BaseEndP);
-      if CompareFilenames(copy(Filename,FileP-PChar(Filename)+1,FileEndP-FileP),
-        copy(BaseDirectory,BaseP-PChar(BaseDirectory)+1,BaseEndP-BaseP))<>0
+      while (FileEndP<=FileL) and IsNameChar(Filename[FileEndP]) do inc(FileEndP);
+      while (BaseEndP<=BaseL) and IsNameChar(BaseDirectory[BaseEndP]) do inc(BaseEndP);
+      if CompareFilenames(copy(Filename,FileP,FileEndP-FileP),
+        copy(BaseDirectory,BaseP,BaseEndP-BaseP))<>0
       then
       then
         break;
         break;
       FileP:=FileEndP;
       FileP:=FileEndP;
       BaseP:=BaseEndP;
       BaseP:=BaseEndP;
       inc(SharedDirs);
       inc(SharedDirs);
     until false;
     until false;
-  end else if (BaseP^ in AllowDirectorySeparators) then
+  end else if (BaseDirectory[BaseP] in AllowDirectorySeparators) then
     exit;
     exit;
 
 
-  //writeln('TryCreateRelativePath skipped matches File="',FileP,'" Base="',BaseP,'"');
+  //writeln('TryCreateRelativePath skipped matches SharedDirs=',SharedDirs,' File="',copy(Filename,FileP),'" Base="',copy(BaseDirectory,BaseP),'"');
   if SharedDirs=0 then exit;
   if SharedDirs=0 then exit;
 
 
   // calculate needed '../'
   // calculate needed '../'
   UpDirCount:=0;
   UpDirCount:=0;
   BaseEndP:=BaseP;
   BaseEndP:=BaseP;
-  while IsNameChar(BaseEndP^) do begin
+  while (BaseEndP<=BaseL) and IsNameChar(BaseDirectory[BaseEndP]) do begin
     inc(UpDirCount);
     inc(UpDirCount);
-    while IsNameChar(BaseEndP^) do inc(BaseEndP);
-    while BaseEndP^ in AllowDirectorySeparators do inc(BaseEndP);
+    while (BaseEndP<=BaseL) and IsNameChar(BaseDirectory[BaseEndP]) do
+      inc(BaseEndP);
+    while (BaseEndP<=BaseL) and (BaseDirectory[BaseEndP] in AllowDirectorySeparators) do
+      inc(BaseEndP);
   end;
   end;
 
 
-  //writeln('TryCreateRelativePath UpDirCount=',UpDirCount,' File="',FileP,'" Base="',BaseP,'"');
+  //writeln('TryCreateRelativePath UpDirCount=',UpDirCount,' File="',copy(Filename,FileP),'" Base="',copy(BaseDirectory,BaseP),'"');
   // create relative filename
   // create relative filename
-  if (FileP^=#0) and (UpDirCount=0) then
+  if (FileP>FileL) and (UpDirCount=0) then
   begin
   begin
     // Filename is the BaseDirectory
     // Filename is the BaseDirectory
     if UsePointDirectory then
     if UsePointDirectory then
@@ -313,7 +313,17 @@ begin
     exit(true);
     exit(true);
   end;
   end;
 
 
-  FileNameRestLen:=length(Filename)-(FileP-PChar(Filename));
+  {$IFDEF Pas2js}
+  s:='';
+  for i:=1 to UpDirCount do
+    s+='..'+PathDelim;
+  if (FileP>FileL) and (UpDirCount>0) then
+    s:=LeftStr(s,length(s)-1)
+  else
+    s+=copy(Filename,FileP);
+  RelPath:=s;
+  {$ELSE}
+  FileNameRestLen:=length(Filename)-(FileP-1);
   SetLength(RelPath,3*UpDirCount+FileNameRestLen);
   SetLength(RelPath,3*UpDirCount+FileNameRestLen);
   ResultPos:=1;
   ResultPos:=1;
   for i:=1 to UpDirCount do begin
   for i:=1 to UpDirCount do begin
@@ -324,9 +334,9 @@ begin
   end;
   end;
   if FileNameRestLen>0 then
   if FileNameRestLen>0 then
     Move(FileP^,RelPath[ResultPos],FileNameRestLen);
     Move(FileP^,RelPath[ResultPos],FileNameRestLen);
+  {$ENDIF}
   Result:=true;
   Result:=true;
 end;
 end;
-{$ENDIF}
 
 
 function ResolveDots(const AFilename: string): string;
 function ResolveDots(const AFilename: string): string;
 //trim double path delims and expand special dirs like .. and .
 //trim double path delims and expand special dirs like .. and .
@@ -652,6 +662,7 @@ function MatchGlobbing(Mask, Name: string): boolean;
 {$IFDEF Pas2js}
 {$IFDEF Pas2js}
 begin
 begin
   if Mask='' then exit(Name='');
   if Mask='' then exit(Name='');
+  if Mask='*' then exit(true);
   {AllowWriteln}
   {AllowWriteln}
   writeln('MatchGlobbing ToDo ',Mask,' Name=',Name);
   writeln('MatchGlobbing ToDo ',Mask,' Name=',Name);
   {AllowWriteln-}
   {AllowWriteln-}