package com.trifork.hotruby.interp;

import com.trifork.hotruby.runtime.CallContext;
import com.trifork.hotruby.runtime.ConstantAccessor;
import com.trifork.hotruby.runtime.Global;
import com.trifork.hotruby.runtime.MetaModule;
import com.trifork.hotruby.runtime.RubyMethod;
import com.trifork.hotruby.runtime.RubyMethodAccessor;
import com.trifork.hotruby.runtime.RubyRuntime;
import com.trifork.hotruby.runtime.Selector;
import com.trifork.hotruby.runtime.ThreadState.ModuleFrame;

public class LexicalBindings implements CallContext {
	
	public Global[] globals;
	public ConstantAccessor[] constants;

	// selectors are customized for call context
	final Selector[] selectors;
	private final MetaModule lexical_context;
	
	// we need the frame to resolve constants correctly
	private final ModuleFrame frame;
	private RubyMethodAccessor super_method;

	public LexicalBindings(ISeq iseq, MetaModule lexical_context, ModuleFrame frame, boolean self_is_module) {
		this.lexical_context = lexical_context;
		this.frame = frame;
		globals = iseq.getGlobals(lexical_context.getRuntime());
		constants = iseq.getConstants(lexical_context, frame);
		selectors = prepare_selectors(iseq.getRuntime(), 
				iseq.getSelectors());
		super_method = iseq.getSuperMethod(lexical_context, self_is_module);
	}
	
	public ModuleFrame getModuleStack() {
		return frame;
	}


	private Selector[] prepare_selectors(RubyRuntime runtime, String[] names) {
		Selector[] result = new Selector[names.length];
		for (int i = 0; i < names.length; i++) {
			result[i] = lexical_context.getSelector(names[i]);
		}
		return result;
	}


	public MetaModule getCaller() {
		return lexical_context;
	}


	public RubyMethod get_super_method() {
		return this.super_method.get();
	}

}

@@ -327,56 +327,8 @@
 or;%0A
-import com.trifork.hotruby.runtime.RubyRuntime;%0A
 impo
@@ -364,24 +364,24 @@
 e.Selector;%0A
+
 import com.t
@@ -483,18 +483,16 @@
 ntext %7B%0A
-%09%0A
 %09public 
@@ -1122,32 +1122,8 @@
 ors(
-iseq.getRuntime(), %0A%09%09%09%09
 iseq
@@ -1275,17 +1275,16 @@
 me;%0A%09%7D%0A%0A
-%0A
 %09private
@@ -1317,29 +1317,8 @@
 ors(
-RubyRuntime runtime, 
 Stri
@@ -1499,25 +1499,24 @@
 result;%0A%09%7D%0A%0A
-%0A
 %09public Meta
@@ -1566,17 +1566,16 @@
 xt;%0A%09%7D%0A%0A
-%0A
 %09public 
@@ -1640,14 +1640,13 @@
 .get();%0A
+
 %09%7D%0A
-%0A
 %7D%0A

Bare lidt oprydning.