Skip to content

Commit

Permalink
[hlmem] Simplify FileReader js with BytesInput
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxiaomao committed Jan 14, 2025
1 parent 6498fa3 commit 9ccfeaa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 37 deletions.
47 changes: 11 additions & 36 deletions other/haxelib/hlmem/FileReader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,46 @@ class FileReader {

#if js
// js read file in once for better performance
var memBytes : haxe.io.Bytes;
var memPos : Int;
var memInput : haxe.io.BytesInput;
#else
var memInput : sys.io.FileInput;
#end

public inline function new(file : String) {
#if js
memBytes = sys.io.File.getBytes(file);
memPos = 0;
memInput = new haxe.io.BytesInput(sys.io.File.getBytes(file));
#else
memInput = sys.io.File.read(file);
#end
}

public inline function close() {
#if js
memBytes = null;
memPos = 0;
#else
if( memInput != null )
memInput.close();
memInput = null;
#end
}

public inline function readString(length : Int) : String {
#if js
var str = memBytes.getString(memPos, 3);
memPos += 3;
#else
var str = memInput.read(3).toString();
#end
return str;
return memInput.readString(length);
}

public inline function readByte() : Int {
#if js
var b = memBytes.get(memPos);
memPos += 1;
#else
var b = memInput.readByte();
#end
return b;
return memInput.readByte();
}

public inline function readInt32() : Int {
#if js
var i = memBytes.getInt32(memPos);
memPos += 4;
#else
var i = memInput.readInt32();
#end
return i;
return memInput.readInt32();
}

public inline function readPointer( is64 : Bool ) : Block.Pointer {
public inline function readPointer( is64 : Bool ) : haxe.Int64 {
var low = readInt32();
var high = is64 ? readInt32() : 0;
return cast haxe.Int64.make(high,low);
return haxe.Int64.make(high,low);
}

public inline function tell() : Float {
#if js
return memPos;
return memInput.position;
#else
return tell2(@:privateAccess memInput.__f);
#end
Expand All @@ -96,11 +71,11 @@ class FileReader {
var dpos = Std.int(pos);
switch( mode ) {
case SeekBegin:
memPos = dpos;
memInput.position = dpos;
case SeekCur:
memPos += dpos;
memInput.position += dpos;
case SeekEnd:
memPos = memBytes.length + dpos;
memInput.position = memInput.length + dpos;
}
#else
if( !seek2(@:privateAccess memInput.__f, pos, mode.getIndex()) )
Expand Down
2 changes: 1 addition & 1 deletion other/haxelib/hlmem/Memory.hx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Memory {
}

inline function readPointer() : Pointer {
return memFileReader.readPointer(is64);
return cast memFileReader.readPointer(is64);
}

function goto( b : Block ) {
Expand Down

0 comments on commit 9ccfeaa

Please sign in to comment.