Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 6bff1ab

Browse files
authored
numbers: Add proper safety to operator overloading (#204)
* numbers: Add null assertions to operator overloading When operator overloading functions are called, then unfortunately work when the left hand side of the operation is nullable (only inside class properties), like this: ```ts class BigInt extends Uint8Array { @operator('+') plus(other: BigInt): BigInt { // ... } } class Wrapper { public constructor( public n: BigInt | null ) {} } let x = BigInt.fromI32(2); let y: BigInt | null = null; // x + y; // give compile time error about nullability let wrapper = new Wrapper(y); wrapper.n = wrapper.n + x; // doesn't give compile time errors as it should ``` In our case it would break in graph-node's runtime side because these operator implementations are written there in the host-exports. These checks only exist while the compiler doesn't catch this itself. * 0.22.0-alpha.3
1 parent c40be8c commit 6bff1ab

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

common/numbers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,25 @@ export class BigInt extends Uint8Array {
137137

138138
@operator('+')
139139
plus(other: BigInt): BigInt {
140+
assert(this !== null, "Failed to sum BigInts because left hand side is 'null'");
140141
return bigInt.plus(this, other)
141142
}
142143

143144
@operator('-')
144145
minus(other: BigInt): BigInt {
146+
assert(this !== null, "Failed to subtract BigInts because left hand side is 'null'");
145147
return bigInt.minus(this, other)
146148
}
147149

148150
@operator('*')
149151
times(other: BigInt): BigInt {
152+
assert(this !== null, "Failed to multiply BigInts because left hand side is 'null'");
150153
return bigInt.times(this, other)
151154
}
152155

153156
@operator('/')
154157
div(other: BigInt): BigInt {
158+
assert(this !== null, "Failed to divide BigInts because left hand side is 'null'");
155159
return bigInt.dividedBy(this, other)
156160
}
157161

@@ -161,6 +165,7 @@ export class BigInt extends Uint8Array {
161165

162166
@operator('%')
163167
mod(other: BigInt): BigInt {
168+
assert(this !== null, "Failed to apply module to BigInt because left hand side is 'null'");
164169
return bigInt.mod(this, other)
165170
}
166171

@@ -319,21 +324,25 @@ export class BigDecimal {
319324

320325
@operator('+')
321326
plus(other: BigDecimal): BigDecimal {
327+
assert(this !== null, "Failed to sum BigDecimals because left hand side is 'null'");
322328
return bigDecimal.plus(this, other)
323329
}
324330

325331
@operator('-')
326332
minus(other: BigDecimal): BigDecimal {
333+
assert(this !== null, "Failed to subtract BigDecimals because left hand side is 'null'");
327334
return bigDecimal.minus(this, other)
328335
}
329336

330337
@operator('*')
331338
times(other: BigDecimal): BigDecimal {
339+
assert(this !== null, "Failed to multiply BigDecimals because left hand side is 'null'");
332340
return bigDecimal.times(this, other)
333341
}
334342

335343
@operator('/')
336344
div(other: BigDecimal): BigDecimal {
345+
assert(this !== null, "Failed to divide BigDecimals because left hand side is 'null'");
337346
return bigDecimal.dividedBy(this, other)
338347
}
339348

@@ -369,6 +378,7 @@ export class BigDecimal {
369378

370379
@operator.prefix('-')
371380
neg(): BigDecimal {
381+
assert(this !== null, "Failed to negate BigDecimal because the value of it is 'null'");
372382
return new BigDecimal(new BigInt(0)) - this
373383
}
374384

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@graphprotocol/graph-ts",
33
"description": "TypeScript/AssemblyScript library for writing subgraph mappings for The Graph",
4-
"version": "0.22.0-alpha.2",
4+
"version": "0.22.0-alpha.3",
55
"module": "index.ts",
66
"types": "index.ts",
77
"main": "index.ts",

0 commit comments

Comments
 (0)