Skip to content
Permalink
Browse files

Merge pull request #539 from krekoten/method_arity

Method arity almost completed
  • Loading branch information
alex committed Mar 22, 2013
2 parents 8d8e46c + 5ccbc90 commit e59876b7997d45d8c92cfedf1fb3fc0eebf6d07a
@@ -1,6 +1 @@
fails:Method#arity returns n, where n is the number of required arguments, when there are zero or more required arguments only
fails:Method#arity returns n, where n is the number of required arguments and method created via defined_method
fails:Method#arity returns -(n+1), where n is the number of required arguments, when there is at least one optional argument
fails:Method#arity returns -(n+1), where n is the number of required arguments, when there is a splat argument, regardless of optional arguments
fails:Method#arity returns the same value regardless of the presence of a block
fails:Method#arity for a Method generated by respond_to_missing? returns -1
@@ -17,6 +17,9 @@ def __deepcopy__(self, memo):
obj.w_class = copy.deepcopy(self.w_class, memo)
return obj

def arity(self, space):
return space.newint(0)


class W_UserFunction(W_FunctionObject):
_immutable_fields_ = ["bytecode", "lexical_scope"]
@@ -43,6 +46,13 @@ def call(self, space, w_receiver, args_w, block):
frame.handle_args(space, self.bytecode, args_w, block)
return space.execute_frame(frame, self.bytecode)

def arity(self, space):
args_count = len(self.bytecode.arg_pos) - len(self.bytecode.defaults)
if len(self.bytecode.defaults) > 0 or self.bytecode.splat_arg_pos != -1:
args_count = -(args_count + 1)

return space.newint(args_count)


class W_BuiltinFunction(W_FunctionObject):
_immutable_fields_ = ["func"]
@@ -1,4 +1,7 @@
from topaz.module import ClassDef
from topaz.objects.blockobject import W_BlockObject
from topaz.objects.functionobject import W_UserFunction
from topaz.objects.moduleobject import AttributeWriter, DefineMethodBlock
from topaz.objects.objectobject import W_Object


@@ -58,6 +61,10 @@ def method_eql(self, space, w_other):
else:
return space.w_false

@classdef.method("arity")
def method_arity(self, space):
return self.w_function.arity(space)


class W_UnboundMethodObject(W_Object):
classdef = ClassDef("UnboundMethod", W_Object.classdef, filepath=__file__)
@@ -30,6 +30,9 @@ def call(self, space, w_obj, args_w, block):
space.set_instance_var(w_obj, self.varname, w_value)
return w_value

def arity(self, space):
return space.newint(1)


class UndefMethod(W_FunctionObject):
_immutable_fields_ = ["name"]
@@ -53,6 +56,13 @@ def call(self, space, w_obj, args_w, block):
method_block = self.block.copy(w_self=w_obj)
return space.invoke_block(method_block, args_w, block)

def arity(self, space):
args_count = len(self.block.bytecode.arg_pos) - len(self.block.bytecode.defaults)
if len(self.block.bytecode.defaults) > 0 or self.block.bytecode.splat_arg_pos != -1:
args_count = -(args_count + 1)

return space.newint(args_count)


class DefineMethodMethod(W_FunctionObject):
_immutable_fields_ = ["name", "w_unbound_method"]

0 comments on commit e59876b

Please sign in to comment.