This class represents the runtime context of an executing script.
This class represents the runtime context of an executing script. Before executing a script, an instance of Context must be created and associated with the thread that will be executing the script. The Context will be used to store information about the executing of the script such as the call stack. Contexts are associated with the current thread using the Call(ContextAction) or Enter() methods.

Different forms of script execution are supported. Scripts may be evaluated from the source directly, or first compiled and then later executed. Interactive execution is also supported.

Some aspects of script execution, such as type conversions and object creation, may be accessed directly through methods of Context.

Example #1
0
			public object Run(Context cx)
			{
				Scriptable scope = cx.InitStandardObjects();
				object rep = cx.EvaluateString(scope, source, "test.js", 0, null);
				NUnit.Framework.Assert.AreEqual(expected, rep);
				return null;
			}
Example #2
0
		public virtual object Action(Context cx, Scriptable scope, Scriptable thisObj, object[] args, int actionType)
		{
			GlobData data = new GlobData();
			data.mode = actionType;
			switch (actionType)
			{
				case RegExpProxyConstants.RA_MATCH:
				{
					object rval;
					data.optarg = 1;
					rval = MatchOrReplace(cx, scope, thisObj, args, this, data, false);
					return data.arrayobj == null ? rval : data.arrayobj;
				}

				case RegExpProxyConstants.RA_SEARCH:
				{
					data.optarg = 1;
					return MatchOrReplace(cx, scope, thisObj, args, this, data, false);
				}

				case RegExpProxyConstants.RA_REPLACE:
				{
					object arg1 = args.Length < 2 ? Undefined.instance : args[1];
					string repstr = null;
					Function lambda = null;
					if (arg1 is Function)
					{
						lambda = (Function)arg1;
					}
					else
					{
						repstr = ScriptRuntime.ToString(arg1);
					}
					data.optarg = 2;
					data.lambda = lambda;
					data.repstr = repstr;
					data.dollar = repstr == null ? -1 : repstr.IndexOf('$');
					data.charBuf = null;
					data.leftIndex = 0;
					object val = MatchOrReplace(cx, scope, thisObj, args, this, data, true);
					if (data.charBuf == null)
					{
						if (data.global || val == null || !val.Equals(true))
						{
							return data.str;
						}
						SubString lc = this.leftContext;
						Replace_glob(data, cx, scope, this, lc.index, lc.length);
					}
					SubString rc = this.rightContext;
					data.charBuf.AppendRange(rc.str, rc.index, rc.index + rc.length);
					return data.charBuf.ToString();
				}

				default:
				{
					throw Kit.CodeBug();
				}
			}
		}
Example #3
0
		private static void RunFileIfExists(Context cx, Scriptable global, FilePath f)
		{
			if (f.IsFile())
			{
				Main.ProcessFileNoThrow(cx, global, f.GetPath());
			}
		}
Example #4
0
		public override Scriptable Construct(Context cx, Scriptable scope, object[] args)
		{
			NativeRegExp re = new NativeRegExp();
			re.Compile(cx, scope, args);
			ScriptRuntime.SetBuiltinProtoAndParent(re, scope, TopLevel.Builtins.RegExp);
			return re;
		}
Example #5
0
			public object Run(Context cx)
			{
				cx.GetWrapFactory().SetJavaPrimitiveWrap(false);
				NUnit.Framework.Assert.AreEqual("string[]", cx.EvaluateString(cx.InitStandardObjects(), "new org.mozilla.javascript.tests.Bug496585Test().method('one', 'two', 'three')", "<test>", 1, null));
				NUnit.Framework.Assert.AreEqual("string+function", cx.EvaluateString(cx.InitStandardObjects(), "new org.mozilla.javascript.tests.Bug496585Test().method('one', function() {})", "<test>", 1, null));
				return null;
			}
Example #6
0
			public object Run(Context _cx)
			{
				ScriptableObject scope = _cx.InitStandardObjects();
				object result = _cx.EvaluateString(scope, script, "test script", 0, null);
				NUnit.Framework.Assert.AreEqual("b1,b2,a0,a1,,a3", Context.ToString(result));
				return null;
			}
