File: LateBinding.cs

package info (click to toggle)
mono 2.6.7-5.1%2Bdeb6u2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 327,392 kB
  • ctags: 413,647
  • sloc: cs: 2,472,296; xml: 1,768,594; ansic: 350,669; sh: 13,676; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (669 lines) | stat: -rw-r--r-- 19,325 bytes parent folder | download | duplicates (5)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
//
// LateBinding.cs:
//
// Author:
//	Cesar Lopez Nataren (cesar@ciencias.unam.mx)
//
// (C) 2003, Cesar Lopez Nataren
// (C) 2005, Novell Inc, (http://novell.com)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Reflection;
using System.Diagnostics;
using Microsoft.JScript.Vsa;
using System.Collections;
using System.Text.RegularExpressions;

namespace Microsoft.JScript {

	public sealed class LateBinding {

		public object obj;
		private static BindingFlags bind_flags = BindingFlags.Public;
		private string right_hand_side;

		public LateBinding (string name)
		{
			this.right_hand_side = name;
		}


		public LateBinding (string name, object obj)
		{
			this.right_hand_side = name;
			this.obj = obj;
		}

		[DebuggerStepThroughAttribute]
		[DebuggerHiddenAttribute]
		public object Call (object [] arguments, bool construct, bool brackets,
					VsaEngine engine)
		{
			object fun = GetObjectProperty (obj, right_hand_side);

			if (fun == null)
				Console.WriteLine ("Call: No function for {0} ({1}).{2}", obj, obj.GetType (), right_hand_side);
			return CallValue (obj, fun, arguments, construct, brackets, engine);
		}

		internal static string MapToInternalName (string name)
		{
			switch (name) {
			case "lastIndexOf":
				return "lastIndexOfGood";
			case "__proto__":
				return "proto";
			default:
				return name.Replace ("$", "dollar_");
			}
		}

		internal static object MapToExternalName (string name)
		{
			switch (name) {
			case "lastIndexOfGood":
				return "lastIndexOf";
			case "proto":
				return "__proto__";
			default:
				return name.Replace ("dollar_", "$");
			}
		}

		internal static void GetMethodFlags (MethodInfo method, out bool has_engine, out bool has_var_args, out bool has_this)
		{
			//
			// Very hackish. It would be better if the
			// anonymous methods could be decorated with the right attributes.
			//
			if (method.DeclaringType.IsSubclassOf (typeof (GlobalScope))) {
				has_engine = true;
				has_var_args = false;
				has_this = true;
				return;
			}

			JSFunctionAttribute [] custom_attrs = (JSFunctionAttribute [])
				method.GetCustomAttributes (typeof (JSFunctionAttribute), true);
			//
			// We need to iterate through the JSFunctionAttributes to find out whether the function wants
			// to get passed the vsaEngine or not so we can pass the right arguments to it.
			//
			has_engine = false;
			has_var_args = false;
			has_this = false;
			foreach (JSFunctionAttribute attr in custom_attrs) {
				JSFunctionAttributeEnum flags = attr.GetAttributeValue ();
				if ((flags & JSFunctionAttributeEnum.HasEngine) != 0)
					has_engine = true;
				if ((flags & JSFunctionAttributeEnum.HasVarArgs) != 0)
					has_var_args = true;
				if ((flags & JSFunctionAttributeEnum.HasThisObject) != 0)
					has_this = true;
			}
		}

		internal static int GetRequiredArgumentCount (MethodInfo method)
		{
			bool has_engine, has_var_args, has_this;
			GetMethodFlags (method, out has_engine, out has_var_args, out has_this);
			return GetRequiredArgumentCount (method.GetParameters ().Length, has_engine, has_var_args, has_this);
		}

		private static int GetRequiredArgumentCount (int argc, bool has_engine, bool has_var_args, bool has_this)
		{
			if (has_this)
				argc--;
			if (has_engine)
				argc--;
			if (has_var_args)
				argc--;
			return argc;
		}

