private DynValue Internal_Call(ExecutionControlToken ecToken, DynValue function, params DynValue[] args) { this.CheckScriptOwnership(function); this.CheckScriptOwnership(args); if (function.Type != DataType.Function && function.Type != DataType.ClrFunction) { DynValue metafunction = m_MainProcessor.GetMetamethod(ecToken, function, "__call"); if (metafunction != null) { DynValue[] metaargs = new DynValue[args.Length + 1]; metaargs[0] = function; for (int i = 0; i < args.Length; i++) { metaargs[i + 1] = args[i]; } function = metafunction; args = metaargs; } else { throw new ArgumentException("function is not a function and has no __call metamethod."); } } else if (function.Type == DataType.ClrFunction) { return(function.Callback.ClrCallback(this.CreateDynamicExecutionContext(ecToken, function.Callback), new CallbackArguments(args, false))); } return(m_MainProcessor.Call(ecToken, function, args)); }
internal ScriptExecutionContext(ExecutionControlToken ecToken, Processor p, CallbackFunction callBackFunction, SourceRef sourceRef, bool isDynamic = false) { IsDynamicExecution = isDynamic; m_Processor = p; m_Callback = callBackFunction; m_EcToken = ecToken; CallingLocation = sourceRef; }
internal ScriptExecutionContext(ExecutionControlToken ecToken, Processor p, CallbackFunction callBackFunction, SourceRef sourceRef, bool isDynamic = false) { this.IsDynamicExecution = isDynamic; _processor = p; _callback = callBackFunction; _ecToken = ecToken; this.CallingLocation = sourceRef; }
/// <summary> /// Gets a "meta" operation on this userdata. If a descriptor does not support this functionality, /// it should return "null" (not a nil). /// These standard metamethods can be supported (the return value should be a function accepting the /// classic parameters of the corresponding metamethod): /// __add, __sub, __mul, __div, __div, __pow, __unm, __eq, __lt, __le, __lt, __len, __concat, /// __pairs, __ipairs, __iterator, __call /// These standard metamethods are supported through other calls for efficiency: /// __index, __newindex, __tostring /// </summary> /// <param name="ecToken">The execution control token of the script processing thread</param> /// <param name="script">The script originating the request</param> /// <param name="obj">The object (null if a static request is done)</param> /// <param name="metaname">The name of the metamember.</param> public DynValue MetaIndex(ExecutionControlToken ecToken, Script script, object obj, string metaname) { var u = obj as IUserDataType; if (u != null) { return(u.MetaIndex(ecToken, script, metaname)); } return(null); }
/// <summary> /// Asynchronously calls the specified function. /// This method is supported only on .NET 4.x and .NET 4.x PCL targets. /// </summary> /// <param name="ecToken">The execution control token to be associated with the execution of this function</param> /// <param name="function">The Lua/MoonSharp function to be called</param> /// <param name="args">The arguments to pass to the function.</param> /// <returns> /// The return value(s) of the function call. /// </returns> /// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception> public Task <DynValue> CallAsync(ExecutionControlToken ecToken, DynValue function, params object[] args) { DynValue[] dargs = new DynValue[args.Length]; for (int i = 0; i < dargs.Length; i++) { dargs[i] = DynValue.FromObject(this, args[i]); } return(CallAsync(ecToken, function, dargs)); }
/// <summary> /// Performs an "index" "set" operation. /// </summary> /// <param name="ecToken">The execution control token of the script processing thread</param> /// <param name="script">The script originating the request</param> /// <param name="obj">The object (null if a static request is done)</param> /// <param name="index">The index.</param> /// <param name="value">The value to be set</param> /// <param name="isDirectIndexing">If set to true, it's indexed with a name, if false it's indexed through brackets.</param> /// <returns></returns> public bool SetIndex(ExecutionControlToken ecToken, Script script, object obj, DynValue index, DynValue value, bool isDirectIndexing) { IUserDataType u = obj as IUserDataType; if (u != null) { return(u.SetIndex(ecToken, script, index, value, isDirectIndexing)); } return(false); }
/// <summary> /// Performs an "index" "get" operation. /// </summary> /// <param name="ecToken">The execution control token of the script processing thread</param> /// <param name="script">The script originating the request</param> /// <param name="obj">The object (null if a static request is done)</param> /// <param name="index">The index.</param> /// <param name="isDirectIndexing">If set to true, it's indexed with a name, if false it's indexed through brackets.</param> /// <returns></returns> public DynValue Index(ExecutionControlToken ecToken, Script script, object obj, DynValue index, bool isDirectIndexing) { IUserDataType u = obj as IUserDataType; if (u != null) { return(u.Index(ecToken, script, index, isDirectIndexing)); } return(null); }
/// <summary> /// Asynchronously calls this function with the specified args /// </summary> /// <param name="ecToken"></param> /// <param name="args">The arguments to pass to the function.</param> /// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception> public Task <DynValue> CallAsync(ExecutionControlToken ecToken, params DynValue[] args) { return(this.OwnerScript.CallAsync(ecToken, this, args)); }
/// <summary> /// Asynchronously calls this function with the specified args /// </summary> /// <param name="ecToken"></param> /// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception> public Task <DynValue> CallAsync(ExecutionControlToken ecToken) { return(this.OwnerScript.CallAsync(ecToken, this)); }
/// <summary> /// Gets an execution context exposing only partial functionality, which should be used for /// those cases where the execution engine is not really running - for example for dynamic expression /// or calls from CLR to CLR callbacks /// </summary> internal ScriptExecutionContext CreateDynamicExecutionContext(ExecutionControlToken ecToken, CallbackFunction func = null) { return(new ScriptExecutionContext(ecToken, m_MainProcessor, func, null, isDynamic: true)); }
/// <summary> /// Asynchronously calls the specified function. /// /// This method is supported only on .NET 4.x and .NET 4.x PCL targets. /// </summary> /// <param name="ecToken">The execution control token to be associated with the execution of this function</param> /// <param name="function">The Lua/MoonSharp function to be called</param> /// <param name="args">The arguments to pass to the function.</param> /// <returns></returns> /// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception> public Task <DynValue> CallAsync(ExecutionControlToken ecToken, object function, params object[] args) { return(CallAsync(ecToken, DynValue.FromObject(this, function), args)); }
/// <summary> /// Asynchronously calls the specified function. /// This method is supported only on .NET 4.x and .NET 4.x PCL targets. /// </summary> /// <param name="ecToken">The execution control token to be associated with the execution of this function</param> /// <param name="function">The Lua/MoonSharp function to be called</param> /// <param name="args">The arguments to pass to the function.</param> /// <returns> /// The return value(s) of the function call. /// </returns> /// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception> public Task <DynValue> CallAsync(ExecutionControlToken ecToken, DynValue function, params DynValue[] args) { return(Task.Factory.StartNew(() => Internal_Call(ecToken, function, args))); }
/// <summary> /// Calls the specified function. /// This method is supported only on .NET 4.x and .NET 4.x PCL targets. /// </summary> /// <param name="ecToken">The execution control token to be associated with the execution of this function</param> /// <param name="function">The Lua/MoonSharp function to be called</param> /// <returns> /// The return value(s) of the function call. /// </returns> /// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception> public Task <DynValue> CallAsync(ExecutionControlToken ecToken, DynValue function) { return(CallAsync(ecToken, function, new DynValue[0])); }
/// <summary> /// Asynchronously loads and executes a file containing a Lua/MoonSharp script. /// /// This method is supported only on .NET 4.x and .NET 4.x PCL targets. /// </summary> /// <param name="ecToken">The execution control token to be associated with the execution of this function</param> /// <param name="filename">The filename.</param> /// <param name="globalContext">The global context.</param> /// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param> /// <returns> /// A DynValue containing the result of the processing of the loaded chunk. /// </returns> public Task <DynValue> DoFileAsync(ExecutionControlToken ecToken, string filename, Table globalContext = null, string codeFriendlyName = null) { return(LoadFileAsync(filename, globalContext, codeFriendlyName) .ContinueWith((Task <DynValue> prevTask) => CallAsync(ecToken, prevTask.Result).Result)); }