From 965b917aabdb45141d66b27412ed8c4698359c67 Mon Sep 17 00:00:00 2001 From: mrcork Date: Thu, 24 Sep 2020 16:54:22 +0800 Subject: [PATCH] lets use nb$float, nb$int, and nb$long to better match cpython --- doc/ReferenceManual.rst | 6 +++--- src/builtin.js | 2 +- src/complex.js | 12 ++++++------ src/float.js | 20 ++++++++++---------- src/int.js | 18 +++++++++--------- src/lib/math.js | 20 ++++++++++---------- src/slotdefs.js | 24 ++++++++++++------------ 7 files changed, 51 insertions(+), 51 deletions(-) diff --git a/doc/ReferenceManual.rst b/doc/ReferenceManual.rst index ae29797e29..8c033c21bc 100644 --- a/doc/ReferenceManual.rst +++ b/doc/ReferenceManual.rst @@ -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"), diff --git a/src/builtin.js b/src/builtin.js index b7d45b5dec..ffd41b3034 100644 --- a/src/builtin.js +++ b/src/builtin.js @@ -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"); diff --git a/src/complex.js b/src/complex.js index b8a8424249..c3077a3573 100644 --- a/src/complex.js +++ b/src/complex.js @@ -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() { @@ -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"); @@ -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) { diff --git a/src/float.js b/src/float.js index fd6d17fa08..efbec3186c 100644 --- a/src/float.js +++ b/src/float.js @@ -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"); } @@ -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); } @@ -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); } @@ -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); @@ -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)), @@ -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, /)", diff --git a/src/int.js b/src/int.js index 1692330f79..6aa942c953 100644 --- a/src/int.js +++ b/src/int.js @@ -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 * @@ -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"); } @@ -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); @@ -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); }, @@ -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); }, }, @@ -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))) { diff --git a/src/lib/math.js b/src/lib/math.js index ce83f2d046..ae82eced08 100644 --- a/src/lib/math.js +++ b/src/lib/math.js @@ -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)) { @@ -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++) { @@ -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); @@ -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 @@ -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)); @@ -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) { @@ -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) { diff --git a/src/slotdefs.js b/src/slotdefs.js index ec22f91e57..331c50c7f5 100644 --- a/src/slotdefs.js +++ b/src/slotdefs.js @@ -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 * @@ -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, /)", @@ -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, /)", @@ -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, /)", @@ -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__", @@ -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", @@ -2082,7 +2082,7 @@ Sk.dunderToSkulpt = { __bool__: "nb$bool", // py2 only - __long__: "nb$lng", + __long__: "nb$long", __lshift__: "nb$lshift", __rlshift__: "nb$reflected_lshift",