Skip to content
Permalink
Browse files

Merge pull request #692 from krekoten/singleton_of_immidiates

Singleton of Numeric and Symbol
  • Loading branch information
alex committed May 23, 2013
2 parents a1bebbc + bb74573 commit 0a38e9b7bbaa35414b5536d4a9092bb92afafbf1

This file was deleted.

@@ -480,13 +480,23 @@ def DEFINE_FUNCTION(self, space, bytecode, frame, pc):
w_name = frame.pop()
w_scope = frame.pop()
assert isinstance(w_func, W_FunctionObject)
# None is special case. It means that we are trying to define
# a method on Symbol or Numeric.
if w_scope is None:
raise space.error(space.w_TypeError,
'can\'t define singleton method "%s" for %s' % (
space.symbol_w(w_name), space.getclass(frame.w_self).name
)
)
w_scope.define_method(space, space.symbol_w(w_name), w_func)
frame.push(space.w_nil)

def ATTACH_FUNCTION(self, space, bytecode, frame, pc):
w_func = frame.pop()
w_name = frame.pop()
w_obj = frame.pop()
if space.is_kind_of(w_obj, space.w_symbol) or space.is_kind_of(w_obj, space.w_numeric):
raise space.error(space.w_TypeError, "no class/module to add method")
assert isinstance(w_func, W_FunctionObject)
w_obj.attach_method(space, space.symbol_w(w_name), w_func)
frame.push(space.w_nil)
@@ -508,6 +518,8 @@ def EVALUATE_MODULE(self, space, bytecode, frame, pc):

def LOAD_SINGLETON_CLASS(self, space, bytecode, frame, pc):
w_obj = frame.pop()
if space.is_kind_of(w_obj, space.w_symbol) or space.is_kind_of(w_obj, space.w_fixnum):
raise space.error(space.w_TypeError, "can't define singleton")
frame.push(space.getsingletonclass(w_obj))

def SEND(self, space, bytecode, frame, pc, meth_idx, num_args):
@@ -51,9 +51,6 @@ def bigint_w(self, space):
def float_w(self, space):
return float(self.intvalue)

def getsingletonclass(self, space):
raise space.error(space.w_TypeError, "can't define singleton")

def find_instance_var(self, space, name):
storage = space.fromcache(FixnumStorage).get_or_create(space, self.intvalue)
return storage.find_instance_var(space, name)
@@ -62,6 +59,11 @@ def set_instance_var(self, space, name, w_value):
storage = space.fromcache(FixnumStorage).get_or_create(space, self.intvalue)
storage.set_instance_var(space, name, w_value)

@classdef.method("extend")
@classdef.method("singleton_class")
def method_singleton_class(self, space):
raise space.error(space.w_TypeError, "can't define singleton")

@classdef.method("inspect")
@classdef.method("to_s")
def method_to_s(self, space):
@@ -114,11 +114,17 @@ def method_singleton_method_undefined(self, space, w_name):
def method_instance_exec(self, space, args_w, block):
if block is None:
raise space.error(space.w_LocalJumpError, "no block given")

if space.is_kind_of(self, space.w_symbol) or space.is_kind_of(self, space.w_numeric):
self_klass = None
else:
self_klass = space.getsingletonclass(self)

return space.invoke_block(
block.copy(
space,
w_self=self,
lexical_scope=StaticScope(space.getsingletonclass(self), block.lexical_scope)
lexical_scope=StaticScope(self_klass, block.lexical_scope)
),
args_w
)
@@ -23,13 +23,15 @@ def symbol_w(self, space):
def str_w(self, space):
return self.symbol

def getsingletonclass(self, space):
raise space.error(space.w_TypeError, "can't define singleton")

@classdef.singleton_method("all_symbols")
def singleton_method_all_symbols(self, space):
return space.newarray(space.symbol_cache.values())

@classdef.method("extend")
@classdef.method("singleton_class")
def method_singleton_class(self, space):
raise space.error(space.w_TypeError, "can't define singleton")

@classdef.method("to_s")
def method_to_s(self, space):
return space.newstr_fromstr(self.symbol)

0 comments on commit 0a38e9b

Please sign in to comment.