Ejemplo n.º 1
0
        public void RegisterFunction(string name, LibraryFunction functionRef, int numberOfRequiredParameters)
        {
            ExternalFunctionRef extFuncRef = new ExternalFunctionRef(name, functionRef, numberOfRequiredParameters);

            // TODO: check for object redefinition
            libraryData.Insert(name, new ExternalFunctionRefValue(extFuncRef));
        }
Ejemplo n.º 2
0
        public void RegisterGlobalFunction(string name, LibraryFunction functionRef, int numberOfRequiredParameters)
        {
            ExternalFunctionRef extFuncRef = new ExternalFunctionRef(name, functionRef, numberOfRequiredParameters);

            // TODO: check for object redefinition
            ValueStore functionObject = state.RegisterGlobalObject(name);

            functionObject.Value = new ExternalFunctionRefValue(extFuncRef);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Constructor, which initializes the new external-function-refference value with the desired refference.
 /// </summary>
 /// <param name="functionRef">An initialized and valid external function refference.</param>
 public ExternalFunctionRefValue(ExternalFunctionRef functionRef)
 {
     valueType = ValueTypeID.TYPE_CFUNCTIONREF;
     val       = functionRef;
 }
Ejemplo n.º 4
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // pointer to _argc
            IValue argcValue = ev.Stack.ReadTop();

            if (argcValue.TypeOf() != ValueTypeID.TYPE_NUMBER)
            {
                Console.WriteLine(">> exp. argc");
                // TODO: add more specific error code here
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADTYPE));
            }

            // get number of passed params
            int numberOfPassedParams = (int)argcValue.GetNumericValue();

            // functionref is stored on the stack[numparams] by pushp instuction,
            // so GetVal() is not neccessary
            IValue functionRefValue = ev.Stack.ReadN(ev.Stack.StackTop - (numberOfPassedParams + 1)); // n + 1 = count with _argc

            switch (functionRefValue.TypeOf())
            {
            case ValueTypeID.TYPE_FUNCTIONREF:
                // store the return address on the stack
                ev.Stack.Push(new RTSAValue(ev.RegPC));

                // check parameter counts
                FunctionRef functionRef = (FunctionRef)functionRefValue.GetObjectValue();
                if (CheckParams(functionRef.NumberOfDefinedParameters, numberOfPassedParams) == false)
                {
                    throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADPARAMCOUNT));
                }

                // set PC to new address and test it
                OpCodeListItem functionStartAddress = functionRef.CodePart.First();
                if (functionStartAddress != null)
                {
                    ev.RegPC = functionStartAddress;
                }
                else
                {
                    // TODO: add more specific error code here
                    throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADJMPTARGET));
                }
                break;

            case ValueTypeID.TYPE_CFUNCTIONREF:
                // check parameter counts
                ExternalFunctionRef extFunctionRef = (ExternalFunctionRef)functionRefValue.GetObjectValue();
                if (CheckParams(extFunctionRef.NumberOfDefinedParameters, numberOfPassedParams) == false)
                {
                    throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADPARAMCOUNT));
                }

                // call the function
                extFunctionRef.FunctionRef(numberOfPassedParams);
                break;

            default:
                //Console.WriteLine(">> type is {0}", functionRefValue.TypeOf().ToString());
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPEFUNCREF));
            }
        }