+: ensure-arg-type-and-count ( tn tn-1 ... t2 t1 args n -- )
+ dup 0= if
+ drop nil objeq? false = if
+ recoverable-exception throw" Too many arguments for primitive procedure."
+ then
+ else
+ -rot nil? if
+ recoverable-exception throw" Too few arguments for primitive procedure."
+ then
+
+ 2dup cdr 2swap car ( ... t1 n args' arg1 )
+ 2rot 1- swap 2swap rot ( ... args' n-1 arg1 t1 )
+ istype? false = if
+ recoverable-exception throw" Incorrect type for primitive procedure."
+ then
+
+ 2drop recurse
+ then
+
+;
+
+: push-args-to-stack ( args -- arg1 arg2 ... argn )
+ begin
+ nil? false =
+ while
+ 2dup car 2swap cdr
+ repeat
+
+ 2drop
+;
+
+: add-fa-checks ( cfa n -- cfa' )
+ here current @ 1+ dup @ , !
+ 0 ,
+ here -rot
+ docol ,
+ ['] 2dup , ['] lit , , ['] ensure-arg-count ,
+ ['] push-args-to-stack ,
+ ['] lit , , ['] execute ,
+ ['] exit ,
+;
+
+: add-fa-type-checks ( cfa t1 t2 ... tn n -- cfa' )
+ here current @ 1+ dup @ , !
+ 0 ,
+ here >R
+ docol ,
+ ['] 2dup ,
+ ['] >R , ['] >R ,
+
+ dup ( cfa t1 t2 ... tn n m )
+
+ begin
+ ?dup 0>
+ while
+ rot ['] lit , , ( cfa t1 t2 ... tn-1 n m )
+ 1-
+ repeat
+
+ ['] R> , ['] R> ,
+
+ ['] lit , , ['] ensure-arg-type-and-count ,
+
+ ['] push-args-to-stack ,
+ ['] lit , , ['] execute ,
+ ['] exit ,
+
+ R>
+;
+
+: make-fa-primitive ( cfa n -- )
+ add-fa-checks make-primitive ;
+
+: make-fa-type-primitive ( cfa t1 t2 ... tn n -- )
+ add-fa-type-checks make-primitive ;
+