Example #7
0
		protected internal override bool HasFeature(Context cx, int featureIndex)
		{
			switch (featureIndex)
			{
				case Context.FEATURE_STRICT_VARS:
				case Context.FEATURE_STRICT_EVAL:
				case Context.FEATURE_STRICT_MODE:
				{
					return strictMode;
				}

				case Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER:
				{
					return allowReservedKeywords;
				}

				case Context.FEATURE_WARNING_AS_ERROR:
				{
					return warningAsError;
				}

				case Context.FEATURE_LOCATION_INFORMATION_IN_ERROR:
				{
					return generatingDebug;
				}
			}
			return base.HasFeature(cx, featureIndex);
		}
Example #8
0
		internal static Ref CreateSpecial(Context cx, object @object, string name)
		{
			Scriptable target = ScriptRuntime.ToObjectOrNull(cx, @object);
			if (target == null)
			{
				throw ScriptRuntime.UndefReadError(@object, name);
			}
			int type;
			if (name.Equals("__proto__"))
			{
				type = SPECIAL_PROTO;
			}
			else
			{
				if (name.Equals("__parent__"))
				{
					type = SPECIAL_PARENT;
				}
				else
				{
					throw new ArgumentException(name);
				}
			}
			if (!cx.HasFeature(Context.FEATURE_PARENT_PROTO_PROPERTIES))
			{
				// Clear special after checking for valid name!
				type = SPECIAL_NONE;
			}
			return new Rhino.SpecialRef(target, type, name);
		}
Example #9
0
		public override object Get(Context cx)
		{
			switch (type)
			{
				case SPECIAL_NONE:
				{
					return ScriptRuntime.GetObjectProp(target, name, cx);
				}

				case SPECIAL_PROTO:
				{
					return target.GetPrototype();
				}

				case SPECIAL_PARENT:
				{
					return target.GetParentScope();
				}

				default:
				{
					throw Kit.CodeBug();
				}
			}
		}
			public object Run(Context cx)
			{
				ScriptableObject top = cx.InitStandardObjects();
				ScriptableObject.PutProperty(top, "foo", foo);
				cx.EvaluateString(top, script, "script", 0, null);
				return null;
			}
Example #11
0
		/// <summary>Create function compiled from Function(...) constructor.</summary>
		/// <remarks>Create function compiled from Function(...) constructor.</remarks>
		internal static Rhino.InterpretedFunction CreateFunction(Context cx, Scriptable scope, InterpreterData idata, object staticSecurityDomain)
		{
			Rhino.InterpretedFunction f;
			f = new Rhino.InterpretedFunction(idata, staticSecurityDomain);
			f.InitScriptFunction(cx, scope);
			return f;
		}
Example #12
0
		/// <seealso cref="Function.Call(Context, Scriptable, Scriptable, object[])">Function.Call(Context, Scriptable, Scriptable, object[])</seealso>
		public override object Call(Context cx, Scriptable scope, Scriptable thisObj, object[] args)
		{
			object sync = syncObject != null ? syncObject : thisObj;
			lock (sync is Wrapper ? ((Wrapper)sync).Unwrap() : sync)
			{
				return ((Function)obj).Call(cx, scope, thisObj, args);
			}
		}
Example #13
0
		public override Scriptable Construct(Context cx, Scriptable scope, object[] extraArgs)
		{
			if (targetFunction is Function)
			{
				return ((Function)targetFunction).Construct(cx, scope, Concat(boundArgs, extraArgs));
			}
			throw ScriptRuntime.TypeError0("msg.not.ctor");
		}
