SpiderScript - Moved header to directory, ready to remove from tree
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / exec_bytecode.c
index 1a74d4c..9de7281 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include "ast.h"
+#include <inttypes.h>
 
-//#define DEBUG_F(v...)        printf(v)
-#define DEBUG_F(v...)
+#define TRACE  0
+
+#if TRACE
+# define DEBUG_F(v...) printf(v)
+#else
+# define DEBUG_F(v...)
+#endif
 
 // === IMPORTS ===
 extern void    AST_RuntimeError(tAST_Node *Node, const char *Format, ...);
@@ -35,8 +41,8 @@ struct sBC_StackEnt
 {
        uint8_t Type;
        union {
-               uint64_t        Integer;
-               double          Real;
+               int64_t Integer;
+               double  Real;
                tSpiderValue    *Reference;     // Used for everything else
                tSpiderObject   *Object;
                tSpiderNamespace        *Namespace;
@@ -78,7 +84,7 @@ int Bytecode_int_IsStackEntTrue(tBC_StackEnt *Ent)
        case SS_DATATYPE_INTEGER:
                return !!Ent->Integer;
        case SS_DATATYPE_REAL:
-               return (-.5f < Ent->Real && Ent->Real < 0.5f);
+               return !(-.5f < Ent->Real && Ent->Real < 0.5f);
        case SS_DATATYPE_OBJECT:
                return Ent->Object != NULL;
        case ET_FUNCTION_START:
@@ -99,6 +105,7 @@ tSpiderValue *Bytecode_int_GetSpiderValue(tBC_StackEnt *Ent, tSpiderValue *tmp)
                        tmp = malloc(sizeof(tSpiderValue));
                        tmp->ReferenceCount = 1;
                } else {
+                       // Stops a stack value from having free() called on it
                        tmp->ReferenceCount = 2;
                }
                break;
@@ -123,6 +130,7 @@ tSpiderValue *Bytecode_int_GetSpiderValue(tBC_StackEnt *Ent, tSpiderValue *tmp)
                AST_RuntimeError(NULL, "_GetSpiderValue on ET_FUNCTION_START");
                return NULL;
        default:
+               SpiderScript_ReferenceValue(Ent->Reference);
                return Ent->Reference;
        }
 }
@@ -147,6 +155,7 @@ void Bytecode_int_SetSpiderValue(tBC_StackEnt *Ent, tSpiderValue *Value)
        case SS_DATATYPE_OBJECT:
                Ent->Type = SS_DATATYPE_OBJECT;
                Ent->Object = Value->Object;
+               Ent->Object->ReferenceCount ++;
                break;
        default:
                SpiderScript_ReferenceValue(Value);
@@ -162,20 +171,78 @@ void Bytecode_int_DerefStackValue(tBC_StackEnt *Ent)
        {
        case SS_DATATYPE_INTEGER:
        case SS_DATATYPE_REAL:
+               break;
+       case SS_DATATYPE_OBJECT:
+               if(Ent->Object) {
+                       Ent->Object->ReferenceCount --;
+                       if(Ent->Object->ReferenceCount == 0) {
+                               Ent->Object->Type->Destructor( Ent->Object );
+                       }
+//                     printf("Object %p derefed (obj refcount = %i)\n", Ent->Object, Ent->Object->ReferenceCount);
+               }
+               Ent->Object = NULL;
+               break;
+       default:
+               if(Ent->Reference)
+                       SpiderScript_DereferenceValue(Ent->Reference);
+               Ent->Reference = NULL;
+               break;
+       }
+}
+void Bytecode_int_RefStackValue(tBC_StackEnt *Ent)
+{
+       switch(Ent->Type)
+       {
+       case SS_DATATYPE_INTEGER:
+       case SS_DATATYPE_REAL:
+               break;
+       case SS_DATATYPE_OBJECT:
+               if(Ent->Object) {
+                       Ent->Object->ReferenceCount ++;
+//                     printf("Object %p referenced (count = %i)\n", Ent->Object, Ent->Object->ReferenceCount);
+               }
+               break;
+       default:
+               if(Ent->Reference)
+                       SpiderScript_ReferenceValue(Ent->Reference);
+               break;
+       }
+}
+
+void Bytecode_int_PrintStackValue(tBC_StackEnt *Ent)
+{
+       switch(Ent->Type)
+       {
+       case SS_DATATYPE_INTEGER:
+               printf("0x%"PRIx64, Ent->Integer);
+               break;
+       case SS_DATATYPE_REAL:
+               printf("%lf", Ent->Real);
+               break;
        case SS_DATATYPE_OBJECT:
+               printf("Obj %p", Ent->Object);
                break;
        default:
-               SpiderScript_DereferenceValue(Ent->Reference);
+               if( Ent->Reference )
+                       printf("*%p (%i refs)", Ent->Reference, Ent->Reference->ReferenceCount);
+               else
+                       printf("NULL");
                break;
        }
 }
 
