Example #1
0
 public void AddBuiltinScript(string name, Builtin builtin)
 {
     builtins[name] = builtin;
 }
Example #2
0
        public bool Use(IokeObject self, IokeObject context, IokeObject message, string name, bool forceReload)
        {
            Runtime runtime = context.runtime;
            Builtin b       = context.runtime.GetBuiltin(name);

            if (b != null)
            {
                if (!forceReload && loaded.Contains(name))
                {
                    return(false);
                }
                else
                {
                    try {
                        b.Load(context.runtime, context, message);
                        if (!forceReload)
                        {
                            loaded.Add(name);
                        }
                        return(true);
                    } catch (Exception e) {
                        IokeObject condition = IokeObject.As(IokeObject.GetCellChain(runtime.Condition,
                                                                                     message,
                                                                                     context,
                                                                                     "Error",
                                                                                     "Load"), context).Mimic(message, context);
                        condition.SetCell("message", message);
                        condition.SetCell("context", context);
                        condition.SetCell("receiver", self);
                        condition.SetCell("moduleName", runtime.NewText(name));
                        condition.SetCell("exceptionMessage", runtime.NewText(e.Message));
                        var st = new System.Diagnostics.StackTrace(e);
                        var ob = new SaneArrayList();
                        foreach (var frame in st.GetFrames())
                        {
                            ob.Add(runtime.NewText(frame.ToString()));
                        }
                        condition.SetCell("exceptionStackTrace", runtime.NewList(ob));

                        bool[] continueLoadChain = new bool[] { false };

                        runtime.WithRestartReturningArguments(() => { runtime.ErrorCondition(condition); },
                                                              context,
                                                              new BooleanGivingRestart("continueLoadChain", continueLoadChain, true, runtime),
                                                              new BooleanGivingRestart("ignoreLoadError", continueLoadChain, false, runtime));
                        if (!continueLoadChain[0])
                        {
                            return(false);
                        }
                    }
                }
            }

            var paths = ((IokeList)IokeObject.dataOf(loadPath)).List;

            string[] suffixes = (name.EndsWith(".ik")) ? SUFFIXES_WITH_BLANK : SUFFIXES;

            // Absolute path
            foreach (string suffix in suffixes)
            {
                string before = "/";
                if (name.StartsWith("/"))
                {
                    before = "";
                }


                try {
                    FileInfo f = new FileInfo(name + suffix);
                    if (f.Exists)
                    {
                        if (!forceReload && loaded.Contains(f.FullName))
                        {
                            return(false);
                        }
                        else
                        {
                            context.runtime.EvaluateFile(f, message, context);
                        }
                        if (!forceReload)
                        {
                            loaded.Add(f.FullName);
                        }
                        return(true);
                    }

                    string xname = (before + name + suffix).Replace("/", ".");
                    if (xname.StartsWith("."))
                    {
                        xname = xname.Substring(1);
                    }
                    Stream s = typeof(IokeSystem).Assembly.GetManifestResourceStream(xname);
                    if (s != null)
                    {
                        if (!forceReload && loaded.Contains(name + suffix))
                        {
                            return(false);
                        }
                        else
                        {
                            context.runtime.EvaluateStream(name + suffix, new StreamReader(s, System.Text.Encoding.UTF8), message, context);
                        }
                        if (!forceReload)
                        {
                            loaded.Add(name + suffix);
                        }
                        return(true);
                    }
                } catch (FileNotFoundException) {
                    // ignore
                } catch (Exception e) {
                    IokeObject condition = IokeObject.As(IokeObject.GetCellChain(runtime.Condition,
                                                                                 message,
                                                                                 context,
                                                                                 "Error",
                                                                                 "Load"), context).Mimic(message, context);
                    condition.SetCell("message", message);
                    condition.SetCell("context", context);
                    condition.SetCell("receiver", self);
                    condition.SetCell("moduleName", runtime.NewText(name));
                    condition.SetCell("exceptionMessage", runtime.NewText(e.Message));
                    var st = new System.Diagnostics.StackTrace(e);
                    var ob = new SaneArrayList();
                    foreach (var frame in st.GetFrames())
                    {
                        ob.Add(runtime.NewText(frame.ToString()));
                    }
                    condition.SetCell("exceptionStackTrace", runtime.NewList(ob));

                    bool[] continueLoadChain = new bool[] { false };

                    runtime.WithRestartReturningArguments(() => { runtime.ErrorCondition(condition); },
                                                          context,
                                                          new BooleanGivingRestart("continueLoadChain", continueLoadChain, true, runtime),
                                                          new BooleanGivingRestart("ignoreLoadError", continueLoadChain, false, runtime));
                    if (!continueLoadChain[0])
                    {
                        return(false);
                    }
                }
            }

            foreach (object o in paths)
            {
                string currentS = Text.GetText(o);

                foreach (string suffix in suffixes)
                {
                    string before = "/";
                    if (name.StartsWith("/"))
                    {
                        before = "";
                    }

                    try {
                        FileInfo f;

                        if (IsAbsoluteFileName(currentS))
                        {
                            f = new FileInfo(Path.Combine(currentS, name + suffix));
                        }
                        else
                        {
                            f = new FileInfo(Path.Combine(Path.Combine(currentWorkingDirectory, currentS), name + suffix));
                        }

                        if (f.Exists)
                        {
                            if (!forceReload && loaded.Contains(f.FullName))
                            {
                                return(false);
                            }
                            else
                            {
                                context.runtime.EvaluateFile(f, message, context);
                                if (!forceReload)
                                {
                                    loaded.Add(f.FullName);
                                }
                                return(true);
                            }
                        }

                        string yname = (before + name + suffix).Replace("/", ".");
                        if (yname.StartsWith("."))
                        {
                            yname = yname.Substring(1);
                        }

                        Stream ss = typeof(IokeSystem).Assembly.GetManifestResourceStream(yname);
                        if (ss != null)
                        {
                            if (!forceReload && loaded.Contains(name + suffix))
                            {
                                return(false);
                            }
                            else
                            {
                                context.runtime.EvaluateStream(name + suffix, new StreamReader(ss, System.Text.Encoding.UTF8), message, context);
                            }
                            if (!forceReload)
                            {
                                loaded.Add(name + suffix);
                            }
                            return(true);
                        }
                    } catch (FileNotFoundException) {
                        // ignore
                    } catch (Exception e) {
                        IokeObject condition = IokeObject.As(IokeObject.GetCellChain(runtime.Condition,
                                                                                     message,
                                                                                     context,
                                                                                     "Error",
                                                                                     "Load"), context).Mimic(message, context);
                        condition.SetCell("message", message);
                        condition.SetCell("context", context);
                        condition.SetCell("receiver", self);
                        condition.SetCell("moduleName", runtime.NewText(name));
                        condition.SetCell("exceptionMessage", runtime.NewText(e.Message));
                        var st = new System.Diagnostics.StackTrace(e);
                        var ob = new SaneArrayList();
                        foreach (var frame in st.GetFrames())
                        {
                            ob.Add(runtime.NewText(frame.ToString()));
                        }
                        condition.SetCell("exceptionStackTrace", runtime.NewList(ob));

                        bool[] continueLoadChain = new bool[] { false };

                        runtime.WithRestartReturningArguments(() => { runtime.ErrorCondition(condition); },
                                                              context,
                                                              new BooleanGivingRestart("continueLoadChain", continueLoadChain, true, runtime),
                                                              new BooleanGivingRestart("ignoreLoadError", continueLoadChain, false, runtime));
                        if (!continueLoadChain[0])
                        {
                            return(false);
                        }
                    }
                }
            }

            IokeObject condition2 = IokeObject.As(IokeObject.GetCellChain(runtime.Condition,
                                                                          message,
                                                                          context,
                                                                          "Error",
                                                                          "Load"), context).Mimic(message, context);

            condition2.SetCell("message", message);
            condition2.SetCell("context", context);
            condition2.SetCell("receiver", self);
            condition2.SetCell("moduleName", runtime.NewText(name));

            runtime.WithReturningRestart("ignoreLoadError", context, () => { runtime.ErrorCondition(condition2); });
            return(false);
        }