mirror of
				https://github.com/rubyjs/therubyracer
				synced 2023-03-27 23:21:42 -04:00 
			
		
		
		
	extract the function tracking into its own separate module.
This commit is contained in:
		
							parent
							
								
									39ef745e97
								
							
						
					
					
						commit
						9f0f36db9b
					
				
					 3 changed files with 45 additions and 36 deletions
				
			
		| 
						 | 
					@ -42,7 +42,9 @@ module V8
 | 
				
			||||||
      when Symbol
 | 
					      when Symbol
 | 
				
			||||||
        C::String::NewSymbol(value.to_s)
 | 
					        C::String::NewSymbol(value.to_s)
 | 
				
			||||||
      when Proc,Method,UnboundMethod
 | 
					      when Proc,Method,UnboundMethod
 | 
				
			||||||
        @functions[value]
 | 
					        @proxies.rb2js(value) do
 | 
				
			||||||
 | 
					          @functions.to_function(value).function
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
      when ::Array
 | 
					      when ::Array
 | 
				
			||||||
        C::Array::New(value.length).tap do |a|
 | 
					        C::Array::New(value.length).tap do |a|
 | 
				
			||||||
          value.each_with_index do |item, i|
 | 
					          value.each_with_index do |item, i|
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,32 +3,38 @@ module V8
 | 
				
			||||||
    class Functions
 | 
					    class Functions
 | 
				
			||||||
      def initialize(portal)
 | 
					      def initialize(portal)
 | 
				
			||||||
        @portal = portal
 | 
					        @portal = portal
 | 
				
			||||||
        @procs, @methods = {},{}
 | 
					        @procs = {}
 | 
				
			||||||
 | 
					        @methods = {}
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      def [](code)
 | 
					      def to_function(code)
 | 
				
			||||||
        self.send(code.class.name, code)
 | 
					        case code
 | 
				
			||||||
 | 
					        when Method, UnboundMethod
 | 
				
			||||||
 | 
					          #TODO: clear this reference when the C::FunctionTemplate becomes weak.
 | 
				
			||||||
 | 
					          @methods[code.to_s] ||= Template.new(@portal, code.kind_of?(Method) ? :invoke_callable : :invoke_unbound_method, code)
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					          if code.respond_to?(:call)
 | 
				
			||||||
 | 
					            #TODO: clear with reference when the C::FunctionTemplate becomes weak.
 | 
				
			||||||
 | 
					            @procs[code] ||= Template.new(@portal, :invoke_callable, code)
 | 
				
			||||||
 | 
					          else
 | 
				
			||||||
 | 
					            fail "invalid code type: #{code.class}"
 | 
				
			||||||
          end
 | 
					          end
 | 
				
			||||||
 | 
					 | 
				
			||||||
      def Proc(p)
 | 
					 | 
				
			||||||
        #TODO: check this for memory leaks
 | 
					 | 
				
			||||||
        @procs[p] ||= begin
 | 
					 | 
				
			||||||
          template = C::FunctionTemplate::New(method(:callproc), p)
 | 
					 | 
				
			||||||
          template.GetFunction()
 | 
					 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      def UnboundMethod(method)
 | 
					      class Template
 | 
				
			||||||
        #TODO: check this for memory leaks.
 | 
					        attr_reader :template, :function
 | 
				
			||||||
        @methods[method.to_s] ||= begin
 | 
					
 | 
				
			||||||
          template = C::FunctionTemplate::New(method(:callmethod), method)
 | 
					        def initialize(portal, impl, code)
 | 
				
			||||||
          template.GetFunction()
 | 
					          @portal = portal
 | 
				
			||||||
        end
 | 
					          @template = V8::C::FunctionTemplate::New(method(impl), code)
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      alias_method :Method, :Proc
 | 
					        def function
 | 
				
			||||||
 | 
					          @template.GetFunction()
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      def callproc(arguments)
 | 
					        def invoke_callable(arguments)
 | 
				
			||||||
          proc = arguments.Data()
 | 
					          proc = arguments.Data()
 | 
				
			||||||
          rbargs = []
 | 
					          rbargs = []
 | 
				
			||||||
          for i in 0..arguments.Length() - 1
 | 
					          for i in 0..arguments.Length() - 1
 | 
				
			||||||
| 
						 | 
					@ -37,7 +43,7 @@ module V8
 | 
				
			||||||
          @portal.rubycall(proc, *rbargs)
 | 
					          @portal.rubycall(proc, *rbargs)
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      def callmethod(arguments)
 | 
					        def invoke_unbound_method(arguments)
 | 
				
			||||||
          method = arguments.Data()
 | 
					          method = arguments.Data()
 | 
				
			||||||
          rbargs = []
 | 
					          rbargs = []
 | 
				
			||||||
          for i in 0..arguments.Length() - 1
 | 
					          for i in 0..arguments.Length() - 1
 | 
				
			||||||
| 
						 | 
					@ -51,3 +57,4 @@ module V8
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
| 
						 | 
					@ -1 +1 @@
 | 
				
			||||||
Subproject commit 8ea1e0d6b9e0a77b026b1b5d80a39c8eff486b92
 | 
					Subproject commit ff7bd687925baf2c207a987c9dc973452236415f
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue