Esempio n. 1
0
            public override void Execute(Runtime runtime, Runtime.Thread thread)
            {
                var args = new object[this.argumentNum];

                for (var i = 0; i < this.argumentNum; i++)
                {
                    args[this.argumentNum - 1 - i] = thread.PopValue();
                }

                var target = thread.PopValue();

                var targetFunc = target as Runtime.Function;

                if (targetFunc != null)
                {
                    if (targetFunc.parameterNames.Count != this.argumentNum)
                    {
                        Runtime.RaiseRuntimeError(thread, this.span, "expected " + targetFunc.parameterNames.Count + " argument(s)");
                    }

                    thread.PushFrame(targetFunc);
                    for (var i = 0; i < this.argumentNum; i++)
                    {
                        thread.CurrentFrame().locals.Add(targetFunc.parameterNames[i], args[i]);
                    }

                    return;
                }

                var targetExternal = target as Runtime.ExternalFunction;

                if (targetExternal != null)
                {
                    try
                    {
                        thread.PushValue(targetExternal.Invoke(args));
                    }
                    catch (System.Exception e)
                    {
                        Runtime.RaiseRuntimeError(thread, this.span, "external function error:\n\n" + e.Message + "\n\n" + e.StackTrace);
                    }

                    return;
                }

                Runtime.RaiseRuntimeError(thread, this.span, "object cannot be called as function");
            }
Esempio n. 2
0
            public override void Execute(Runtime runtime, Runtime.Thread thread)
            {
                var value  = thread.PopValue();
                var target = thread.PopValue();

                if (target == null)
                {
                    Runtime.RaiseRuntimeError(thread, this.span, "field access on null value");
                }

                var obj = target;

                while (obj != null)
                {
                    Dictionary <string, object> fields;

                    if (obj is Runtime.Object)
                    {
                        fields = ((Runtime.Object)obj).fields;
                    }

                    else
                    {
                        break;
                    }

                    object setFunc;
                    if (fields.TryGetValue("set:" + this.fieldName, out setFunc))
                    {
                        if (setFunc is Runtime.Function)
                        {
                            var f = (Runtime.Function)setFunc;
                            thread.PushFrame(f);
                            thread.CurrentFrame().locals.Add(f.parameterNames[0], target);
                            thread.CurrentFrame().locals.Add(f.parameterNames[1], value);
                        }
                        else
                        {
                            thread.PushValue(((Runtime.ExternalFunction)setFunc).Invoke(new object[] { target, value }));
                        }

                        return;
                    }

                    if (fields.ContainsKey(this.fieldName))
                    {
                        fields[this.fieldName] = value;
                        return;
                    }

                    if (obj is Runtime.Object)
                    {
                        obj = ((Runtime.Object)obj).proto;
                    }
                    else
                    {
                        break;
                    }
                }

                Runtime.RaiseRuntimeError(thread, this.span, "unknown field `" + this.fieldName + "`");
            }
Esempio n. 3
0
            public override void Execute(Runtime runtime, Runtime.Thread thread)
            {
                var target = thread.PopValue();

                if (target == null)
                {
                    Runtime.RaiseRuntimeError(thread, this.span, "field access on null value");
                }

                var obj = target;

                while (obj != null)
                {
                    Dictionary <string, object> fields;

                    if (obj is double)
                    {
                        fields = runtime.doubleMethods;
                    }

                    else if (obj is bool)
                    {
                        fields = runtime.boolMethods;
                    }

                    else if (obj is string)
                    {
                        fields = runtime.stringMethods;
                    }

                    else if (obj is Runtime.Object)
                    {
                        fields = ((Runtime.Object)obj).fields;
                    }

                    else
                    {
                        break;
                    }

                    object getFunc;
                    if (!this.duplicateTargetAfter && fields.TryGetValue("get:" + this.fieldName, out getFunc))
                    {
                        if (getFunc is Runtime.Function)
                        {
                            var f = (Runtime.Function)getFunc;
                            thread.PushFrame(f);
                            thread.CurrentFrame().locals.Add(f.parameterNames[0], target);
                        }
                        else
                        {
                            thread.PushValue(((Runtime.ExternalFunction)getFunc).Invoke(new object[] { target }));
                        }

                        return;
                    }

                    object value;
                    if (fields.TryGetValue(this.fieldName, out value))
                    {
                        thread.PushValue(value);
                        if (this.duplicateTargetAfter)
                        {
                            thread.PushValue(target);
                        }

                        return;
                    }

                    if (obj is Runtime.Object)
                    {
                        obj = ((Runtime.Object)obj).proto;
                    }
                    else
                    {
                        break;
                    }
                }

                Runtime.RaiseRuntimeError(thread, this.span, "unknown field `" + this.fieldName + "`");
            }