6
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3
|
|
4 namespace Jint.Runtime.VM
|
|
5 {
|
|
6 class FunctionBuidler
|
|
7 {
|
|
8 Scope m_parentScope;
|
|
9
|
|
10 /// <summary>
|
|
11 /// maps local variable names to the registers
|
|
12 /// </summary>
|
|
13 Dictionary<string,int> m_locals;
|
|
14
|
|
15 /// <summary>
|
|
16 /// maps formal parameters to local variables
|
|
17 /// </summary>
|
|
18 int[] m_formalParameters;
|
|
19
|
|
20 int m_frameSize;
|
|
21
|
|
22 IInstruction m_code;
|
|
23
|
|
24 RuntimeContext m_runtime;
|
|
25
|
|
26 public FunctionBuilder (string[] argumentNames, RuntimeContext runtime, Scope parentScope)
|
|
27 {
|
|
28 m_parentScope;
|
|
29 m_frameSize = 2; // reserve for this and scope
|
|
30
|
|
31 m_locals = new Dictionary<string,int>();
|
|
32
|
|
33 if (argumentNames!= null) {
|
|
34 m_formalParameters = new int[argumentNames.Length];
|
|
35 for(int i = 0; i < argumentNames.Length; i++)
|
|
36 m_formalParameters[i] = AllocVariable(argumentNames[i]);
|
|
37 }
|
|
38 }
|
|
39
|
|
40 // all vars should be allocated before temp registers
|
|
41 public int AllocVariable(string name) {
|
|
42 int id;
|
|
43
|
|
44 if (m_locals.TryGetValue (name, out id))
|
|
45 return id;
|
|
46
|
|
47 id = m_frameSize++;
|
|
48 m_locals [name] = id;
|
|
49 return id;
|
|
50 }
|
|
51
|
|
52 public int AllocateRegister() {
|
|
53 return m_frameSize++;
|
|
54 }
|
|
55
|
|
56 public void Invoke (object that, object[] args) {
|
|
57 var frame = new Frame (m_frameSize, m_runtime);
|
|
58 var scope = new Scope (m_locals, frame, m_declaringScope);
|
|
59 frame.SetValue (Frame.ThisRegister, that);
|
|
60 frame.SetValue (Frame.ScopeRegister, scope);
|
|
61
|
|
62 var paramLen = Math.Min (m_formalParameters.Length, args.Length);
|
|
63
|
|
64 for (int i=0; i< paramLen; i++)
|
|
65 frame.SetValue (m_formalParameters [i], args [i]);
|
|
66 }
|
|
67
|
|
68 }
|
|
69 }
|
|
70
|