Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions appinventor/schemekit/src/SCMProcedure.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithProcedure:(pic_value)procedure interpreter:(SCMInterpreter *)interpreter;
- (nullable id<SCMValue>)invoke;
- (nullable id<SCMValue>)invokeWithArguments:(NSArray<id> * _Nonnull)arguments;
- (NSInteger)numberOfArguments;
- (BOOL)variadic;

@property(readonly) NSInteger numberOfArguments;
@property(readonly) BOOL variadic;

@end

Expand Down
19 changes: 19 additions & 0 deletions appinventor/schemekit/src/SCMProcedure.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// http://www.apache.org/licenses/LICENSE-2.0

#import "SCMProcedure.h"
#import "picrin.h"
#include "picrin/private/object.h"
#include "picrin/private/vm.h"
#import "SCMInterpreter-Private.h"

@interface SCMProcedure () {
Expand Down Expand Up @@ -90,4 +93,20 @@ - (nonnull id)copyWithZone:(nullable NSZone *)zone {
return [interpreter_ apply:self.value to:arguments];
}

- (NSInteger)numberOfArguments {
if (pic_type(interpreter_.state, value_) == PIC_TYPE_IREP) {
struct proc *proc = pic_proc_ptr(interpreter_.state, value_);
return (NSInteger) proc->u.i.irep->argc - 1;
}
return 0;
}

- (BOOL)variadic {
if (pic_type(interpreter_.state, value_) == PIC_TYPE_IREP) {
struct proc *proc = pic_proc_ptr(interpreter_.state, value_);
return proc->u.i.irep->varg;
}
return NO;
}

@end
18 changes: 18 additions & 0 deletions appinventor/schemekit/tests/SCMProcedureTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,22 @@ - (void)testApplyWithArgs {
XCTAssertEqual(4, result.intValue);
}

- (void)testNumArgs {
[interpreter evalForm:@"(yail:invoke *test-environment* 'setObject:forKey: (lambda (x y) (+ x y)) \"proc3\")"];
XCTAssertNil(interpreter.exception);
SCMProcedure *proc = (SCMProcedure *) env[@"proc3"];
XCTAssertNotNil(proc);
XCTAssertEqual(2, proc.numberOfArguments);
XCTAssertFalse(proc.variadic);
}

- (void)testVariadic {
[interpreter evalForm:@"(yail:invoke *test-environment* 'setObject:forKey: (lambda (x y . z) #f) \"proc4\")"];
XCTAssertNil(interpreter.exception);
SCMProcedure *proc = (SCMProcedure *) env[@"proc4"];
XCTAssertNotNil(proc);
XCTAssertEqual(2, proc.numberOfArguments);
XCTAssertTrue([proc variadic]);
}

@end