Changeset 323
- Timestamp:
- 06/28/08 21:49:25 (3 months ago)
- Files:
-
- branches/v2new/compile.bat (modified) (1 diff)
- branches/v2new/minid/api.d (modified) (1 diff)
- branches/v2new/minid/arraylib.d (modified) (1 diff)
- branches/v2new/minid/baselib.d (modified) (27 diffs)
- branches/v2new/minid/ex.d (modified) (4 diffs)
- branches/v2new/minid/func.d (modified) (1 diff)
- branches/v2new/minid/hash.d (modified) (1 diff)
- branches/v2new/minid/interpreter.d (modified) (11 diffs)
- branches/v2new/minid/namespace.d (modified) (1 diff)
- branches/v2new/minid/obj.d (modified) (1 diff)
- branches/v2new/samples/simple.md (modified) (1 diff)
- branches/v2new/test.d (modified) (1 diff)
- branches/v2new/test.opt (modified) (previous)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/v2new/compile.bat
r319 r323 1 1 @echo off 2 build test.d -clean -debug - unittest -w2 build test.d -clean -debug -g -unittest -w 3 3 rem build test.d -clean -release -w branches/v2new/minid/api.d
r319 r323 27 27 { 28 28 import minid.alloc; 29 import minid.ex; 29 30 import minid.gc; 30 31 import minid.types; branches/v2new/minid/arraylib.d
r278 r323 24 24 module minid.arraylib; 25 25 26 import minid.types;27 import minid.utils;28 26 import tango.core.Array; 29 27 import tango.core.Tuple; 30 28 import tango.math.Math; 31 29 32 final class ArrayLib 30 import minid.interpreter; 31 import minid.types; 32 33 struct ArrayLib 33 34 { 34 35 static: 35 public void init(MD Context context)36 public void init(MDThread* t) 36 37 { 37 38 context.setModuleLoader("array", context.newClosure(function int(MDState s, uint numParams) branches/v2new/minid/baselib.d
r321 r323 23 23 24 24 module 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;33 25 34 26 import Integer = tango.text.convert.Integer; … … 40 32 import utf = tango.text.convert.Utf; 41 33 34 import minid.ex; 35 import minid.func; 36 import minid.interpreter; 37 import minid.misc; 38 import minid.namespace; 39 import minid.obj; 40 import minid.string; 41 import minid.types; 42 import minid.vm; 43 42 44 private void register(MDThread* t, dchar[] name, NativeFunc func, size_t numUpvals = 0) 43 45 { … … 57 59 newGlobal(t, "Object"); 58 60 59 //// StringBuffer61 // StringBuffer 60 62 // globals["StringBuffer"d] = new MDStringBufferClass(_Object); 61 // 63 62 64 // Really basic stuff 63 65 // globals["getTraceback"d] = new MDClosure(globals.ns, &getTraceback, "getTraceback"); … … 66 68 // globals["setModuleLoader"d] = new MDClosure(globals.ns, &setModuleLoader, "setModuleLoader"); 67 69 // globals["reloadModule"d] = new MDClosure(globals.ns, &reloadModule, "reloadModule"); 68 // globals["removeKey"d] = new MDClosure(globals.ns, &removeKey, "removeKey");70 register(t, "removeKey", &removeKey); 69 71 register(t, "rawSet", &rawSet); 70 72 register(t, "rawGet", &rawGet); 71 73 register(t, "runMain", &runMain); 72 74 73 //// Functional stuff75 // Functional stuff 74 76 register(t, "curry", &curry); 75 77 register(t, "bindContext", &bindContext); 76 // 77 //// Reflection-esque stuff78 // 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); 85 87 // globals["hasAttributes"d] = new MDClosure(globals.ns, &hasAttributes, "hasAttributes"); 86 88 // 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 101 104 register(t, "toString", &toString); 102 105 register(t, "rawToString", &rawToString); … … 118 121 // register(t, "readln", &readln); 119 122 120 //// Dynamic compilation stuff123 // Dynamic compilation stuff 121 124 // globals["loadString"d] = new MDClosure(globals.ns, &loadString, "loadString"); 122 125 // globals["eval"d] = new MDClosure(globals.ns, &eval, "eval"); 123 126 // globals["loadJSON"d] = new MDClosure(globals.ns, &loadJSON, "loadJSON"); 124 127 // 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); 168 162 } 169 163 … … 211 205 return 1; 212 206 } 213 214 207 /* 215 208 nuint setModuleLoader(MDThread* t, nuint numParams) … … 224 217 return 1; 225 218 } 226 219 */ 227 220 nuint removeKey(MDThread* t, nuint numParams) 228 221 { 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"); 234 246 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 else251 s.throwRuntimeException("Container must be a table or namespace");252 253 247 return 0; 254 248 } 255 */256 249 257 250 nuint rawSet(MDThread* t, nuint numParams) … … 350 343 } 351 344 352 /*353 345 // =================================================================================================================================== 354 346 // Reflection-esque stuff … … 356 348 nuint findGlobal(MDThread* t, nuint numParams) 357 349 { 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); 362 360 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); 373 418 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); 414 429 return 1; 415 430 } … … 417 432 nuint hasField(MDThread* t, nuint numParams) 418 433 { 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)); 420 437 return 1; 421 438 } … … 423 440 nuint hasMethod(MDThread* t, nuint numParams) 424 441 { 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 /* 429 449 nuint hasAttributes(MDThread* t, nuint numParams) 430 450 { … … 462 482 return 1; 463 483 } 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); 468 489 return 1; 469 490 } … … 471 492 // =================================================================================================================================== 472 493 // Conversions 473 */ 494 474 495 nuint toString(MDThread* t, nuint numParams) 475 496 { … … 733 754 // if(s.hasPendingHalt()) 734 755 // throw new MDHaltException(); 735 // 756 // 736 757 // Stdout('['); 737 758 // outputRepr(k); … … 783 804 else if(isArray(t, v)) 784 805 outputArray(v); 785 else if(isTable(t, v) && ! hasMethod(t, v, "toString"))806 else if(isTable(t, v) && !.hasMethod(t, v, "toString")) 786 807 outputTable(v); 787 808 else … … 880 901 // =================================================================================================================================== 881 902 // 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 */ 901 904 nuint namespaceApply(MDThread* t, nuint numParams) 902 905 { 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); 911 935 return 1; 912 936 } … … 917 941 nuint threadReset(MDThread* t, nuint numParams) 918 942 { 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 925 953 return 0; 926 954 } … … 928 956 nuint threadState(MDThread* t, nuint numParams) 929 957 { 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 934 963 nuint isInitial(MDThread* t, nuint numParams) 935 964 { 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); 937 967 return 1; 938 968 } … … 940 970 nuint isRunning(MDThread* t, nuint numParams) 941 971 { 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); 943 974 return 1; 944 975 } … … 946 977 nuint isWaiting(MDThread* t, nuint numParams) 947 978 { 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); 949 981 return 1; 950 982 } … … 952 984 nuint isSuspended(MDThread* t, nuint numParams) 953 985 { 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); 955 988 return 1; 956 989 } … … 958 991 nuint isDead(MDThread* t, nuint numParams) 959 992 { 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); 961 995 return 1; 962 996 } … … 964 998 nuint threadIterator(MDThread* t, nuint numParams) 965 999 { 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) 977 1010 return 0; 978 1011 979 return numRets ;1012 return numRets + 1; 980 1013 } 981 1014 982 1015 nuint threadApply(MDThread* t, nuint numParams) 983 1016 { 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); 998 1037 return 3; 999 1038 } … … 1004 1043 nuint functionEnvironment(MDThread* t, nuint numParams) 1005 1044 { 1006 MDClosure cl = s.getContext!(MDClosure); 1007 1008 s.push(cl.environment); 1045 checkParam(t, 0, MDValue.Type.Function); 1046 getFuncEnv(t, 0); 1009 1047 1010 1048 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 1016 1058 nuint functionIsNative(MDThread* t, nuint numParams) 1017 1059 { 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 1022 1065 nuint functionNumParams(MDThread* t, nuint numParams) 1023 1066 { 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 1028 1072 nuint functionIsVararg(MDThread* t, nuint numParams) 1029 1073 { 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 /* 1034 1079 // =================================================================================================================================== 1035 1080 // StringBuffer … … 1084 1129 else 1085 1130 ret = new MDStringBuffer(this); 1086 1131 1087 1132 s.push(ret); 1088 1133 return 1; … … 1129 1174 { 1130 1175 MDStringBuffer other = cast(MDStringBuffer)param.as!(MDObject); 1131 1176 1132 1177 if(other) 1133 1178 { … … 1399 1444 mLength -= (end - start); 1400 1445 } 1401 1446 1402 1447 public MDString toMDString() 1403 1448 { branches/v2new/minid/ex.d
r321 r323 28 28 import tango.text.Util; 29 29 30 import minid.api; 30 import minid.interpreter; 31 import minid.types; 31 32 32 33 // ================================================================================================================================================ … … 267 268 } 268 269 270 public void checkObjParam()(MDThread* t, nint index) 271 { 272 checkAnyParam(t, index); 273 274 if(!isObject(t, index)) 275 paramTypeError(t, index, "object"); 276 } 277 278 public 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 269 298 public T* checkObjParam(T, bool strict = true)(MDThread* t, nint index, dchar[] name) 270 299 { … … 273 302 } 274 303 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) 304 public 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 314 public void paramTypeError(MDThread* t, nint index, dchar[] expected) 334 315 { 335 316 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)); 337 322 } 338 323 … … 390 375 391 376 return getString(t, index); 377 } 378 379 public 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; 392 388 } 393 389 branches/v2new/minid/func.d
r319 r323 73 73 } 74 74 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 75 96 // ================================================================================================================================================ 76 97 // Private branches/v2new/minid/hash.d
r319 r323 199 199 } 200 200 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 201 216 package int opApply(int delegate(ref K, ref V) dg) 202 217 { branches/v2new/minid/interpreter.d
r321 r323 1155 1155 } 1156 1156 1157 public 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 1157 1170 /** 1158 1171 Returns the native D object at the given _slot, or throws an error if it isn'_t one. … … 2235 2248 The stack index of the newly-pushed value. 2236 2249 */ 2237 public nint super of(MDThread* t, nint slot)2238 { 2239 return push(t, super ofImpl(t, getValue(t, slot)));2250 public nint superOf(MDThread* t, nint slot) 2251 { 2252 return push(t, superOfImpl(t, getValue(t, slot))); 2240 2253 } 2241 2254 … … 2351 2364 } 2352 2365 2366 public nint fieldsOf(MDThread* t, nint obj)&nbs
