-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcompile_include.rb
55 lines (47 loc) · 1.55 KB
/
compile_include.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#
# "include" in Ruby is fairly complex:
#
# * For each named object (constants, methods) in the module,
# if the object has not been defined *in the class*, it will
# be overridden.
#
# * If a method of the same name exists in the supercass, it
# will be aliased
#
# * If a method is *subsequently* defined in the module, it will
# be added to the casses that include it.
#
# Basically this is as if you inject an extra super-class between
# the class and its real super-class, similar to eigen-classes.
#
# FIXME: Verify reality priority of superclass vs. eigenclass.
#
# Biggest problem is instance variables: Instance variabes in
# a module must use access methods in the client classes.
#
# Either hash lookup, or we may consider synthesizing accessors
# and auto-add them to classes that include it. Issue is
#
# FIXME: Verify logic with multiple includes.
#
#
class Compiler
def compile_include(scope, incl)
# At this point we want to:
#
# 1. Attach scope of included module, so static constant lookups
# works (are re-directed to original class.
mscope = scope.find_constant(incl)
raise "Module not found: #{incl}" if !mscope
scope.include_module(mscope)
# 2. For each method, looks up method in current super class
# compares to current class vtable, and overwrites only
# if not matching.
# (NOT IMPLEMENTED YET)
# FIXME:
# - Ensure we handle eigenclasses properly.
# - Ensure *subsequent* changes are updated correctly.
# - Dynamic constant lookups.
Value.new([:subexpr])
end
end