+/**
+ * \brief Free a value
+ * \note Just calls Object_Dereference
+ */
+void SpiderScript_FreeValue(tSpiderValue *Value)
+{
+ Object_Dereference(Value);
+}
+
+/**
+ * \brief Dump a value into a string
+ * \return Heap string
+ */
+char *SpiderScript_DumpValue(tSpiderValue *Value)
+{
+ char *ret;
+ if( Value == ERRPTR )
+ return strdup("ERRPTR");
+ if( Value == NULL )
+ return strdup("null");
+
+ switch( (enum eSpiderScript_DataTypes)Value->Type )
+ {
+ case SS_DATATYPE_UNDEF: return strdup("undefined");
+
+ case SS_DATATYPE_INTEGER:
+ ret = malloc( sizeof(Value->Integer)*2 + 3 );
+ sprintf(ret, "0x%lx", Value->Integer);
+ return ret;
+
+ case SS_DATATYPE_REAL:
+ ret = malloc( sprintf(NULL, "%f", Value->Real) + 1 );
+ sprintf(ret, "%f", Value->Real);
+ return ret;
+
+ case SS_DATATYPE_STRING:
+ ret = malloc( Value->String.Length + 3 );
+ ret[0] = '"';
+ strcpy(ret+1, Value->String.Data);
+ ret[Value->String.Length+1] = '"';
+ ret[Value->String.Length+2] = '\0';
+ return ret;
+
+ case SS_DATATYPE_OBJECT:
+ ret = malloc( sprintf(NULL, "{%s *%p}", Value->Object->Type->Name, Value->Object) + 1 );
+ sprintf(ret, "{%s *%p}", Value->Object->Type->Name, Value->Object);
+ return ret;
+
+ case SS_DATATYPE_OPAQUE:
+ ret = malloc( sprintf(NULL, "*%p", Value->Opaque.Data) + 1 );
+ sprintf(ret, "*%p", Value->Opaque.Data);
+ return ret;
+
+ case SS_DATATYPE_ARRAY:
+ return strdup("Array");
+
+ default:
+ AST_RuntimeError(NULL, "Unknown type %i in Object_Dump", Value->Type);
+ return NULL;
+ }
+
+}
+
+/**
+ * \brief Execute a script function
+ * \param Script Script context to execute in
+ * \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)
+{
+ char *trueName = NULL;
+ 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)
+ {
+ Object_Dereference(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 '%s'\n", trueName);
+ return ERRPTR;
+ }
+
+ return ret;
+}
+
+/**
+ * \brief Execute an object method function
+ * \param Script Script context to execute in
+ * \param Function Function name to execute
+ * \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)
+{
+ tSpiderFunction *fcn;
+ tSpiderValue this;
+ tSpiderValue *newargs[NArguments+1];
+ int i;
+
+ for( fcn = Object->Type->Methods; fcn; fcn = fcn->Next )
+ {
+ if( strcmp(fcn->Name, MethodName) == 0 )
+ break;
+ }
+
+ if( !fcn )
+ {
+ AST_RuntimeError(NULL, "Class '%s' does not have a method '%s'",
+ Object->Type->Name, MethodName);
+ return ERRPTR;
+ }
+
+ this.Type = SS_DATATYPE_OBJECT;
+ this.ReferenceCount = 1;
+ this.Object = Object;
+
+ newargs[0] = &this;
+ memcpy(&newargs[1], Arguments, NArguments*sizeof(tSpiderValue*));
+
+ // TODO: Type Checking
+ for( i = 0; fcn->ArgTypes[i]; i ++ )
+ {
+ if( i >= NArguments ) {
+ AST_RuntimeError(NULL, "Argument count mismatch (%i passed)",
+ NArguments);
+ return ERRPTR;
+ }
+ if( Arguments[i] && Arguments[i]->Type != fcn->ArgTypes[i] )
+ {
+ AST_RuntimeError(NULL, "Argument type mismatch (%i, expected %i)",
+ Arguments[i]->Type, fcn->ArgTypes[i]);
+ return ERRPTR;
+ }
+ }
+
+ return fcn->Handler(Script, NArguments+1, newargs);
+}
+
+/**
+ * \brief Execute a script function
+ * \param Script Script context to execute in
+ * \param Function Function name to execute
+ * \param NArguments Number of arguments to pass
+ * \param Arguments Arguments passed
+ */
+tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
+ tSpiderNamespace *Namespace, const char *ClassName,
+ int NArguments, tSpiderValue **Arguments)
+{
+ int bFound = 0; // Used to keep nesting levels down
+ tSpiderValue *ret = ERRPTR;
+ tSpiderObjectDef *class;
+
+ // First: Find the function in the script
+ // TODO: Implement scripted classes
+ #if 0
+ {
+ tAST_Function *astClass;
+ for( astClass = Script->Script->Classes; astClass; astClass = astClass->Next )
+ {
+ if( strcmp(astClass->Name, ClassName) == 0 )
+ break;
+ }
+ // Execute!
+ if(astClass)
+ {
+ 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 ++;
+
+ 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 )
+ {
+ Object_Dereference(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;
+ }
+ }
+ }
+ #endif
+
+ // Didn't find it in script?
+ if(!bFound)
+ {
+ class = NULL; // Just to allow the below code to be neat
+
+ // Second: Scan current namespace
+ if( !class && Namespace )
+ {
+ for( class = Namespace->Classes; class; class = class->Next )
+ {
+ if( strcmp( class->Name, ClassName ) == 0 )
+ break;
+ }
+ }
+
+ #if 0
+ // Third: Search the variant's global exports
+ if( !class )
+ {
+ for( class = Script->Variant->Classes; class; class = fcn->Next )
+ {
+ if( strcmp( class->Name, Function ) == 0 )
+ break;
+ }
+ }
+ #endif
+
+ #if 0
+ // Fourth: Search language exports
+ if( !class )
+ {
+ for( class = gpExports_First; class; class = fcn->Next )
+ {
+ if( strcmp( class->Name, ClassName ) == 0 )
+ break;
+ }
+ }
+ #endif
+
+ // Execute!
+ if(class)
+ {
+ tSpiderObject *obj;
+ // TODO: Type Checking
+ obj = class->Constructor( NArguments, Arguments );
+ if( obj == NULL || obj == ERRPTR )
+ return (void *)obj;
+
+ ret = malloc( sizeof(tSpiderValue) );
+ ret->Type = SS_DATATYPE_OBJECT;
+ ret->ReferenceCount = 1;
+ ret->Object = obj;
+ bFound = 1;
+ }
+ }
+
+ // Not found?
+ if(!bFound)
+ {
+ fprintf(stderr, "Undefined reference to '%s'\n", ClassName);
+ return ERRPTR;
+ }
+
+ return ret;
+}
+
+