Пример #1
0
        public virtual object Current(Context cx)
        {
            if (!enumValues)
            {
                return(currentId);
            }

            object result;

            string s = ScriptRuntime.ToStringIdOrIndex(cx, currentId);

            if (s == null)
            {
                int index = ScriptRuntime.lastIndexResult(cx);
                result = obj.Get(index, obj);
            }
            else
            {
                result = obj.Get(s, obj);
            }

            return(result);
        }
Пример #2
0
        public static void ImportType(IScriptable scope, Type type)
        {
            if (!type.IsPublic)
            {
                return;
            }

            if (ScriptRuntime.IsNativeRuntimeType(type))
            {
                return;
            }

            // Cannot define 'Object'
            if (type.Name == "Object")
            {
                return;
            }


            string [] ns = type.FullName.Split('.');

            IScriptable parent = scope;

            for (int i = 0; i < ns.Length - 1; i++)
            {
                IScriptable obj = (parent.Get(ns [i], parent) as IScriptable);
                if (obj == null)
                {
                    obj = new BuiltinObject();
                    parent.Put(ns [i], parent, obj);
                }
                parent = obj;
            }

            object thisObj = null;

            if (type.IsEnum)
            {
                thisObj = new CliEnum((Enum)Activator.CreateInstance(type));
            }
            else
            {
                thisObj = CliType.GetNativeCliType(type);
            }
            // Define as toplevel object
            scope.Put(ns [ns.Length - 1], scope, thisObj);

            // Define as full qualified name
            parent.Put(ns [ns.Length - 1], parent, thisObj);
        }
 /// <summary> Gets an indexed property from an object or any object in its prototype chain.
 /// <p>
 /// Searches the prototype chain for a property with integral index
 /// <code>index</code>. Note that if you wish to look for properties with numerical
 /// but non-integral indicies, you should use getProperty(Scriptable,String) with
 /// the string value of the index.
 /// <p>
 /// </summary>
 /// <param name="obj">a JavaScript object
 /// </param>
 /// <param name="index">an integral index
 /// </param>
 /// <returns> the value of a property with index <code>index</code> found in
 /// <code>obj</code> or any object in its prototype chain, or
 /// <code>Scriptable.NOT_FOUND</code> if not found
 /// </returns>
 public static object GetProperty (IScriptable obj, int index)
 {
     IScriptable start = obj;
     object result;
     do {
         result = obj.Get (index, start);
         if (result != UniqueTag.NotFound)
             break;
         obj = obj.GetPrototype ();
     }
     while (obj != null);
     return result;
 }
Пример #4
0
        private static object nameOrFunction(Context cx, IScriptable scope, IScriptable parentScope, string name, bool asFunctionCall)
        {
            object result;
            IScriptable thisObj = scope; // It is used only if asFunctionCall==true.

            XMLObject firstXMLObject = null;
            for (; ; ) {
                if (scope is BuiltinWith) {
                    IScriptable withObj = scope.GetPrototype ();
                    if (withObj is XMLObject) {
                        XMLObject xmlObj = (XMLObject)withObj;
                        if (xmlObj.EcmaHas (cx, name)) {
                            // function this should be the target object of with
                            thisObj = xmlObj;
                            result = xmlObj.EcmaGet (cx, name);
                            break;
                        }
                        if (firstXMLObject == null) {
                            firstXMLObject = xmlObj;
                        }
                    }
                    else {
                        result = ScriptableObject.GetProperty (withObj, name);
                        if (result != UniqueTag.NotFound) {
                            // function this should be the target object of with
                            thisObj = withObj;
                            break;
                        }
                    }
                }
                else if (scope is BuiltinCall) {
                    // NativeCall does not prototype chain and Scriptable.get
                    // can be called directly.
                    result = scope.Get (name, scope);
                    if (result != UniqueTag.NotFound) {
                        if (asFunctionCall) {
                            // ECMA 262 requires that this for nested funtions
                            // should be top scope
                            thisObj = ScriptableObject.GetTopLevelScope (parentScope);
                        }
                        break;
                    }
                }
                else {
                    // Can happen if embedding decided that nested
                    // scopes are useful for what ever reasons.
                    result = ScriptableObject.GetProperty (scope, name);
                    if (result != UniqueTag.NotFound) {
                        thisObj = scope;
                        break;
                    }
                }
                scope = parentScope;
                parentScope = parentScope.ParentScope;
                if (parentScope == null) {
                    result = topScopeName (cx, scope, name);
                    if (result == UniqueTag.NotFound) {
                        if (firstXMLObject == null || asFunctionCall) {
                            throw NotFoundError (scope, name);
                        }
                        // The name was not found, but we did find an XML
                        // object in the scope chain and we are looking for name,
                        // not function. The result should be an empty XMLList
                        // in name context.
                        result = firstXMLObject.EcmaGet (cx, name);
                    }
                    // For top scope thisObj for functions is always scope itself.
                    thisObj = scope;
                    break;
                }
            }

            if (asFunctionCall) {
                if (!(result is ICallable)) {
                    throw NotFunctionError (result, name);
                }
                storeScriptable (cx, thisObj);
            }

            return result;
        }