		internal static object [] assemble_args (object obj, MethodInfo method, object [] arguments, VsaEngine engine)
		{
			bool has_engine, has_var_args, has_this;
			GetMethodFlags (method, out has_engine, out has_var_args, out has_this);

			ParameterInfo [] args = method.GetParameters ();
			int total_argc = args.Length;
			int req_argc = GetRequiredArgumentCount (total_argc, has_engine, has_var_args, has_this);
			Type [] arg_types = new Type [req_argc];
			// var args are not at the beginning of the argument list so we need to adjust the index
			int j = total_argc - req_argc - (has_var_args ? 1 : 0);

			for (int i = 0; i < req_argc; i++, j++)
				arg_types [i] = args [j].ParameterType;

			return assemble_args (obj, has_engine, has_var_args, has_this, arg_types, arguments, engine);
		}

		internal static object [] assemble_args (object obj, bool has_engine, bool has_var_args, bool has_this,
			Type [] arg_types, object [] arguments, VsaEngine engine)
		{
			ArrayList arg_list = new ArrayList (arguments);
			int req_argc = arg_types.Length;
			int missing_args = req_argc - arg_list.Count;

			// Add missing args
			for (int i = 0; i < missing_args; i++)
				arg_list.Add (null);

			// Convert types of argument to match method signature if necessary
			for (int i = 0; i < req_argc; i++) {
				Type arg_type = arg_types [i];
				object arg = arg_list [i];
				if (!arg_type.IsInstanceOfType (arg)) {
					object new_arg = null;
					if (arg_type == typeof (object)) {
						if (arg != null && arg != DBNull.Value)
							new_arg = Convert.ToObject (arg, engine);
						else
							new_arg = arg;
					} else if (arg_type == typeof (double))
						new_arg = Convert.ToNumber (arg);
					else if (arg_type == typeof (string))
						new_arg = Convert.ToString (arg);
					else {
						Console.WriteLine ("assemble_args: Can not convert to type {0}", arg_type);
						throw new NotImplementedException ();
					}

					arg_list [i] = new_arg;
				}
			}

			if (!has_var_args) {
				// Remove unneeded args
				int added_args = -missing_args;
				/*if (added_args > 0)
					Console.WriteLine ("warning JS1148: There are too many arguments. The extra arguments will be ignored");*/
				for (int i = 0; i < added_args; i++)
					arg_list.RemoveAt (arg_list.Count - 1);
			} else {
				int va_idx = req_argc;
				if (!has_this)
					va_idx--;
				int va_count = arg_list.Count - va_idx;

				object [] var_args = new object [va_count];

				int j = va_idx;
				object arg;
				for (int i = 0; i < va_count; i++, j++) {
					arg = arg_list [j];
					if (arg != null)
						var_args [i] = arg;
				}

				arg_list.RemoveRange (va_idx, va_count);
				arg_list.Add (var_args);
			}

			return build_args (obj, arg_list.ToArray (), engine, has_engine, has_this);
		}

		internal static object [] build_args (object obj, object [] arguments, VsaEngine engine,
			bool has_engine, bool has_this)
		{
			ArrayList args = new ArrayList ();
			if (has_this)
				args.Add (obj);
			if (has_engine)
				args.Add (engine);
			foreach (object o in arguments)
				args.Add (o);
			return args.ToArray ();
		}

		[DebuggerStepThroughAttribute]
		[DebuggerHiddenAttribute]
		public static object CallValue (object thisObj, object val, object [] arguments,
						bool construct, bool brackets, VsaEngine engine)
		{
			if (construct) {
				if (brackets) {
				}

				if (val is Closure)
					return ((Closure) val).func.CreateInstance (arguments);
				else if (val is FunctionObject)
					return ((FunctionObject) val).CreateInstance (arguments);
			} else if (brackets) {
				object first_arg = arguments.Length > 0 ? arguments [0] : null;
				return GetObjectProperty ((ScriptObject) Convert.ToObject (val, engine), Convert.ToString (first_arg));
			} else {
				if (val is Closure)
					return ((Closure) val).func.Invoke (thisObj, arguments);
				else if (val is FunctionObject)
					return ((FunctionObject) val).Invoke (thisObj, arguments);
				else if (val is RegExpObject) {
					object first_arg = arguments.Length > 0 ? arguments [0] : null;
					return RegExpPrototype.exec (val, first_arg);
				} else
					return null;
			}

			Console.WriteLine ("CallValue: construct = {0}, brackets = {1}, this = {2}, val = {3} ({4}), arg[0] = {5}",
				construct, brackets, thisObj.GetType (), val, val.GetType (), arguments [0]);
			throw new NotImplementedException ();
		}

