Skip to content

Commit

Permalink
Enhancing #268 for null, invalid type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Oct 23, 2024
1 parent 8099870 commit 34246be
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 115 deletions.
21 changes: 14 additions & 7 deletions src/interpreter/runtimeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,22 @@ export class RuntimeObject extends Context {
)
}

assertIsNumber(): asserts this is BasicRuntimeObject<number> {
this.assertIs(NUMBER_MODULE, this.innerNumber)
assertIsNumber(message: string, variableName: string, validateNotNull = true): asserts this is BasicRuntimeObject<number> {
if (validateNotNull) this.assertIsNotNull(message, variableName)
if (this.innerNumber === undefined) throw new TypeError(`Message ${message}: ${variableName} (${this.getShortLabel()}) should be an instance of ${NUMBER_MODULE}`)
}

assertIsBoolean(message: string, variableName: string): asserts this is BasicRuntimeObject<boolean> {
if (this.innerBoolean === undefined) throw new RangeError(`${message}: ${variableName} should be an instance of ${BOOLEAN_MODULE}`)
if (this.innerBoolean === undefined) throw new TypeError(`Message ${message}: ${variableName} (${this.getShortLabel()}) should be an instance of ${BOOLEAN_MODULE}`)
}

assertIsString(message: string, variableName: string): asserts this is BasicRuntimeObject<string> {
if (this.innerString === undefined) throw new RangeError(`${message}: ${variableName} should be an instance of ${STRING_MODULE}`)
assertIsString(message: string, variableName: string, validateNotNull = true): asserts this is BasicRuntimeObject<string> {
if (validateNotNull) this.assertIsNotNull(message, variableName)
if (this.innerString === undefined) throw new TypeError(`Message ${message}: ${variableName} (${this.getShortLabel()}) should be an instance of ${STRING_MODULE}`)
}

assertIsCollection(): asserts this is BasicRuntimeObject<RuntimeObject[]> {
if (!this.innerCollection) throw new TypeError(`Malformed Runtime Object: Collection inner value should be a List<RuntimeObject> but was ${this.innerValue}`)
if (!this.innerCollection) throw new TypeError(`Malformed Runtime Object: expected a List of values but was ${this.innerValue}`)
}

assertIsException(): asserts this is BasicRuntimeObject<Error | undefined> {
Expand All @@ -235,7 +237,7 @@ export class RuntimeObject extends Context {
}

assertIsNotNull(message: string, variableName: string): asserts this is BasicRuntimeObject<Exclude<InnerValue, null>> {
if (this.innerValue === null) throw new RangeError(`${message}: ${variableName} was not expected to be null`)
if (this.innerValue === null) throw new RangeError(`Message ${message} does not support parameter '${variableName}' to be null`)
}

protected assertIs(moduleFQN: Name, innerValue?: InnerValue): void {
Expand All @@ -256,6 +258,11 @@ export class RuntimeObject extends Context {
return this.module.name ?? 'Object'
}

getShortLabel(): string {
if (!this.innerValue) return `an instance of ${this.module.fullyQualifiedName}`
return this.innerString !== undefined ? `"${this.getShortRepresentation()}"`: this.getShortRepresentation()
}

getShortRepresentation(): string {
return this.innerValue?.toString().trim() ?? ''
}
Expand Down
13 changes: 7 additions & 6 deletions src/wre/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const { round } = Math

const game: Natives = {
game: {
*addVisual(self: RuntimeObject, visual: RuntimeObject): Execution<void> {
visual.assertIsNotNull('addVisual', 'visual')
if (!visual.module.lookupMethod('position', 0)) throw new TypeError('addVisual: visual lacks a position message')
*addVisual(self: RuntimeObject, positionable: RuntimeObject): Execution<void> {
positionable.assertIsNotNull('addVisual', 'positionable')
if (!positionable.module.lookupMethod('position', 0)) throw new TypeError('Message addVisual: positionable lacks a position message')

const visuals = self.get('visuals')!.innerCollection!

// TODO: shouldn´t we say "visual is already included"
if(visuals.includes(visual)) throw new TypeError(visual.module.fullyQualifiedName)
if(visuals.includes(positionable)) throw new TypeError(positionable.module.fullyQualifiedName)

visuals.push(visual)
visuals.push(positionable)
},

*removeVisual(self: RuntimeObject, visual: RuntimeObject): Execution<void> {
Expand Down Expand Up @@ -126,6 +126,7 @@ const game: Natives = {
const sounds = game.get('sounds')?.innerCollection
if (!sounds) game.set('sounds', yield* this.list(self))
else {
// TODO: shouldn´t we say 'sound was already included in the game'?
if (sounds.includes(self)) throw new TypeError(self.module.fullyQualifiedName)
else sounds.push(self)
}
Expand Down Expand Up @@ -167,7 +168,7 @@ const game: Natives = {
if(!newVolume) return self.get('volume')

const volume: RuntimeObject = newVolume
volume.assertIsNumber()
volume.assertIsNumber('volume', 'newVolume', false)

if (volume.innerNumber < 0 || volume.innerNumber > 1) throw new RangeError('volumen: newVolume should be between 0 and 1')

Expand Down
Loading

0 comments on commit 34246be

Please sign in to comment.