rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Casting `infinity` to integer makes value `0`

Open cometkim opened this issue 1 year ago • 32 comments

let t1 = infinity->Belt.Float.toInt
let t2 = infinity->Float.toInt

generates

import * as PervasivesU from "./stdlib/pervasivesU.js";

var t1 = PervasivesU.infinity | 0;

var t2 = PervasivesU.infinity | 0;

both are 0. so we don't actually have a way to compare int value with the infinity without %raw. because infinity in pervasive is float

Infinity is often used in arithmetic operations as well as a value indicating "no limit" (e.g. pagination)

#6038 also mentioned

cometkim avatar Apr 21 '24 01:04 cometkim

I am not sure, I am getting you. Of course you can use pathlib, pkg_resources.read_text, pkgutil.get_data, and importlib.resources or whatever functions they all work. Some of them work with package names, some of them work with filenames. Only some people associate their data files with packages, but I would recommend doing it, since it makes it easy to work with them in Python code with clean calls.

For the root directory of inside your program, which is apparently what you are looking for, the best thing would be to use os.path.dirname(sys.modules["__main__").__file__ which is probably something worth adding to the documentation.

Does this address your point?

I find your proposal of this being library code, nice. The typical Python APIs have no distinction of inside/outside of course, but os.path.dirname(sys.argv[0]) is kind of good enough there, isn't it.

kayhayen avatar Jun 27 '24 09:06 kayhayen

Thanks! sys.modules["__main__"].__file__ fixed my issue. Regarding os.path.dirname(sys.argv[0]), as previouslly stated it will work as long as you don't run it from another folder calling it from the PATH.

For example, on Windows:

set PATH=%PATH%;path\to\compiled\binary\

Calling binary.exe will make sys.argv[0] return just 'binary.exe' with absolutely no information about its location.

I will have to investigate that. There is __compiled__.contained_dir to find the outside directory, but I believe the onefile is supposed to produce an absolute path there. If it's not, I would consider that a bug of course.

kayhayen avatar Jun 28 '24 05:06 kayhayen

You are correct, this is not absolute at all, making it unreliable for use.

contained_dir is intended mainly for you avoid having to do different things for macOS apps vs. everything else, where to get outside of the dist dir, you only need a dirname, but for macOS that's not enough to get outside the .app.

kayhayen avatar Jun 28 '24 05:06 kayhayen

So, I changed it for the Python side of things for standalone and accelerated mode both, but onefile should pass it corrected already, however it just uses GetCommandLineW so I guess, I need to replace the first argument in there with a parsed one, but need to figure out, how to do the command line parsing on Windows safely first.

kayhayen avatar Jun 28 '24 06:06 kayhayen

As an aside, making __compiled__.containing_dir absolute was only added relatively recently, and the wrong place to do it for that object only, since it's derived from the sys.argv[0], if that's absolute, it would be automatically good, so I can also correct that one, and containing_dir could easily be wrong it called via PATH indeed.

kayhayen avatar Jun 28 '24 06:06 kayhayen

Ok, that was easy, CommandLineToArgvW exists and can be used to disassemble and later recombine, I guess.

kayhayen avatar Jun 28 '24 06:06 kayhayen

So, I did it for onefile, standalone, accelerated on all OSes, it's on staging branch right now, I kind of don't dare changing it late in the 2.3 series, but that's got to make things a lot more robust. The unfortunate thing is that it's not fully compatible code for Python, where that's not happening, the path needs manual resolution there and it works only if calling it from the current directory. So I guess, for Python fully compatible code, some sort of PATH resolution needs to be done I guess, where using file was always good, but that's just the way it is I fear. Maybe I change my recommendation over to allow for compiled.__containing_dir with file fallback, but I hate non-standard Python changes in user code.

kayhayen avatar Jun 28 '24 07:06 kayhayen

This is currently on the develop and staging branches and will be part of the 2.4 release.

kayhayen avatar Jul 08 '24 07:07 kayhayen

This is part of the stable release 2.4 that I just made.

kayhayen avatar Jul 20 '24 13:07 kayhayen

sys.argv returns incorrect results:

Command: abc.exe argv1 argv2

In Nuitka 2.3.11 - sys.argv = ['\path\abc.exe', 'argv1', 'argv2'] But in Nuitka 2.4 - sys.argv = ['\path\abc.exeargv1', 'argv2']

I had to rollback to 2.3.11 due to this issue... Please check this quickly.

Choonholic avatar Jul 20 '24 16:07 Choonholic