-/**
- * \brief Execute a script function
- * \param Script Script context to execute in
- * \param Namespace Namespace to search for the function
- * \param Function Function name to execute
- * \param NArguments Number of arguments to pass
- * \param Arguments Arguments passed
- */
-tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
- tSpiderNamespace *Namespace, const char *Function,
- int NArguments, tSpiderValue **Arguments)
-{
- int bFound = 0; // Used to keep nesting levels down
- tSpiderValue *ret = ERRPTR;
- tSpiderFunction *fcn;
-
- // First: Find the function in the script
- {
- tAST_Function *astFcn;
- for( astFcn = Script->Script->Functions; astFcn; astFcn = astFcn->Next )
- {
- if( strcmp(astFcn->Name, Function) == 0 )
- break;
- }
- // Execute!
- if(astFcn)
- {
- tAST_BlockState bs;
- tAST_Node *arg;
- int i = 0;
-
- // Build a block State
- bs.FirstVar = NULL;
- bs.RetVal = NULL;
- bs.Parent = NULL;
- bs.BaseNamespace = &Script->Variant->RootNamespace;
- bs.CurNamespace = NULL;
- bs.Script = Script;
- bs.Ident = giNextBlockIdent ++;
-
- // Parse arguments
- for( arg = astFcn->Arguments; arg; arg = arg->NextSibling, i++ )
- {
- if( i >= NArguments ) break; // TODO: Return gracefully
- // TODO: Type checks
- Variable_Define(&bs,
- arg->DefVar.DataType, arg->DefVar.Name,
- Arguments[i]);
- }
-
- // Execute function
- ret = AST_ExecuteNode(&bs, astFcn->Code);
- if(ret != ERRPTR)
- {
- SpiderScript_DereferenceValue(ret); // Dereference output of last block statement
- ret = bs.RetVal; // Set to return value of block
- }
- bFound = 1;
-
- while(bs.FirstVar)
- {
- tAST_Variable *nextVar = bs.FirstVar->Next;
- Variable_Destroy( bs.FirstVar );
- bs.FirstVar = nextVar;
- }
- }
- }
-
- // Didn't find it in script?
- if(!bFound)
- {
- fcn = NULL; // Just to allow the below code to be neat
-
- // Second: Scan current namespace
- if( !fcn && Namespace )
- {
- for( fcn = Namespace->Functions; fcn; fcn = fcn->Next )
- {
- if( strcmp( fcn->Name, Function ) == 0 )
- break;
- }
- }
-
- // Third: Search the variant's global exports
- if( !fcn )
- {
- for( fcn = Script->Variant->Functions; fcn; fcn = fcn->Next )
- {
- if( strcmp( fcn->Name, Function ) == 0 )
- break;
- }
- }
-
- // Fourth: Search language exports
- if( !fcn )
- {
- for( fcn = gpExports_First; fcn; fcn = fcn->Next )
- {
- if( strcmp( fcn->Name, Function ) == 0 )
- break;
- }
- }
-
- // Execute!
- if(fcn)
- {
- // TODO: Type Checking
- ret = fcn->Handler( Script, NArguments, Arguments );
- bFound = 1;
- }
- }
-
- // Not found?
- if(!bFound)
- {
- fprintf(stderr, "Undefined reference to function '%s' (ns='%s')\n",
- Function, Namespace->Name);
- return ERRPTR;
- }
-
- return ret;
-}
-
-/**
- * \brief Execute an object method function
- * \param Script Script context to execute in
- * \param Object Object in which to find the method
- * \param MethodName Name of method to call
- * \param NArguments Number of arguments to pass
- * \param Arguments Arguments passed
- */
-tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
- tSpiderObject *Object, const char *MethodName,
- int NArguments, tSpiderValue **Arguments)