Skip to content

Commit d180931

Browse files
committed
wip
1 parent d7708d9 commit d180931

File tree

5 files changed

+161
-55
lines changed

5 files changed

+161
-55
lines changed

macros/disabled.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,18 @@ let import = macro {
207207
}
208208
function fun(dom, rng, options) {
209209
var domStr = dom.map(function (d, idx) {
210+
if (!(d instanceof Contract)) {
211+
throw new Error(d + ' is not a contract');
212+
}
210213
return options && options.namesStr ? options.namesStr[idx] + ': ' + d : d;
211214
}).join(', ');
212215
var domName = '(' + domStr + ')';
216+
if (!(rng instanceof Contract)) {
217+
throw new Error(rng + ' is not a contract');
218+
}
213219
var rngStr = options && options.namesStr ? options.namesStr[options.namesStr.length - 1] + ': ' + rng : rng;
214-
var thisName = options && options.thisContract ? ' this ' + options.thisContract : '';
215-
var contractName = domName + thisName + ' -> ' + rngStr + (options && options.dependencyStr ? ' | ' + options.dependencyStr : '');
220+
var thisName = options && options.thisContract ? '\n | this: ' + options.thisContract : '';
221+
var contractName = domName + ' -> ' + rngStr + thisName + (options && options.dependencyStr ? ' | ' + options.dependencyStr : '');
216222
var c = new Contract(contractName, 'fun', function (blame, unwrapTypeVar) {
217223
return function (f) {
218224
blame = blame.addParents(contractName);
@@ -273,6 +279,9 @@ let import = macro {
273279
return c;
274280
}
275281
function optional(contract, options) {
282+
if (!(contract instanceof Contract)) {
283+
throw new Error(contract + ' is not a contract');
284+
}
276285
var contractName = 'opt ' + contract;
277286
return new Contract(contractName, 'optional', function (blame, unwrapTypeVar) {
278287
return function (val) {
@@ -282,6 +291,9 @@ let import = macro {
282291
});
283292
}
284293
function repeat(contract, options) {
294+
if (!(contract instanceof Contract)) {
295+
throw new Error(contract + ' is not a contract');
296+
}
285297
var contractName = '....' + contract;
286298
return new Contract(contractName, 'repeat', function (blame, unwrapTypeVar) {
287299
return function (val) {
@@ -293,6 +305,9 @@ let import = macro {
293305
function array(arrContract, options) {
294306
var proxyPrefix = options && options.proxy ? '!' : '';
295307
var contractName = proxyPrefix + '[' + arrContract.map(function (c$2) {
308+
if (!(c$2 instanceof Contract)) {
309+
throw new Error(c$2 + ' is not a contract');
310+
}
296311
return c$2;
297312
}).join(', ') + ']';
298313
var contractNum = arrContract.length;
@@ -345,6 +360,9 @@ let import = macro {
345360
var contractKeys = Object.keys(objContract);
346361
var proxyPrefix = options && options.proxy ? '!' : '';
347362
var contractName = proxyPrefix + '{' + contractKeys.map(function (prop) {
363+
if (!(objContract[prop] instanceof Contract)) {
364+
throw new Error(objContract[prop] + ' is not a contract');
365+
}
348366
return prop + ': ' + objContract[prop];
349367
}).join(', ') + '}';
350368
var keyNum = contractKeys.length;
@@ -355,11 +373,14 @@ let import = macro {
355373
}
356374
contractKeys.forEach(function (key) {
357375
if (!(objContract[key].type === 'optional' && obj[key] === undefined)) {
358-
var propProj = objContract[key].proj(blame.addLocation('the ' + key + ' property of'));
376+
// self contracts use the original object contract
377+
var c$2 = objContract[key];
378+
// var c = objContract[key].type === "self" ? this : objContract[key];
379+
var propProj = c$2.proj(blame.addLocation('the ' + key + ' property of'));
359380
var checkedProperty = propProj(obj[key]);
360381
obj[key] = checkedProperty;
361382
}
362-
});
383+
}.bind(this));
363384
if (options && options.proxy) {
364385
return new Proxy(obj, {
365386
set: function (target, key, value) {
@@ -375,11 +396,20 @@ let import = macro {
375396
} else {
376397
return obj;
377398
}
378-
};
399+
}.bind(this);
379400
});
380401
return c;
381402
}
403+
function self() {
404+
var name = 'self';
405+
}
382406
function or(left, right) {
407+
if (!(left instanceof Contract)) {
408+
throw new Error(left + ' is not a contract');
409+
}
410+
if (!(right instanceof Contract)) {
411+
throw new Error(right + ' is not a contract');
412+
}
383413
var contractName = left + ' or ' + right;
384414
return new Contract(contractName, 'or', function (blame) {
385415
return function (val) {
@@ -440,6 +470,10 @@ let import = macro {
440470
check: check,
441471
fun: fun,
442472
or: or,
473+
self: new Contract('self', 'self', function (b) {
474+
return function () {
475+
};
476+
}),
443477
repeat: repeat,
444478
optional: optional,
445479
object: object,

macros/index.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,18 @@ let import = macro {
207207
}
208208
function fun(dom, rng, options) {
209209
var domStr = dom.map(function (d, idx) {
210+
if (!(d instanceof Contract)) {
211+
throw new Error(d + ' is not a contract');
212+
}
210213
return options && options.namesStr ? options.namesStr[idx] + ': ' + d : d;
211214
}).join(', ');
212215
var domName = '(' + domStr + ')';
216+
if (!(rng instanceof Contract)) {
217+
throw new Error(rng + ' is not a contract');
218+
}
213219
var rngStr = options && options.namesStr ? options.namesStr[options.namesStr.length - 1] + ': ' + rng : rng;
214-
var thisName = options && options.thisContract ? ' this ' + options.thisContract : '';
215-
var contractName = domName + thisName + ' -> ' + rngStr + (options && options.dependencyStr ? ' | ' + options.dependencyStr : '');
220+
var thisName = options && options.thisContract ? '\n | this: ' + options.thisContract : '';
221+
var contractName = domName + ' -> ' + rngStr + thisName + (options && options.dependencyStr ? ' | ' + options.dependencyStr : '');
216222
var c = new Contract(contractName, 'fun', function (blame, unwrapTypeVar) {
217223
return function (f) {
218224
blame = blame.addParents(contractName);
@@ -273,6 +279,9 @@ let import = macro {
273279
return c;
274280
}
275281
function optional(contract, options) {
282+
if (!(contract instanceof Contract)) {
283+
throw new Error(contract + ' is not a contract');
284+
}
276285
var contractName = 'opt ' + contract;
277286
return new Contract(contractName, 'optional', function (blame, unwrapTypeVar) {
278287
return function (val) {
@@ -282,6 +291,9 @@ let import = macro {
282291
});
283292
}
284293
function repeat(contract, options) {
294+
if (!(contract instanceof Contract)) {
295+
throw new Error(contract + ' is not a contract');
296+
}
285297
var contractName = '....' + contract;
286298
return new Contract(contractName, 'repeat', function (blame, unwrapTypeVar) {
287299
return function (val) {
@@ -293,6 +305,9 @@ let import = macro {
293305
function array(arrContract, options) {
294306
var proxyPrefix = options && options.proxy ? '!' : '';
295307
var contractName = proxyPrefix + '[' + arrContract.map(function (c$2) {
308+
if (!(c$2 instanceof Contract)) {
309+
throw new Error(c$2 + ' is not a contract');
310+
}
296311
return c$2;
297312
}).join(', ') + ']';
298313
var contractNum = arrContract.length;
@@ -345,6 +360,9 @@ let import = macro {
345360
var contractKeys = Object.keys(objContract);
346361
var proxyPrefix = options && options.proxy ? '!' : '';
347362
var contractName = proxyPrefix + '{' + contractKeys.map(function (prop) {
363+
if (!(objContract[prop] instanceof Contract)) {
364+
throw new Error(objContract[prop] + ' is not a contract');
365+
}
348366
return prop + ': ' + objContract[prop];
349367
}).join(', ') + '}';
350368
var keyNum = contractKeys.length;
@@ -355,11 +373,14 @@ let import = macro {
355373
}
356374
contractKeys.forEach(function (key) {
357375
if (!(objContract[key].type === 'optional' && obj[key] === undefined)) {
358-
var propProj = objContract[key].proj(blame.addLocation('the ' + key + ' property of'));
376+
// self contracts use the original object contract
377+
var c$2 = objContract[key];
378+
// var c = objContract[key].type === "self" ? this : objContract[key];
379+
var propProj = c$2.proj(blame.addLocation('the ' + key + ' property of'));
359380
var checkedProperty = propProj(obj[key]);
360381
obj[key] = checkedProperty;
361382
}
362-
});
383+
}.bind(this));
363384
if (options && options.proxy) {
364385
return new Proxy(obj, {
365386
set: function (target, key, value) {
@@ -375,11 +396,20 @@ let import = macro {
375396
} else {
376397
return obj;
377398
}
378-
};
399+
}.bind(this);
379400
});
380401
return c;
381402
}
403+
function self() {
404+
var name = 'self';
405+
}
382406
function or(left, right) {
407+
if (!(left instanceof Contract)) {
408+
throw new Error(left + ' is not a contract');
409+
}
410+
if (!(right instanceof Contract)) {
411+
throw new Error(right + ' is not a contract');
412+
}
383413
var contractName = left + ' or ' + right;
384414
return new Contract(contractName, 'or', function (blame) {
385415
return function (val) {
@@ -440,6 +470,10 @@ let import = macro {
440470
check: check,
441471
fun: fun,
442472
or: or,
473+
self: new Contract('self', 'self', function (b) {
474+
return function () {
475+
};
476+
}),
443477
repeat: repeat,
444478
optional: optional,
445479
object: object,
@@ -476,7 +510,7 @@ macro stringify {
476510
}
477511

478512
macro base_contract {
479-
rule { $name } => { _c.$name }
513+
rule { $name } => { typeof $name !== 'undefined' ? $name : _c.$name }
480514
}
481515

482516
macroclass named_contract {
@@ -522,7 +556,7 @@ macro function_contract {
522556
dependencyStr: stringify ($guard)
523557
})
524558
}
525-
rule { ($dom:any_contract (,) ...) this $this:object_contract -> $range:any_contract } => {
559+
rule { ($dom:any_contract (,) ...) -> $range:any_contract | this $[:] $this:object_contract } => {
526560
_c.fun([$dom (,) ...], $range, {
527561
thisContract: $this
528562
})

0 commit comments

Comments
 (0)