Exemple #1
0
        public bool EqualsIgnoreClass(object obj)
        {
            TypeDef_Function other = obj as TypeDef_Function;

            if (null == other)
            {
                return(false);
            }

            if (minArgs != other.minArgs)
            {
                return(false);
            }

            if (argTypes.Count == other.argTypes.Count)
            {
                for (int ii = 0; ii < argTypes.Count; ++ii)
                {
                    if (argTypes[ii] != other.argTypes[ii])
                    {
                        return(false);
                    }
                }
            }
            else
            {
                return(false);
            }

            return((retType == other.retType) && (varargs == other.varargs));
        }
Exemple #2
0
        public override bool Equals(object obj)
        {
            TypeDef_Function other = obj as TypeDef_Function;

            if (!EqualsIgnoreClass(obj))
            {
                return(false);
            }

            if (null != classType)
            {
                if (null == other.classType)
                {
                    return(false);
                }
                if (classType != other.classType)
                {
                    return(false);
                }
            }
            else if (null != other.classType)
            {
                return(false);
            }

            return(true);
        }
Exemple #3
0
        public virtual bool CanStoreValue(ExecContext context, ITypeDef valueType)
        {
            //if (valueType is TypeDef) {
            //	return null == (valueType as TypeDef).IsNull();
            //}
            if (valueType.IsNull())
            {
                return(true);
            }

            TypeDef_Function funcValueType = valueType as TypeDef_Function;

            if (null == funcValueType)
            {
                return(false);
            }

            if (!retType.Equals(IntrinsicTypeDefs.VOID) && !retType.CanStoreValue(context, funcValueType.retType))
            {
                return(false);
            }

            if (null != classType)
            {
                if (null == funcValueType.classType || !classType.CanStoreValue(context, funcValueType.classType))
                {
                    return(false);
                }
                // This is the point of isStatic in this class. This is saying, "I can save a reference to a class member function only if it is static."
            }
            else if (null != funcValueType.classType && !funcValueType.isStaticMember)
            {
                return(false);
            }

            // Mismatch if other's max args exceeds our max args.
            if (argTypes.Count > funcValueType.argTypes.Count)
            {
                return(false);
            }

            // Mismatch if we have fewer min args than they do.
            // (Meaning we could be called with fewer arguments than they have default values for.)
            if (minArgs < funcValueType.minArgs)
            {
                return(false);
            }

            // Make sure that for every argument WE have, THEIR argument's type matches.
            for (int ii = 0; ii < argTypes.Count; ++ii)
            {
                if (!funcValueType.argTypes[ii].CanStoreValue(context, argTypes[ii]))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #4
0
 // This is for non-terminal blocks: for, if, and expr-lists. NOT fuction calls.
 public void Set(int depth, string name = "<scope>")
 {
     _varStackStart = depth;
     _name          = name;
     _terminal      = false;
     _hardTerminal  = false;
     _classInstance = null;
     _classDef      = null;
     _funcType      = null;
     _isStatic      = false;
 }
Exemple #5
0
 // ATM, this is only used by Exec.
 public void SetHardTerminal(int depth, string name = "<hardTerminal>")
 {
     _varStackStart = depth;
     _name          = "hardTerminal";
     _terminal      = true;
     _hardTerminal  = true;
     _classInstance = null;
     _classDef      = null;
     _funcType      = null;
     _isStatic      = false;
 }
Exemple #6
0
 // This is for function calls, both regular calls and class member calls.
 public void Set(int depth, string name, TypeDef_Function funcType, ClassValue classInstance, bool isStaticIn)
 {
     _varStackStart = depth;
     _name          = "function call (" + name + ")";
     _terminal      = true;
     _hardTerminal  = false;
     _classInstance = classInstance;
     _classDef      = null;
     _funcType      = funcType;
     _isStatic      = isStaticIn;
 }
Exemple #7
0
 // This is for static function calls, or for class member function calls during typecheck
 // when we don't have or need an instance.
 public void Set(int depth, TypeDef_Function funcType, ClassDef classDef, bool isStaticIn)
 {
     _varStackStart = depth;
     _name          = "class (" + classDef.name + ")";
     _terminal      = true;
     _hardTerminal  = false;
     _classInstance = null;
     _classDef      = classDef;
     _funcType      = funcType;
     _isStatic      = isStaticIn;
 }
Exemple #8
0
 // Terminal should be true in the "usual" case, which seems to be class member
 // functions. Should be false in the case of defstructors, because those aren't
 // real functions and thus not closures: they're just little blocks of code
 // inside the surrounding code that have access to class members.
 public void Set(int depth, ClassValue classInstance, bool terminalIn = true)
 {
     _varStackStart = depth;
     _name          = "class (" + classInstance.classDef.name + ")";
     _terminal      = terminalIn;
     _hardTerminal  = false;
     _classInstance = classInstance;
     _classDef      = null;
     _funcType      = null;
     _isStatic      = false;
 }
Exemple #9
0
        public static TypeDef_Function GetTypeDef_Function(ITypeDef retType, List <ITypeDef> argTypes, int minArgs, bool _varargs, TypeDef_Class classType, bool isConst, bool isStatic)
        {
            if (minArgs < 0)
            {
                minArgs = argTypes.Count;
            }

            string args = "";

            for (int ii = 0; ii < argTypes.Count; ++ii)
            {
                string defaultValueString = "";
                if (ii >= minArgs)
                {
                    defaultValueString = " ?";
                }
                args += (ii == 0 ? "" : ", ") + argTypes[ii] + defaultValueString;
            }

            string classPart = "";

            if (null != classType)
            {
                classPart = ":" + classType.className;
            }

            string name = "function" + classPart + "<" + retType + "(" + args + ")>";

            if (_varargs)
            {
                name = name + " varargs";
            }
            if (isStatic)
            {
                name = name + " static";
            }
            if (isConst)
            {
                name = name + " const";
            }

            if (_typeRegistry.ContainsKey(name))
            {
                return(_typeRegistry[name] as TypeDef_Function);
            }

            TypeDef_Function newDef = new TypeDef_Function(retType, argTypes, minArgs, _varargs, classType, isConst, isStatic);

#if PEBBLE_TRACETYPES
            Console.WriteLine("Registering new function type: " + name);
#endif
            _typeRegistry.Add(name, newDef);
            return(newDef);
        }
Exemple #10
0
        public bool PushClassCall_StaticOrTypeCheck(TypeDef_Function funcType, ClassDef classDef, bool isStatic, ExecContext context)
        {
            if (CALLSTACKMAXDEPTH == _callCount)
            {
                return(false);
            }

            _callStack[_callCount].Set(_varCount, funcType, classDef, isStatic);
            ++_callCount;

#if PEBBLE_TRACESTACK
            TraceScope("PushClassCall_StaticOrTypeCheck(" + classDef.name + ", " + (isStatic ? "static)" : "type check)"));
#endif

            return(true);
        }
Exemple #11
0
        public bool PushCall(TypeDef_Function funcType, string funcName, ClassValue instance, bool isStatic, ExecContext context)
        {
            if (CALLSTACKMAXDEPTH == _callCount)
            {
                return(false);
            }

            _callStack[_callCount].Set(_varCount, funcName, funcType, instance, isStatic);
            ++_callCount;

#if PEBBLE_TRACESTACK
            TraceScope("PushCall");
#endif

            return(true);
        }
Exemple #12
0
        public virtual string ToString(ExecContext context)
        {
            Variable var = GetByName("ThisToString");

            if (null != var && var.type is TypeDef_Function)
            {
                TypeDef_Function tdf = (TypeDef_Function)var.type;
                if (null != var.value && tdf.retType.Equals(IntrinsicTypeDefs.STRING) && tdf.argTypes.Count == 0)
                {
                    FunctionValue funcVal = (FunctionValue)var.value;
                    return((string)funcVal.Evaluate(context, new List <object>(), this));
                }
            }

            return("[" + classDef.name + " instance]");
        }
Exemple #13
0
        public static void Register(Engine engine)
        {
            //@ class List<T>
            TypeDef_Class ourType = TypeFactory.GetTypeDef_Class("List", new ArgList {
                IntrinsicTypeDefs.TEMPLATE_0
            }, false);
            ClassDef classDef = engine.defaultContext.CreateClass("List", ourType, null, new List <string> {
                "T"
            });

            classDef.childAllocator = () => {
                return(new PebbleList());
            };
            classDef.Initialize();

            //@ List<T> Add(T newValue, ...) or List<T> Push(T newValue, ...)
            //   Adds one or more elements to the end of the list.
            //   Cannot be used in a foreach loop.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    PebbleList scope = thisScope as PebbleList;
                    if (scope.enumeratingCount > 0)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Add: Attempt to modify a list that is being enumerated by a foreach loop.");
                        return(null);
                    }

                    var list        = scope.list;
                    var listType    = (TypeDef_Class)scope.classDef.typeDef;
                    var elementType = listType.genericTypes[0];
                    for (int ii = 0; ii < args.Count; ++ii)
                    {
                        object ret = args[ii];
                        list.Add(new Variable(null, elementType, ret));
                    }

                    return(thisScope);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList {
                    IntrinsicTypeDefs.TEMPLATE_0
                }, eval, true, ourType);
                classDef.AddMemberLiteral("Add", newValue.valType, newValue);
                classDef.AddMemberLiteral("Push", newValue.valType, newValue);
            }

            //@ List<T> Clear()
            //   Removes all elements from the list.
            //   Cannot be used in a foreach loop.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    PebbleList pebList = thisScope as PebbleList;
                    if (pebList.enumeratingCount > 0)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Clear: Attempt to modify a list that is being enumerated by a foreach loop.");
                        return(null);
                    }

                    var list = pebList.list;
                    list.Clear();
                    return(thisScope);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList {
                }, eval, false, ourType);
                classDef.AddMemberLiteral("Clear", newValue.valType, newValue);
            }

            //@ num Count()
            //   Returns the number of elements in the list.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    var list = (thisScope as PebbleList).list;
                    return(System.Convert.ToDouble(list.Count));
                };

                FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.NUMBER, new ArgList {
                }, eval, false, ourType);
                classDef.AddMemberLiteral("Count", newValue.valType, newValue);
            }

            //@ T Get(num index)
            //   Returns the value of the element of the list at the given index.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    double dix = (double)args[0];
                    int    ix  = (int)dix;

                    var list = (thisScope as PebbleList).list;

                    // Bounds checking.
                    if (ix < 0 || ix >= list.Count)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "Get: Index " + ix + " out of bounds of array of length " + list.Count + ".");
                        return(null);
                    }

                    return(list[ix].value);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.TEMPLATE_0, new ArgList {
                    IntrinsicTypeDefs.NUMBER
                }, eval, false, ourType);
                classDef.AddMemberLiteral("Get", newValue.valType, newValue);
            }

            //@ List<T> Insert(num index, T item)
            //   Inserts a new element into the list at the given index. Existing elements at and after the given index are pushed further down the list.
            //   Cannot be used in a foreach loop.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    PebbleList scope = thisScope as PebbleList;
                    if (scope.enumeratingCount > 0)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Insert: Attempt to modify a list that is being enumerated by a foreach loop.");
                        return(null);
                    }

                    var list        = scope.list;
                    var listType    = (TypeDef_Class)scope.classDef.typeDef;
                    var elementType = listType.genericTypes[0];
                    var indexDouble = (double)args[0];
                    var item        = args[1];
                    var index       = Convert.ToInt32(indexDouble);
                    if (index < 0 || index > list.Count)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "Insert: array index out of bounds.");
                        return(null);
                    }

                    list.Insert(index, new Variable(null, elementType, item));

                    return(thisScope);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList {
                    IntrinsicTypeDefs.NUMBER, IntrinsicTypeDefs.TEMPLATE_0
                }, eval, false, ourType);
                classDef.AddMemberLiteral("Insert", newValue.valType, newValue);
            }

            //@ T Pop()
            //   Returns the value of the last element of the list and removes it from the list.
            //   Cannot be used in a foreach loop.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    PebbleList pebList = thisScope as PebbleList;
                    if (pebList.enumeratingCount > 0)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Pop: Attempt to remove an element from a list that is being enumerated in a foreach loop.");
                        return(null);
                    }

                    var list = pebList.list;
                    int ix   = list.Count - 1;
                    if (ix < 0)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "Pop: List is empty.");
                        return(null);
                    }

                    var result = list[ix].value;
                    list.RemoveAt(ix);
                    return(result);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.TEMPLATE_0, new ArgList {
                }, eval, false, ourType);
                classDef.AddMemberLiteral("Pop", newValue.valType, newValue);
            }

            //@ List<T> RemoveAt(num index)
            //   Removes element at the given index, and returns the list.
            //   Cannot be used in a foreach loop.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    double dix = (double)args[0];
                    int    ix  = (int)dix;

                    PebbleList pebList = thisScope as PebbleList;
                    if (pebList.enumeratingCount > 0)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "RemoveAt: Attempt to modify a list that is being enumerated by a foreach loop.");
                        return(null);
                    }

                    var list = pebList.list;
                    if (ix < 0 || ix >= list.Count)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "RemoveAt: Index " + ix + " out of bounds of array of length " + list.Count + ".");
                        return(null);
                    }

                    list.RemoveAt(ix);
                    return(thisScope);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList {
                    IntrinsicTypeDefs.NUMBER
                }, eval, false, ourType);
                classDef.AddMemberLiteral("RemoveAt", newValue.valType, newValue);
            }

            //@ List<T> RemoveRange(num start, num count)
            //   Removes elements in the given range of indices, and returns the list.
            //   Cannot be used in a foreach loop.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    double dstart = (double)args[0];
                    int    start  = (int)dstart;
                    double dcount = (double)args[1];
                    int    count  = (int)dcount;

                    PebbleList pebList = thisScope as PebbleList;
                    if (pebList.enumeratingCount > 0)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "RemoveRange: Attempt to modify a list that is being enumerated by a foreach loop.");
                        return(null);
                    }

                    var list = pebList.list;
                    if (start < 0 || start >= list.Count)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "RemoveRange: Start " + start + " out of bounds of array of length " + list.Count + ".");
                        return(null);
                    }
                    if (count < 0)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "RemoveRange: Count (" + count + ") cannot be negative.");
                        return(null);
                    }
                    if ((start + count) >= list.Count)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "RemoveRange: Count " + count + " exceeds array length (" + list.Count + ").");
                        return(null);
                    }

                    list.RemoveRange(start, count);
                    return(thisScope);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList {
                    IntrinsicTypeDefs.NUMBER, IntrinsicTypeDefs.NUMBER
                }, eval, false, ourType);
                classDef.AddMemberLiteral("RemoveRange", newValue.valType, newValue);
            }

            //@ List<T> Reverse()
            //   Reverses the list and returns it.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    var list = (thisScope as PebbleList).list;
                    list.Reverse();
                    return(thisScope);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList {
                }, eval, false, ourType);
                classDef.AddMemberLiteral("Reverse", newValue.valType, newValue);
            }

            //@ List<T> Set(num index, T newValue)
            //   Changes the value of the element at the given index, and returns the list.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    double dix = (double)args[0];
                    int    ix  = (int)dix;

                    object value = args[1];

                    var list = (thisScope as PebbleList).list;

                    // Bounds checking.
                    if (ix < 0 || ix >= list.Count)
                    {
                        context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "Set: Index " + ix + " out of bounds of array of length " + list.Count + ".");
                        return(null);
                    }

                    list[ix].value = value;

                    return(thisScope);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList {
                    IntrinsicTypeDefs.NUMBER, IntrinsicTypeDefs.TEMPLATE_0
                }, eval, false, ourType);
                classDef.AddMemberLiteral("Set", newValue.valType, newValue);
            }

            //@ List<T> Shuffle()
            //   Shuffles the list, putting the elements in random order.
            //	 Returns the list.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    var list = (thisScope as PebbleList).list;

                    Random rng = new Random();

                    int n = list.Count;
                    while (n > 1)
                    {
                        --n;
                        int      k     = rng.Next(n + 1);
                        Variable value = list[k];
                        list[k] = list[n];
                        list[n] = value;
                    }

                    return(thisScope);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList {
                }, eval, false, ourType);
                classDef.AddMemberLiteral("Shuffle", newValue.valType, newValue);
            }

            //@ List<T> Sort(functype<num(T, T>)> comparator)
            //   Sorts the list using the given comparator function.
            //   The comparator should behave the same as a C# Comparer. The first argument should be earlier in the
            //   list than the second, return a number < 0. If It should be later, return a number > 0. If their order
            //   is irrelevant, return 0.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    var list = (thisScope as PebbleList).list;

                    if (null == args[0])
                    {
                        context.SetRuntimeError(RuntimeErrorType.ArgumentInvalid, "Sort: comparator may not be null.");
                        return(null);
                    }

                    FunctionValue comparator = (FunctionValue)args[0];

                    List <object> argvals = new List <object>();
                    argvals.Add(0);
                    argvals.Add(0);

                    Comparison <Variable> hostComparator = new Comparison <Variable>(
                        (a, b) => {
                        argvals[0] = a.value;
                        argvals[1] = b.value;

                        // Note we use null instead of thisScope here. There is no way the sort function could be a
                        // class member because Sort's signature only takes function's whose type has no class.
                        double result = (double)comparator.Evaluate(context, argvals, null);
                        return(Convert.ToInt32(result));
                    }
                        );

                    list.Sort(hostComparator);
                    return(thisScope);
                };

                TypeDef_Function comparatorType = TypeFactory.GetTypeDef_Function(IntrinsicTypeDefs.NUMBER, new ArgList {
                    IntrinsicTypeDefs.TEMPLATE_0, IntrinsicTypeDefs.TEMPLATE_0
                }, -1, false, null, false, false);

                FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList {
                    comparatorType
                }, eval, false, ourType);
                classDef.AddMemberLiteral("Sort", newValue.valType, newValue);
            }

            //@ string ThisToScript(string prefix)
            //   ThisToScript is used by Serialize. A classes' ThisToScript function should return code which can rebuild the class.
            //   Note that it's only the content of the class, not the "new A" part. ie., it's the code that goes in the defstructor.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    string result = "";
                    string prefix = (string)args[0] + "\t";

                    var list = (thisScope as PebbleList).list;
                    for (int ii = 0; ii < list.Count; ++ii)
                    {
                        result += prefix + "Add(" + CoreLib.ValueToScript(context, list[ii].value, prefix + "\t", false) + ");\n";
                    }

                    return(result);
                };

                FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.STRING, new ArgList {
                    IntrinsicTypeDefs.STRING
                }, eval, false, ourType);
                classDef.AddMemberLiteral("ThisToScript", newValue.valType, newValue);
            }

            //@ string ToString()
            //   Returns a string representation of at least the first few elements of the list.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    var    list   = (thisScope as PebbleList).list;
                    string result = "List(" + list.Count + ")[";
                    for (int ii = 0; ii < Math.Min(4, list.Count); ++ii)
                    {
                        if (ii > 0)
                        {
                            result += ", ";
                        }
                        result += CoreLib.ValueToString(context, list[ii].value, true);
                    }
                    if (list.Count > 4)
                    {
                        result += ", ...";
                    }
                    return(result + "]");
                };

                FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.STRING, new ArgList {
                }, eval, false, ourType);
                classDef.AddMemberLiteral("ThisToString", newValue.valType, newValue);
            }

            classDef.FinalizeClass(engine.defaultContext);

            UnitTests.testFuncDelegates.Add("CoreList", RunTests);
        }
