Skip to content
Permalink
Browse files

Merge pull request #697 from krekoten/module_constants

Module#constants and Module.constants
  • Loading branch information
alex committed May 11, 2013
2 parents 77ef2dc + 2180504 commit 3a25b8417e734cac29f70fa49ef873a87a63c61f
Showing with 44 additions and 9 deletions.
  1. +0 −7 spec/tags/core/module/constants_tags.txt
  2. +12 −0 topaz/objects/classobject.py
  3. +32 −2 topaz/objects/moduleobject.py
@@ -1,8 +1 @@
fails:Module.constants returns an array of the names of all toplevel constants
fails:Module.constants returns an array of Symbol names
fails:Module.constants returns Module's constants when given a parameter
fails:Module#constants returns all constants including inherited when passed true
fails:Module#constants returns all constants including inherited when passed some object
fails:Module#constants doesn't returns inherited constants when passed false
fails:Module#constants doesn't returns inherited constants when passed nil
fails:Module#constants returns only public constants
@@ -50,6 +50,18 @@ def find_const(self, space, name):
w_res = self.superclass.find_const(space, name)
return w_res

def inherited_constants(self, space):
consts = {}
for const in W_ModuleObject.local_constants(self, space):
consts[const] = None
w_cls = self.superclass
while w_cls is not None:
for const in w_cls.local_constants(space):
consts[const] = None
w_cls = w_cls.superclass

return consts.keys()

def find_method(self, space, name):
method = W_ModuleObject.find_method(self, space, name)
if method is None and self.superclass is not None:
@@ -191,6 +191,25 @@ def included_constants(self, space):
consts[const] = None
return consts.keys()

def lexical_constants(self, space):
consts = {}
frame = space.getexecutioncontext().gettoprubyframe()
scope = frame.lexical_scope

while scope is not None:
assert isinstance(scope, W_ModuleObject)
for const in scope.w_mod.constants_w:
consts[const] = None
scope = scope.backscope

return consts.keys()

def local_constants(self, space):
return self.constants_w.keys()

def inherited_constants(self, space):
return self.local_constants(space)

def find_local_const(self, space, name):
return self._find_const_pure(name, self.version)

@@ -474,8 +493,19 @@ def method_private_constant(self, space, args_w):
pass

@classdef.method("constants")
def method_constants(self, space):
return space.newarray([space.newsymbol(n) for n in self.included_constants(space)])
def method_constants(self, space, w_inherit=None):
if self is space.w_module and w_inherit is None:
consts = {}
for const in self.lexical_constants(space):
consts[const] = None
for const in self.inherited_constants(space):
consts[const] = None
return space.newarray([space.newsymbol(n) for n in consts.keys()])

if w_inherit is None or space.is_true(w_inherit):
return space.newarray([space.newsymbol(n) for n in self.included_constants(space)])
else:
return space.newarray([space.newsymbol(n) for n in self.constants_w.keys()])

@classdef.method("const_missing", name="symbol")
def method_const_missing(self, space, name):

0 comments on commit 3a25b84

Please sign in to comment.