Skip to content

Commit

Permalink
lets use nb$float, nb$int, and nb$long to better match cpython
Browse files Browse the repository at this point in the history
  • Loading branch information
s-cork committed Sep 24, 2020
1 parent c5d9186 commit 965b917
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 51 deletions.
6 changes: 3 additions & 3 deletions doc/ReferenceManual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ Misc
Slot("__invert__", "nb$invert", "unary",
opcode="UNARY_INVERT"),
Slot("__coerce__", "nb$coerce", "coercion"), # not needed
Slot("__int__", "nb$int_", "unary"), # expects exact int as return
Slot("__long__", "nb$lng", "unary"), # expects exact long as return
Slot("__float__", "nb$float_", "unary"), # expects exact float as return
Slot("__int__", "nb$int", "unary"), # expects exact int as return
Slot("__long__", "nb$long", "unary"), # expects exact long as return
Slot("__float__", "nb$float", "unary"), # expects exact float as return
Slot("__oct__", "nb$oct", "unary"),
Slot("__hex__", "nb$hex", "unary"),
Expand Down
2 changes: 1 addition & 1 deletion src/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ Sk.builtin.sum = function sum(iter, start) {
if (i.constructor === Sk.builtin.int_) {
tot = tot.nb$add(i);
} else if (i.constructor === Sk.builtin.float_) {
tot = tot.nb$float_().nb$add(i);
tot = tot.nb$float().nb$add(i);
return new Sk.misceval.Break("float");
} else {
tot = Sk.abstr.numberBinOp(tot, i, "Add");
Expand Down
12 changes: 6 additions & 6 deletions src/complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ Sk.builtin.complex = Sk.abstr.buildNativeClass("complex", {
},

// number slots
nb$int_() {
nb$int() {
throw new Sk.builtin.TypeError("can't convert complex to int");
},
nb$lng() {
nb$long() {
throw new Sk.builtin.TypeError("can't convert complex to long");
},
nb$float_() {
nb$float() {
throw new Sk.builtin.TypeError("can't convert complex to float");
},
nb$positive() {
Expand Down Expand Up @@ -194,8 +194,8 @@ function PyFloat_AsDouble(op) {
let v = op.v;
if (typeof v === "number") {
return v;
} else if (op.nb$float_) {
v = op.nb$float_();
} else if (op.nb$float) {
v = op.nb$float();
}
if (v === undefined) {
throw new Sk.builtin.TypeError("a float is required");
Expand Down Expand Up @@ -293,7 +293,7 @@ function complex_from_py(real, imag) {

// just a temporary function to match cpython
function check_number(nb) {
return nb.nb$float_ !== undefined;
return nb.nb$float !== undefined;
}

if (r != null) {
Expand Down
20 changes: 10 additions & 10 deletions src/float.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
} else if (typeof x === "string") {
// be careful with converting a string as it could result in infinity
this.v = parseFloat(x);
} else if (x.nb$float_) {
return x.nb$float_(); // allow this as a slow path
} else if (x.nb$float) {
return x.nb$float(); // allow this as a slow path
} else {
Sk.asserts.fail("bad argument to float constructor");
}
Expand All @@ -40,7 +40,7 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
if (hash !== undefined) {
return hash;
} else if (Number.isInteger(v)) {
hash = this.nb$int_().tp$hash();
hash = this.nb$int().tp$hash();
} else {
hash = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER - Number.MAX_SAFE_INTEGER / 2);
}
Expand All @@ -61,8 +61,8 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
// is args always an empty list?
if (arg === undefined) {
x = new Sk.builtin.float_(0.0);
} else if (arg.nb$float_) {
x = arg.nb$float_();
} else if (arg.nb$float) {
x = arg.nb$float();
} else if (Sk.builtin.checkString(arg)) {
x = _str_to_float(arg.v);
}
Expand All @@ -79,7 +79,7 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
},

// number slots
nb$int_() {
nb$int() {
let v = this.v;
if (v < 0) {
v = Math.ceil(v);
Expand All @@ -95,9 +95,9 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
return new Sk.builtin.int_(JSBI.BigInt(v));
}
},
nb$float_: cloneSelf,
nb$lng() {
return new Sk.builtin.lng(this.nb$int_().v);
nb$float: cloneSelf,
nb$long() {
return new Sk.builtin.lng(this.nb$int().v);
},
nb$add: numberSlot((v, w) => new Sk.builtin.float_(v + w)),

Expand Down Expand Up @@ -167,7 +167,7 @@ Sk.builtin.float_ = Sk.abstr.buildNativeClass("float", {
},
__trunc__: {
$meth() {
return this.nb$int_();
return this.nb$int();
},
$flags: { NoArgs: true },
$textsig: "($self, /)",
Expand Down
18 changes: 9 additions & 9 deletions src/int.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* If the number is a string then the size will be checked to determined whether it should be a number or BigInt
* It assumed that a number passed it is within `Number.MaxSafeInteger`
* Similarly if a BigInt is passed it is assumed that this is larger than `Number.MaxSafeInteger`
* Internal code like `float.nb$int_` checks the resulting JS instance before calling `new Sk.builtin.int_`
* Internal code like `float.nb$int` checks the resulting JS instance before calling `new Sk.builtin.int_`
*
* @param {number|JSBI|string=} x
*
Expand All @@ -22,8 +22,8 @@ Sk.builtin.int_ = Sk.abstr.buildNativeClass("int", {
v = 0;
} else if (typeof x === "string") {
v = stringToNumberOrBig(x);
} else if (x.nb$int_) {
return x.nb$int_(); // allow this as a slow path
} else if (x.nb$int) {
return x.nb$int(); // allow this as a slow path
} else {
Sk.asserts.fail("bad argument to int constructor");
}
Expand Down Expand Up @@ -68,11 +68,11 @@ Sk.builtin.int_ = Sk.abstr.buildNativeClass("int", {
ob$lt: compareSlot((v, w) => v < w, JSBI.lessThan),
ob$le: compareSlot((v, w) => v <= w, JSBI.lessThanOrEqual),

nb$int_: cloneSelf,
nb$int: cloneSelf,
nb$index() {
return this.v;
},
nb$float_() {
nb$float() {
const v = this.v;
if (typeof v === "number") {
return new Sk.builtin.float_(v);
Expand Down Expand Up @@ -111,7 +111,7 @@ Sk.builtin.int_ = Sk.abstr.buildNativeClass("int", {
nb$multiply: numberSlot((v, w) => v * w, JSBI.multiply),
nb$divide(other) {
if (Sk.__future__.division) {
return this.nb$float_().nb$divide(other);
return this.nb$float().nb$divide(other);
}
return this.nb$floor_divide(other);
},
Expand Down Expand Up @@ -187,7 +187,7 @@ Sk.builtin.int_ = Sk.abstr.buildNativeClass("int", {
}
return Sk.builtin.NotImplemented.NotImplemented$;
},
nb$lng() {
nb$long() {
return new Sk.builtin.lng(this.v);
},
},
Expand Down Expand Up @@ -654,8 +654,8 @@ function getInt(x, base) {
return new Sk.builtin.int_(Sk.str2number(x.v, base));
} else if (base !== null) {
throw new Sk.builtin.TypeError("int() can't convert non-string with explicit base");
} else if (x.nb$int_) {
return x.nb$int_();
} else if (x.nb$int) {
return x.nb$int();
}

if ((func = Sk.abstr.lookupSpecial(x, Sk.builtin.str.$trunc))) {
Expand Down
20 changes: 10 additions & 10 deletions src/lib/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ const $builtinmodule = function (name) {
let _x = x.v;
let _y = y.v;
if (typeof _x !== "number") {
_x = x.nb$float_().v;
_x = x.nb$float().v;
}
if (typeof _y !== "number") {
_y = y.nb$float_().v;
_y = y.nb$float().v;
}

if ((_y == Infinity || _y == -Infinity) && isFinite(_x)) {
Expand Down Expand Up @@ -168,7 +168,7 @@ const $builtinmodule = function (name) {
i = 0;
let _x = x.v;
if (typeof _x !== "number") {
_x = x.nb$float_().v;
_x = x.nb$float().v;
}
x = _x;
for (let j = 0, len = partials.length; j < len; j++) {
Expand Down Expand Up @@ -321,7 +321,7 @@ const $builtinmodule = function (name) {

let _x = x.v;
if (typeof _x !== "number") {
_x = x.nb$float_().v;
_x = x.nb$float().v;
}
const _i = Sk.builtin.asnum$(i);

Expand Down Expand Up @@ -373,10 +373,10 @@ const $builtinmodule = function (name) {
let _x = x.v;
let _y = y.v;
if (typeof _x !== "number") {
_x = x.nb$float_().v;
_x = x.nb$float().v;
}
if (typeof _y !== "number") {
_y = y.nb$float_().v;
_y = y.nb$float().v;
}

// deal with most common cases first
Expand Down Expand Up @@ -430,7 +430,7 @@ const $builtinmodule = function (name) {
Sk.builtin.pyCheckType("x", "number", Sk.builtin.checkNumber(x));
let _x = x.v;
if (typeof _x !== "number") {
_x = x.nb$float_().v;
_x = x.nb$float().v;
}
if (_x == Infinity || _x == -Infinity || isNaN(_x)) {
return new Sk.builtin.float_(Math.exp(_x));
Expand Down Expand Up @@ -501,7 +501,7 @@ const $builtinmodule = function (name) {

let _x = x.v;
if (typeof _x !== "number") {
_x = x.nb$float_().v;
_x = x.nb$float().v;
}

if (_x <= -1.0) {
Expand Down Expand Up @@ -568,10 +568,10 @@ const $builtinmodule = function (name) {
let _x = x.v;
let _y = y.v;
if (typeof _x !== "number") {
_x = x.nb$float_().v;
_x = x.nb$float().v;
}
if (typeof _y !== "number") {
_y = y.nb$float_().v;
_y = y.nb$float().v;
}

if (_x == 0 && _y < 0) {
Expand Down
24 changes: 12 additions & 12 deletions src/slotdefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ slots.__delitem__ = {
*
* Examples:
* - [nb$add]{@link Sk.slots.nb$add}
* - [nb$int_]{@link Sk.slots.nb$int_}
* - [nb$int]{@link Sk.slots.nb$int}
* - [nb$divide]{@link Sk.slots.nb$divide} - note we do not use `nb$true_divide`
* - [nb$bool]{@link Sk.slots.nb$bool} - should return a js boolean
*
Expand Down Expand Up @@ -1511,13 +1511,13 @@ slots.__ior__ = {
};
/**
* @memberof Sk.slots
* @method nb$int_
* @method nb$int
* @implements __int__
* @suppress {checkTypes}
*/
slots.__int__ = {
$name: "__int__",
$slot_name: "nb$int_",
$slot_name: "nb$int",
$slot_func: slotFuncNoArgsWithCheck("__int__", Sk.builtin.checkInt, "int"),
$wrapper: wrapperCallNoArgs,
$textsig: "($self, /)",
Expand All @@ -1526,13 +1526,13 @@ slots.__int__ = {
};
/**
* @memberof Sk.slots
* @method nb$float_
* @method nb$float
* @implements __float__
* @suppress {checkTypes}
*/
slots.__float__ = {
$name: "__float__",
$slot_name: "nb$float_",
$slot_name: "nb$float",
$slot_func: slotFuncNoArgsWithCheck("__float__", Sk.builtin.checkFloat, "float"),
$wrapper: wrapperCallNoArgs,
$textsig: "($self, /)",
Expand Down Expand Up @@ -1747,7 +1747,7 @@ slots.__imatmul__ = {
// py2 ONLY slots
slots.__long__ = {
$name: "__long__",
$slot_name: "nb$lng",
$slot_name: "nb$long",
$slot_func: slotFuncNoArgsWithCheck("__long__", Sk.builtin.checkInt, "int"),
$wrapper: wrapperCallNoArgs,
$textsig: "($self, /)",
Expand Down Expand Up @@ -1843,9 +1843,9 @@ Sk.subSlots = {
nb$abs: "__abs__",
nb$negative: "__neg__",
nb$positive: "__pos__",
nb$int_: "__int__",
nb$lng: "__long__",
nb$float_: "__float__",
nb$int: "__int__",
nb$long: "__long__",
nb$float: "__float__",
nb$add: "__add__",
nb$reflected_add: "__radd__",
nb$inplace_add: "__iadd__",
Expand Down Expand Up @@ -2052,8 +2052,8 @@ Sk.dunderToSkulpt = {
__abs__: "nb$abs",
__neg__: "nb$negative",
__pos__: "nb$positive",
__int__: "nb$int_",
__float__: "nb$float_",
__int__: "nb$int",
__float__: "nb$float",

__add__: "nb$add",
__radd__: "nb$reflected_add",
Expand Down Expand Up @@ -2082,7 +2082,7 @@ Sk.dunderToSkulpt = {

__bool__: "nb$bool",
// py2 only
__long__: "nb$lng",
__long__: "nb$long",

__lshift__: "nb$lshift",
__rlshift__: "nb$reflected_lshift",
Expand Down

0 comments on commit 965b917

Please sign in to comment.