-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JS] Use CanCastAsInteger
in JSC and V8
#2624
base: master
Are you sure you want to change the base?
Conversation
I'm wondering about what I'd actually want to happen when wrapping a C++ API with overloads between double and integer types. One pattern that comes to mind is the two overloads provide the same functionality and essentially exist to avoid forcing the caller to convert an integer to a double if they have an integer to start with, so if we already have a double (as we do in SWIG/Javascript) just calling the double overload seems ressonable, and doing work to decide if that double could be passed as an integer seems like unnecessary complication. The other realistic pattern I came up with was serialisation, e.g:
If whatever takes the serialised data expects a particular type in a particular context we need exact control over which overload gets called and to wrap this usably for Javascript you'd used I wondered what SWIG/Lua does since Lua only recently gained an actual integer type which SWIG doesn't yet know about that (#1150), but hacking up a quick I'm not sure if any other target languages are "double-only". @wsfulton What do you think? |
Not exactly the same situation admittedly, but some other target languages have a |
SWIG has a mechanism for handling overloading of numeric types described in https://swig.org/Doc4.1/SWIGPlus.html#SWIGPlus_overloaded_methods. Javascript does not seem to implement the type precedence via the I do note that Python uses a v8 has the diff --git a/Lib/javascript/v8/javascriptprimtypes.swg b/Lib/javascript/v8/javascriptprimtypes.swg
index 8ed571df1..ede171909 100644
--- a/Lib/javascript/v8/javascriptprimtypes.swg
+++ b/Lib/javascript/v8/javascriptprimtypes.swg
@@ -44,6 +44,9 @@ int SWIG_AsVal_dec(int)(SWIGV8_VALUE valRef, int* val)
if (!valRef->IsNumber()) {
return SWIG_TypeError;
}
+ if (!valRef->IsInt32()) {
+ return SWIG_TypeError;
+ }
if(val) *val = SWIGV8_INTEGER_VALUE(valRef);
return SWIG_OK; While the internal representation of scripting languages might be by double only or not, there are usually methods to work out if the type is a double or an integer like With the above patch and if the C++ methods are declared in the correct order (that the missing dispatch ranking would fix), then overloading with integer and double works. For example: void send(int i) { std::cout << "int " << i << std::endl; }
void send(double i) { std::cout << "double " << i << std::endl; } Above will call int overload with void send(double i) { std::cout << "double " << i << std::endl; }
void send(int i) { std::cout << "int " << i << std::endl; } only the double overload will be used. Once the typecheck typemaps are used in the Javascript implementation it won't matter what order the c++ code is declared in. overload_numeric is a relevant testcase for testing an implementation, but it could do with extending by adding in a different order of the |
Ok, I will consider adding the I am afraid that there is no generic way to find if a V8 number is an integer except to use |
Okay, let's come back to this after #2545 is merged. |
@wsfulton There is one major problem with adding |
The typecheck typemaps are fundamental to overloading, it'll have to be okay. We do make changes that may require extra work after a release especially when fixing bugs like this. It will of course need documenting with some helpful advice added to the changes file. |
As everything is a floating point number in JS, use
CanCastAsInteger
to identify integer values.Additionally, treat an
OverflowError
as aTypeError
when overloading to allow (some form) of overloading between integers and floating point values.