Exemple #1
0
 public InitDreamObjectState(DreamThread thread, DreamObject dreamObject, DreamObject usr, DreamProcArguments arguments)
     : base(thread)
 {
     IoCManager.InjectDependencies(this);
     _dreamObject = dreamObject;
     _usr         = usr;
     _arguments   = arguments;
 }
Exemple #2
0
 public State(NativeProc proc, DreamThread thread, DreamObject src, DreamObject usr, DreamProcArguments arguments)
     : base(thread)
 {
     _proc     = proc;
     Src       = src;
     Usr       = usr;
     Arguments = arguments;
 }
Exemple #3
0
        public DreamThread Spawn()
        {
            var thread = new DreamThread();

            var state = new DMProcState(this, thread);

            thread.PushProcState(state);

            return(thread);
        }
        public override ProcState CreateState(DreamThread thread, DreamObject src, DreamObject usr, DreamProcArguments arguments)
        {
            if (_defaultArgumentValues != null)
            {
                foreach (KeyValuePair <string, DreamValue> defaultArgumentValue in _defaultArgumentValues)
                {
                    int argumentIndex = ArgumentNames.IndexOf(defaultArgumentValue.Key);

                    if (arguments.GetArgument(argumentIndex, defaultArgumentValue.Key) == DreamValue.Null)
                    {
                        arguments.NamedArguments.Add(defaultArgumentValue.Key, defaultArgumentValue.Value);
                    }
                }
            }

            return(new State(this, _taskFunc, thread, src, usr, arguments));
        }
Exemple #5
0
        public DMProcState(DMProcState other, DreamThread thread)
            : base(thread)
        {
            if (other.EnumeratorStack.Count > 0)
            {
                throw new NotImplementedException();
            }

            _proc         = other._proc;
            Instance      = other.Instance;
            Usr           = other.Usr;
            ArgumentCount = other.ArgumentCount;
            _pc           = other._pc;

            _stack = _stackPool.Rent(other._stack.Length);
            Array.Copy(other._stack, _stack, _stack.Length);

            _localVariables = _dreamValuePool.Rent(256);
            Array.Copy(other._localVariables, _localVariables, 256);
        }
Exemple #6
0
        public static DreamValue NativeProc_Replace(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            DreamRegex dreamRegex = DreamMetaObjectRegex.ObjectToDreamRegex[instance];

            DreamValue haystack = arguments.GetArgument(0, "haystack");
            DreamValue replace  = arguments.GetArgument(1, "replacement");
            int        start    = arguments.GetArgument(2, "Start").GetValueAsInteger();
            int        end      = arguments.GetArgument(3, "End").GetValueAsInteger();

            if (!haystack.TryGetValueAsString(out var haystackString))
            {
                if (haystack == DreamValue.Null)
                {
                    return(DreamValue.Null);
                }
                //TODO Check what actually happens
                throw new ArgumentException("Bad regex haystack");
            }
            string haystackSubstring = haystackString;

            if (end != 0)
            {
                haystackSubstring = haystackString.Substring(0, end - start);
            }

            if (replace.TryGetValueAsProc(out DreamProc replaceProc))
            {
                return(DoProcReplace(replaceProc));
            }
            if (replace.TryGetValueAsString(out string replaceString))
            {
                return(DoTextReplace(replaceString));
            }

            if (replace.TryGetValueAsPath(out var procPath) && procPath.LastElement is not null)
            {
                var dreamMan = IoCManager.Resolve <IDreamManager>();
                if (dreamMan.ObjectTree.TryGetGlobalProc(procPath.LastElement, out DreamProc? proc))
                {
                    return(DoProcReplace(proc));
                }
            }

            throw new ArgumentException("Replacement argument must be a string or a proc");

            DreamValue DoProcReplace(DreamProc proc)
            {
                if (dreamRegex.IsGlobal)
                {
                    throw new NotImplementedException("Proc global regex replacements are not implemented");
                }
                var match              = dreamRegex.Regex.Match(haystackSubstring);
                var groups             = match.Groups;
                List <DreamValue> args = new List <DreamValue>(groups.Count);

                foreach (Group group in groups)
                {
                    args.Add(new DreamValue(group.Value));
                }
                var result = DreamThread.Run(async(state) => await state.Call(proc, instance, null, new DreamProcArguments(args)));

                if (result.TryGetValueAsString(out var replacement))
                {
                    return(DoTextReplace(replacement));
                }
                //TODO Confirm this behavior
                if (result == DreamValue.Null)
                {
                    return(new DreamValue(haystackSubstring));
                }
                throw new ArgumentException("Replacement is not a string");
            }

            DreamValue DoTextReplace(string replacement)
            {
                string replaced = dreamRegex.Regex.Replace(haystackSubstring, replacement, dreamRegex.IsGlobal ? -1 : 1, start - 1);

                if (end != 0)
                {
                    replaced += haystackString.Substring(end - start + 1);
                }

                instance.SetVariable("text", new DreamValue(replaced));
                return(new DreamValue(replaced));
            }
        }
 public State(AsyncNativeProc proc, Func <State, Task <DreamValue> > taskFunc, DreamThread thread, DreamObject src, DreamObject usr, DreamProcArguments arguments)
     : base(thread)
 {
     _proc     = proc;
     _taskFunc = taskFunc;
     Src       = src;
     Usr       = usr;
     Arguments = arguments;
 }
 public static ProcState CreateAnonymousState(DreamThread thread, Func <State, Task <DreamValue> > taskFunc)
 {
     return(new State(null, taskFunc, thread, null, null, new DreamProcArguments(null)));
 }
Exemple #9
0
 public static ProcState CreateAnonymousState(DreamThread thread, HandlerFn handler)
 {
     return(new State(null, thread, null, null, new DreamProcArguments(null)));
 }
Exemple #10
0
 public override DMProcState CreateState(DreamThread thread, DreamObject src, DreamObject usr, DreamProcArguments arguments)
 {
     return(new DMProcState(this, thread, _maxStackSize, src, usr, arguments));
 }