Skip to content

Commit

Permalink
JS fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
externl committed Nov 15, 2024
1 parent 418ea56 commit 3135536
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
8 changes: 6 additions & 2 deletions js/src/Ice/Properties.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ declare module "ice" {
*
* @param key - The property key.
* @returns The property value interpreted as an integer.
* @throws {@link PropertyException} - Thrown if the property value is not a valid integer.
*
* @see {@link setProperty}
*/
Expand All @@ -55,6 +56,8 @@ declare module "ice" {
*
* @param key - The property key.
* @returns The property value interpreted as an integer.
* @throws {@link PropertyException} - Thrown if the property is not a known Ice property or the value is
* not a valid integer.
*
* @see {@link setProperty}
*/
Expand All @@ -64,12 +67,13 @@ declare module "ice" {
* Get a property as an integer. If the property is not set, the given default value is returned.
*
* @param key The property key.
* @param value The default value to use if the property does not exist.
* @param defaultValue The default value to use if the property does not exist.
* @returns The property value interpreted as an integer, or the default value.
* @throws {@link PropertyException} - Thrown if the property value is not a valid integer.
*
* @see {@link setProperty}
*/
getPropertyAsIntWithDefault(key: string, value: number): number;
getPropertyAsIntWithDefault(key: string, defaultValue: number): number;

/**
* Retrieves a property value as a list of strings. The strings must be separated by whitespace or commas.
Expand Down
12 changes: 9 additions & 3 deletions js/src/Ice/Properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,23 @@ export class Properties {
let defaultValue = 0;
if (defaultValueString != "") {
defaultValue = parseInt(defaultValueString);
DEV: console.assert(!isNaN(defaultValue));
}
return this.getPropertyAsIntWithDefault(key, defaultValue);
}

getPropertyAsIntWithDefault(key, value) {
getPropertyAsIntWithDefault(key, defaultValue) {
const pv = this._properties.get(key);
if (pv !== undefined) {
pv.used = true;
return parseInt(pv.value);
} else {
const value = parseInt(pv.value);
if (isNaN(value)) {
throw new PropertyException(`property '${key}' has an invalid integer value: '${pv.value}'`);

}
return value;
} else {
return defaultValue;
}
}

Expand Down
13 changes: 13 additions & 0 deletions js/test/Ice/properties/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ export class Client extends TestHelper {
test(properties.getIceProperty("Ice.MessageSizeMax") == "20");
out.writeLine("ok");
}

{
out.write("testing that trying to read a non-numeric value as an int throws... ");
const properties = Ice.createProperties();
try {
properties.setProperty("Foo", "bar");
properties.getPropertyAsInt("Foo");
test(false);
} catch (ex) {
test(ex instanceof Ice.PropertyException);
}
out.writeLine("ok");
}
}

async run(args: string[]) {
Expand Down

0 comments on commit 3135536

Please sign in to comment.