public override void PerformTest() { _keys = new NativeArray<string>(); _keysToIndexMapping = new NativeObject(); _internalObject = new NativeObject(); Add("hello", "world"); Add("goodbye", "buddy"); Add("yester", "day"); // TODO: Do this in a separate thread and if the thread hasn't exited in 20 seconds, the test failed Remove("hello"); Remove("goodbye"); }
public BoundFunction(Context cx, Scriptable scope, Callable targetFunction, Scriptable boundThis, object[] boundArgs) { this.targetFunction = targetFunction; this.boundThis = boundThis; this.boundArgs = boundArgs; if (targetFunction is BaseFunction) { length = Math.Max(0, ((BaseFunction)targetFunction).GetLength() - boundArgs.Length); } else { length = 0; } ScriptRuntime.SetFunctionProtoAndParent(this, scope); Function thrower = ScriptRuntime.TypeErrorThrower(); NativeObject throwing = new NativeObject(); throwing.Put("get", throwing, thrower); throwing.Put("set", throwing, thrower); throwing.Put("enumerable", throwing, false); throwing.Put("configurable", throwing, false); throwing.PreventExtensions(); this.DefineOwnProperty(cx, "caller", throwing, false); this.DefineOwnProperty(cx, "arguments", throwing, false); }
/// <summary> /// Note that if the <code>delegee</code> is <code>null</code>, /// this method creates a new instance of the Delegator itself /// rathert than forwarding the call to the /// <code>delegee</code>. /// </summary> /// <remarks> /// Note that if the <code>delegee</code> is <code>null</code>, /// this method creates a new instance of the Delegator itself /// rathert than forwarding the call to the /// <code>delegee</code>. This permits the use of Delegator /// prototypes. /// </remarks> /// <param name="cx">the current Context for this thread</param> /// <param name="scope"> /// an enclosing scope of the caller except /// when the function is called from a closure. /// </param> /// <param name="args">the array of arguments</param> /// <returns>the allocated object</returns> /// <seealso cref="Function.Construct(Context, Scriptable, object[])">Function.Construct(Context, Scriptable, object[])</seealso> public virtual Scriptable Construct(Context cx, Scriptable scope, object[] args) { if (obj == null) { //this little trick allows us to declare prototype objects for //Delegators Rhino.Delegator n = NewInstance(); Scriptable delegee; if (args.Length == 0) { delegee = new NativeObject(); } else { delegee = ScriptRuntime.ToObject(cx, scope, args[0]); } n.SetDelegee(delegee); return n; } else { return ((Function)obj).Construct(cx, scope, args); } }
public static Scriptable NewCatchScope(Exception t, Scriptable lastCatchScope, string exceptionName, Context cx, Scriptable scope) { object obj; bool cacheObj; if (t is JavaScriptException) { cacheObj = false; obj = ((JavaScriptException)t).GetValue(); } else { cacheObj = true; // Create wrapper object unless it was associated with // the previous scope object if (lastCatchScope != null) { NativeObject last = (NativeObject)lastCatchScope; obj = last.GetAssociatedValue(t); if (obj == null) { Kit.CodeBug(); } } else { obj = WrapException(t, scope, cx); } } NativeObject catchScopeObject = new NativeObject(); // See ECMA 12.4 catchScopeObject.DefineProperty(exceptionName, obj, ScriptableObject.PERMANENT); if (IsVisible(cx, t)) { // Add special Rhino object __exception__ defined in the catch // scope that can be used to retrieve the Java exception associated // with the JavaScript exception (to get stack trace info, etc.) catchScopeObject.DefineProperty("__exception__", Context.JavaToJS(t, scope), ScriptableObject.PERMANENT | ScriptableObject.DONTENUM); } if (cacheObj) { catchScopeObject.AssociateValue(t, obj); } return catchScopeObject; }
public static ScriptableObject InitStandardObjects(Context cx, ScriptableObject scope, bool @sealed) { if (scope == null) { scope = new NativeObject(); } scope.AssociateValue(LIBRARY_SCOPE_KEY, scope); (new ClassCache()).Associate(scope); BaseFunction.Init(scope, @sealed); NativeObject.Init(scope, @sealed); Scriptable objectProto = ScriptableObject.GetObjectPrototype(scope); // Function.prototype.__proto__ should be Object.prototype Scriptable functionProto = ScriptableObject.GetClassPrototype(scope, "Function"); functionProto.SetPrototype(objectProto); // Set the prototype of the object passed in if need be if (scope.GetPrototype() == null) { scope.SetPrototype(objectProto); } // must precede NativeGlobal since it's needed therein NativeError.Init(scope, @sealed); NativeGlobal.Init(cx, scope, @sealed); NativeArray.Init(scope, @sealed); if (cx.GetOptimizationLevel() > 0) { // When optimizing, attempt to fulfill all requests for new Array(N) // with a higher threshold before switching to a sparse // representation NativeArray.SetMaximumInitialCapacity(200000); } NativeString.Init(scope, @sealed); NativeBoolean.Init(scope, @sealed); NativeNumber.Init(scope, @sealed); NativeDate.Init(scope, @sealed); NativeMath.Init(scope, @sealed); NativeJSON.Init(scope, @sealed); NativeWith.Init(scope, @sealed); NativeCall.Init(scope, @sealed); NativeScript.Init(scope, @sealed); NativeIterator.Init(scope, @sealed); // Also initializes NativeGenerator bool withXml = cx.HasFeature(Context.FEATURE_E4X) && cx.GetE4xImplementationFactory() != null; // define lazy-loaded properties using their class name new LazilyLoadedCtor(scope, "RegExp", "org.mozilla.javascript.regexp.NativeRegExp", @sealed, true); new LazilyLoadedCtor(scope, "Packages", "org.mozilla.javascript.NativeJavaTopPackage", @sealed, true); new LazilyLoadedCtor(scope, "getClass", "org.mozilla.javascript.NativeJavaTopPackage", @sealed, true); new LazilyLoadedCtor(scope, "JavaAdapter", "org.mozilla.javascript.JavaAdapter", @sealed, true); new LazilyLoadedCtor(scope, "JavaImporter", "org.mozilla.javascript.ImporterTopLevel", @sealed, true); new LazilyLoadedCtor(scope, "Continuation", "org.mozilla.javascript.NativeContinuation", @sealed, true); foreach (string packageName in GetTopPackageNames()) { new LazilyLoadedCtor(scope, packageName, "org.mozilla.javascript.NativeJavaTopPackage", @sealed, true); } if (withXml) { string xmlImpl = cx.GetE4xImplementationFactory().GetImplementationClassName(); new LazilyLoadedCtor(scope, "XML", xmlImpl, @sealed, true); new LazilyLoadedCtor(scope, "XMLList", xmlImpl, @sealed, true); new LazilyLoadedCtor(scope, "Namespace", xmlImpl, @sealed, true); new LazilyLoadedCtor(scope, "QName", xmlImpl, @sealed, true); } if (scope is TopLevel) { ((TopLevel)scope).CacheBuiltins(); } return scope; }
public static object Stringify(Context cx, Scriptable scope, object value, object replacer, object space) { string indent = string.Empty; string gap = string.Empty; IList<object> propertyList = null; Callable replacerFunction = null; if (replacer is Callable) { replacerFunction = (Callable)replacer; } else { if (replacer is NativeArray) { propertyList = new List<object>(); NativeArray replacerArray = (NativeArray)replacer; foreach (int i in replacerArray.GetIndexIds()) { object v = replacerArray.Get(i, replacerArray); if (v is string || v is Number) { propertyList.Add(v); } else { if (v is NativeString || v is NativeNumber) { propertyList.Add(ScriptRuntime.ToString(v)); } } } } } if (space is NativeNumber) { space = ScriptRuntime.ToNumber(space); } else { if (space is NativeString) { space = ScriptRuntime.ToString(space); } } if (space is Number) { int gapLength = (int)ScriptRuntime.ToInteger(space); gapLength = Math.Min(MAX_STRINGIFY_GAP_LENGTH, gapLength); gap = (gapLength > 0) ? Repeat(' ', gapLength) : string.Empty; space = gapLength; } else { if (space is string) { gap = (string)space; if (gap.Length > MAX_STRINGIFY_GAP_LENGTH) { gap = Sharpen.Runtime.Substring(gap, 0, MAX_STRINGIFY_GAP_LENGTH); } } } NativeJSON.StringifyState state = new NativeJSON.StringifyState(cx, scope, indent, gap, replacerFunction, propertyList, space); ScriptableObject wrapper = new NativeObject(); wrapper.SetParentScope(scope); wrapper.SetPrototype(ScriptableObject.GetObjectPrototype(scope)); wrapper.DefineProperty(string.Empty, value, 0); return Str(string.Empty, wrapper, state); }
internal static void Initialize() { _loadedAssemblies = new NativeObject(); _cachedAssemblies = new NativeObject(); }
private static string SerializeNativeObject(NativeObject nativeObj) { StringBuilder buffer = new StringBuilder(); buffer.Append("{"); string[] keys = nativeObj.GetKeys(); for (int i = 0; i < keys.Length; i++) { string key = keys[i]; if (key == "$" || key == "prototype") { continue; } buffer.Append(Serialize(nativeObj[key])); buffer.Append(","); if (i < keys.Length - 1) { buffer.Append(","); } } buffer.Append("}"); return buffer.ToString(); }
private static object Deserialize(NativeObject json) { Logging.Debug("Trying to deserialize into managed object"); if (!json.ContainsKey("__JsonRpcType__")) { Logging.Debug("No __JsonRpcType__ found"); return json; } string jsonRpcTypeString = json["__JsonRpcType__"].Cast<string>(); Type type = Type.GetType(jsonRpcTypeString); if (type == null) { throw new Exception("Unable to find type: " + jsonRpcTypeString); } // TODO: if type is interface, get implementing Type from JsonRpcImplementationAttribute object obj = Activator.CreateInstance(type); PropertyInfo[] properties = type.GetProperties(); object[] args = new object[1]; foreach (PropertyInfo property in properties) { if (property.SetMethod != null && json.ContainsKey(property.Name)) { Console.WriteLine("Setting property " + property.Name + " through reflection"); args[0] = json[property.Name]; property.SetMethod.Invoke(obj, args); } } return obj; }
/// <summary>Create a new JavaScript object.</summary> /// <remarks> /// Create a new JavaScript object. /// Equivalent to evaluating "new Object()". /// </remarks> /// <param name="scope"> /// the scope to search for the constructor and to evaluate /// against /// </param> /// <returns>the new object</returns> public virtual Scriptable NewObject(Scriptable scope) { NativeObject result = new NativeObject(); ScriptRuntime.SetBuiltinProtoAndParent(result, scope, TopLevel.Builtins.Object); return result; }
private object SetupDefaultPrototype() { lock (this) { if (prototypeProperty != null) { return prototypeProperty; } NativeObject obj = new NativeObject(); int attr = ScriptableObject.DONTENUM; obj.DefineProperty("constructor", this, attr); // put the prototype property into the object now, then in the // wacky case of a user defining a function Object(), we don't // get an infinite loop trying to find the prototype. prototypeProperty = obj; Scriptable proto = GetObjectPrototype(this); if (proto != obj) { // not the one we just made, it must remain grounded obj.SetPrototype(proto); } return obj; } }
/// <summary>Creates new script object.</summary> /// <remarks> /// Creates new script object. /// The default implementation of /// <see cref="Construct(Context, Scriptable, object[])">Construct(Context, Scriptable, object[])</see> /// uses the method to /// to get the value for <tt>thisObj</tt> argument when invoking /// <see cref="Call(Context, Scriptable, Scriptable, object[])">Call(Context, Scriptable, Scriptable, object[])</see> /// . /// The methos is allowed to return <tt>null</tt> to indicate that /// <see cref="Call(Context, Scriptable, Scriptable, object[])">Call(Context, Scriptable, Scriptable, object[])</see> /// will create a new object itself. In this case /// <see cref="Construct(Context, Scriptable, object[])">Construct(Context, Scriptable, object[])</see> /// will set scope and prototype on the result /// <see cref="Call(Context, Scriptable, Scriptable, object[])">Call(Context, Scriptable, Scriptable, object[])</see> /// unless they are already set. /// </remarks> public virtual Scriptable CreateObject(Context cx, Scriptable scope) { Scriptable newInstance = new NativeObject(); newInstance.SetPrototype(GetClassPrototype()); newInstance.SetParentScope(GetParentScope()); return newInstance; }