/// <summary> /// Safely execute a function /// </summary> /// <param name="info">Callback information for the function</param> internal void SafeExecute(FunctionCallbackInfo info) { try { Execute(info); } catch (InterpreterException) { throw; } catch (Exception e) { ThrowHelper.NativeFunctionError(Name, e); } }
/// <summary> /// Execute a function inside the interpreter /// </summary> /// <param name="function">Function to be executed</param> /// <param name="parameters">Parameters to be passed to a function</param> /// <returns>The result of the function</returns> public override KObject CallFunction(KFunction function, List <KLocal> parameters) { FunctionSymbol fSymbol = function.Value; FunctionSpace fSpace = new FunctionSpace(fSymbol); MemorySpace savedSpace = _currentSpace; _currentSpace = fSpace; if (!function.IsNative) { parameters.ForEach(x => fSpace[x.Name] = x); } KObject result = null; _stack.Push(fSpace); try { if (function.IsNative) { FunctionCallbackInfo cInfo = new FunctionCallbackInfo(parameters, this); ((NativeFunctionSymbol)fSymbol).NativeFunction.SafeExecute(cInfo); result = cInfo.ReturnValue.Value; } else { Execute(fSymbol.BlockAst); } } catch (ReturnValue returnValue) { result = returnValue.Value; } finally { _currentSpace = savedSpace; } _stack.Pop(); return(result); }
/// <summary> /// Function extended that contains the real logic for the function /// </summary> /// <param name="info">Callback information for the function</param> public abstract void Execute(FunctionCallbackInfo info);