+#if TRACE
+# define PRINT_STACKVAL(val)   Bytecode_int_PrintStackValue(&val)
+#else
+# define PRINT_STACKVAL(val)
+#endif
+
 #define GET_STACKVAL(dst)      if((ret = Bytecode_int_StackPop(Stack, &dst))) { \
        AST_RuntimeError(NULL, "Stack pop failed, empty stack");\
        return ret; \
 }
 #define PUT_STACKVAL(src)      if((ret = Bytecode_int_StackPush(Stack, &src))) { \
-       AST_RuntimeError(NULL, "Stack pop failed, empty stack");\
+       AST_RuntimeError(NULL, "Stack push failed, full stack");\
        return ret; \
 }
 #define OP_INDX(op_ptr)        ((op_ptr)->Content.StringInt.Integer)
@@ -209,11 +276,14 @@ tSpiderValue *Bytecode_ExecuteFunction(tSpiderScript *Script, tScript_Function *
                return NULL;
        }
        free(stack);
+       
        ret = Bytecode_int_GetSpiderValue(&val, &tmpsval);
        // Ensure it's a heap value
        if(ret == &tmpsval) {
                ret = malloc(sizeof(tSpiderValue));
                memcpy(ret, &tmpsval, sizeof(tSpiderValue));
+               // Set to 2 in _GetSpiderValue, so stack doesn't have free() called
+               ret->ReferenceCount = 1;
        }
 
        return ret;
@@ -240,6 +310,135 @@ tSpiderNamespace *Bytecode_int_ResolveNamespace(tSpiderNamespace *Start, const c
        return ns;
 }
 
