Develop and Download Open Source Software

Browse CVS Repository

Contents of /netruby/netruby/Proc.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1.1.1 - (show annotations) (download) (vendor branch)
Mon Apr 8 13:29:48 2002 UTC (22 years ago) by arton
Branch: MAIN, vendor
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
initial version 0.8

1 /*
2 Copyright(C) 2001-2002 arton
3
4 Permission is granted for use, copying, modification, distribution,
5 and distribution of modified versions of this work as long as the
6 above copyright notice is included.
7 */
8
9 using System;
10 using System.Diagnostics;
11 using System.Collections;
12 using System.IO;
13 using System.Text;
14 using System.Reflection;
15 using System.Threading;
16 using System.Security;
17
18 namespace arton.NETRuby
19 {
20 public class RProc : RData
21 {
22 internal RProc(NetRuby rb) :
23 base(rb, rb.cProc)
24 {
25 CreateBlock(rb);
26 }
27 internal RProc(NetRuby rb, RMetaObject meta) :
28 base(rb, meta)
29 {
30 CreateBlock(rb);
31 }
32 private void CreateBlock(NetRuby rb)
33 {
34 RThread th = rb.GetCurrentContext();
35 block = th.block.Clone();
36 block.origThread = th;
37 block.wrapper = th.wrapper;
38 block.iter = (block.prev != null) ? ITER.PRE : ITER.NOT;
39 block.frame = block.frame.Dup();
40 if (th.frame.prev != null)
41 {
42 block.frame.lastFunc = th.frame.prev.lastFunc;
43 block.frame.lastClass = th.frame.prev.lastClass;
44 }
45 if (block.iter != 0)
46 {
47 block.DupPrev();
48 }
49 else
50 {
51 block.prev = null;
52 }
53 block.scope.Dup();
54 safeLevel = th.safeLevel;
55 }
56 internal int SafeLevel
57 {
58 get { return safeLevel; }
59 set {
60 if (IsTainted)
61 {
62 ruby.GetCurrentContext().safeLevel = safeLevel;
63 }
64 }
65 }
66 internal Block block;
67 internal int safeLevel;
68
69 public override string ToString()
70 {
71 string name = ruby.ClassOf(this).Name;
72 return String.Format("#<{0}:0x{1:x8}>", name, block.TagID);
73 }
74 public int Arity
75 {
76 get {
77 int n = -1;
78 if (block.var != null)
79 {
80 if (block.var is RNBlockNoArg)
81 {
82 n = 0;
83 }
84 else if (block.var is RNMAsgn)
85 {
86 RNode list = block.var.head;
87 n = 0;
88 while (list != null)
89 {
90 n++;
91 list = list.next;
92 }
93 if (block.var.args != null)
94 n = -n - 1;
95 }
96 }
97 return n;
98 }
99 }
100 public object Call(params object[] args)
101 {
102 return ruby.Call(this, args);
103 }
104 }
105
106 public class RProcClass : RClass
107 {
108 private RProcClass(NetRuby rb) :
109 base(rb, "Proc", rb.cObject)
110 {
111 }
112
113 static public RProc NewProc(object[] argv, RMetaObject meta)
114 {
115 NetRuby ruby = meta.ruby;
116 if (ruby.IsBlockGiven == false && (bool)ruby.ruby_block_given_p(null) == false)
117 {
118 throw new ArgumentException("tried to create Proc object without a block");
119 }
120 RProc proc = new RProc(ruby, meta);
121 ruby.CallInit(proc, argv);
122 return proc;
123 }
124
125 internal static object proc_new(RBasic r, params object[] args)
126 {
127 RBasic o = NewProc(args, (RMetaObject)r);
128 return o;
129 }
130 internal static object proc_call(RBasic r, params object[] args)
131 {
132 NetRuby ruby = r.ruby;
133 RThread th = ruby.GetCurrentContext();
134 if (th.IsBlockGiven && th.frame.lastFunc != 0)
135 {
136 ruby.warning(String.Format("block for {0}#{1} is useless",
137 ruby.ClassOf(r).Name,
138 ruby.id2name(th.frame.lastFunc)));
139 }
140 return ruby.Call((RProc)r, args);
141 }
142 internal static object proc_arity(RBasic r, params object[] args)
143 {
144 return ((RProc)r).Arity;
145 }
146 internal static object proc_lambda(RBasic r, params object[] args)
147 {
148 return r.ruby.Lambda();
149 }
150
151 internal static void Init(NetRuby rb)
152 {
153 RProcClass prc = new RProcClass(rb);
154 prc.DefineClass("Proc", rb.cObject);
155 rb.cProc = prc;
156 prc.DefineSingletonMethod("new", new RMethod(proc_new), -1);
157 prc.DefineMethod("call", new RMethod(proc_call), -2);
158 prc.DefineMethod("arity", new RMethod(proc_arity), 0);
159 prc.DefineMethod("[]", new RMethod(proc_call), -2);
160 rb.DefineGlobalFunction("proc", new RMethod(proc_lambda), 0);
161 rb.DefineGlobalFunction("lambda", new RMethod(proc_lambda), 0);
162 }
163 }
164 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26