NewObject() public method

Create a new JavaScript object.
Create a new JavaScript object. Equivalent to evaluating "new Object()".
public NewObject ( Scriptable scope ) : Scriptable
scope Scriptable /// the scope to search for the constructor and to evaluate /// against ///
return Scriptable
Ejemplo n.º 1
0
		public virtual void RunJsTest(Context cx, Scriptable shared, string name, string source)
		{
			// create a lightweight top-level scope
			Scriptable scope = cx.NewObject(shared);
			scope.SetPrototype(shared);
			System.Console.Out.Write(name + ": ");
			object result;
			try
			{
				result = cx.EvaluateString(scope, source, "jstest input", 1, null);
			}
			catch (Exception e)
			{
				System.Console.Out.WriteLine("FAILED");
				throw;
			}
			NUnit.Framework.Assert.IsTrue(result != null);
			NUnit.Framework.Assert.IsTrue("success".Equals(result));
			System.Console.Out.WriteLine("passed");
		}
Ejemplo n.º 2
0
		public static Scriptable NewObjectLiteral(object[] propertyIds, object[] propertyValues, int[] getterSetters, Context cx, Scriptable scope)
		{
			Scriptable @object = cx.NewObject(scope);
			for (int i = 0, end = propertyIds.Length; i != end; ++i)
			{
				object id = propertyIds[i];
				int getterSetter = getterSetters == null ? 0 : getterSetters[i];
				object value = propertyValues[i];
				if (id is string)
				{
					if (getterSetter == 0)
					{
						if (IsSpecialProperty((string)id))
						{
							SpecialRef(@object, (string)id, cx).Set(cx, value);
						}
						else
						{
							@object.Put((string)id, @object, value);
						}
					}
					else
					{
						ScriptableObject so = (ScriptableObject)@object;
						Callable getterOrSetter = (Callable)value;
						bool isSetter = getterSetter == 1;
						so.SetGetterOrSetter((string)id, 0, getterOrSetter, isSetter);
					}
				}
				else
				{
					int index = System.Convert.ToInt32(((int)id));
					@object.Put(index, @object, value);
				}
			}
			return @object;
		}
Ejemplo n.º 3
0
		public static Scriptable WrapException(Exception t, Scriptable scope, Context cx)
		{
			RhinoException re;
			string errorName;
			string errorMsg;
			Exception javaException = null;
			if (t is EcmaError)
			{
				EcmaError ee = (EcmaError)t;
				re = ee;
				errorName = ee.GetName();
				errorMsg = ee.GetErrorMessage();
			}
			else
			{
				if (t is WrappedException)
				{
					WrappedException we = (WrappedException)t;
					re = we;
					javaException = we.GetWrappedException();
					errorName = "JavaException";
					errorMsg = javaException.GetType().FullName + ": " + javaException.Message;
				}
				else
				{
					if (t is EvaluatorException)
					{
						// Pure evaluator exception, nor WrappedException instance
						EvaluatorException ee = (EvaluatorException)t;
						re = ee;
						errorName = "InternalError";
						errorMsg = ee.Message;
					}
					else
					{
						if (cx.HasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS))
						{
							// With FEATURE_ENHANCED_JAVA_ACCESS, scripts can catch
							// all exception types
							re = new WrappedException(t);
							errorName = "JavaException";
							errorMsg = t.ToString();
						}
						else
						{
							// Script can catch only instances of JavaScriptException,
							// EcmaError and EvaluatorException
							throw Kit.CodeBug();
						}
					}
				}
			}
			string sourceUri = re.SourceName();
			if (sourceUri == null)
			{
				sourceUri = string.Empty;
			}
			int line = re.LineNumber();
			object[] args;
			if (line > 0)
			{
				args = new object[] { errorMsg, sourceUri, Sharpen.Extensions.ValueOf(line) };
			}
			else
			{
				args = new object[] { errorMsg, sourceUri };
			}
			Scriptable errorObject = cx.NewObject(scope, errorName, args);
			ScriptableObject.PutProperty(errorObject, "name", errorName);
			// set exception in Error objects to enable non-ECMA "stack" property
			if (errorObject is NativeError)
			{
				((NativeError)errorObject).SetStackProvider(re);
			}
			if (javaException != null && IsVisible(cx, javaException))
			{
				object wrap = cx.GetWrapFactory().Wrap(cx, scope, javaException, null);
				ScriptableObject.DefineProperty(errorObject, "javaException", wrap, ScriptableObject.PERMANENT | ScriptableObject.READONLY);
			}
			if (IsVisible(cx, re))
			{
				object wrap = cx.GetWrapFactory().Wrap(cx, scope, re, null);
				ScriptableObject.DefineProperty(errorObject, "rhinoException", wrap, ScriptableObject.PERMANENT | ScriptableObject.READONLY);
			}
			return errorObject;
		}
Ejemplo n.º 4
0
		public static object Parse(Context cx, Scriptable scope, string jtext, Callable reviver)
		{
			object unfiltered = Parse(cx, scope, jtext);
			Scriptable root = cx.NewObject(scope);
			root.Put(string.Empty, root, unfiltered);
			return Walk(cx, scope, reviver, root, string.Empty);
		}
