j2s/lib/values/number.ts

33 lines
1.3 KiB
TypeScript
Raw Normal View History

define("values/number", () => {
var Number = env.global.Number = function(this: Number | undefined, arg: any) {
var val;
if (arguments.length === 0) val = 0;
else val = arg - 0;
if (this === undefined || this === null) return val;
else (this as any).value = val;
} as NumberConstructor;
2023-08-05 15:37:18 +00:00
Number.prototype = (0 as any).__proto__ as Number;
setConstr(Number.prototype, Number, env);
2023-08-05 15:37:18 +00:00
setProps(Number.prototype, env, {
valueOf() {
if (typeof this === 'number') return this;
else return (this as any).value;
},
toString() {
if (typeof this === 'number') return this + '';
else return (this as any).value + '';
}
});
2023-08-05 15:37:18 +00:00
setProps(Number, env, {
parseInt(val) { return Math.trunc(Number.parseFloat(val)); },
parseFloat(val) { return env.internals.parseFloat(val); },
});
2023-08-05 15:37:18 +00:00
env.global.Object.defineProperty(env.global, 'parseInt', { value: Number.parseInt, writable: false });
env.global.Object.defineProperty(env.global, 'parseFloat', { value: Number.parseFloat, writable: false });
env.global.Object.defineProperty(env.global, 'NaN', { value: 0 / 0, writable: false });
env.global.Object.defineProperty(env.global, 'Infinity', { value: 1 / 0, writable: false });
});