+/**
+ * \brief Call an external function (may recurse into Bytecode_ExecuteFunction, but may not)
+ */
+int Bytecode_int_CallExternFunction(tSpiderScript *Script, tBC_Stack *Stack, tSpiderNamespace *DefaultNS, tBC_Op *op )
+{
+       const char      *name = OP_STRING(op);
+        int    arg_count = OP_INDX(op);
+        int    i, ret = 0;
+       tSpiderValue    *args[arg_count];
+       tSpiderValue    *rv;
+       tBC_StackEnt    val1;
+       const char      *namespaces[] = {NULL}; // TODO: Default/imported namespaces
+
+       DEBUG_F("CALL (general) %s %i args\n", name, arg_count);
+       
+       // Read arguments
+       for( i = arg_count; i --; )
+       {
+               GET_STACKVAL(val1);
+               args[i] = Bytecode_int_GetSpiderValue(&val1, NULL);
+               Bytecode_int_DerefStackValue(&val1);
+       }
+       
+       // Call the function etc.
+       if( op->Operation == BC_OP_CALLFUNCTION )
+       {
+               rv = SpiderScript_ExecuteFunction(Script, name, namespaces, arg_count, args, &op->CacheEnt);
+       }
+       else if( op->Operation == BC_OP_CREATEOBJ )
+       {
+               rv = SpiderScript_CreateObject(Script, name, namespaces, arg_count, args);
+       }
+       else if( op->Operation == BC_OP_CALLMETHOD )
+       {
+               tSpiderObject   *obj;
+               GET_STACKVAL(val1);
+               
+               if(val1.Type == SS_DATATYPE_OBJECT)
+                       obj = val1.Object;
+               else if(val1.Type == ET_REFERENCE && val1.Reference && val1.Reference->Type == SS_DATATYPE_OBJECT)
+                       obj = val1.Reference->Object;
+               else {
+                       // Error
+                       AST_RuntimeError(NULL, "OP_CALLMETHOD on non object");
+                       return -1;
+               }
+               rv = SpiderScript_ExecuteMethod(Script, obj, name, arg_count, args);
+               Bytecode_int_DerefStackValue(&val1);
+       }
+       else
+       {
+               AST_RuntimeError(NULL, "BUG - Unknown operation for CALL/CREATEOBJ (%i)", op->Operation);
+               rv = ERRPTR;
+       }
+       if(rv == ERRPTR) {
+               AST_RuntimeError(NULL, "SpiderScript_ExecuteFunction returned ERRPTR");
+               return -1;
+       }
+       // Clean up args
+       for( i = arg_count; i --; )
+               SpiderScript_DereferenceValue(args[i]);
+       // Get and push return
+       Bytecode_int_SetSpiderValue(&val1, rv);
+       PUT_STACKVAL(val1);
+       // Deref return
+       SpiderScript_DereferenceValue(rv);
+
+       #if 0
+       if(!rv) {
+               printf("%s returned NULL\n", name);
+       }
+       if( rv && rv != ERRPTR && rv->ReferenceCount != 1 ) {
+               printf("Return value from %s reference count fail (%i)\n",
+                       name, rv->ReferenceCount);
+       }
+       #endif  
+
+       return 0;
+}
+
+int Bytecode_int_LocalBinOp_Integer(int Operation, tBC_StackEnt *Val1, tBC_StackEnt *Val2)
+{
+       switch(Operation)
+       {
+       case BC_OP_ADD:         Val1->Integer = Val1->Integer + Val2->Integer;  break;
+       case BC_OP_SUBTRACT:    Val1->Integer = Val1->Integer - Val2->Integer;  break;
+       case BC_OP_MULTIPLY:    Val1->Integer = Val1->Integer * Val2->Integer;  break;
+       case BC_OP_DIVIDE:      Val1->Integer = Val1->Integer / Val2->Integer;  break;
+       
+       case BC_OP_EQUALS:              Val1->Integer = (Val1->Integer == Val2->Integer);       break;
+       case BC_OP_NOTEQUALS:           Val1->Integer = (Val1->Integer != Val2->Integer);       break;
+       case BC_OP_LESSTHAN:            Val1->Integer = (Val1->Integer <  Val2->Integer);       break;
+       case BC_OP_LESSTHANOREQUAL:     Val1->Integer = (Val1->Integer <= Val2->Integer);       break;
+       case BC_OP_GREATERTHAN:         Val1->Integer = (Val1->Integer >  Val2->Integer);       break;
+       case BC_OP_GREATERTHANOREQUAL:  Val1->Integer = (Val1->Integer >= Val2->Integer);       break;
+       
+       case BC_OP_BITAND:      Val1->Integer = Val1->Integer & Val2->Integer;  break;
+       case BC_OP_BITOR:       Val1->Integer = Val1->Integer | Val2->Integer;  break;
+       case BC_OP_BITXOR:      Val1->Integer = Val1->Integer ^ Val2->Integer;  break;
+       case BC_OP_MODULO:      Val1->Integer = Val1->Integer % Val2->Integer;  break;
+       default:        AST_RuntimeError(NULL, "Invalid operation on datatype Integer"); return -1;
+       }
+       return 0;
+}
+
+int Bytecode_int_LocalBinOp_Real(int Operation, tBC_StackEnt *Val1, tBC_StackEnt *Val2)
+{
+       switch(Operation)
+       {
+       // Real = Real OP Real
+       case BC_OP_ADD:         Val1->Real = Val1->Real + Val2->Real;   return 0;
+       case BC_OP_SUBTRACT:    Val1->Real = Val1->Real - Val2->Real;   return 0;
+       case BC_OP_MULTIPLY:    Val1->Real = Val1->Real * Val2->Real;   return 0;
+       case BC_OP_DIVIDE:      Val1->Real = Val1->Real / Val2->Real;   return 0;
+
+       // Bool/Integer = Real OP Real
+       case BC_OP_EQUALS:              Val1->Integer = (Val1->Real == Val2->Real);     break;
+       case BC_OP_NOTEQUALS:           Val1->Integer = (Val1->Real != Val2->Real);     break;
+       case BC_OP_LESSTHAN:            Val1->Integer = (Val1->Real <  Val2->Real);     break;
+       case BC_OP_LESSTHANOREQUAL:     Val1->Integer = (Val1->Real <= Val2->Real);     break;
+       case BC_OP_GREATERTHAN:         Val1->Integer = (Val1->Real >  Val2->Real);     break;
+       case BC_OP_GREATERTHANOREQUAL:  Val1->Integer = (Val1->Real >= Val2->Real);     break;
+       
+       default:        AST_RuntimeError(NULL, "Invalid operation on datatype Real"); return -1;
+       }
+       Val1->Type = SS_DATATYPE_INTEGER;       // Becomes logical
+       return 0;
+}
+
 #define STATE_HDR()    DEBUG_F("%p %2i ", op, Stack->EntryCount)
 
 /**
@@ -255,10 +454,15 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
        tSpiderValue    tmpVal1, tmpVal2;       // temp storage
        tSpiderValue    *pval1, *pval2, *ret_val;
        tSpiderNamespace        *default_namespace = &Script->Variant->RootNamespace;
+
+       // Initialise local vars
+       for( i = 0; i < local_var_count; i ++ )
+               local_vars[i].Type = ET_NULL;
        
        // Pop off arguments
        if( ArgCount > Fcn->ArgumentCount )     return -1;
        DEBUG_F("Fcn->ArgumentCount = %i\n", Fcn->ArgumentCount);
+       // - Handle optional arguments
        for( i = Fcn->ArgumentCount; i > ArgCount; )
        {
                i --;
@@ -286,11 +490,12 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
                switch(op->Operation)
                {
                case BC_OP_NOP:
+                       STATE_HDR();
+                       DEBUG_F("NOP\n");
                        break;
                // Jumps
                case BC_OP_JUMP:
                        STATE_HDR();
-                       // NOTE: Evil, all jumps are off by -1, so fix that
                        jmp_target = Fcn->BCFcn->Labels[ OP_INDX(op) ]->Next;
                        DEBUG_F("JUMP #%i %p\n", OP_INDX(op), jmp_target);
                        nextop = jmp_target;
@@ -323,6 +528,10 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
                        }
                        STATE_HDR();
                        DEBUG_F("DEFVAR %i of type %i\n", slot, type);
+                       if( local_vars[slot].Type != ET_NULL ) {
+                               Bytecode_int_DerefStackValue( &local_vars[slot] );
+                               local_vars[slot].Type = ET_NULL;
+                       }
                        memset(&local_vars[slot], 0, sizeof(local_vars[0]));
                        local_vars[slot].Type = type;
                        } break;
@@ -339,24 +548,119 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
                        break;
 
                // Variables
-               case BC_OP_LOADVAR:
+               case BC_OP_LOADVAR: {
+                        int    slot = OP_INDX(op);
                        STATE_HDR();
-                       DEBUG_F("LOADVAR %i\n", OP_INDX(op));
-                       if( OP_INDX(op) < 0 || OP_INDX(op) >= local_var_count ) {
-                               AST_RuntimeError(NULL, "Loading from invalid slot %i", OP_INDX(op));
+                       DEBUG_F("LOADVAR %i ", slot);
+                       if( slot < 0 || slot >= local_var_count ) {
+                               AST_RuntimeError(NULL, "Loading from invalid slot %i", slot);
                                return -1;
                        }
-                       PUT_STACKVAL(local_vars[OP_INDX(op)]);
-//                     DUMP_STACKVAL(local_vars[OP_INDX(op)]);
-                       break;
-               case BC_OP_SAVEVAR:
+                       DEBUG_F("("); PRINT_STACKVAL(local_vars[slot]); DEBUG_F(")\n");
+                       PUT_STACKVAL(local_vars[slot]);
+                       Bytecode_int_RefStackValue( &local_vars[slot] );
+                       } break;
+               case BC_OP_SAVEVAR: {
+                        int    slot = OP_INDX(op);
                        STATE_HDR();
-                       DEBUG_F("SAVEVAR %i\n", OP_INDX(op));
-                       if( OP_INDX(op) < 0 || OP_INDX(op) >= local_var_count ) {
-                               AST_RuntimeError(NULL, "Loading from invalid slot %i", OP_INDX(op));
+                       DEBUG_F("SAVEVAR %i = ", slot);
+                       if( slot < 0 || slot >= local_var_count ) {
+                               AST_RuntimeError(NULL, "Loading from invalid slot %i", slot);
                                return -1;
                        }
-                       GET_STACKVAL(local_vars[OP_INDX(op)]);
+                       // Remove whatever was in there before
+                       DEBUG_F("[Deref "); PRINT_STACKVAL(local_vars[slot]); DEBUG_F("] ");
+                       Bytecode_int_DerefStackValue( &local_vars[slot] );
+                       // Place new in
+                       GET_STACKVAL(local_vars[slot]);
+                       PRINT_STACKVAL(local_vars[slot]);
+                       DEBUG_F("\n");
+                       } break;
+
+               // Array index (get or set)
+               case BC_OP_INDEX:
+               case BC_OP_SETINDEX:
+                       STATE_HDR();
+                       GET_STACKVAL(val1);     // Index
+                       // TODO: Check that index is an integer
+                       if( val1.Type != SS_DATATYPE_INTEGER ) {
+                               nextop = NULL;
+                               break;
+                       }
+
+                       // Get array as raw spider value
+                       GET_STACKVAL(val2);     // Array
+                       pval1 = Bytecode_int_GetSpiderValue(&val2, &tmpVal1);
+                       Bytecode_int_DerefStackValue(&val2);
+
+                       if( op->Operation == BC_OP_SETINDEX ) {
+                               GET_STACKVAL(val2);
+                               pval2 = Bytecode_int_GetSpiderValue(&val2, NULL);
+                               Bytecode_int_DerefStackValue(&val2);
+                               
+                               DEBUG_F("SETINDEX %i ", val1.Integer); PRINT_STACKVAL(val2); DEBUG_F("\n");
+                       
+                               ret_val = AST_ExecuteNode_Index(Script, NULL, pval1, val1.Integer, pval2);
+                               if(ret_val == ERRPTR) { nextop = NULL; break; }
+                               SpiderScript_DereferenceValue(pval2);
+                       }
+                       else {
+                               DEBUG_F("INDEX %i ", val1.Integer);
+                               ret_val = AST_ExecuteNode_Index(Script, NULL, pval1, val1.Integer, ERRPTR);
+                               if(ret_val == ERRPTR) { nextop = NULL; break; }
+                               
+                               Bytecode_int_SetSpiderValue(&val1, ret_val);
+                               SpiderScript_DereferenceValue(ret_val);
+                               PUT_STACKVAL(val1);
+
+                               DEBUG_F("[Got "); PRINT_STACKVAL(val1); DEBUG_F("]\n");
+
+                       }
+                       // Dereference the array (or object, ...)
+                       if(pval1 != &tmpVal1)   SpiderScript_DereferenceValue(pval1);
+                       break;
+               
+               // Object element (get or set)
+               case BC_OP_ELEMENT:
+               case BC_OP_SETELEMENT:
+                       STATE_HDR();
+                       
+                       GET_STACKVAL(val1);
+                       // - Integers/Reals can't have elements :)
+                       if( val1.Type != ET_REFERENCE ) {
+                               nextop = NULL;
+                               break;
+                       }
+
+                       pval1 = Bytecode_int_GetSpiderValue(&val1, NULL);
+                       Bytecode_int_DerefStackValue(&val1);
+
+                       if( op->Operation == BC_OP_SETELEMENT ) {
+                               GET_STACKVAL(val2);
+                               pval2 = Bytecode_int_GetSpiderValue(&val2, &tmpVal2);
+                               Bytecode_int_DerefStackValue(&val2);
+                               
+                               DEBUG_F("SETELEMENT %s ", OP_STRING(op)); PRINT_STACKVAL(val2); DEBUG_F("\n");
+
+                               ret_val = AST_ExecuteNode_Element(Script, NULL, pval1, OP_STRING(op), pval2);
+                               if(ret_val == ERRPTR) { nextop = NULL; break; }
+                               
+                               if(pval2 != &tmpVal2)   SpiderScript_DereferenceValue(pval2);
+                       }
+                       else {
+                               DEBUG_F("ELEMENT %s ", OP_STRING(op));
+                               
+                               ret_val = AST_ExecuteNode_Element(Script, NULL, pval1, OP_STRING(op), ERRPTR);
+                               if(ret_val == ERRPTR) { nextop = NULL; break; }
+
+                               Bytecode_int_SetSpiderValue(&val2, ret_val);
+                               SpiderScript_DereferenceValue(ret_val);
+                               PUT_STACKVAL(val2);
+       
+                               DEBUG_F("[Got "); PRINT_STACKVAL(val2); DEBUG_F("]\n");
+                       }
+                       
+                       SpiderScript_DereferenceValue(pval1);
                        break;
 
                // Constants:
@@ -381,6 +685,13 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
                        val1.Reference = SpiderScript_CreateString(OP_INDX(op), OP_STRING(op));
                        PUT_STACKVAL(val1);
                        break;
+               case BC_OP_LOADNULL:
+                       STATE_HDR();
+                       DEBUG_F("LOADNULL\n");
+                       val1.Type = ET_REFERENCE;
+                       val1.Reference = NULL;
+                       PUT_STACKVAL(val1);
+                       break;
 
                case BC_OP_CAST:
                        STATE_HDR();
@@ -391,33 +702,44 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
                                PUT_STACKVAL(val1);
                                break;
                        }
-                       switch(val2.Type * 100 + val1.Type )
-                       {
-                       case SS_DATATYPE_INTEGER*100 + SS_DATATYPE_REAL:
-                               val2.Integer = val1.Real;
-                               PUT_STACKVAL(val2);
-                               break;
-                       case SS_DATATYPE_REAL*100 + SS_DATATYPE_INTEGER:
+                       if( val2.Type == SS_DATATYPE_INTEGER && val1.Type == SS_DATATYPE_REAL ) {
                                val2.Integer = val1.Real;
-                               PUT_STACKVAL(val2);
-                               break;
-                       default: {
+                       }
+                       else if( val2.Type == SS_DATATYPE_REAL && val2.Type == SS_DATATYPE_INTEGER ) {
+                               val2.Real = val1.Integer;
+                       }
+                       else {
                                pval1 = Bytecode_int_GetSpiderValue(&val1, &tmpVal1);
                                pval2 = SpiderScript_CastValueTo(val2.Type, pval1);
-                               if(pval1 != &tmpVal1)   SpiderScript_DereferenceValue(pval1);
+                               
                                Bytecode_int_SetSpiderValue(&val2, pval2);
                                SpiderScript_DereferenceValue(pval2);
-                               PUT_STACKVAL(val2);
-                               } break;
+                               
+                               if(pval1 != &tmpVal1)   SpiderScript_DereferenceValue(pval1);
+                               Bytecode_int_DerefStackValue(&val1);
+//                             printf("CAST (%x->%x) - Original %i references remaining\n",
+//                                     pval1->Type, OP_INDX(op),
+//                                     pval1->ReferenceCount);
                        }
+                       PUT_STACKVAL(val2);
                        break;
 
                case BC_OP_DUPSTACK:
                        STATE_HDR();
-                       DEBUG_F("DUPSTACK\n");
+                       DEBUG_F("DUPSTACK ");
                        GET_STACKVAL(val1);
+                       PRINT_STACKVAL(val1);
+                       DEBUG_F("\n");
                        PUT_STACKVAL(val1);
                        PUT_STACKVAL(val1);
+                       Bytecode_int_RefStackValue(&val1);
+                       break;
+
+               // Discard the top item from the stack
+               case BC_OP_DELSTACK:
+                       STATE_HDR();
+                       DEBUG_F("DELSTACK\n");
+                       GET_STACKVAL(val1);
                        break;
 
                // Unary Operations
@@ -431,14 +753,19 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
                        Bytecode_int_StackPush(Stack, &val2);
                        Bytecode_int_DerefStackValue(&val1);
                        break;
+               
                case BC_OP_BITNOT:
-                       if(!ast_op)     ast_op = NODETYPE_BWNOT;
+                       if(!ast_op)     ast_op = NODETYPE_BWNOT,        opstr = "BITNOT";
+               case BC_OP_NEG:
+                       if(!ast_op)     ast_op = NODETYPE_NEGATE,       opstr = "NEG";
 
                        STATE_HDR();
-                       DEBUG_F("UNIOP %i\n", ast_op);
+                       DEBUG_F("%s\n", opstr);
 
                        GET_STACKVAL(val1);
                        pval1 = Bytecode_int_GetSpiderValue(&val1, &tmpVal1);
+                       Bytecode_int_DerefStackValue(&val1);                    
+
                        ret_val = AST_ExecuteNode_UniOp(Script, NULL, ast_op, pval1);
                        if(pval1 != &tmpVal1)   SpiderScript_DereferenceValue(pval1);
                        Bytecode_int_SetSpiderValue(&val1, ret_val);
@@ -454,6 +781,32 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
                        if(!ast_op)     ast_op = NODETYPE_LOGICALOR,    opstr = "LOGICOR";
                case BC_OP_LOGICXOR:
                        if(!ast_op)     ast_op = NODETYPE_LOGICALXOR,   opstr = "LOGICXOR";
+       
+                       STATE_HDR();
+                       DEBUG_F("%s\n", opstr);
+
+                       GET_STACKVAL(val1);
+                       GET_STACKVAL(val2);
+                       
+                       switch(op->Operation)
+                       {
+                       case BC_OP_LOGICAND:
+                               i = Bytecode_int_IsStackEntTrue(&val1) && Bytecode_int_IsStackEntTrue(&val2);
+                               break;
+                       case BC_OP_LOGICOR:
+                               i = Bytecode_int_IsStackEntTrue(&val1) || Bytecode_int_IsStackEntTrue(&val2);
+                               break;
+                       case BC_OP_LOGICXOR:
+                               i = Bytecode_int_IsStackEntTrue(&val1) ^ Bytecode_int_IsStackEntTrue(&val2);
+                               break;
+                       }
+                       Bytecode_int_DerefStackValue(&val1);
+                       Bytecode_int_DerefStackValue(&val2);
+
+                       val1.Type = SS_DATATYPE_INTEGER;
+                       val1.Integer = i;
+                       Bytecode_int_StackPush(Stack, &val1);
+                       break;
 
                case BC_OP_BITAND:
                        if(!ast_op)     ast_op = NODETYPE_BWAND,        opstr = "BITAND";
@@ -482,6 +835,8 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
 
                case BC_OP_EQUALS:
                        if(!ast_op)     ast_op = NODETYPE_EQUALS,       opstr = "EQUALS";
+               case BC_OP_NOTEQUALS:
+                       if(!ast_op)     ast_op = NODETYPE_NOTEQUALS,    opstr = "NOTEQUALS";
                case BC_OP_LESSTHAN:
                        if(!ast_op)     ast_op = NODETYPE_LESSTHAN,     opstr = "LESSTHAN";
                case BC_OP_LESSTHANOREQUAL:
@@ -494,81 +849,90 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
                        STATE_HDR();
                        DEBUG_F("BINOP %i %s (bc %i)\n", ast_op, opstr, op->Operation);
 
-                       GET_STACKVAL(val2);
-                       GET_STACKVAL(val1);
+                       GET_STACKVAL(val2);     // Right
+                       GET_STACKVAL(val1);     // Left
+
+                       DEBUG_F(" ("); PRINT_STACKVAL(val1); DEBUG_F(")");
+                       DEBUG_F(" ("); PRINT_STACKVAL(val2); DEBUG_F(")\n");
+
+                       // Perform integer operations locally
+                       if( val1.Type == SS_DATATYPE_INTEGER && val2.Type == SS_DATATYPE_INTEGER )
+                       {
+                               if( Bytecode_int_LocalBinOp_Integer(op->Operation, &val1, &val2) ) {
+                                       nextop = NULL;
+                                       break;
+                               }
+                               PUT_STACKVAL(val1);
+                               break;
+                       }
+
+                       if(val1. Type == SS_DATATYPE_REAL && val2.Type == SS_DATATYPE_REAL )
+                       {
+                               if( Bytecode_int_LocalBinOp_Real(op->Operation, &val1, &val2) ) {
+                                       nextop = NULL;
+                                       break;
+                               }
+                               PUT_STACKVAL(val1);
+                               break;
+                       }
+               
                        pval1 = Bytecode_int_GetSpiderValue(&val1, &tmpVal1);
                        pval2 = Bytecode_int_GetSpiderValue(&val2, &tmpVal2);
+                       Bytecode_int_DerefStackValue(&val1);
+                       Bytecode_int_DerefStackValue(&val2);
+
+                       // Hand to AST execution code
                        ret_val = AST_ExecuteNode_BinOp(Script, NULL, ast_op, pval1, pval2);
                        if(pval1 != &tmpVal1)   SpiderScript_DereferenceValue(pval1);
                        if(pval2 != &tmpVal2)   SpiderScript_DereferenceValue(pval2);
+
+                       if(ret_val == ERRPTR) {
+                               AST_RuntimeError(NULL, "_BinOp returned ERRPTR");
+                               nextop = NULL;
+                               break;
+                       }
                        Bytecode_int_SetSpiderValue(&val1, ret_val);
                        if(ret_val != &tmpVal1) SpiderScript_DereferenceValue(ret_val);
-                       Bytecode_int_StackPush(Stack, &val1);
+                       PUT_STACKVAL(val1);
                        break;
 
                // Functions etc
-               case BC_OP_CALLFUNCTION: {
-                       tScript_Function        *fcn;
-                       const char      *name = OP_STRING(op);
-                        int    arg_count = OP_INDX(op);
-                       
+               case BC_OP_CREATEOBJ:
+               case BC_OP_CALLFUNCTION:
+               case BC_OP_CALLMETHOD:
                        STATE_HDR();
-                       DEBUG_F("CALL FUNCTION %s %i args\n", name, arg_count);
 
-                       // Check current script functions (for fast call)
-                       for(fcn = Script->Functions; fcn; fcn = fcn->Next)
-                       {
-                               if(strcmp(name, fcn->Name) == 0) {
-                                       break;
-                               }
-                       }
-                       if(fcn && fcn->BCFcn)
+                       if( op->Operation == BC_OP_CALLFUNCTION )
                        {
-                               DEBUG_F(" - Fast call\n");
-                               Bytecode_int_ExecuteFunction(Script, fcn, Stack, arg_count);
-                               break;
-                       }
-                       
-                       // Slower call
-                       {
-                               tSpiderNamespace        *ns = NULL;
-                               tSpiderValue    *args[arg_count];
-                               tSpiderValue    *rv;
-//                             for( i = 0; i < arg_count; i ++ )
-                               for( i = arg_count; i --; )
+                               tScript_Function        *fcn = NULL;
+                               const char      *name = OP_STRING(op);
+                                int    arg_count = OP_INDX(op);
+                               DEBUG_F("CALL (local) %s %i args\n", name, arg_count);
+                               // Check current script functions (for fast call)
+                               for(fcn = Script->Functions; fcn; fcn = fcn->Next)
                                {
-                                       GET_STACKVAL(val1);
-                                       args[i] = Bytecode_int_GetSpiderValue(&val1, NULL);
-                               }
-                               
-                               if( name[0] == BC_NS_SEPARATOR ) {
-                                       name ++;
-                                       ns = Bytecode_int_ResolveNamespace(&Script->Variant->RootNamespace, name, &name);
+                                       if(strcmp(name, fcn->Name) == 0) {
+                                               break;
+                                       }
                                }
-                               else {
-                                       // TODO: Support multiple default namespaces
-                                       ns = Bytecode_int_ResolveNamespace(default_namespace, name, &name);
-                               }
-                               
-                               rv = SpiderScript_ExecuteFunction(Script, ns, name, arg_count, args);
-                               if(rv == ERRPTR) {
-                                       AST_RuntimeError(NULL, "SpiderScript_ExecuteFunction returned ERRPTR");
-                                       nextop = NULL;
+                               if(fcn && fcn->BCFcn)
+                               {
+                                       DEBUG_F(" - Fast call\n");
+                                       Bytecode_int_ExecuteFunction(Script, fcn, Stack, arg_count);
                                        break;
                                }
-                               // Clean up args
-                               for( i = arg_count; i --; )
-                                       SpiderScript_DereferenceValue(args[i]);
-                               // Get and push return
-                               Bytecode_int_SetSpiderValue(&val1, rv);
-                               PUT_STACKVAL(val1);
-                               // Deref return
-                               SpiderScript_DereferenceValue(rv);
                        }
-                       } break;
+               
+                       // Slower call
+                       if( Bytecode_int_CallExternFunction( Script, Stack, default_namespace, op ) ) {
+                               nextop = NULL;
+                               break;
+                       }
+                       break;
 
                case BC_OP_RETURN:
                        STATE_HDR();
+
                        DEBUG_F("RETURN\n");
                        nextop = NULL;
                        break;
@@ -576,7 +940,7 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
                default:
                        // TODO:
                        STATE_HDR();
-                       printf("Unknown operation %i\n", op->Operation);
+                       AST_RuntimeError(NULL, "Unknown operation %i\n", op->Operation);
                        nextop = NULL;
                        break;
                }
@@ -585,14 +949,34 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t
        
        // Clean up
        // - Delete local vars
-       printf("TODO: Clean up local vars\n");
+       for( i = 0; i < local_var_count; i ++ )
+       {
+               if( local_vars[i].Type != ET_NULL )
+               {
+                       DEBUG_F("Var %i - ", i); 
+                       PRINT_STACKVAL(local_vars[i]);
+                       Bytecode_int_DerefStackValue(&local_vars[i]);
+                       DEBUG_F("\n");
+               }
+               else
+                       DEBUG_F("Var %i - empty\n", i);
+       }
        
        // - Restore stack
-//     printf("TODO: Roll back stack\n");
-//     while( Stack->EntryCount && Stack->Entries[ --Stack->EntryCount ].Type != ET_FUNCTION_START )
-//     {
-//             Bytecode_int_DerefStackValue( &Stack->Entries[Stack->EntryCount] );
-//     }
+       if( Stack->Entries[Stack->EntryCount - 1].Type == ET_FUNCTION_START )
+               Stack->EntryCount --;
+       else
+       {
+                int    n_rolled = 1;
+               GET_STACKVAL(val1);
+               while( Stack->EntryCount && Stack->Entries[ --Stack->EntryCount ].Type != ET_FUNCTION_START )
+               {
+                       Bytecode_int_DerefStackValue( &Stack->Entries[Stack->EntryCount] );
+                       n_rolled ++;
+               }
+               PUT_STACKVAL(val1);
+               DEBUG_F("Rolled back %i entries\n", n_rolled);
+       }
        
 
        return 0;

UCC git Repository :: git.ucc.asn.au