Ejemplo n.º 5
0
		private Scriptable ExecuteModuleScript(Context cx, string id, Scriptable exports, ModuleScript moduleScript, bool isMain)
		{
			ScriptableObject moduleObject = (ScriptableObject)cx.NewObject(nativeScope);
			Uri uri = moduleScript.GetUri();
			Uri @base = moduleScript.GetBase();
			DefineReadOnlyProperty(moduleObject, "id", id);
			if (!sandboxed)
			{
				DefineReadOnlyProperty(moduleObject, "uri", uri.ToString());
			}
			Scriptable executionScope = new ModuleScope(nativeScope, uri, @base);
			// Set this so it can access the global JS environment objects.
			// This means we're currently using the "MGN" approach (ModuleScript
			// with Global Natives) as specified here:
			// <http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension>
			executionScope.Put("exports", executionScope, exports);
			executionScope.Put("module", executionScope, moduleObject);
			moduleObject.Put("exports", moduleObject, exports);
			Install(executionScope);
			if (isMain)
			{
				DefineReadOnlyProperty(this, "main", moduleObject);
			}
			ExecuteOptionalScript(preExec, cx, executionScope);
			moduleScript.GetScript().Exec(cx, executionScope);
			ExecuteOptionalScript(postExec, cx, executionScope);
			return ScriptRuntime.ToObject(nativeScope, ScriptableObject.GetProperty(moduleObject, "exports"));
		}
Ejemplo n.º 6
0
		private Scriptable GetExportedModuleInterface(Context cx, string id, Uri uri, Uri @base, bool isMain)
		{
			// Check if the requested module is already completely loaded
			Scriptable exports = exportedModuleInterfaces.Get(id);
			if (exports != null)
			{
				if (isMain)
				{
					throw new InvalidOperationException("Attempt to set main module after it was loaded");
				}
				return exports;
			}
			// Check if it is currently being loaded on the current thread
			// (supporting circular dependencies).
			IDictionary<string, Scriptable> threadLoadingModules = loadingModuleInterfaces.Value;
			if (threadLoadingModules != null)
			{
				exports = threadLoadingModules.Get(id);
				if (exports != null)
				{
					return exports;
				}
			}
			// The requested module is neither already loaded, nor is it being
			// loaded on the current thread. End of fast path. We must synchronize
			// now, as we have to guarantee that at most one thread can load
			// modules at any one time. Otherwise, two threads could end up
			// attempting to load two circularly dependent modules in opposite
			// order, which would lead to either unacceptable non-determinism or
			// deadlock, depending on whether we underprotected or overprotected it
			// with locks.
			lock (loadLock)
			{
				// Recheck if it is already loaded - other thread might've
				// completed loading it just as we entered the synchronized block.
				exports = exportedModuleInterfaces.Get(id);
				if (exports != null)
				{
					return exports;
				}
				// Nope, still not loaded; we're loading it then.
				ModuleScript moduleScript = GetModule(cx, id, uri, @base);
				if (sandboxed && !moduleScript.IsSandboxed())
				{
					throw ScriptRuntime.ThrowError(cx, nativeScope, "Module \"" + id + "\" is not contained in sandbox.");
				}
				exports = cx.NewObject(nativeScope);
				// Are we the outermost locked invocation on this thread?
				bool outermostLocked = threadLoadingModules == null;
				if (outermostLocked)
				{
					threadLoadingModules = new Dictionary<string, Scriptable>();
					loadingModuleInterfaces.Value = threadLoadingModules;
				}
				// Must make the module exports available immediately on the
				// current thread, to satisfy the CommonJS Modules/1.1 requirement
				// that "If there is a dependency cycle, the foreign module may not
				// have finished executing at the time it is required by one of its
				// transitive dependencies; in this case, the object returned by
				// "require" must contain at least the exports that the foreign
				// module has prepared before the call to require that led to the
				// current module's execution."
				threadLoadingModules.Put(id, exports);
				try
				{
					// Support non-standard Node.js feature to allow modules to
					// replace the exports object by setting module.exports.
					Scriptable newExports = ExecuteModuleScript(cx, id, exports, moduleScript, isMain);
					if (exports != newExports)
					{
						threadLoadingModules.Put(id, newExports);
						exports = newExports;
					}
				}
				catch (Exception e)
				{
					// Throw loaded module away if there was an exception
					Sharpen.Collections.Remove(threadLoadingModules, id);
					throw;
				}
				finally
				{
					if (outermostLocked)
					{
						// Make loaded modules visible to other threads only after
						// the topmost triggering load has completed. This strategy
						// (compared to the one where we'd make each module
						// globally available as soon as it loads) prevents other
						// threads from observing a partially loaded circular
						// dependency of a module that completed loading.
						exportedModuleInterfaces.PutAll(threadLoadingModules);
						loadingModuleInterfaces.Value = null;
					}
				}
			}
			return exports;
		}
Ejemplo n.º 7
0
		public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args)
		{
			if (!f.HasTag(XMLCTOR_TAG))
			{
				return base.ExecIdCall(f, cx, scope, thisObj, args);
			}
			int id = f.MethodId();
			switch (id)
			{
				case Id_defaultSettings:
				{
					options.SetDefault();
					Scriptable obj = cx.NewObject(scope);
					WriteSetting(obj);
					return obj;
				}

				case Id_settings:
				{
					Scriptable obj = cx.NewObject(scope);
					WriteSetting(obj);
					return obj;
				}

				case Id_setSettings:
				{
					if (args.Length == 0 || args[0] == null || args[0] == Undefined.instance)
					{
						options.SetDefault();
					}
					else
					{
						if (args[0] is Scriptable)
						{
							ReadSettings((Scriptable)args[0]);
						}
					}
					return Undefined.instance;
				}
			}
			throw new ArgumentException(id.ToString());
		}