Example #14
0
		public override object Call(Context cx, Scriptable scope, Scriptable thisObj, object[] args)
		{
			if (script != null)
			{
				return script.Exec(cx, scope);
			}
			return Undefined.instance;
		}
			public object Run(Context cx)
			{
				Scriptable scope1 = cx.InitStandardObjects(new PrimitiveTypeScopeResolutionTest.MySimpleScriptableObject("scope1"));
				Scriptable scope2 = cx.InitStandardObjects(new PrimitiveTypeScopeResolutionTest.MySimpleScriptableObject("scope2"));
				cx.EvaluateString(scope2, scriptScope2, "source2", 1, null);
				scope1.Put("scope2", scope1, scope2);
				return cx.EvaluateString(scope1, scriptScope1, "source1", 1, null);
			}
Example #16
0
			protected override bool HasFeature(Context cx, int featureIndex)
			{
				if (featureIndex == Context.FEATURE_ENHANCED_JAVA_ACCESS)
				{
					return true;
				}
				return base.HasFeature(cx, featureIndex);
			}
Example #17
0
		public override object Call(Context cx, Scriptable scope, Scriptable thisObj, object[] args)
		{
			if (args.Length > 0 && args[0] is NativeRegExp && (args.Length == 1 || args[1] == Undefined.instance))
			{
				return args[0];
			}
			return Construct(cx, scope, args);
		}
Example #18
0
			protected override bool HasFeature(Context cx, int featureIndex)
			{
				if (featureIndex == Context.FEATURE_DYNAMIC_SCOPE)
				{
					return true;
				}
				return base.HasFeature(cx, featureIndex);
			}
			protected override void ObserveInstructionCount(Context cx, int instructionCount)
			{
				ObserveInstructionCountTest.MyContext mcx = (ObserveInstructionCountTest.MyContext)cx;
				mcx.quota -= instructionCount;
				if (mcx.quota <= 0)
				{
					throw new ObserveInstructionCountTest.QuotaExceeded();
				}
			}
Example #20
0
		public static void Init(Context cx, Scriptable scope, bool @sealed)
		{
			Rhino.Xmlimpl.XMLLibImpl lib = new Rhino.Xmlimpl.XMLLibImpl(scope);
			XMLLib bound = lib.BindToScope(scope);
			if (bound == lib)
			{
				lib.ExportToScope(@sealed);
			}
		}
		/// <summary>
		/// More complicated example: this form of call allows variable
		/// argument lists, and allows access to the 'this' object.
		/// </summary>
		/// <remarks>
		/// More complicated example: this form of call allows variable
		/// argument lists, and allows access to the 'this' object. For
		/// a global function, the 'this' object is the global object.
		/// In this case we look up a value that we associated with the global
		/// object using
		/// <see cref="Rhino.ScriptableObject.GetAssociatedValue(object)">Rhino.ScriptableObject.GetAssociatedValue(object)</see>
		/// .
		/// </remarks>
		public static object G(Context cx, Scriptable thisObj, object[] args, Function funObj)
		{
			object arg = args.Length > 0 ? args[0] : Undefined.instance;
			object privateValue = Undefined.instance;
			if (thisObj is ScriptableObject)
			{
				privateValue = ((ScriptableObject)thisObj).GetAssociatedValue(key);
			}
			return arg.ToString() + privateValue;
		}
Example #22
0
			protected override bool HasFeature(Context cx, int featureIndex)
			{
				switch (featureIndex)
				{
					case Context.FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME:
					{
						return true;
					}
				}
				return base.HasFeature(cx, featureIndex);
			}
Example #23
0
		protected internal override void OnContextCreated(Context cx)
		{
			cx.SetLanguageVersion(languageVersion);
			cx.SetOptimizationLevel(optimizationLevel);
			if (errorReporter != null)
			{
				cx.SetErrorReporter(errorReporter);
			}
			cx.SetGeneratingDebug(generatingDebug);
			base.OnContextCreated(cx);
		}
		/// <exception cref="System.Exception"></exception>
		public virtual ModuleScript GetModuleScript(Context cx, string moduleId, Uri uri, Uri @base, Scriptable paths)
		{
			foreach (ModuleScriptProvider provider in providers)
			{
				ModuleScript script = provider.GetModuleScript(cx, moduleId, uri, @base, paths);
				if (script != null)
				{
					return script;
				}
			}
			return null;
		}