Exemple #14
0
        public static void Register(Engine engine)
        {
            TypeDef_Class ourType  = TypeFactory.GetTypeDef_Class("Debug", null, false);
            ClassDef      classDef = engine.defaultContext.CreateClass("Debug", ourType, null, null, true);

            classDef.Initialize();

            // string DumpClass(string className = "")
            //   Returns a string dump of the fields of a class with the given name.
            //   If no argument is given (or string is empty), instead returns a list of all classes.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    string className = (string)args[0];

                    if ("" == className)
                    {
                        return(context.GetDebugTypeString(true, false, false));
                    }
                    else
                    {
                        ClassDef def = context.GetClass(className);
                        if (null == def)
                        {
                            return("Class '" + className + "' not found.");
                        }

                        return(def.GetDebugString());
                    }
                };

                // Note: Here is an example of how you provide default argument values for a host function.
                List <Expr_Literal> defaultArgVals = new List <Expr_Literal>();
                defaultArgVals.Add(new Expr_Literal(null, "", IntrinsicTypeDefs.STRING));

                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.STRING, new ArgList {
                    IntrinsicTypeDefs.STRING
                }, eval, false, null, false, defaultArgVals);
                classDef.AddMemberLiteral("DumpClass", newValue.valType, newValue, true);
            }

            // string DumpStack()
            //   Returns a string printout of the stack at the point the function is called.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    return(context.ToString());
                };
                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.STRING, new ArgList {
                }, eval, false);
                classDef.AddMemberLiteral("DumpStack", newValue.valType, newValue, true);
            }

            // string DumpTypes()
            //   Returns a string printout of the registered types.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    return(context.GetDebugTypeString());
                };
                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.STRING, new ArgList {
                }, eval, false);
                classDef.AddMemberLiteral("DumpTypes", newValue.valType, newValue, true);
            }

            // string GetTotalMemory()
            //   Wraps GC.GetTotalMemory, which returns the number of bytes estimated to be allocated by C#.
            //   Note that this is NOT just memory allocated by Pebble.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    bool forceFullCorrection = (bool)args[0];

                    return(Convert.ToDouble(GC.GetTotalMemory(forceFullCorrection)));
                };
                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.NUMBER, new ArgList {
                    IntrinsicTypeDefs.BOOL
                }, eval, false);
                classDef.AddMemberLiteral("GetTotalMemory", newValue.valType, newValue, true);
            }

            // functype<bool(bool, string)> SetAssertCallback(functype<bool(bool, string)>)
            //   Sets callback for assert results, returns previous callback.
            //   Callback gets success as first variable, message as second. If returns false, system throws an Assert runtime exception.
            {
                TypeRef_Function handlerTypeRef = new TypeRef_Function(new TypeRef("bool"), new List <ITypeRef>()
                {
                    new TypeRef("bool"), new TypeRef("string")
                });
                bool             error          = false;
                TypeDef_Function handlerTypeDef = (TypeDef_Function)handlerTypeRef.Resolve(engine.defaultContext, ref error);
                Pb.Assert(!error, "Internal error: SetAssertHandler initialization.");

                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    FunctionValue newHandler = (FunctionValue)args[0];
                    FunctionValue oldHandler = Expr_Assert.handler;
                    Expr_Assert.handler = newHandler;
                    return(oldHandler);
                };
                FunctionValue newValue = new FunctionValue_Host(handlerTypeDef, new ArgList {
                    handlerTypeDef
                }, eval, false);
                classDef.AddMemberLiteral("SetAssertCallback", newValue.valType, newValue, true);
            }

            //@ bool SetLogCompileErrors(bool log)
            //   Sets whether or not compile errors should be logged to the Engine's log function.
            //   Returns the previous value.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    bool oldValue = context.engine.logCompileErrors;
                    bool newVal   = (bool)args[0];
                    context.engine.logCompileErrors = newVal;
                    return(oldValue);
                };
                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.BOOL, new ArgList {
                    IntrinsicTypeDefs.BOOL
                }, eval, false);
                classDef.AddMemberLiteral("SetLogCompileErrors", newValue.valType, newValue, true);
            }

            // void TimerStart()
            //   Starts a debug timer. If one is already running it will be set to this new start time.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    timer = Stopwatch.StartNew();
                    return(null);
                };
                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.VOID, new ArgList {
                }, eval, false);
                classDef.AddMemberLiteral("TimerStart", newValue.valType, newValue, true);
            }

            // num TimerGet()
            //   Returns elapsed ms since TimerStart called, or -1 if TimerStart wasn't previously called.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    if (null == timer)
                    {
                        return(-1.0);
                    }
                    return(Convert.ToDouble(timer.ElapsedMilliseconds));
                };
                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.NUMBER, new ArgList {
                }, eval, false);
                classDef.AddMemberLiteral("TimerGet", newValue.valType, newValue, true);
            }

            classDef.FinalizeClass(engine.defaultContext);
        }
Exemple #15
0
 public FunctionValue_Host(TypeDef_Function _valType, EvaluateDelegate eval)
 {
     valType  = _valType;
     Evaluate = eval;
 }
Exemple #16
0
 public static TypeDef_Function GetClassVersion(TypeDef_Function original, TypeDef_Class classType, bool isStatic)
 {
     return(GetTypeDef_Function(original.retType, original.argTypes, original.minArgs, original.varargs, classType, original.isConst, isStatic));
 }