Пример #5
0
        internal static string defaultObjectToSource(Context cx, IScriptable scope, IScriptable thisObj, object [] args)
        {
            using (Helpers.StackOverflowVerifier sov = new Helpers.StackOverflowVerifier (1024)) {
                bool toplevel, iterating;
                if (cx.iterating == null) {
                    toplevel = true;
                    iterating = false;
                    cx.iterating = new ObjToIntMap (31);
                }
                else {
                    toplevel = false;
                    iterating = cx.iterating.has (thisObj);
                }

                System.Text.StringBuilder result = new System.Text.StringBuilder (128);
                if (toplevel) {
                    result.Append ("(");
                }
                result.Append ('{');

                // Make sure cx.iterating is set to null when done
                // so we don't leak memory
                try {
                    if (!iterating) {
                        cx.iterating.intern (thisObj); // stop recursion.
                        object [] ids = thisObj.GetIds ();
                        for (int i = 0; i < ids.Length; i++) {
                            if (i > 0)
                                result.Append (", ");
                            object id = ids [i];
                            object value;
                            if (id is int) {
                                int intId = ((int)id);
                                value = thisObj.Get (intId, thisObj);
                                result.Append (intId);
                            }
                            else {
                                string strId = (string)id;
                                value = thisObj.Get (strId, thisObj);
                                if (ScriptRuntime.isValidIdentifierName (strId)) {
                                    result.Append (strId);
                                }
                                else {
                                    result.Append ('\'');
                                    result.Append (ScriptRuntime.escapeString (strId, '\''));
                                    result.Append ('\'');
                                }
                            }
                            result.Append (':');
                            result.Append (ScriptRuntime.uneval (cx, scope, value));
                        }
                    }
                }
                finally {
                    if (toplevel) {
                        cx.iterating = null;
                    }
                }

                result.Append ('}');
                if (toplevel) {
                    result.Append (')');
                }
                return result.ToString ();
            }
        }
Пример #6
0
        public static object nameIncrDecr(IScriptable scopeChain, string id, int incrDecrMask)
        {
            IScriptable target;
            object value;
            {
                do {
                    target = scopeChain;
                    do {
                        value = target.Get (id, scopeChain);
                        if (value != UniqueTag.NotFound) {

                            goto search_brk;
                        }
                        target = target.GetPrototype ();
                    }
                    while (target != null);
                    scopeChain = scopeChain.ParentScope;
                }
                while (scopeChain != null);
                throw NotFoundError (scopeChain, id);
            }

            search_brk:
            ;

            return doScriptableIncrDecr (target, id, scopeChain, value, incrDecrMask);
        }
Пример #7
0
 public virtual object Get(string name, IScriptable start)
 {
     return(obj.Get(name, start));
 }