Example #25
0
			protected override bool HasFeature(Context cx, int featureIndex)
			{
				switch (featureIndex)
				{
					case Context.FEATURE_STRICT_MODE:
					case Context.FEATURE_WARNING_AS_ERROR:
					{
						return true;
					}
				}
				return base.HasFeature(cx, featureIndex);
			}
Example #26
0
			public object Run(Context cx)
			{
				Scriptable scope = cx.InitStandardObjects();
				try
				{
					cx.EvaluateString(scope, _source, "test.js", 0, null);
				}
				catch (JavaScriptException e)
				{
					NUnit.Framework.Assert.AreEqual(_expectedStackTrace, e.GetScriptStackTrace());
					return null;
				}
				throw new Exception("Exception expected!");
			}
Example #27
0
		public virtual void InitFromContext(Context cx)
		{
			SetErrorReporter(cx.GetErrorReporter());
			languageVersion = cx.GetLanguageVersion();
			generateDebugInfo = (!cx.IsGeneratingDebugChanged() || cx.IsGeneratingDebug());
			reservedKeywordAsIdentifier = cx.HasFeature(Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER);
			allowMemberExprAsFunctionName = cx.HasFeature(Context.FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME);
			strictMode = cx.HasFeature(Context.FEATURE_STRICT_MODE);
			warningAsError = cx.HasFeature(Context.FEATURE_WARNING_AS_ERROR);
			xmlAvailable = cx.HasFeature(Context.FEATURE_E4X);
			optimizationLevel = cx.GetOptimizationLevel();
			generatingSource = cx.IsGeneratingSource();
			activationNames = cx.activationNames;
			// Observer code generation in compiled code :
			generateObserverCount = cx.generateObserverCount;
		}
Example #28
0
		public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args)
		{
			if (!f.HasTag(FTAG))
			{
				return base.ExecIdCall(f, cx, scope, thisObj, args);
			}
			int id = f.MethodId();
			switch (id)
			{
				case Id_constructor:
				{
					throw Context.ReportRuntimeError("Direct call is not supported");
				}
			}
			throw new ArgumentException(id.ToString());
		}
Example #29
0
		/// <summary>
		/// If "obj" is a java.util.Iterator or a java.lang.Iterable, return a
		/// wrapping as a JavaScript Iterator.
		/// </summary>
		/// <remarks>
		/// If "obj" is a java.util.Iterator or a java.lang.Iterable, return a
		/// wrapping as a JavaScript Iterator. Otherwise, return null.
		/// This method is in VMBridge since Iterable is a JDK 1.5 addition.
		/// </remarks>
		public override IEnumerator<object> GetJavaIterator(Context cx, Scriptable scope, object obj)
		{
			if (obj is Wrapper)
			{
				object unwrapped = ((Wrapper)obj).Unwrap();
				IEnumerator<object> iterator = null;
				if (unwrapped is IEnumerator)
				{
					iterator = (IEnumerator<object>)unwrapped;
				}
				if (unwrapped is IEnumerable)
				{
					iterator = ((IEnumerable<object>)unwrapped).GetEnumerator();
				}
				return iterator;
			}
			return null;
		}
Example #30
0
		public virtual void InitStandardObjects(Context cx, bool @sealed)
		{
			// Assume that Context.initStandardObjects initialize JavaImporter
			// property lazily so the above init call is not yet called
			cx.InitStandardObjects(this, @sealed);
			topScopeFlag = true;
			// If seal is true then exportAsJSClass(cx, seal) would seal
			// this obj. Since this is scope as well, it would not allow
			// to add variables.
			IdFunctionObject ctor = ExportAsJSClass(MAX_PROTOTYPE_ID, this, false);
			if (@sealed)
			{
				ctor.SealObject();
			}
			// delete "constructor" defined by exportAsJSClass so "constructor"
			// name would refer to Object.constructor
			// and not to JavaImporter.prototype.constructor.
			Delete("constructor");
		}