// (loop ['any | (NIL 'any . prg) | (T 'any . prg) ..]) -> any any doLoop(any ex) { any x, y, a; for (;;) { x = cdr(ex); do { if (isCell(y = car(x))) { if (isNil(car(y))) { y = cdr(y); if (isNil(a = EVAL(car(y)))) return prog(cdr(y)); val(At) = a; } else if (car(y) == T) { y = cdr(y); if (!isNil(a = EVAL(car(y)))) { val(At) = a; return prog(cdr(y)); } } else evList(y); } } while (isCell(x = cdr(x))); } }
// (job 'lst . prg) -> any any doJob(any ex) { any x = cdr(ex); any y = EVAL(car(x)); cell c1; struct { // bindFrame struct bindFrame *link; int i, cnt; struct {any sym; any val;} bnd[length(y)]; } f; Push(c1,y); f.link = Env.bind, Env.bind = (bindFrame*)&f; f.i = f.cnt = 0; while (isCell(y)) { f.bnd[f.cnt].sym = caar(y); f.bnd[f.cnt].val = val(caar(y)); val(caar(y)) = cdar(y); ++f.cnt, y = cdr(y); } x = prog(cdr(x)); for (f.cnt = 0, y = Pop(c1); isCell(y); ++f.cnt, y = cdr(y)) { cdar(y) = val(caar(y)); val(caar(y)) = f.bnd[f.cnt].val; } Env.bind = f.link; return x; }
// (new ['flg|num] ['typ ['any ..]]) -> obj any doNew(any ex) { any x, y, *h; cell c1, c2; x = cdr(ex); if (isCell(y = EVAL(car(x)))) Push(c1, consSym(y,Nil)); else { if (isNil(y)) data(c1) = consSym(Nil,Nil); else { y = newId(ex, isNum(y)? (int)unDig(y)/2 : 1); if (data(c1) = findHash(y, h = Extern + ehash(y))) tail(data(c1)) = y; else *h = cons(data(c1) = consSym(Nil,y), *h); mkExt(data(c1)); } Save(c1); x = cdr(x), val(data(c1)) = EVAL(car(x)); } TheKey = T, TheCls = NULL; if (y = method(data(c1))) evMethod(data(c1), y, cdr(x)); else { Push(c2, Nil); while (isCell(x = cdr(x))) { data(c2) = EVAL(car(x)), x = cdr(x); put(data(c1), data(c2), EVAL(car(x))); } } return Pop(c1); }
// (term-prinl ['num 'num] 'any ..) -> any any plisp_term_prinl(any ex) { any x, y; long n1, n2; // if number of args > 1, we accept // a min of 3 args - x, y and the value // to print. if (plen(ex) > 1 && isNum(cadr(ex)) && isNum(caddr(ex))) { x = cdr(ex), y = EVAL(car(x)); NeedNum(ex, y); n1 = unBox(y); // we get x here. x = cdr(x), y = EVAL(car(x)); NeedNum(ex, y); n2 = unBox(y); // we get y here. term_gotoxy(n1, n2); // now, get the rest of the params // and prinl. while (isCell(x = cdr(x))) ptermh_prin(y = EVAL(car(x))); } else { // We don't have the coordinates. // we just print the first value // in the list (including NIL). x = cdr(ex), y = EVAL(car(x)); ptermh_prin(y); while (isCell(x = cdr(x))) ptermh_prin(y = EVAL(car(x))); } newline(); return y; }
// (prog1 'any1 . prg) -> any1 any doProg1(any x) { cell c1; x = cdr(x), Push(c1, val(At) = EVAL(car(x))); while (isCell(x = cdr(x))) if (isCell(car(x))) evList(car(x)); return Pop(c1); }
// (use sym . prg) -> any // (use (sym ..) . prg) -> any any doUse(any x) { any y; x = cdr(x); if (isSym(y = car(x))) { bindFrame f; Bind(y,f); x = prog(cdr(x)); Unbind(f); } else { struct { // bindFrame struct bindFrame *link; int i, cnt; struct {any sym; any val;} bnd[length(y)]; } f; f.link = Env.bind, Env.bind = (bindFrame*)&f; f.i = f.cnt = 0; do { f.bnd[f.cnt].sym = car(y); f.bnd[f.cnt].val = val(car(y)); ++f.cnt; } while (isCell(y = cdr(y))); x = prog(cdr(x)); while (--f.cnt >= 0) val(f.bnd[f.cnt].sym) = f.bnd[f.cnt].val; Env.bind = f.link; } return x; }
//------------------------------------------------------------------------------------- void MethodDescription::addToStream(MemoryStream* mstream, PyObject* args) { uint8 argsSize = argTypes_.size(); int offset = 0; // 将utype放进去,方便对端识别这个方法 // 这里如果aliasID_大于0则采用一个优化的办法, 使用1字节传输 if(aliasID_ < 0) { (*mstream) << utype_; } else { uint8 utype = (uint8)aliasID_; (*mstream) << utype; } // 如果是exposed方法则先将entityID打包进去 if(isExposed() && g_componentType == CELLAPP_TYPE && isCell()) { offset = 1; ENTITY_ID eid = PyLong_AsLong(PyTuple_GetItem(args, 0)); (*mstream) << eid; } // 将每一个参数添加到流中 for(uint8 i=0; i <argsSize; i++) { PyObject* pyArg = PyTuple_GetItem(args, i + offset); argTypes_[i]->addToStream(mstream, pyArg); } }
//------------------------------------------------------------------------------------- PyObject* MethodDescription::createFromStream(MemoryStream* mstream) { size_t argSize = getArgSize(); PyObject* pyArgsTuple = NULL; int offset = 0; if(isExposed() && g_componentType == CELLAPP_TYPE && isCell()) { offset = 1; pyArgsTuple = PyTuple_New(argSize + offset); // 设置一个调用者ID提供给脚本判断来源是否正确 KBE_ASSERT(currCallerID_ > 0); PyTuple_SET_ITEM(pyArgsTuple, 0, PyLong_FromLong(currCallerID_)); } else pyArgsTuple = PyTuple_New(argSize); for(size_t index=0; index<argSize; index++) { PyObject* pyitem = argTypes_[index]->createFromStream(mstream); if(pyitem == NULL) { WARNING_MSG(fmt::format("MethodDescription::createFromStream:{} arg[{}][{}] is NULL.\n", this->getName(), index, argTypes_[index]->getName())); } PyTuple_SET_ITEM(pyArgsTuple, index + offset, pyitem); } return pyArgsTuple; }
static void putSrc(any s, any k) { if (!isNil(val(Dbg)) && !isExt(s) && InFile && InFile->name) { any x, y; cell c1; Push(c1, boxCnt(InFile->src)); data(c1) = cons(data(c1), mkStr(InFile->name)); x = get(s, Dbg); if (!k) { if (isNil(x)) put(s, Dbg, cons(data(c1), Nil)); else car(x) = data(c1); } else if (isNil(x)) put(s, Dbg, cons(Nil, cons(data(c1), Nil))); else { for (y = cdr(x); isCell(y); y = cdr(y)) if (caar(y) == k) { cdar(y) = data(c1); drop(c1); return; } cdr(x) = cons(cons(k, data(c1)), cdr(x)); } drop(c1); } }
// ($ sym|lst lst . prg) -> any any doTrace(any x) { any foo, body; outFile *oSave; void (*putSave)(int); cell c1; x = cdr(x); if (isNil(val(Dbg))) return prog(cddr(x)); oSave = OutFile, putSave = Env.put; OutFile = OutFiles[STDERR_FILENO], Env.put = putStdout; foo = car(x); x = cdr(x), body = cdr(x); traceIndent(++Env.trace, foo, " :"); for (x = car(x); isCell(x); x = cdr(x)) space(), print(val(car(x))); if (!isNil(x)) { if (x != At) space(), print(val(x)); else { int i = Env.next; while (--i >= 0) space(), print(data(Env.arg[i])); } } newline(); Env.put = putSave, OutFile = oSave; Push(c1, prog(body)); OutFile = OutFiles[STDERR_FILENO], Env.put = putStdout; traceIndent(Env.trace--, foo, " = "), print(data(c1)); newline(); Env.put = putSave, OutFile = oSave; return Pop(c1); }
char* JSValue::description() { static const size_t size = 32; static char description[size]; if (!*this) snprintf(description, size, "<JSValue()>"); else if (isInt32()) snprintf(description, size, "Int32: %d", asInt32()); else if (isDouble()) snprintf(description, size, "Double: %lf", asDouble()); else if (isCell()) snprintf(description, size, "Cell: %p", asCell()); else if (isTrue()) snprintf(description, size, "True"); else if (isFalse()) snprintf(description, size, "False"); else if (isNull()) snprintf(description, size, "Null"); else { ASSERT(isUndefined()); snprintf(description, size, "Undefined"); } return description; }
MxArray MxArray::at(mwIndex index) const { if (!isCell()) mexErrMsgIdAndTxt("mexopencv:error", "MxArray is not cell"); if (numel() <= index) mexErrMsgIdAndTxt("mexopencv:error", "Index out of range"); return MxArray(mxGetCell(p_, index)); }
void MxArray::set(mwIndex index, const MxArray& value) { if (!isCell()) mexErrMsgIdAndTxt("mexopencv:error", "MxArray is not cell"); if (numel() <= index) mexErrMsgIdAndTxt("mexopencv:error", "Index out of range"); mxSetCell(const_cast<mxArray*>(p_), index, static_cast<mxArray*>(value)); }
static any extra(any x) { any y; for (x = val(x); isCell(car(x)); x = cdr(x)); while (isCell(x)) { if (x == Env.cls || !(y = extra(car(x)))) { while (isCell(x = cdr(x))) if (y = method(car(TheCls = x))) return y; return NULL; } if (y && num(y) != 1) return y; x = cdr(x); } return (any)1; }
any method(any x) { any y, z; if (isCell(y = val(x))) { while (isCell(z = car(y))) { if (car(z) == TheKey) return cdr(z); if (!isCell(y = cdr(y))) return NULL; } do if (x = method(car(TheCls = y))) return x; while (isCell(y = cdr(y))); } return NULL; }
// (case 'any (any1 . prg1) (any2 . prg2) ..) -> any any doCase(any x) { any y, z; x = cdr(x), val(At) = EVAL(car(x)); while (isCell(x = cdr(x))) { y = car(x), z = car(y); if (z == T || equal(val(At), z)) return prog(cdr(y)); if (isCell(z)) { do if (equal(val(At), car(z))) return prog(cdr(y)); while (isCell(z = cdr(z))); } } return Nil; }
double JSValue::toNumberSlowCase(ExecState* exec) const { ASSERT(!isInt32() && !isDouble()); if (isCell()) return asCell()->toNumber(exec); if (isTrue()) return 1.0; return isUndefined() ? PNaN : 0; // null and false both convert to 0. }
void MxArray::set(mwIndex index, const MxArray& value) { if (index < 0 || numel() <= index) mexErrMsgIdAndTxt("mexopencv:error", "Accessing invalid range"); if (!isCell()) mexErrMsgIdAndTxt("mexopencv:error", "Not cell array"); mxSetCell(const_cast<mxArray*>(p_), index, static_cast<mxArray*>(value)); }
// (super ['any ..]) -> any any doSuper(any ex) { any x, y, cls, key; TheKey = Env.key; x = val(Env.cls? car(Env.cls) : val(This)); while (isCell(car(x))) x = cdr(x); while (isCell(x)) { if (y = method(car(TheCls = x))) { cls = Env.cls, Env.cls = TheCls; key = Env.key, Env.key = TheKey; x = evExpr(y, cdr(ex)); Env.key = key, Env.cls = cls; return x; } x = cdr(x); } err(ex, TheKey, "Bad super"); }
// (or 'any ..) -> any any doOr(any x) { any a; x = cdr(x); do if (!isNil(a = EVAL(car(x)))) return val(At) = a; while (isCell(x = cdr(x))); return Nil; }
// (nond ('any1 . prg1) ('any2 . prg2) ..) -> any any doNond(any x) { any a; while (isCell(x = cdr(x))) { if (isNil(a = EVAL(caar(x)))) return prog(cdar(x)); val(At) = a; } return Nil; }
// (nand 'any ..) -> flg any doNand(any x) { any a; x = cdr(x); do { if (isNil(a = EVAL(car(x)))) return T; val(At) = a; } while (isCell(x = cdr(x))); return Nil; }
JSObject* JSValue::toThisObjectSlowCase(ExecState* exec) const { ASSERT(!isCell()); if (isInt32() || isDouble()) return constructNumber(exec, asValue()); if (isTrue() || isFalse()) return constructBooleanFromImmediateBoolean(exec, asValue()); ASSERT(isUndefinedOrNull()); return exec->globalThisValue(); }
// (cond ('any1 . prg1) ('any2 . prg2) ..) -> any any doCond(any x) { any a; while (isCell(x = cdr(x))) { if (!isNil(a = EVAL(caar(x)))) { val(At) = a; return prog(cdar(x)); } } return Nil; }
JSObject* JSValue::synthesizePrototype(ExecState* exec) const { ASSERT(!isCell()); if (isNumber()) return exec->lexicalGlobalObject()->numberPrototype(); if (isBoolean()) return exec->lexicalGlobalObject()->booleanPrototype(); JSNotAnObjectErrorStub* exception = createNotAnObjectErrorStub(exec, isNull()); exec->setException(exception); return new (exec) JSNotAnObject(exec, exception); }
JSObject* JSValue::synthesizeObject(ExecState* exec) const { ASSERT(!isCell()); if (isNumber()) return constructNumber(exec, asValue()); if (isBoolean()) return constructBooleanFromImmediateBoolean(exec, asValue()); JSNotAnObjectErrorStub* exception = createNotAnObjectErrorStub(exec, isNull()); exec->setException(exception); return new (exec) JSNotAnObject(exec, exception); }
// (do 'flg|num ['any | (NIL 'any . prg) | (T 'any . prg) ..]) -> any any doDo(any x) { any y, z, a; cell c1; x = cdr(x); if (isNil(data(c1) = EVAL(car(x)))) return Nil; Save(c1); if (isNum(data(c1))) { if (isNeg(data(c1))) { drop(c1); return Nil; } data(c1) = bigCopy(data(c1)); } x = cdr(x), z = Nil; for (;;) { if (isNum(data(c1))) { if (IsZero(data(c1))) { drop(c1); return z; } digSub1(data(c1)); } y = x; do { if (!isNum(z = car(y))) { if (isSym(z)) z = val(z); else if (isNil(car(z))) { z = cdr(z); if (isNil(a = EVAL(car(z)))) { drop(c1); return prog(cdr(z)); } val(At) = a; z = Nil; } else if (car(z) == T) { z = cdr(z); if (!isNil(a = EVAL(car(z)))) { val(At) = a; drop(c1); return prog(cdr(z)); } z = Nil; } else z = evList(z); } } while (isCell(y = cdr(y))); } }
static bool isa(any cls, any x) { any z; z = x = val(x); while (isCell(x)) { if (!isCell(car(x))) { while (isSym(car(x))) { if (isExt(car(x))) return NO; if (cls == car(x) || isa(cls, car(x))) return YES; if (!isCell(x = cdr(x)) || z == x) return NO; } return NO; } if (z == (x = cdr(x))) return NO; } return NO; }
// (nor 'any ..) -> flg any doNor(any x) { any a; x = cdr(x); do if (!isNil(a = EVAL(car(x)))) { val(At) = a; return Nil; } while (isCell(x = cdr(x))); return T; }
JSObject* JSValue::toObjectSlowCase(ExecState* exec) const { ASSERT(!isCell()); if (isInt32() || isDouble()) return constructNumber(exec, asValue()); if (isTrue() || isFalse()) return constructBooleanFromImmediateBoolean(exec, asValue()); ASSERT(isUndefinedOrNull()); JSNotAnObjectErrorStub* exception = createNotAnObjectErrorStub(exec, isNull()); exec->setException(exception); return new (exec) JSNotAnObject(exec, exception); }