From cd225b2cc66b93804390ca3e46f5a0b0cd9b6c53 Mon Sep 17 00:00:00 2001 From: tanjj4 Date: Fri, 15 Sep 2023 10:23:13 +0800 Subject: [PATCH 1/3] added tofixed function --- devs/run-tests/02numbers.ts | 8 +++++++- packages/core/src/corelib.d.ts | 13 +++++++++++++ packages/core/src/index.ts | 3 ++- packages/core/src/number.ts | 3 +++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 packages/core/src/number.ts diff --git a/devs/run-tests/02numbers.ts b/devs/run-tests/02numbers.ts index a8c93e682d..d7972fc1ad 100644 --- a/devs/run-tests/02numbers.ts +++ b/devs/run-tests/02numbers.ts @@ -242,10 +242,16 @@ function testEnumToString() { assert(enumTest + "" === "1", "enum tostring in concatenation") } +function testToFixed() { + const numObj = 12345.6789; + + assert(numObj.toFixed(5) === '12346') // '12346'; rounding, no fractional part +} + testComma() testNums() testNaN() testUnaryPlus() testEnumToString() - +testToFixed() diff --git a/packages/core/src/corelib.d.ts b/packages/core/src/corelib.d.ts index 09a94a2b29..1f3a299e55 100644 --- a/packages/core/src/corelib.d.ts +++ b/packages/core/src/corelib.d.ts @@ -197,6 +197,19 @@ interface StringConstructor { declare var String: StringConstructor interface Boolean {} + +interface NumberConstructor { + (number: Number): number + readonly prototype: Number +} + +declare var Number: NumberConstructor + +interface Number { + + // todo + toFixed(digits?: number): string +} interface Number {} interface RegExp {} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 99a878bcdc..8d8b740b2e 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -14,4 +14,5 @@ import "./button" import "./magneticfieldlevel" import "./buzzer" import "./ledstrip" -import "./gamepad" \ No newline at end of file +import "./gamepad" +import "./number" \ No newline at end of file diff --git a/packages/core/src/number.ts b/packages/core/src/number.ts new file mode 100644 index 0000000000..a8cf82b0d7 --- /dev/null +++ b/packages/core/src/number.ts @@ -0,0 +1,3 @@ +Number.prototype.toFixed = function(digits: number = 0): string { + return ""; +} \ No newline at end of file From 4564d67739a2329114458922cb001c627c304e95 Mon Sep 17 00:00:00 2001 From: tanjj4 Date: Mon, 6 Nov 2023 17:35:22 +0800 Subject: [PATCH 2/3] adding properties to objects listed in builtin_proto_idx --- runtime/devicescript/objects.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime/devicescript/objects.c b/runtime/devicescript/objects.c index b75762b7b7..1e80756c70 100644 --- a/runtime/devicescript/objects.c +++ b/runtime/devicescript/objects.c @@ -261,8 +261,9 @@ static const uint8_t builtin_proto_idx[] = { [DEVS_BUILTIN_OBJECT_BUFFER] = 10, [DEVS_BUILTIN_OBJECT_GPIO_PROTOTYPE] = 11, [DEVS_BUILTIN_OBJECT_GPIO] = 12, + [DEVS_BUILTIN_OBJECT_NUMBER_PROTOTYPE] = 13, }; -#define MAX_PROTO 12 +#define MAX_PROTO 13 devs_maplike_t *devs_get_builtin_object(devs_ctx_t *ctx, unsigned idx) { if (idx < sizeof(builtin_proto_idx)) { From 07f496a80b645284bcb116502634b64bc6d92078 Mon Sep 17 00:00:00 2001 From: tanjj4 Date: Tue, 7 Nov 2023 15:39:50 +0800 Subject: [PATCH 3/3] adding assert message --- devs/run-tests/02numbers.ts | 5 ++--- packages/core/src/corelib.d.ts | 2 -- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/devs/run-tests/02numbers.ts b/devs/run-tests/02numbers.ts index d7972fc1ad..e26621618e 100644 --- a/devs/run-tests/02numbers.ts +++ b/devs/run-tests/02numbers.ts @@ -245,7 +245,7 @@ function testEnumToString() { function testToFixed() { const numObj = 12345.6789; - assert(numObj.toFixed(5) === '12346') // '12346'; rounding, no fractional part + assert(numObj.toFixed(5) === '12346', 'testToFixed') // '12346'; rounding, no fractional part } testComma() @@ -253,5 +253,4 @@ testNums() testNaN() testUnaryPlus() testEnumToString() -testToFixed() - +testToFixed() \ No newline at end of file diff --git a/packages/core/src/corelib.d.ts b/packages/core/src/corelib.d.ts index 1f3a299e55..845dd7bc3b 100644 --- a/packages/core/src/corelib.d.ts +++ b/packages/core/src/corelib.d.ts @@ -199,7 +199,6 @@ declare var String: StringConstructor interface Boolean {} interface NumberConstructor { - (number: Number): number readonly prototype: Number } @@ -210,7 +209,6 @@ interface Number { // todo toFixed(digits?: number): string } -interface Number {} interface RegExp {} interface IterableIterator {}