/// <summary> /// The spawn function runs a given function or script in a different /// thread. /// </summary> /// <remarks> /// The spawn function runs a given function or script in a different /// thread. /// js> function g() { a = 7; } /// js> a = 3; /// 3 /// js> spawn(g) /// Thread[Thread-1,5,main] /// js> a /// 3 /// </remarks> public static object Spawn(Context cx, Scriptable thisObj, object[] args, Function funObj) { Scriptable scope = funObj.GetParentScope(); Runner runner; if (args.Length != 0 && args[0] is Function) { object[] newArgs = null; if (args.Length > 1 && args[1] is Scriptable) { newArgs = cx.GetElements((Scriptable)args[1]); } if (newArgs == null) { newArgs = ScriptRuntime.emptyArgs; } runner = new Runner(scope, (Function)args[0], newArgs); } else { if (args.Length != 0 && args[0] is Script) { runner = new Runner(scope, (Script)args[0]); } else { throw ReportRuntimeError("msg.spawn.args"); } } runner.factory = cx.GetFactory(); Sharpen.Thread thread = new Sharpen.Thread(runner); thread.Start(); return thread; }
private static Rhino.Tools.Shell.Global GetInstance(Function function) { Scriptable scope = function.GetParentScope(); if (!(scope is Rhino.Tools.Shell.Global)) { throw ReportRuntimeError("msg.bad.shell.function.scope", scope.ToString()); } return (Rhino.Tools.Shell.Global)scope; }