- char *trueName = NULL;
- int bFound = 0; // Used to keep nesting levels down
- tSpiderValue *ret = ERRPTR;
-
- // Handle namespaces
- if( Function[0] == '.' ) {
- trueName = (char*)&Function[1];
- }
- else if( !Script->CurNamespace ) {
- trueName = (char*)Function;
- }
- else {
- int len = strlen(Script->CurNamespace) + 1 + strlen(Function);
- trueName = malloc( len + 1 );
- strcpy(trueName, Script->CurNamespace);
- strcat(trueName, ".");
- strcat(trueName, Function);
- }
-
- // First: Find the function in the script
- {
- tAST_Function *fcn = Script->Script->Functions;
- for( ; fcn; fcn = fcn->Next ) {
- if( strcmp(fcn->Name, trueName) == 0 )
- break;
- }
- // Execute!
- if(fcn) {
- tAST_BlockState bs;
- bs.FirstVar = NULL;
- bs.RetVal = NULL;
- bs.Parent = NULL;
- bs.Script = Script;
- {
- tAST_Node *arg;
- int i = 0;
- for( arg = fcn->Arguments; arg; arg = arg->NextSibling, i++ )
- {
- // TODO: Type checks
- Variable_Define(&bs, arg->DefVar.DataType, arg->DefVar.Name);
- if( i >= NArguments ) break; // TODO: Return gracefully
- Variable_SetValue(&bs, arg->DefVar.Name, Arguments[i]);
- }
- }
- ret = AST_ExecuteNode(&bs, fcn->Code);
- Object_Dereference(ret);
- ret = bs.RetVal;
- 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)
- {
- tSpiderFunction *fcn;
- // Second: Search the variant's exports
- for( fcn = Script->Variant->Functions; fcn; fcn = fcn->Next )
- {
- if( strcmp( fcn->Name, trueName ) == 0 )
- break;
- }
- // Execute!
- if(fcn) {
- // TODO: Type Checking
- ret = fcn->Handler( Script, NArguments, Arguments );
- bFound = 1;
- }
- }
-
- // Not in variant exports? Search the language internal ones
- if(!bFound)
- {
- tSpiderFunction *fcn;
- // Third: Search language exports
- for( fcn = gpExports_First; fcn; fcn = fcn->Next )
- {
- if( strcmp( fcn->Name, trueName ) == 0 )
- break;
- }
- // Execute!
- if(fcn) {
- ret = fcn->Handler( Script, NArguments, Arguments );
- bFound = 1;
- }
- }
-
- // Not found?
- if(!bFound)
- {
- fprintf(stderr, "Undefined reference to '%s'\n", trueName);
- }
-
- if( trueName != Function && trueName != &Function[1] )
- free(trueName);
-
- return ret;
-