Browse Source

* Correct poll_oneoff (for sleep)

Michael Van Canneyt 8 months ago
parent
commit
3de2b72867
1 changed files with 35 additions and 2 deletions
  1. 35 2
      packages/wasi/src/wasienv.pas

+ 35 - 2
packages/wasi/src/wasienv.pas

@@ -1425,11 +1425,44 @@ begin
   Result:=FImportObject;
   Result:=FImportObject;
 end;
 end;
 
 
+
 function TPas2JSWASIEnvironment.poll_oneoff(sin, sout, nsubscriptions,
 function TPas2JSWASIEnvironment.poll_oneoff(sin, sout, nsubscriptions,
   nevents: NativeInt): NativeInt;
   nevents: NativeInt): NativeInt;
+
+const
+  TagOffset = 8;
+  TimeoutOffset = 24;
+  PrecisionOffset = 32;
+
+var
+  Tag : NativeInt;
+  Precision,TimeOut,msTimeOut : TJSBigInt;
+  msTimeout32 : Integer;
+  arr : TJSint32array;
+  mem : TJSDataView;
+
 begin
 begin
-  console.log('Unimplemented: TPas2JSWASIEnvironment.poll_oneoff');
+  {$IFNDEF WEB_WORKER}
+  // Only used for sleep at the moment. We check the tag
+  Tag:=GetMemInfoInt8(sin+tagOffset);
+  if (Tag=WASI_EVENTTYPE_CLOCK) then
+    begin
+    mem:=getModuleMemoryDataView;
+    TimeOut:= Mem.getBigInt64(sin+TimeOutOffset,IsLittleEndian);
+    Precision:=Mem.GetBigInt64(sin+PrecisionOffset,IsLittleEndian);
+    asm
+    msTimeOut = TimeOut / Precision;
+    end;
+    arr:=TJSint32array.new(FMemory.buffer);
+    TJSAtomics.Store(arr,256,0);
+    msTimeout32:=StrToInt(msTimeOut.toString);
+    Writeln('Timeout is: ',msTImeout32);
+    TJSAtomics.wait(Arr,256,0,msTimeout32);
+    Writeln('Done timeout');
+    end;
+  {$ELSE}
   Result:= WASI_ENOSYS;
   Result:= WASI_ENOSYS;
+  {$ENDIF}
 end;
 end;
 
 
 function TPas2JSWASIEnvironment.proc_exit(rval: NativeInt): NativeInt;
 function TPas2JSWASIEnvironment.proc_exit(rval: NativeInt): NativeInt;
@@ -2438,7 +2471,7 @@ Var
 
 
 begin
 begin
   view:=getModuleMemoryDataView();
   view:=getModuleMemoryDataView();
-  Result:=GetBigUint64(View,aLoc,IsLittleEndian);
+  Result:=GetBigInt64(View,aLoc,IsLittleEndian);
 end;
 end;
 
 
 function TPas2JSWASIEnvironment.GetMemInfoUInt8(aLoc: TWasmMemoryLocation): Byte;
 function TPas2JSWASIEnvironment.GetMemInfoUInt8(aLoc: TWasmMemoryLocation): Byte;