		[DebuggerStepThroughAttribute]
		[DebuggerHiddenAttribute]
		public static object CallValue2 (object val, object thisObj, object [] arguments,
						 bool construct, bool brackets, VsaEngine engine)
		{
			throw new NotImplementedException ();
		}


		public bool Delete ()
		{
			// We are currently only calling LateBinding.Delete where we know that the
			// properties can't be deleted.
			return false;
		}


		public static bool DeleteMember (object obj, string name)
		{
			if (obj is Closure)
				obj = ((Closure) obj).func;

			ScriptObject js_obj = obj as ScriptObject;
			if (js_obj != null) {
				// Numeric index on Array?
				uint index;
				if (js_obj is ArrayObject && IsArrayIndex (name, out index)) {
					if (js_obj.elems.ContainsKey (index)) {
						js_obj.elems.Remove (index);
						// Note: It appears that deleting an element should never update length
						return true;
					}
				}

				if (js_obj.elems.ContainsKey (name)) {
					InvalidateCacheEntry (js_obj, name);
					js_obj.elems.Remove (name);
					return true;
				}
			}
 
			return false;
		}

		[DebuggerStepThroughAttribute]
		[DebuggerHiddenAttribute]
		public object GetNonMissingValue ()
		{
			return GetObjectProperty ((ScriptObject) obj, right_hand_side);
		}

		[DebuggerStepThroughAttribute]
		[DebuggerHiddenAttribute]
		public object GetValue2 ()
		{
			throw new NotImplementedException ();
		}

		[DebuggerStepThroughAttribute]
		[DebuggerHiddenAttribute]
		public static void SetIndexedPropertyValueStatic (object obj, object [] arguments,
								  object value)
		{
			foreach (object key in arguments)
				DirectSetObjectProperty (obj, Convert.ToString (key), value);
		}

		[DebuggerStepThroughAttribute]
		[DebuggerHiddenAttribute]
		public void SetValue (object value)
		{
			//
			// FIXME: We should look for native properties in the prototype chain before we
			// create a new property in the object itself.
			//
			DirectSetObjectProperty ((ScriptObject) obj, right_hand_side, value);
		}

		private static void DirectSetObjectProperty (object obj, string rhs, object value)
		{
			ArrayObject ary = obj as ArrayObject;
			StringObject str = obj as StringObject;
			ScriptObject js_obj = obj as ScriptObject;
			bool is_js_obj = js_obj != null;

			if (is_js_obj)
				InvalidateCacheEntry (js_obj, rhs);

			// Numeric index?
			uint index;
			if (IsArrayIndex (rhs, out index)) {
				if (ary != null) {
					Hashtable elems = ary.elems;
					bool had_value = elems.ContainsKey (index);
					elems [index] = value;
					if (!had_value)
						AdjustArrayLength (ary, index);
					return;
				} else if (str != null) {
					return;
				}
			}
			if (!TrySetNativeProperty (obj, rhs, value) && is_js_obj) {
				FieldInfo field = js_obj.GetField (rhs);
				if (field == null)
					field = js_obj.AddField (rhs);
				field.SetValue (rhs, value);
			}
		}

		private static void AdjustArrayLength (ArrayObject ary, uint index)
		{
			uint old_len = (uint) ary.length;
			if (index > 0 && index != 4294967295 && index > old_len)
				ary.length = index + 1;
		}

