Skip to content

Commit

Permalink
typecheck: actually enforce that we do not parameter pass into a devi…
Browse files Browse the repository at this point in the history
…ce attribute
  • Loading branch information
gcampax committed Nov 1, 2019
1 parent 7759ad3 commit 0255957
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/typecheck.js
Expand Up @@ -79,6 +79,14 @@ class Scope {
this._lambda_args = {};
}

clone() {
const newself = new Scope(this._parentScope);
Object.assign(newself._scope, this._scope);
newself.$has_event = this.$has_event;
Object.assign(newself._lambda_args, this._lambda_args);
return newself;
}

cleanOutput() {
this._scope = {};
}
Expand Down Expand Up @@ -620,6 +628,9 @@ function typeCheckInputArgs(ast, schema, scope, classes) {

if (ast.selector.isDevice) {
let dupes = new Set;

const attrscope = scope.clone();
attrscope.cleanOutput();
for (let attr of ast.selector.attributes) {
if (dupes.has(attr.name))
throw new TypeError(`Duplicate device attribute ${attr.name}`);
Expand All @@ -628,7 +639,7 @@ function typeCheckInputArgs(ast, schema, scope, classes) {
if (VALID_DEVICE_ATTRIBUTES.indexOf(attr.name) < 0)
throw new TypeError(`Invalid device attribute ${attr.name}`);

const valueType = typeForValue(attr.value, scope);
const valueType = typeForValue(attr.value, attrscope);
if (!Type.isAssignable(valueType, Type.String, {}, false) || attr.value.isUndefined)
throw new TypeError(`Invalid type for device attribute ${attr.name}, have ${valueType}, need String`);
}
Expand Down
8 changes: 7 additions & 1 deletion test/sample.apps
Expand Up @@ -1941,11 +1941,17 @@ now => compute flat(review, author == 'bob') of @org.schema.hotel() => notify;
now => @light-bulb(name="bathroom").set_power(power=enum(on));
now => @light-bulb(name="ceiling").set_power(power=enum(on));

let query x(p_name : String) := @light-bulb(name=p_name).set_power(power=enum(on));
let action x(p_name : String) := @light-bulb(name=p_name).set_power(power=enum(on));

====

dataset @light-bulb {
action (p_name : String) := @light-bulb(name=p_name).set_power(power=enum(on))
#_[utterances=["turn on my $p_name lights"]];
}

====

// ** typecheck: expect TypeError **
// no param passing into a device attribute
now => @com.bing.web_search() => @light-bulb(name=title).set_power(power=enum(on));

0 comments on commit 0255957

Please sign in to comment.