@@ -207,12 +207,18 @@ let import = macro {
207
207
}
208
208
function fun ( dom , rng , options ) {
209
209
var domStr = dom . map ( function ( d , idx ) {
210
+ if ( ! ( d instanceof Contract ) ) {
211
+ throw new Error ( d + ' is not a contract' ) ;
212
+ }
210
213
return options && options . namesStr ? options . namesStr [ idx ] + ': ' + d : d ;
211
214
} ) . join ( ', ' ) ;
212
215
var domName = '(' + domStr + ')' ;
216
+ if ( ! ( rng instanceof Contract ) ) {
217
+ throw new Error ( rng + ' is not a contract' ) ;
218
+ }
213
219
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 : '' ) ;
216
222
var c = new Contract ( contractName , 'fun' , function ( blame , unwrapTypeVar ) {
217
223
return function ( f ) {
218
224
blame = blame . addParents ( contractName ) ;
@@ -273,6 +279,9 @@ let import = macro {
273
279
return c ;
274
280
}
275
281
function optional ( contract , options ) {
282
+ if ( ! ( contract instanceof Contract ) ) {
283
+ throw new Error ( contract + ' is not a contract' ) ;
284
+ }
276
285
var contractName = 'opt ' + contract ;
277
286
return new Contract ( contractName , 'optional' , function ( blame , unwrapTypeVar ) {
278
287
return function ( val ) {
@@ -282,6 +291,9 @@ let import = macro {
282
291
} ) ;
283
292
}
284
293
function repeat ( contract , options ) {
294
+ if ( ! ( contract instanceof Contract ) ) {
295
+ throw new Error ( contract + ' is not a contract' ) ;
296
+ }
285
297
var contractName = '....' + contract ;
286
298
return new Contract ( contractName , 'repeat' , function ( blame , unwrapTypeVar ) {
287
299
return function ( val ) {
@@ -293,6 +305,9 @@ let import = macro {
293
305
function array ( arrContract , options ) {
294
306
var proxyPrefix = options && options . proxy ? '!' : '' ;
295
307
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
+ }
296
311
return c$2 ;
297
312
} ) . join ( ', ' ) + ']' ;
298
313
var contractNum = arrContract . length ;
@@ -345,6 +360,9 @@ let import = macro {
345
360
var contractKeys = Object . keys ( objContract ) ;
346
361
var proxyPrefix = options && options . proxy ? '!' : '' ;
347
362
var contractName = proxyPrefix + '{' + contractKeys . map ( function ( prop ) {
363
+ if ( ! ( objContract [ prop ] instanceof Contract ) ) {
364
+ throw new Error ( objContract [ prop ] + ' is not a contract' ) ;
365
+ }
348
366
return prop + ': ' + objContract [ prop ] ;
349
367
} ) . join ( ', ' ) + '}' ;
350
368
var keyNum = contractKeys . length ;
@@ -355,11 +373,14 @@ let import = macro {
355
373
}
356
374
contractKeys . forEach ( function ( key ) {
357
375
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' ) ) ;
359
380
var checkedProperty = propProj ( obj [ key ] ) ;
360
381
obj [ key ] = checkedProperty ;
361
382
}
362
- } ) ;
383
+ } . bind ( this ) ) ;
363
384
if ( options && options . proxy ) {
364
385
return new Proxy ( obj , {
365
386
set : function ( target , key , value ) {
@@ -375,11 +396,20 @@ let import = macro {
375
396
} else {
376
397
return obj ;
377
398
}
378
- } ;
399
+ } . bind ( this ) ;
379
400
} ) ;
380
401
return c ;
381
402
}
403
+ function self ( ) {
404
+ var name = 'self' ;
405
+ }
382
406
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
+ }
383
413
var contractName = left + ' or ' + right ;
384
414
return new Contract ( contractName , 'or' , function ( blame ) {
385
415
return function ( val ) {
@@ -440,6 +470,10 @@ let import = macro {
440
470
check : check ,
441
471
fun : fun ,
442
472
or : or ,
473
+ self : new Contract ( 'self' , 'self' , function ( b ) {
474
+ return function ( ) {
475
+ } ;
476
+ } ) ,
443
477
repeat : repeat ,
444
478
optional : optional ,
445
479
object : object ,
@@ -476,7 +510,7 @@ macro stringify {
476
510
}
477
511
478
512
macro base_contract {
479
- rule { $name } => { _c . $name }
513
+ rule { $name } => { typeof $name !== 'undefined' ? $name : _c . $name }
480
514
}
481
515
482
516
macroclass named_contract {
@@ -522,7 +556,7 @@ macro function_contract {
522
556
dependencyStr : stringify ( $guard )
523
557
} )
524
558
}
525
- rule { ( $dom :any_contract ( , ) . . . ) this $this : object_contract - > $range :any_contract } => {
559
+ rule { ( $dom :any_contract ( , ) . . . ) - > $range :any_contract | this $ [ : ] $this : object_contract } => {
526
560
_c . fun ( [ $dom ( , ) . . . ] , $range , {
527
561
thisContract : $this
528
562
} )
0 commit comments