		private static bool TrySetNativeProperty (object obj, string key, object value)
		{
			key = MapToInternalName (key);
			// * seems to be a wilcard inside member names
			if (key.IndexOf ('*') != -1)
				return false;

			if (obj is Closure)
				obj = ((Closure) obj).func;

			Type type = (obj is GlobalScope) ? typeof (GlobalObject) : obj.GetType ();
			MemberInfo [] members = type.GetMember (key);

			if (members.Length != 0) {
				MemberInfo member = members [0];

				switch (member.MemberType) {
				case MemberTypes.Property:
					MethodInfo method = ((PropertyInfo) member).GetSetMethod ();
					if (method == null) {
						return true;
						// Strict behavior: /fast+
						//throw new JScriptException (JSError.AssignmentToReadOnly, type + ":" + key);
					}
					method.Invoke (obj, new object [] { value });
					return true;

				case MemberTypes.Method:
					break;

				default:
					Console.WriteLine ("TrySetNativeProperty: Unsupported member type {0} for {1}.{2}",
						member.MemberType, obj, key);
					break;
				}
			}

			return false;
		}

		private static object GetObjectProperty (object obj, string key)
		{
			return GetObjectProperty (obj, key, 0);
		}

		private static object GetObjectProperty (object obj, string key, int nesting)
		{
			object result;

			ScriptObject prop_obj;
			ScriptObject js_obj = obj as ScriptObject;
			bool is_js_obj = js_obj != null;
			if (is_js_obj && nesting > 0 && GetCacheEntry (out prop_obj, js_obj, key)) {
				if (prop_obj == null)
					return null;

				if (TryDirectGetObjectProperty (out result, prop_obj, key))
					return result;
			}

			if (TryDirectGetObjectProperty (out result, obj, key)) {
				if (is_js_obj && nesting > 0)
					SetCacheEntry (js_obj, key, js_obj);
				return result;
			}

			if (is_js_obj) {
				ScriptObject cur_obj = js_obj.proto as ScriptObject;
				if (cur_obj != null) {
					result = GetObjectProperty (cur_obj, key, nesting + 1);
					if (nesting > 0)
						SetCacheEntry (js_obj, key, cur_obj);
					return result;
				}

				if (nesting > 0)
					SetCacheEntry (js_obj, key, null);
			}
			return null;
		}

		internal static bool HasObjectProperty (object obj, string key)
		{
			return HasObjectProperty (obj, key, 0);
		}

		private static bool HasObjectProperty (object obj, string key, int nesting)
		{
			ScriptObject prop_obj;
			ScriptObject js_obj = obj as ScriptObject;
			bool is_js_obj = js_obj != null;

			if (is_js_obj && nesting > 0 && GetCacheEntry (out prop_obj, js_obj, key))
				return prop_obj != null;

			if (DirectHasObjectProperty (obj, key)) {
				if (is_js_obj && nesting > 0)
					SetCacheEntry (js_obj, key, js_obj);
				return true;
			}

			if (is_js_obj) {
				ScriptObject cur_obj = js_obj.proto as ScriptObject;
				if (cur_obj != null) {
					bool success = HasObjectProperty (cur_obj, key, nesting + 1);
					if (nesting > 0)
						SetCacheEntry (js_obj, key, cur_obj);
					return success;
				}

				if (nesting > 0)
					SetCacheEntry (js_obj, key, null);
			}
			return false;
		}

		internal static bool DirectHasObjectProperty (object obj, string key)
		{
			object result;
			return TryDirectGetObjectProperty (out result, obj, key);
		}

		private static bool TryDirectGetObjectProperty (out object result, object obj, string rhs)
		{
			ArrayObject ary = obj as ArrayObject;
			StringObject str = obj as StringObject;
			ScriptObject js_obj = obj as ScriptObject;

			if (js_obj != null) {
				Hashtable elems = js_obj.elems;

				// Numeric index?
				uint index;
				if (IsArrayIndex (rhs, out index)) {
					if (ary != null) {
						result = elems [index];
						return true;
					} else if (str != null) {
						string str_val = str.value;
						if (index < str_val.Length)
							result = str_val.Substring ((int) index, 1);
						else
							result = null;
						return true;
					}
				}

				if (elems.ContainsKey (rhs)) {
					object val = elems [rhs];
					JSFieldInfo field = val as JSFieldInfo;
					if (field != null)
						result = field.GetValue (rhs);
					else
						result = val;
					return true;
				}
			}

			bool success = TryGetNativeProperty (out result, obj, rhs);
			return success;
		}

