Changeset 323

Show
Ignore:
Timestamp:
06/28/08 21:49:25 (3 months ago)
Author:
JarrettBillingsley
Message:

ferp.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/v2new/compile.bat

    r319 r323  
    11@echo off 
    2 build test.d -clean -debug -unittest -w 
     2build test.d -clean -debug -g -unittest -w 
    33rem build test.d -clean -release -w 
  • branches/v2new/minid/api.d

    r319 r323  
    2727{ 
    2828    import minid.alloc; 
     29    import minid.ex; 
    2930    import minid.gc; 
    3031    import minid.types; 
  • branches/v2new/minid/arraylib.d

    r278 r323  
    2424module minid.arraylib; 
    2525 
    26 import minid.types; 
    27 import minid.utils; 
    2826import tango.core.Array; 
    2927import tango.core.Tuple; 
    3028import tango.math.Math; 
    3129 
    32 final class ArrayLib 
     30import minid.interpreter; 
     31import minid.types; 
     32 
     33struct ArrayLib 
    3334{ 
    3435static: 
    35     public void init(MDContext context) 
     36    public void init(MDThread* t) 
    3637    { 
    3738        context.setModuleLoader("array", context.newClosure(function int(MDState s, uint numParams) 
  • branches/v2new/minid/baselib.d

    r321 r323  
    2323 
    2424module minid.baselib; 
    25  
    26 import minid.ex; 
    27 import minid.interpreter; 
    28 import minid.misc; 
    29 import minid.obj; 
    30 import minid.string; 
    31 import minid.types; 
    32 import minid.vm; 
    3325 
    3426import Integer = tango.text.convert.Integer; 
     
    4032import utf = tango.text.convert.Utf; 
    4133 
     34import minid.ex; 
     35import minid.func; 
     36import minid.interpreter; 
     37import minid.misc; 
     38import minid.namespace; 
     39import minid.obj; 
     40import minid.string; 
     41import minid.types; 
     42import minid.vm; 
     43 
    4244private void register(MDThread* t, dchar[] name, NativeFunc func, size_t numUpvals = 0) 
    4345{ 
     
    5759        newGlobal(t, "Object"); 
    5860 
    59 //        // StringBuffer 
     61      // StringBuffer 
    6062//      globals["StringBuffer"d] =    new MDStringBufferClass(_Object); 
    61 //  
     63 
    6264        // Really basic stuff 
    6365//      globals["getTraceback"d] =    new MDClosure(globals.ns, &getTraceback,          "getTraceback"); 
     
    6668//      globals["setModuleLoader"d] = new MDClosure(globals.ns, &setModuleLoader,       "setModuleLoader"); 
    6769//      globals["reloadModule"d] =    new MDClosure(globals.ns, &reloadModule,          "reloadModule"); 
    68 //         globals["removeKey"d] =       new MDClosure(globals.ns, &removeKey,             "removeKey"); 
     70       register(t, "removeKey", &removeKey); 
    6971        register(t, "rawSet", &rawSet); 
    7072        register(t, "rawGet", &rawGet); 
    7173        register(t, "runMain", &runMain); 
    7274 
    73 //        // Functional stuff 
     75      // Functional stuff 
    7476        register(t, "curry", &curry); 
    7577        register(t, "bindContext", &bindContext); 
    76 //  
    77 //        // Reflection-esque stuff 
    78 //         globals["findGlobal"d] =      new MDClosure(globals.ns, &findGlobal,            "findGlobal"); 
    79 //         globals["isSet"d] =           new MDClosure(globals.ns, &isSet,                 "isSet"); 
    80 //         globals["typeof"d] =          new MDClosure(globals.ns, &mdtypeof,              "typeof"); 
    81 //         globals["fieldsOf"d] =        new MDClosure(globals.ns, &fieldsOf,              "fieldsOf"); 
    82 //         globals["allFieldsOf"d] =     new MDClosure(globals.ns, &allFieldsOf,           "allFieldsOf"); 
    83 //         globals["hasField"d] =        new MDClosure(globals.ns, &hasField,              "hasField"); 
    84 //         globals["hasMethod"d] =       new MDClosure(globals.ns, &hasMethod,             "hasMethod"); 
     78 
     79      // Reflection-esque stuff 
     80       register(t, "findGlobal", &findGlobal); 
     81       register(t, "isSet", &isSet); 
     82       register(t, "typeof", &mdtypeof); 
     83       register(t, "fieldsOf", &fieldsOf); 
     84       register(t, "allFieldsOf", &allFieldsOf); 
     85       register(t, "hasField", &hasField); 
     86       register(t, "hasMethod", &hasMethod); 
    8587//      globals["hasAttributes"d] =   new MDClosure(globals.ns, &hasAttributes,         "hasAttributes"); 
    8688//      globals["attributesOf"d] =    new MDClosure(globals.ns, &attributesOf,          "attributesOf"); 
    87 //      globals["isNull"d] =          new MDClosure(globals.ns, &isParam!("null"),      "isNull"); 
    88 //      globals["isBool"d] =          new MDClosure(globals.ns, &isParam!("bool"),      "isBool"); 
    89 //      globals["isInt"d] =           new MDClosure(globals.ns, &isParam!("int"),       "isInt"); 
    90 //      globals["isFloat"d] =         new MDClosure(globals.ns, &isParam!("float"),     "isFloat"); 
    91 //      globals["isChar"d] =          new MDClosure(globals.ns, &isParam!("char"),      "isChar"); 
    92 //      globals["isString"d] =        new MDClosure(globals.ns, &isParam!("string"),    "isString"); 
    93 //      globals["isTable"d] =         new MDClosure(globals.ns, &isParam!("table"),     "isTable"); 
    94 //      globals["isArray"d] =         new MDClosure(globals.ns, &isParam!("array"),     "isArray"); 
    95 //      globals["isFunction"d] =      new MDClosure(globals.ns, &isParam!("function"),  "isFunction"); 
    96 //      globals["isObject"d] =        new MDClosure(globals.ns, &isParam!("object"),    "isObject"); 
    97 //      globals["isNamespace"d] =     new MDClosure(globals.ns, &isParam!("namespace"), "isNamespace"); 
    98 //      globals["isThread"d] =        new MDClosure(globals.ns, &isParam!("thread"),    "isThread"); 
    99 //  
    100 //      // Conversions 
     89        register(t, "isNull", &isParam!(MDValue.Type.Null)); 
     90        register(t, "isBool", &isParam!(MDValue.Type.Bool)); 
     91        register(t, "isInt", &isParam!(MDValue.Type.Int)); 
     92        register(t, "isFloat", &isParam!(MDValue.Type.Float)); 
     93        register(t, "isChar", &isParam!(MDValue.Type.Char)); 
     94        register(t, "isString", &isParam!(MDValue.Type.String)); 
     95        register(t, "isTable", &isParam!(MDValue.Type.Table)); 
     96        register(t, "isArray", &isParam!(MDValue.Type.Array)); 
     97        register(t, "isFunction", &isParam!(MDValue.Type.Function)); 
     98        register(t, "isObject", &isParam!(MDValue.Type.Object)); 
     99        register(t, "isNamespace", &isParam!(MDValue.Type.Namespace)); 
     100        register(t, "isThread", &isParam!(MDValue.Type.Thread)); 
     101        register(t, "isNativeObj", &isParam!(MDValue.Type.NativeObj)); 
     102 
     103        // Conversions 
    101104        register(t, "toString", &toString); 
    102105        register(t, "rawToString", &rawToString); 
     
    118121//      register(t, "readln", &readln); 
    119122 
    120 //        // Dynamic compilation stuff 
     123      // Dynamic compilation stuff 
    121124//      globals["loadString"d] =      new MDClosure(globals.ns, &loadString,            "loadString"); 
    122125//      globals["eval"d] =            new MDClosure(globals.ns, &eval,                  "eval"); 
    123126//      globals["loadJSON"d] =        new MDClosure(globals.ns, &loadJSON,              "loadJSON"); 
    124127//      globals["toJSON"d] =          new MDClosure(globals.ns, &toJSON,                "toJSON"); 
    125 //  
    126 //      // The Namespace type's metatable 
    127 //      MDNamespace namespace = new MDNamespace("namespace"d, globals.ns); 
    128 //  
    129 //      namespace.addList 
    130 //      ( 
    131 //          "opApply"d, new MDClosure(namespace, &namespaceApply,  "namespace.opApply") 
    132 //      ); 
    133 //  
    134 //      context.setMetatable(MDValue.Type.Namespace, namespace); 
    135 //  
    136 //      // The Thread type's metatable 
    137 //      MDNamespace thread = new MDNamespace("thread"d, globals.ns); 
    138 //  
    139 //      thread.addList 
    140 //      ( 
    141 //          "reset"d,       new MDClosure(thread, &threadReset, "thread.reset"), 
    142 //          "state"d,       new MDClosure(thread, &threadState, "thread.state"), 
    143 //          "isInitial"d,   new MDClosure(thread, &isInitial,   "thread.isInitial"), 
    144 //          "isRunning"d,   new MDClosure(thread, &isRunning,   "thread.isRunning"), 
    145 //          "isWaiting"d,   new MDClosure(thread, &isWaiting,   "thread.isWaiting"), 
    146 //          "isSuspended"d, new MDClosure(thread, &isSuspended, "thread.isSuspended"), 
    147 //          "isDead"d,      new MDClosure(thread, &isDead,      "thread.isDead"), 
    148 //          "opApply"d,     new MDClosure(thread, &threadApply, "thread.opApply", 
    149 //          [ 
    150 //              MDValue(new MDClosure(thread, &threadIterator, "thread.iterator")) 
    151 //          ]) 
    152 //      ); 
    153 //  
    154 //      context.setMetatable(MDValue.Type.Thread, thread); 
    155 //  
    156 //      // The Function type's metatable 
    157 //      MDNamespace func = new MDNamespace("function"d, globals.ns); 
    158 //       
    159 //      func.addList 
    160 //      ( 
    161 //          "environment"d, new MDClosure(func, &functionEnvironment, "function.environment"), 
    162 //          "isNative"d,    new MDClosure(func, &functionIsNative,    "function.isNative"), 
    163 //          "numParams"d,   new MDClosure(func, &functionNumParams,   "function.numParams"), 
    164 //          "isVararg"d,    new MDClosure(func, &functionIsVararg,    "function.isVararg") 
    165 //      ); 
    166 //  
    167 //      context.setMetatable(MDValue.Type.Function, func); 
     128 
     129        // The Namespace type's metatable 
     130        newNamespace(t, "namespace"); 
     131         
     132        newFunction(t, &namespaceApply, "namespace.opApply"); fielda(t, -2, "opApply"); 
     133 
     134        setTypeMT(t, MDValue.Type.Namespace); 
     135 
     136        // The Thread type's metatable 
     137        newNamespace(t, "thread"); 
     138 
     139        newFunction(t, &threadReset, "thread.reset");       fielda(t, -2, "reset"); 
     140        newFunction(t, &threadState, "thread.state");       fielda(t, -2, "state"); 
     141        newFunction(t, &isInitial,   "thread.isInitial");   fielda(t, -2, "isInitial"); 
     142        newFunction(t, &isRunning,   "thread.isRunning");   fielda(t, -2, "isRunning"); 
     143        newFunction(t, &isWaiting,   "thread.isWaiting");   fielda(t, -2, "isWaiting"); 
     144        newFunction(t, &isSuspended, "thread.isSuspended"); fielda(t, -2, "isSuspended"); 
     145        newFunction(t, &isDead,      "thread.isDead");      fielda(t, -2, "isDead"); 
     146 
     147        newFunction(t, &threadIterator, "thread.iterator"); 
     148        newFunction(t, &threadApply, "thread.opApply", 1); 
     149        fielda(t, -2, "opApply"); 
     150 
     151        setTypeMT(t, MDValue.Type.Thread); 
     152 
     153        // The Function type's metatable 
     154        newNamespace(t, "function"); 
     155 
     156        newFunction(t, &functionEnvironment, "function.environment"); fielda(t, -2, "environment"); 
     157        newFunction(t, &functionIsNative,    "function.isNative");    fielda(t, -2, "isNative"); 
     158        newFunction(t, &functionNumParams,   "function.numParams");   fielda(t, -2, "numParams"); 
     159        newFunction(t, &functionIsVararg,    "function.isVararg");    fielda(t, -2, "isVararg"); 
     160 
     161        setTypeMT(t, MDValue.Type.Function); 
    168162    } 
    169163 
     
    211205        return 1; 
    212206    } 
    213  
    214207/* 
    215208    nuint setModuleLoader(MDThread* t, nuint numParams) 
     
    224217        return 1; 
    225218    } 
    226  
     219*/ 
    227220    nuint removeKey(MDThread* t, nuint numParams) 
    228221    { 
    229         MDValue container = s.getParam(0u); 
    230  
    231         if(container.isTable()) 
    232         { 
    233             MDValue key = s.getParam(1u); 
     222        checkAnyParam(t, 1); 
     223 
     224        if(isTable(t, 1)) 
     225        { 
     226            checkAnyParam(t, 2); 
     227            dup(t, 2); 
     228            pushNull(t); 
     229            idxa(t, 1); 
     230        } 
     231        else if(isNamespace(t, 1)) 
     232        { 
     233            checkStringParam(t, 2); 
     234 
     235            if(!opin(t, 2, 1)) 
     236            { 
     237                pushToString(t, 2); 
     238                throwException(t, "Key '{}' does not exist in namespace '{}'", getString(t, 2), getString(t, -1)); 
     239            } 
     240 
     241            // TODO: is this OK? 
     242            namespace.remove(getNamespace(t, 1), getStringObj(t, 2)); 
     243        } 
     244        else 
     245            paramTypeError(t, 1, "table|namespace"); 
    234246             
    235             if(key.isNull) 
    236                 s.throwRuntimeException("Table key cannot be null"); 
    237                  
    238             container.as!(MDTable).remove(key); 
    239         } 
    240         else if(container.isNamespace()) 
    241         { 
    242             MDNamespace ns = container.as!(MDNamespace); 
    243             MDString key = s.getParam!(MDString)(1); 
    244  
    245             if(!(key in ns)) 
    246                 s.throwRuntimeException("Key '{}' does not exist in namespace '{}'", key, ns.nameString()); 
    247  
    248             ns.remove(key); 
    249         } 
    250         else 
    251             s.throwRuntimeException("Container must be a table or namespace"); 
    252  
    253247        return 0; 
    254248    } 
    255 */ 
    256249 
    257250    nuint rawSet(MDThread* t, nuint numParams) 
     
    350343    } 
    351344 
    352 /* 
    353345    // =================================================================================================================================== 
    354346    // Reflection-esque stuff 
     
    356348    nuint findGlobal(MDThread* t, nuint numParams) 
    357349    { 
    358         auto ns = s.findGlobal(s.getParam!(MDString)(0), 1); 
    359  
    360         if(ns is null) 
    361             s.pushNull(); 
     350        if(!.findGlobal(t, checkStringParam(t, 1))) 
     351            pushNull(t); 
     352 
     353        return 1; 
     354    } 
     355 
     356    nuint isSet(MDThread* t, nuint numParams) 
     357    { 
     358        if(!.findGlobal(t, checkStringParam(t, 1))) 
     359            pushBool(t, false); 
    362360        else 
    363             s.push(ns); 
    364  
    365         return 1; 
    366     } 
    367  
    368     nuint isSet(MDThread* t, nuint numParams) 
    369     { 
    370         s.push(s.findGlobal(s.getParam!(MDString)(0), 1) !is null); 
    371         return 1; 
    372     } 
     361        { 
     362            pop(t); 
     363            pushBool(t, true); 
     364        } 
     365 
     366        return 1; 
     367    } 
     368 
     369    nuint mdtypeof(MDThread* t, nuint numParams) 
     370    { 
     371        checkAnyParam(t, 1); 
     372        pushString(t, MDValue.typeString(type(t, 1))); 
     373        return 1; 
     374    } 
     375 
     376    nuint fieldsOf(MDThread* t, nuint numParams) 
     377    { 
     378        checkObjParam(t, 1); 
     379        .fieldsOf(t, 1); 
     380        return 1; 
     381    } 
     382 
     383    nuint allFieldsOf(MDThread* t, nuint numParams) 
     384    { 
     385        // Upvalue 0 is the current object 
     386        // Upvalue 1 is the current index into the namespace 
     387        static nuint iter(MDThread* t, nuint numParams) 
     388        { 
     389            getUpval(t, 0); 
     390            auto o = getObject(t, -1); 
     391 
     392            getUpval(t, 1); 
     393            ptrdiff_t index = getInt(t, -1); 
     394 
     395            MDString** key = void; 
     396            MDValue* value = void; 
     397 
     398            if(!obj.next(o, index, key, value)) 
     399            { 
     400                superOf(t, -2); 
     401 
     402                if(isNull(t, -1)) 
     403                    return 0; 
     404 
     405                setUpval(t, 0); 
     406                pushInt(t, -1); 
     407                setUpval(t, 1); 
     408                pop(t, 2); 
     409 
     410                // try again 
     411                return iter(t, numParams); 
     412            } 
     413             
     414            pop(t, 2); 
     415 
     416            pushInt(t, index); 
     417            setUpval(t, 1); 
    373418     
    374     nuint mdtypeof(MDThread* t, nuint numParams) 
    375     { 
    376         s.push(s.getParam(0u).typeString()); 
    377         return 1; 
    378     } 
    379  
    380     nuint fieldsOf(MDThread* t, nuint numParams) 
    381     { 
    382         if(s.isParam!("object")(0)) 
    383             s.push(s.getParam!(MDObject)(0).fields); 
    384         else 
    385             s.throwRuntimeException("Expected object, not '{}'", s.getParam(0u).typeString()); 
    386  
    387         return 1; 
    388     } 
    389      
    390     nuint allFieldsOf(MDThread* t, nuint numParams) 
    391     { 
    392         auto o = s.getParam!(MDObject)(0); 
    393  
    394         struct iter 
    395         { 
    396             MDObject obj; 
    397              
    398             nuint iter(MDThread* t, nuint numParams) 
    399             { 
    400                 s.yield(0); 
    401  
    402                 for(auto o = obj; o !is null; o = o.proto) 
    403                     foreach(k, v; o.fields) 
    404                         s.yield(0, MDValue(k), v); 
    405                          
    406                 return 0; 
    407             } 
    408         } 
    409          
    410         auto i = new iter; 
    411         i.obj = o; 
    412         s.push(new MDState(s.context, new MDClosure(s.context.globals.ns, &i.iter, "allFieldsOf"))); 
    413          
     419            pushStringObj(t, *key); 
     420            push(t, *value); 
     421 
     422            return 2; 
     423        } 
     424 
     425        checkParam(t, 1, MDValue.Type.Object); 
     426        dup(t, 1); 
     427        pushInt(t, -1); 
     428        newFunction(t, &iter, "allFieldsOfIter", 2); 
    414429        return 1; 
    415430    } 
     
    417432    nuint hasField(MDThread* t, nuint numParams) 
    418433    { 
    419         s.push(s.hasField(s.getParam(0u), s.getParam!(MDString)(1))); 
     434        checkAnyParam(t, 1); 
     435        auto n = checkStringParam(t, 2); 
     436        pushBool(t, .hasField(t, 1, n)); 
    420437        return 1; 
    421438    } 
     
    423440    nuint hasMethod(MDThread* t, nuint numParams) 
    424441    { 
    425         s.push(s.hasMethod(s.getParam(0u), s.getParam!(MDString)(1))); 
    426         return 1; 
    427     } 
    428  
     442        checkAnyParam(t, 1); 
     443        auto n = checkStringParam(t, 2); 
     444        pushBool(t, .hasMethod(t, 1, n)); 
     445        return 1; 
     446    } 
     447 
     448/* 
    429449    nuint hasAttributes(MDThread* t, nuint numParams) 
    430450    { 
     
    462482        return 1; 
    463483    } 
    464      
    465     int isParam(char[] type)(MDState s, uint numParams) 
    466     { 
    467         s.push(s.isParam!(type)(0)); 
     484*/ 
     485    nuint isParam(MDValue.Type Type)(MDThread* t, nuint numParams) 
     486    { 
     487        checkAnyParam(t, 1); 
     488        pushBool(t, type(t, 1) == Type); 
    468489        return 1; 
    469490    } 
     
    471492    // =================================================================================================================================== 
    472493    // Conversions 
    473 */ 
     494 
    474495    nuint toString(MDThread* t, nuint numParams) 
    475496    { 
     
    733754//                          if(s.hasPendingHalt()) 
    734755//                              throw new MDHaltException(); 
    735 //  
     756// 
    736757//                          Stdout('['); 
    737758//                          outputRepr(k); 
     
    783804            else if(isArray(t, v)) 
    784805                outputArray(v); 
    785             else if(isTable(t, v) && !hasMethod(t, v, "toString")) 
     806            else if(isTable(t, v) && !.hasMethod(t, v, "toString")) 
    786807                outputTable(v); 
    787808            else 
     
    880901    // =================================================================================================================================== 
    881902    // Namespace metatable 
    882  
    883     nuint namespaceIterator(MDThread* t, nuint numParams) 
    884     { 
    885         MDNamespace namespace = s.getUpvalue!(MDNamespace)(0); 
    886         MDArray keys = s.getUpvalue!(MDArray)(1); 
    887         int index = s.getUpvalue!(int)(2); 
    888  
    889         index++; 
    890         s.setUpvalue(2u, index); 
    891  
    892         if(index >= keys.length) 
    893             return 0; 
    894  
    895         s.push(keys[index]); 
    896         s.push(namespace[keys[index].as!(MDString)]); 
    897  
    898         return 2; 
    899     } 
    900  
     903*/ 
    901904    nuint namespaceApply(MDThread* t, nuint numParams) 
    902905    { 
    903         MDNamespace ns = s.getContext!(MDNamespace); 
    904  
    905         MDValue[3] upvalues; 
    906         upvalues[0] = ns; 
    907         upvalues[1] = ns.keys; 
    908         upvalues[2] = -1; 
    909  
    910         s.push(s.context.newClosure(&namespaceIterator, "namespaceIterator", upvalues)); 
     906        static nuint iter(MDThread* t, nuint numParams) 
     907        { 
     908            getUpval(t, 0); 
     909            auto ns = getNamespace(t, -1); 
     910            pop(t); 
     911     
     912            getUpval(t, 1); 
     913            ptrdiff_t index = getInt(t, -1); 
     914            pop(t); 
     915     
     916            MDString** key = void; 
     917            MDValue* value = void; 
     918     
     919            if(!namespace.next(ns, index, key, value)) 
     920                return 0; 
     921     
     922            pushInt(t, index); 
     923            setUpval(t, 1); 
     924     
     925            pushStringObj(t, *key); 
     926            push(t, *value); 
     927     
     928            return 2; 
     929        } 
     930 
     931        checkParam(t, 0, MDValue.Type.Namespace); 
     932        dup(t, 0); 
     933        pushInt(t, -1); 
     934        newFunction(t, &iter, "namespaceIterator", 2); 
    911935        return 1; 
    912936    } 
     
    917941    nuint threadReset(MDThread* t, nuint numParams) 
    918942    { 
    919         MDClosure cl; 
    920  
    921         if(numParams > 0) 
    922             cl = s.getParam!(MDClosure)(0); 
    923  
    924         s.getContext!(MDState).reset(cl); 
     943        checkParam(t, 0, MDValue.Type.Thread); 
     944 
     945        if(optParam(t, 1, MDValue.Type.Function)) 
     946        { 
     947            dup(t, 1); 
     948            resetThread(t, 0, true); 
     949        } 
     950        else 
     951            resetThread(t, 0); 
     952 
    925953        return 0; 
    926954    } 
     
    928956    nuint threadState(MDThread* t, nuint numParams) 
    929957    { 
    930         s.push(s.getContext!(MDState).stateString()); 
    931         return 1; 
    932     } 
    933      
     958        checkParam(t, 0, MDValue.Type.Thread); 
     959        pushInt(t, state(getThread(t, 0))); 
     960        return 1; 
     961    } 
     962 
    934963    nuint isInitial(MDThread* t, nuint numParams) 
    935964    { 
    936         s.push(s.getContext!(MDState).state() == MDState.State.Initial); 
     965        checkParam(t, 0, MDValue.Type.Thread); 
     966        pushBool(t, state(getThread(t, 0)) == MDThread.State.Initial); 
    937967        return 1; 
    938968    } 
     
    940970    nuint isRunning(MDThread* t, nuint numParams) 
    941971    { 
    942         s.push(s.getContext!(MDState).state() == MDState.State.Running); 
     972        checkParam(t, 0, MDValue.Type.Thread); 
     973        pushBool(t, state(getThread(t, 0)) == MDThread.State.Running); 
    943974        return 1; 
    944975    } 
     
    946977    nuint isWaiting(MDThread* t, nuint numParams) 
    947978    { 
    948         s.push(s.getContext!(MDState).state() == MDState.State.Waiting); 
     979        checkParam(t, 0, MDValue.Type.Thread); 
     980        pushBool(t, state(getThread(t, 0)) == MDThread.State.Waiting); 
    949981        return 1; 
    950982    } 
     
    952984    nuint isSuspended(MDThread* t, nuint numParams) 
    953985    { 
    954         s.push(s.getContext!(MDState).state() == MDState.State.Suspended); 
     986        checkParam(t, 0, MDValue.Type.Thread); 
     987        pushBool(t, state(getThread(t, 0)) == MDThread.State.Suspended); 
    955988        return 1; 
    956989    } 
     
    958991    nuint isDead(MDThread* t, nuint numParams) 
    959992    { 
    960         s.push(s.getContext!(MDState).state() == MDState.State.Dead); 
     993        checkParam(t, 0, MDValue.Type.Thread); 
     994        pushBool(t, state(getThread(t, 0)) == MDThread.State.Dead); 
    961995        return 1; 
    962996    } 
     
    964998    nuint threadIterator(MDThread* t, nuint numParams) 
    965999    { 
    966         MDState thread = s.getContext!(MDState); 
    967         int index = s.getParam!(int)(0); 
    968         index++; 
    969  
    970         s.push(index); 
    971          
    972         uint threadIdx = s.push(thread); 
    973         s.pushNull(); 
    974         uint numRets = s.rawCall(threadIdx, -1) + 1; 
    975  
    976         if(thread.state == MDState.State.Dead) 
     1000        checkParam(t, 0, MDValue.Type.Thread); 
     1001        auto thread = getThread(t, 0); 
     1002 
     1003        pushInt(t, checkIntParam(t, 1) + 1); 
     1004 
     1005        auto slot = pushThread(t, thread); 
     1006        pushNull(t); 
     1007        auto numRets = rawCall(t, slot, -1); 
     1008 
     1009        if(state(thread) == MDThread.State.Dead) 
    9771010            return 0; 
    9781011 
    979         return numRets
     1012        return numRets + 1
    9801013    } 
    9811014 
    9821015    nuint threadApply(MDThread* t, nuint numParams) 
    9831016    { 
    984         MDState thread = s.getContext!(MDState); 
    985         MDValue init = s.getParam(0u); 
    986  
    987         if(thread.state != MDState.State.Initial) 
    988             s.throwRuntimeException("Iterated coroutine must be in the initial state"); 
    989  
    990         uint funcReg = s.push(thread); 
    991         s.push(thread); 
    992         s.push(init); 
    993         s.rawCall(funcReg, 0); 
    994  
    995         s.push(s.getUpvalue(0u)); 
    996         s.push(thread); 
    997         s.push(-1); 
     1017        checkParam(t, 0, MDValue.Type.Thread); 
     1018        auto haveParam = isValidIndex(t, 1); 
     1019        auto thread = getThread(t, 0); 
     1020 
     1021        if(state(thread) != MDThread.State.Initial) 
     1022            throwException(t, "Iterated coroutine must be in the initial state"); 
     1023 
     1024        auto slot = pushThread(t, thread); 
     1025        dup(t); 
     1026 
     1027        if(haveParam) 
     1028            dup(t, 1); 
     1029        else 
     1030            pushNull(t); 
     1031 
     1032        rawCall(t, slot, 0); 
     1033 
     1034        getUpval(t, 0); 
     1035        pushThread(t, thread); 
     1036        pushInt(t, -1); 
    9981037        return 3; 
    9991038    } 
     
    10041043    nuint functionEnvironment(MDThread* t, nuint numParams) 
    10051044    { 
    1006         MDClosure cl = s.getContext!(MDClosure); 
    1007          
    1008         s.push(cl.environment); 
     1045        checkParam(t, 0, MDValue.Type.Function); 
     1046        getFuncEnv(t, 0); 
    10091047 
    10101048        if(numParams > 0) 
    1011             cl.environment = s.getParam!(MDNamespace)(0); 
    1012  
    1013         return 1; 
    1014     } 
    1015      
     1049        { 
     1050            checkParam(t, 1, MDValue.Type.Namespace); 
     1051            dup(t, 1); 
     1052            setFuncEnv(t, 0); 
     1053        } 
     1054 
     1055        return 1; 
     1056    } 
     1057 
    10161058    nuint functionIsNative(MDThread* t, nuint numParams) 
    10171059    { 
    1018         s.push(s.getContext!(MDClosure).isNative); 
    1019         return 1; 
    1020     } 
    1021      
     1060        checkParam(t, 0, MDValue.Type.Function); 
     1061        pushBool(t, func.isNative(getFunction(t, 0))); 
     1062        return 1; 
     1063    } 
     1064 
    10221065    nuint functionNumParams(MDThread* t, nuint numParams) 
    10231066    { 
    1024         s.push(s.getContext!(MDClosure).numParams); 
    1025         return 1; 
    1026     } 
    1027      
     1067        checkParam(t, 0, MDValue.Type.Function); 
     1068        pushInt(t, func.numParams(getFunction(t, 0))); 
     1069        return 1; 
     1070    } 
     1071 
    10281072    nuint functionIsVararg(MDThread* t, nuint numParams) 
    10291073    { 
    1030         s.push(s.getContext!(MDClosure).isVararg); 
    1031         return 1; 
    1032     } 
    1033  
     1074        checkParam(t, 0, MDValue.Type.Function); 
     1075        pushBool(t, func.isVararg(getFunction(t, 0))); 
     1076        return 1; 
     1077    } 
     1078/* 
    10341079    // =================================================================================================================================== 
    10351080    // StringBuffer 
     
    10841129            else 
    10851130                ret = new MDStringBuffer(this); 
    1086                  
     1131 
    10871132            s.push(ret); 
    10881133            return 1; 
     
    11291174                { 
    11301175                    MDStringBuffer other = cast(MDStringBuffer)param.as!(MDObject); 
    1131                      
     1176 
    11321177                    if(other) 
    11331178                    { 
     
    13991444            mLength -= (end - start); 
    14001445        } 
    1401          
     1446 
    14021447        public MDString toMDString() 
    14031448        { 
  • branches/v2new/minid/ex.d

    r321 r323  
    2828import tango.text.Util; 
    2929 
    30 import minid.api; 
     30import minid.interpreter; 
     31import minid.types; 
    3132 
    3233// ================================================================================================================================================ 
     
    267268} 
    268269 
     270public void checkObjParam()(MDThread* t, nint index) 
     271{ 
     272    checkAnyParam(t, index); 
     273     
     274    if(!isObject(t, index)) 
     275        paramTypeError(t, index, "object"); 
     276} 
     277 
     278public void checkObjParam(bool strict = true)(MDThread* t, nint index, dchar[] name) 
     279{ 
     280    index = absIndex(t, index); 
     281    checkObjParam(t, index); 
     282 
     283    lookup(t, name); 
     284 
     285    if(!as(t, index, -1) || (strict ? opis(t, index, -1) : false)) 
     286    { 
     287        pushTypeString(t, index); 
     288 
     289        if(index == 0) 
     290            throwException(t, "Expected instance of object {} for 'this', not {}", name, getString(t, -1)); 
     291        else 
     292            throwException(t, "Expected instance of object {} for parameter {}, not {}", name, index, getString(t, -1)); 
     293    } 
     294 
     295    pop(t); 
     296} 
     297 
    269298public T* checkObjParam(T, bool strict = true)(MDThread* t, nint index, dchar[] name) 
    270299{ 
     
    273302} 
    274303 
    275 public void checkObjParam(bool strict = true)(MDThread* t, nint index, dchar[] name) 
    276 
    277     if(index < 0) 
    278         index = stackSize(t) + index; 
    279  
    280     if(!isObject(t, index)) 
    281     { 
    282         if(index == 0) 
    283             throwException(t, "'this' is not an object"); 
    284         else 
    285             throwException(t, "Parameter {} is not an object", index); 
    286     } 
    287  
    288     lookup(t, name); 
    289  
    290     if(!as(t, index, -1) || (strict ? opis(t, index, -1) : false)) 
    291     { 
    292         if(index == 0) 
    293             throwException(t, "'this' is not an instance of {}", name); 
    294         else 
    295             throwException(t, "Parameter {} is not an instance of {}", name); 
    296     } 
    297  
    298     pop(t); 
    299 
    300  
    301 // EPOCH FAIL. 
    302 public void checkParam(MDThread* t, nint index, nuint typeMask) 
    303 
    304     assert(typeMask != 0, "typemask must be something"); 
    305  
    306     checkAnyParam(t, index); 
    307  
    308     if(!((1 << type(t, index)) & typeMask)) 
    309     { 
    310         auto buf = StrBuffer(t); 
    311  
    312         bool first = true; 
    313  
    314         for(auto type = cast(uint)MDValue.Type.Null; type <= cast(uint)MDValue.Type.NativeObj; type++) 
    315         { 
    316             if(!(typeMask & (1 << type))) 
    317                 continue; 
    318  
    319             if(first) 
    320                 first = false; 
    321             else 
    322                 buf.addChar('|'); 
    323  
    324             buf.addString(MDValue.typeString(cast(MDValue.Type)type)); 
    325         } 
    326          
    327         buf.finish(); 
    328  
    329         paramTypeError(t, index, getString(t, -1)); 
    330     } 
    331 
    332  
    333 private void paramTypeError(MDThread* t, nint index, dchar[] expected) 
     304public void checkParam(MDThread* t, nint index, MDValue.Type type) 
     305
     306    assert(type >= MDValue.Type.Null && type <= MDValue.Type.NativeObj, "invalid type"); 
     307 
     308    checkAnyParam(t, index); 
     309 
     310    if(.type(t, index) != type) 
     311        paramTypeError(t, index, MDValue.typeString(type)); 
     312
     313 
     314public void paramTypeError(MDThread* t, nint index, dchar[] expected) 
    334315{ 
    335316    pushTypeString(t, index); 
    336     throwException(t, "Expected type '{}' for parameter {}, not '{}'", expected, index, getString(t, -1)); 
     317 
     318    if(index == 0) 
     319        throwException(t, "Expected type '{}' for 'this', not '{}'", expected, getString(t, -1)); 
     320    else 
     321        throwException(t, "Expected type '{}' for parameter {}, not '{}'", expected, index, getString(t, -1)); 
    337322} 
    338323 
     
    390375 
    391376    return getString(t, index); 
     377} 
     378 
     379public bool optParam(MDThread* t, nint index, MDValue.Type type) 
     380{ 
     381    if(!isValidIndex(t, index) || isNull(t, index)) 
     382        return false; 
     383 
     384    if(.type(t, index) != type) 
     385        paramTypeError(t, index, MDValue.typeString(type)); 
     386 
     387    return true; 
    392388} 
    393389 
  • branches/v2new/minid/func.d

    r319 r323  
    7373    } 
    7474     
     75    package bool isNative(MDFunction* f) 
     76    { 
     77        return f.isNative; 
     78    } 
     79     
     80    package nint numParams(MDFunction* f) 
     81    { 
     82        if(f.isNative) 
     83            return 0; 
     84        else 
     85            return f.scriptFunc.numParams; 
     86    } 
     87     
     88    package bool isVararg(MDFunction* f) 
     89    { 
     90        if(f.isNative) 
     91            return true; 
     92        else 
     93            return f.scriptFunc.isVararg; 
     94    } 
     95 
    7596    // ================================================================================================================================================ 
    7697    // Private 
  • branches/v2new/minid/hash.d

    r319 r323  
    199199    } 
    200200     
     201    package bool next(ref ptrdiff_t idx, ref K* key, ref V* val) 
     202    { 
     203        for(idx++; idx < mNodes.length; idx++) 
     204        { 
     205            if(mNodes[idx].used) 
     206            { 
     207                key = &mNodes[idx].key; 
     208                val = &mNodes[idx].value; 
     209                return true; 
     210            } 
     211        } 
     212 
     213        return false; 
     214    } 
     215     
    201216    package int opApply(int delegate(ref K, ref V) dg) 
    202217    { 
  • branches/v2new/minid/interpreter.d

    r321 r323  
    11551155} 
    11561156 
     1157public MDThread* getThread(MDThread* t, nint slot) 
     1158{ 
     1159    auto v = getValue(t, slot); 
     1160 
     1161    if(v.type != MDValue.Type.Thread) 
     1162    { 
     1163        pushTypeString(t, slot); 
     1164        throwException(t, "getThread - expected 'thread' but got '{}'", getString(t, -1)); 
     1165    } 
     1166 
     1167    return v.mThread; 
     1168} 
     1169 
    11571170/** 
    11581171Returns the native D object at the given _slot, or throws an error if it isn'_t one. 
     
    22352248    The stack index of the newly-pushed value. 
    22362249*/ 
    2237 public nint superof(MDThread* t, nint slot) 
    2238 { 
    2239     return push(t, superofImpl(t, getValue(t, slot))); 
     2250public nint superOf(MDThread* t, nint slot) 
     2251{ 
     2252    return push(t, superOfImpl(t, getValue(t, slot))); 
    22402253} 
    22412254 
     
    23512364} 
    23522365 
     2366public nint fieldsOf(MDThread* t, nint obj)&nbs