		private static bool TryGetNativeProperty (out object result, object obj, string key)
		{
			// * seems to be a wilcard inside member names
			if (key.IndexOf ('*') != -1) {
				result = null;
				return false;
			}
			key = MapToInternalName (key);
			
			if (obj is Closure)
				obj = ((Closure) obj).func;

			Type type = (obj is GlobalScope) ? typeof (GlobalObject) : obj.GetType ();
			MemberInfo [] members = type.GetMember (key,
				BindingFlags.FlattenHierarchy | BindingFlags.GetField | BindingFlags.GetProperty |
				BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);

			if (members.Length != 0) {
				MemberInfo member = members [0];

				switch (member.MemberType) {
				case MemberTypes.Field:
					result = ((FieldInfo) member).GetValue (obj);
					return true;

				case MemberTypes.Property:
					MethodInfo method = ((PropertyInfo) member).GetGetMethod ();
					result = method.Invoke (obj, new object [] { });
					return true;

				case MemberTypes.Method:
					result = new FunctionObject ((MethodInfo) member);
					return true;

				default:
					Console.WriteLine ("TryGetNativeProperty: Unsupported member type {0} for {1}.{2}",
						member.MemberType, obj, key);
					break;
				}
			}

			result = null;
			return false;
		}

		#region Cache Helpers
		static private bool USE_CACHE = true;

		private static bool GetCacheEntry (out ScriptObject prop_obj, ScriptObject obj, string key)
		{
			if (USE_CACHE) {
				//Console.WriteLine ("Getting cache for {0} ({1}) property {2}", obj, obj.GetType (), key);
				Hashtable property_cache = obj.property_cache;
				if (property_cache.ContainsKey (key)) {
					//Console.WriteLine ("\t=> in cache: {0} ({1})", property_cache [cache_key],
					//	property_cache [cache_key].GetType ());
					prop_obj = property_cache [key] as ScriptObject;
					return true;
				}
			}
			//Console.WriteLine ("\t=> not in cache");
			prop_obj = null;
			return false;
		}

		private static void SetCacheEntry (ScriptObject obj, string key, ScriptObject prop_obj)
		{
			if (!USE_CACHE)
				return;

			ArrayObject ary = obj as ArrayObject;
			// Ignore numeric indices on arrays
			if (ary != null && IsArrayIndex (key))
				return;

			//Console.WriteLine ("Setting cache for {0} ({1}) property {2} to {3} ({4})", obj, obj.GetType (),
			//	key, prop_obj, prop_obj.GetType ());

			Hashtable property_cache = obj.property_cache;
			property_cache.Remove (key);
			property_cache.Add (key, prop_obj);
		}

		private static void InvalidateCacheEntry (ScriptObject obj, string key)
		{
			if (!USE_CACHE)
				return;

			//Console.WriteLine ("Invalidating cache for {0} ({1}) property {2}", obj, obj.GetType (), key);

			obj.property_cache.Remove (key);
		}
		#endregion

		private static readonly Regex NonDigitRegex = new Regex (@"\D",
			RegexOptions.Compiled | RegexOptions.ECMAScript);

		internal static bool IsArrayIndex (string key, out uint index)
		{
			if (key == "" || NonDigitRegex.IsMatch (key)) {
				index = 0;
				return false;
			}
			index = Convert.ToUint32 (key);
			return Convert.ToString (index) == key;
		}

		internal static bool IsArrayIndex (string key)
		{
			if (key == "" || NonDigitRegex.IsMatch (key))
				return false;
			uint index = Convert.ToUint32 (key);
			return Convert.ToString (index) == key;
		}
	}
}