/* function load(script: Path, timeout: Number = 0): Void */ static EjsObj *workerLoad(Ejs *ejs, EjsWorker *worker, int argc, EjsObj **argv) { int timeout; assert(argc == 0 || ejsIs(ejs, argv[0], Path)); worker->scriptFile = sclone(((EjsPath*) argv[0])->value); timeout = argc == 2 ? ejsGetInt(ejs, argv[1]): 0; return startWorker(ejs, worker, timeout); }
/* function eval(script: String, timeout: Boolean = -1): Obj */ static EjsObj *workerEval(Ejs *ejs, EjsWorker *worker, int argc, EjsObj **argv) { int timeout; assert(ejsIs(ejs, argv[0], String)); worker->scriptLiteral = (EjsString*) argv[0]; timeout = argc == 2 ? ejsGetInt(ejs, argv[1]): MAXINT; return startWorker(ejs, worker, timeout); }
/* Write data to the file function write(data: Object): Number */ PUBLIC EjsObj *writeFile(Ejs *ejs, EjsFile *fp, int argc, EjsObj **argv) { EjsArray *args; EjsByteArray *ap; EjsObj *vp; EjsString *str; cchar *buf; ssize len, written; int i; assert(argc == 1 && ejsIs(ejs, argv[0], Array)); args = (EjsArray*) argv[0]; if (!(fp->mode & EJS_FILE_WRITE)) { ejsThrowStateError(ejs, "File not opened for writing"); return 0; } written = 0; for (i = 0; i < args->length; i++) { vp = ejsGetProperty(ejs, (EjsObj*) args, i); assert(vp); switch (TYPE(vp)->sid) { case S_ByteArray: ap = (EjsByteArray*) vp; // TODO UNICODE ENCODING buf = (cchar*) &ap->value[ap->readPosition]; len = ap->writePosition - ap->readPosition; break; case S_String: // UNICODE #if UNICODE && FUTURE buf = awtom(((EjsString*) vp)->value, &len); #else buf = ((EjsString*) vp)->value; len = ((EjsString*) vp)->length; #endif break; default: str = ejsToString(ejs, vp); buf = awtom(((EjsString*) str)->value, &len); break; } if (mprWriteFile(fp->file, buf, len) != len) { ejsThrowIOError(ejs, "Cannot write to %s", fp->path); return 0; } written += len; /* Use GC to free buf as it may not be allocated */ } return (EjsObj*) ejsCreateNumber(ejs, (MprNumber) written); }
/* native function XML(value: Object = null) */ static EjsObj *xmlConstructor(Ejs *ejs, EjsXML *thisObj, int argc, EjsObj **argv) { EjsObj *arg, *vp; wchar *str; // TODO - should be also able to handle a File object if (thisObj == 0) { /* Called as a function - cast the arg */ if (argc > 0) { if ((arg = ejsCast(ejs, argv[0], String)) == 0) { return 0; } } thisObj = ejsCreateXML(ejs, 0, N(NULL, NULL), NULL, NULL); } if (argc == 0) { return (EjsObj*) thisObj; } arg = argv[0]; assert(arg); if (!ejsIsDefined(ejs, arg)) { return (EjsObj*) thisObj; } arg = ejsCast(ejs, argv[0], String); if (arg && ejsIs(ejs, arg, String)) { str = ((EjsString*) arg)->value; if (str == 0) { return 0; } while (isspace((uchar) *str)) str++; if (*str == '<') { /* XML Literal */ ejsLoadXMLString(ejs, thisObj, (EjsString*) arg); } else { /* Load from file */ loadXml(ejs, thisObj, argc, argv); } } else if (arg && ejsIsXML(ejs, arg)) { if ((vp = xmlToString(ejs, argv[0], 0, NULL)) != 0) { ejsLoadXMLString(ejs, thisObj, (EjsString*) vp); } } else { ejsThrowArgError(ejs, "Bad type passed to XML constructor"); return 0; } return (EjsObj*) thisObj; }
static bool compareArrayElement(Ejs *ejs, EjsObj *v1, EjsObj *v2) { if (v1 == v2) { return 1; } if (TYPE(v1) != TYPE(v2)) { return 0; } if (ejsIs(ejs, v1, Number)) { return ((EjsNumber*) v1)->value == ((EjsNumber*) v2)->value; } if (ejsIs(ejs, v1, String)) { return (EjsString*) v1 == (EjsString*) v2; } if (ejsIs(ejs, v1, Path)) { return smatch(((EjsPath*) v1)->value, ((EjsPath*) v2)->value); } // TODO - should expand for other types return 0; }
/* run(deep: Boolean = false) TODO -- change args to be a string "check", "all" */ static EjsObj *gc_run(Ejs *ejs, EjsObj *thisObj, int argc, EjsObj **argv) { int deep; assure(!ejs->state->paused); if (!ejs->state->paused) { deep = ((argc == 1) && ejsIs(ejs, argv[1], Boolean)); mprRequestGC(MPR_FORCE_GC | (deep ? MPR_COMPLETE_GC : 0)); } return 0; }
/* function sendBlock(content, options): Number */ static EjsNumber *ws_sendBlock(Ejs *ejs, EjsWebSocket *ws, int argc, EjsObj **argv) { EjsByteArray *ba; EjsAny *content, *vp; ssize nbytes; cchar *str; int last, mode, type, flags; assert(argc == 2); if (ws->conn->state < HTTP_STATE_PARSED && !waitForHttpState(ws, HTTP_STATE_PARSED, -1, 1)) { return ESV(null); } content = argv[0]; last = ejsGetPropertyByName(ejs, argv[1], EN("last")) != ESV(false); if ((vp = ejsGetPropertyByName(ejs, argv[1], EN("mode"))) != 0) { mode = (int) ejsGetNumber(ejs, vp); if (mode != HTTP_BUFFER && mode != HTTP_BLOCK && mode != HTTP_NON_BLOCK) { ejsThrowArgError(ejs, "Bad message mode"); return 0; } } else { mode = HTTP_BUFFER; } if ((vp = ejsGetPropertyByName(ejs, argv[1], EN("type"))) != 0) { type = (int) ejsGetNumber(ejs, vp); if (type != WS_MSG_CONT && type != WS_MSG_TEXT && type != WS_MSG_BINARY) { ejsThrowArgError(ejs, "Bad message type"); return 0; } } else { type = WS_MSG_TEXT; } flags = mode; if (!last) { flags |= HTTP_MORE; } if (ejsIs(ejs, content, ByteArray)) { ba = (EjsByteArray*) content; nbytes = ejsGetByteArrayAvailableData(ba); nbytes = httpSendBlock(ws->conn, type, (cchar*) &ba->value[ba->readPosition], nbytes, flags); } else { str = ejsToMulti(ejs, content); nbytes = httpSendBlock(ws->conn, type, str, slen(str), flags); } if (nbytes < 0) { ejsThrowIOError(ejs, "Cannot send block"); return 0; } return ejsCreateNumber(ejs, (MprNumber) nbytes); }
/* native static function set newQuota(quota: Number): Void */ static EjsObj *gc_set_newQuota(Ejs *ejs, EjsObj *thisObj, int argc, EjsObj **argv) { int quota; assure(argc == 1 && ejsIs(ejs, argv[0], Number)); quota = ejsGetInt(ejs, argv[0]); if (quota < MPR_NEW_QUOTA && quota != 0) { ejsThrowArgError(ejs, "Bad work quota"); return 0; } mprGetMpr()->heap->newQuota = quota; return 0; }
/* function setCredentials(username: String?, password: String?, authType: String?): Void */ static EjsObj *http_setCredentials(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { cchar *authType, *password, *user; user = (argc <= 0) ? 0 : ejsToMulti(ejs, argv[0]); password = (argc <= 1) ? 0 : ejsToMulti(ejs, argv[1]); authType = (argc <= 2) ? 0 : ejsToMulti(ejs, argv[2]); if (ejsIs(ejs, argv[0], Null)) { httpResetCredentials(hp->conn); } else { httpSetCredentials(hp->conn, user, password, authType); } return 0; }
/* Quick sort partition */ static int partition(Ejs *ejs, EjsArray *array, EjsFunction *compare, int direction, int p, int r) { EjsString *sx, *so; EjsNumber *result; EjsAny *argv[3], *tmp; EjsObj *x; int i, j, order; x = array->data[r]; sx = 0; if (compare) { if ((argv[1] = ejsCreateNumber(ejs, r)) == 0) { return 0; } } else { if ((sx = ejsToString(ejs, x)) == 0) { return 0; } } j = p - 1; for (i = p; i < r; i++) { if (compare) { argv[0] = array; argv[2] = ejsCreateNumber(ejs, i); result = ejsRunFunction(ejs, compare, NULL, 3, argv); if (!ejsIs(ejs, result, Number)) { return 0; } order = ejsGetInt(ejs, result); } else { if ((so = ejsToString(ejs, array->data[i])) == 0) { return 0; } order = ejsCompareString(ejs, sx, so); } order *= direction; if (order > 0) { j = j + 1; tmp = array->data[j]; array->data[j] = array->data[i]; array->data[i] = tmp; } } array->data[r] = array->data[j + 1]; array->data[j + 1] = x; return j + 1; }
/* Read data bytes from a file function readBytes(count: Number = -1): ByteArray */ static EjsObj *readFileBytes(Ejs *ejs, EjsFile *fp, int argc, EjsObj **argv) { EjsByteArray *result; MprPath info; ssize count, totalRead; if (argc == 0) { count = -1; } else if (argc != 1) { count = 0; ejsThrowArgError(ejs, "Bad args"); return 0; } else { assert(argc == 1 && ejsIs(ejs, argv[0], Number)); count = ejsGetInt(ejs, argv[0]); } if (fp->file == 0) { ejsThrowStateError(ejs, "File not open"); return 0; } if (!(fp->mode & EJS_FILE_READ)) { ejsThrowStateError(ejs, "File not opened for reading"); return 0; } if (count < 0) { // TODO OPT could this be cached in fp->info if (mprGetPathInfo(fp->path, &info) == 0) { count = (int) info.size; count -= (int) mprGetFilePosition(fp->file); } else { count = ME_MAX_BUFFER; } assert(count >= 0); } result = ejsCreateByteArray(ejs, count); if (result == 0) { ejsThrowMemoryError(ejs); return 0; } totalRead = readData(ejs, fp, result, 0, count); if (totalRead < 0) { ejsThrowIOError(ejs, "Cannot read from file: %s", fp->path); return 0; } else if (totalRead == 0) { return ESV(null); } ejsSetByteArrayPositions(ejs, result, 0, totalRead); return (EjsObj*) result; }
/* function emit(level: Number, ...data): Number */ static EjsNumber *lf_emit(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv) { EjsArray *args; EjsByteArray *ap; EjsObj *vp; EjsString *str; char *msg, *arg; ssize len, written; int i, level, paused; assure(argc >= 2 && ejsIs(ejs, argv[1], Array)); level = ejsGetInt(ejs, argv[0]); args = (EjsArray*) argv[1]; written = 0; msg = 0; paused = ejsBlockGC(ejs); for (i = 0; i < args->length; i++) { vp = ejsGetProperty(ejs, args, i); assure(vp); switch (TYPE(vp)->sid) { case S_ByteArray: ap = (EjsByteArray*) vp; // TODO ENCODING arg = (char*) &ap->value[ap->readPosition]; len = ap->writePosition - ap->readPosition; break; case S_String: // MOB - use NULL instead of &len arg = awtom(((EjsString*) vp)->value, &len); break; default: str = ejsToString(ejs, vp); // MOB - use NULL instead of &len arg = awtom(((EjsString*) str)->value, &len); break; } msg = srejoin(msg, arg, NULL); } if (msg) { mprRawLog(level, "%s", msg); written += slen(msg); } ejsUnblockGC(ejs, paused); return ejsCreateNumber(ejs, (MprNumber) slen(msg)); }
/* Convert an arg to a URI. Can handle strings, paths, URIs and object hashes. Will cast all else to strings and then parse. */ static EjsUri *castToUri(Ejs *ejs, EjsObj *arg) { EjsUri *up; up = ejsCreateObj(ejs, ESV(Uri), 0); if (ejsIs(ejs, arg, String)) { up->uri = httpCreateUri(up, ejsToMulti(ejs, arg), 0); } else if (ejsIs(ejs, arg, Uri)) { up->uri = httpCloneUri(((EjsUri*) arg)->uri, 0); } else if (ejsIs(ejs, arg, Path)) { ustr = ((EjsPath*) arg)->path; up->uri = httpCreateUri(up, ustr, 0); } else if (ejsGetLength(ejs, arg) > 0) { up->uri = createHttpUriFromHash(ejs, up, arg, 0); } else { arg = (EjsObj) ejsToString(ejs, arg); up->uri = httpCreateUri(up, ejsToMulti(ejs, arg), 0); } return up; }
/* Set the length of an array. override function set length(value: Number): void */ static EjsObj *setArrayLength(Ejs *ejs, EjsArray *ap, int argc, EjsObj **argv) { EjsObj **data, **dest; int length; assert(argc == 1 && ejsIs(ejs, argv[0], Number)); assert(ejsIs(ejs, ap, Array)); length = (int) ((EjsNumber*) argv[0])->value; if (length < 0) { length = 0; } if (length > ap->length) { if (growArray(ejs, ap, length) < 0) { return 0; } data = ap->data; for (dest = &data[ap->length]; dest < &data[length]; dest++) { *dest = 0; } } ap->length = length; return 0; }
/* Function to iterate and return the next element index. NOTE: this is not a method of Array. Rather, it is a callback function for Iterator */ static EjsNumber *nextKey(Ejs *ejs, EjsIterator *ip, int argc, EjsObj **argv) { EjsFile *fp; fp = (EjsFile*) ip->target; if (!ejsIs(ejs, fp, File)) { ejsThrowReferenceError(ejs, "Wrong type"); return 0; } if (ip->index < fp->info.size) { return ejsCreateNumber(ejs, ip->index++); } ejsThrowStopIteration(ejs); return 0; }
/* Interpret from the console or from a literal command */ static int interpretCommands(EcCompiler *cp, cchar *cmd) { Ejs *ejs; EjsString *result; char *tmpArgv[1]; int err; ejs = cp->ejs; cp->interactive = 1; if (ecOpenConsoleStream(cp, (cmd) ? commandGets: consoleGets, cmd) < 0) { mprError("Cannot open input"); return EJS_ERR; } tmpArgv[0] = EC_INPUT_STREAM; while (!cp->stream->eof && !mprIsStopping()) { err = 0; cp->uid = 0; ejs->result = ESV(undefined); if (ecCompile(cp, 1, tmpArgv) < 0) { mprRawLog(0, "%s", cp->errorMsg); ejs->result = ESV(undefined); err++; } if (!err && cp->errorCount == 0) { if (ejsRunProgram(ejs, NULL, NULL) < 0) { ejsReportError(ejs, "Error in script"); } } if (!ejs->exception && ejs->result != ESV(undefined)) { if (ejsIs(ejs, ejs->result, Date) /* MOB || ejsIsType(ejs, ejs->result) */) { if ((result = (EjsString*) ejsToString(ejs, ejs->result)) != 0) { mprPrintf("%@\n", result); } } else if (ejs->result != ESV(null)) { if ((result = (EjsString*) ejsSerialize(ejs, ejs->result, EJS_JSON_SHOW_PRETTY)) != 0) { mprPrintf("%@\n", result); } } } ecResetInput(cp); cp->errorCount = 0; cp->fatalError = 0; } ecCloseStream(cp); return 0; }
/* Read data as a string function readString(count: Number = -1): String */ static EjsString *readFileString(Ejs *ejs, EjsFile *fp, int argc, EjsObj **argv) { EjsString *result; MprPath info; ssize totalRead; int count; if (argc == 0) { count = -1; } else if (argc != 1) { count = 0; ejsThrowArgError(ejs, "Bad args"); return 0; } else { assert(argc == 1 && ejsIs(ejs, argv[0], Number)); count = ejsGetInt(ejs, argv[0]); } if (fp->file == 0) { ejsThrowStateError(ejs, "File not open"); return 0; } if (!(fp->mode & EJS_FILE_READ)) { ejsThrowStateError(ejs, "File not opened for reading"); return 0; } if (count < 0) { // TODO OPT could this be cached in fp->info if (mprGetPathInfo(fp->path, &info) == 0) { count = (int) info.size; count -= (int) mprGetFilePosition(fp->file); } else { count = ME_MAX_BUFFER; } assert(count >= 0); } if ((result = ejsCreateBareString(ejs, count)) == NULL) { ejsThrowMemoryError(ejs); return 0; } totalRead = mprReadFile(fp->file, result->value, count); if (totalRead != count) { ejsThrowIOError(ejs, "Cannot read from file: %s", fp->path); return 0; } return ejsInternString(result); }
/* Seek to a new location in the file and set the File marker to a new read/write position. function set position(value: Number): void */ static EjsObj *setFilePosition(Ejs *ejs, EjsFile *fp, int argc, EjsObj **argv) { MprOff pos; assert(argc == 1 && ejsIs(ejs, argv[0], Number)); pos = ejsGetInt(ejs, argv[0]); if (fp->file == 0) { ejsThrowStateError(ejs, "File not opened"); return 0; } pos = ejsGetInt(ejs, argv[0]); if (mprSeekFile(fp->file, SEEK_SET, pos) != pos) { ejsThrowIOError(ejs, "Cannot seek to %Ld", pos); } return 0; }
/* Prepare form data as a series of key-value pairs. Data is formatted according to www-url-encoded specs by mprSetHttpFormData. Objects are flattened into a one level key/value pairs. Keys can have embedded "." separators. E.g. name=value&address=77%20Park%20Lane */ static void prepForm(Ejs *ejs, EjsHttp *hp, cchar *prefix, EjsObj *data) { EjsName qname; EjsObj *vp; EjsString *value; cchar *key, *sep, *vstr; char *encodedKey, *encodedValue, *newPrefix, *newKey; int i, count; count = ejsGetLength(ejs, data); for (i = 0; i < count; i++) { if (ejsIs(ejs, data, Array)) { key = itos(i); } else { qname = ejsGetPropertyName(ejs, data, i); key = ejsToMulti(ejs, qname.name); } vp = ejsGetProperty(ejs, data, i); if (vp == 0) { continue; } if (ejsGetLength(ejs, vp) > 0) { if (prefix) { newPrefix = sfmt("%s.%s", prefix, key); prepForm(ejs, hp, newPrefix, vp); } else { prepForm(ejs, hp, key, vp); } } else { value = ejsToString(ejs, vp); sep = (mprGetBufLength(hp->requestContent) > 0) ? "&" : ""; if (prefix) { newKey = sjoin(prefix, ".", key, NULL); encodedKey = mprUriEncode(newKey, MPR_ENCODE_URI_COMPONENT); } else { encodedKey = mprUriEncode(key, MPR_ENCODE_URI_COMPONENT); } vstr = ejsToMulti(ejs, value); encodedValue = mprUriEncode(vstr, MPR_ENCODE_URI_COMPONENT); mprPutToBuf(hp->requestContent, "%s%s=%s", sep, encodedKey, encodedValue); } } }
static cchar *getStrOption(Ejs *ejs, EjsObj *options, cchar *field, cchar *defaultValue, bool optional) { EjsObj *vp; EjsString *str; vp = ejsGetPropertyByName(ejs, options, EN(field)); if (vp == 0) { if (optional) { return sclone(defaultValue); } ejsThrowArgError(ejs, "Required option %s is missing", field); return 0; } str = ejsToString(ejs, vp); if (!ejsIs(ejs, str, String)) { ejsThrowArgError(ejs, "Bad option type for field \"%s\"", field); return 0; } return ejsToMulti(ejs, str); }
/* Append items to the end of the array. @return Returns the new length of the array. function push(...items): Number */ static EjsNumber *pushArray(Ejs *ejs, EjsArray *ap, int argc, EjsAny **argv) { EjsArray *args; EjsObj **src, **dest; int i, oldLen; assert(argc == 1 && ejsIs(ejs, argv[0], Array)); args = (EjsArray*) argv[0]; oldLen = ap->length; if (growArray(ejs, ap, ap->length + args->length) < 0) { return 0; } dest = ap->data; src = args->data; for (i = 0; i < args->length; i++) { dest[i + oldLen] = src[i]; } return ejsCreateNumber(ejs, ap->length); }
/* function Http(uri: Uri = null) */ static EjsHttp *httpConstructor(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { ejsLoadHttpService(ejs); hp->ejs = ejs; if ((hp->conn = httpCreateConn(ejs->http, NULL, ejs->dispatcher)) == 0) { ejsThrowMemoryError(ejs); return 0; } httpPrepClientConn(hp->conn, 0); httpSetConnNotifier(hp->conn, httpEventChange); httpSetConnContext(hp->conn, hp); if (argc == 1 && ejsIs(ejs, argv[0], Null)) { hp->uri = httpUriToString(((EjsUri*) argv[0])->uri, HTTP_COMPLETE_URI); } hp->method = sclone("GET"); hp->requestContent = mprCreateBuf(BIT_MAX_BUFFER, -1); hp->responseContent = mprCreateBuf(BIT_MAX_BUFFER, -1); return hp; }
// TODO - rename static int ejsGetNumOption(Ejs *ejs, EjsObj *options, cchar *field, int defaultValue, bool optional) { EjsObj *vp; EjsNumber *num; vp = ejsGetPropertyByName(ejs, options, EN(field)); if (vp == 0) { if (optional) { return defaultValue; } ejsThrowArgError(ejs, "Required option \"%s\" is missing", field); return 0; } num = ejsToNumber(ejs, vp); if (!ejsIs(ejs, num, Number)) { ejsThrowArgError(ejs, "Bad option type for field \"%s\"", field); return 0; } return (int) num->value; }
static EjsObj *saveXml(Ejs *ejs, EjsXML *xml, int argc, EjsObj **argv) { MprBuf *buf; MprFile *file; char *filename; ssize bytes, len; if (argc != 1 || !ejsIs(ejs, argv[0], String)) { ejsThrowArgError(ejs, "Bad args. Usage: save(filename);"); return 0; } filename = awtom(((EjsString*) argv[0])->value, NULL); /* Create a buffer to hold the output. All in memory. */ buf = mprCreateBuf(ME_MAX_BUFFER, -1); mprPutStringToBuf(buf, "<?xml version=\"1.0\"?>\n"); if (ejsXMLToBuf(ejs, buf, xml, 0) < 0) { return 0; } file = mprOpenFile(filename, O_CREAT | O_TRUNC | O_WRONLY | O_TEXT, 0664); if (file == 0) { ejsThrowIOError(ejs, "Cannot open: %s, %d", filename, mprGetOsError(ejs)); return 0; } len = mprGetBufLength(buf); bytes = mprWriteFile(file, buf->start, len); if (bytes != len) { ejsThrowIOError(ejs, "Cannot write to: %s", filename); mprCloseFile(file); return 0; } mprWriteFile(file, "\n", 1); mprCloseFile(file); return 0; }
static EjsWorker *workerConstructor(Ejs *ejs, EjsWorker *worker, int argc, EjsObj **argv) { EjsArray *search; EjsObj *options, *value; cchar *name, *scriptFile; ejsBlockGC(ejs); scriptFile = (argc >= 1 && argv[0] != ESV(null)) ? ((EjsPath*) argv[0])->value : 0; options = (argc == 2 && argv[1] != ESV(null)) ? (EjsObj*) argv[1]: NULL; name = 0; search = 0; if (options) { search = ejsGetPropertyByName(ejs, options, EN("search")); value = ejsGetPropertyByName(ejs, options, EN("name")); if (ejsIs(ejs, value, String)) { name = ejsToMulti(ejs, value); } } worker->ejs = ejs; worker->state = EJS_WORKER_BEGIN; return initWorker(ejs, worker, 0, name, search, scriptFile); }
/* Insert elements. Insert elements at the specified position. Negative indicies are measured from the end of the array. @return Returns a the original array. function insert(pos: Number, ...args): Array */ static EjsArray *insertArray(Ejs *ejs, EjsArray *ap, int argc, EjsObj **argv) { EjsArray *args; EjsObj **src, **dest; int i, pos, delta, endInsert; assert(argc == 2 && ejsIs(ejs, argv[1], Array)); pos = ejsGetInt(ejs, argv[0]); if (pos < 0) { pos += ap->length; } if (pos < 0) { pos = 0; } if (pos >= ap->length) { pos = ap->length; } args = (EjsArray*) argv[1]; if (growArray(ejs, ap, ap->length + args->length) < 0) { return 0; } delta = args->length; dest = ap->data; src = args->data; endInsert = pos + delta; for (i = ap->length - 1; i >= endInsert; i--) { dest[i] = dest[i - delta]; } for (i = 0; i < delta; i++) { dest[pos++] = src[i]; } return ap; }
static EjsRequest *createRequest(EjsHttpServer *sp, HttpConn *conn) { Ejs *ejs; EjsRequest *req; EjsPath *documents; cchar *dir; ejs = sp->ejs; documents = ejsGetProperty(ejs, sp, ES_ejs_web_HttpServer_documents); if (ejsIs(ejs, documents, Path)) { dir = documents->value; } else { /* Safety fall back */ dir = conn->rx->route->home; } req = ejsCreateRequest(ejs, sp, conn, dir); httpSetConnContext(conn, req); #if FUTURE if (sp->pipe) { def = ejsRunFunction(ejs, sp->createPipeline, if ((vp = ejsGetPropertyByName(ejs, def, ejsName(&name, "", "handler"))) != 0) { handler = ejsToMulti(ejs, vp); }
/* UNUSED */ static EjsString *joinArray(Ejs *ejs, EjsArray *ap, int argc, EjsObj **argv) { EjsString *result, *sep; EjsObj *vp; int i; sep = (argc == 1) ? (EjsString*) argv[0] : NULL; if (sep == ESV(empty) && ap->length == 1 && ejsIs(ejs, ap->data[0], String)) { /* Optimized path for joining [string]. This happens frequently with fun(...args) */ return (EjsString*) ap->data[0]; } result = ESV(empty); for (i = 0; i < ap->length; i++) { vp = ap->data[i]; if (!ejsIsDefined(ejs, vp)) { continue; } if (i > 0 && sep) { result = ejsJoinString(ejs, result, sep); } result = ejsJoinString(ejs, result, ejsToString(ejs, vp)); } return result; }
/* Cast the operand to the specified type function cast(type: Type) : Object */ static EjsAny *castError(Ejs *ejs, EjsError *error, EjsType *type) { EjsString *stack, *msg; EjsString *us; char *buf; switch (type->sid) { case S_Boolean: return ejsCreateBoolean(ejs, 1); case S_String: stack = (EjsString*) ejsRunFunctionBySlot(ejs, error, ES_Error_formatStack, 0, NULL); us = ejsIs(ejs, stack, String) ? stack : ESV(empty); msg = ejsGetProperty(ejs, error, ES_Error_message); if ((buf = sfmt("%@ Exception: %@\nStack:\n%@\n", TYPE(error)->qname.name, msg, us)) == NULL) { ejsThrowMemoryError(ejs); } return ejsCreateStringFromAsc(ejs, buf); default: ejsThrowTypeError(ejs, "Unknown type"); } return 0; }
static HttpUri *createHttpUriFromHash(Ejs *ejs, EjsObj *arg, int flags) { EjsObj *schemeObj, *hostObj, *portObj, *pathObj, *referenceObj, *queryObj, *uriObj; cchar *scheme, *host, *path, *reference, *query; int port; /* This permits a uri property override. Used in ejs.web::View.getOptions() */ uriObj = ejsGetPropertyByName(ejs, arg, EN("uri")); if (uriObj) { return toHttpUri(ejs, uriObj, 1); } schemeObj = ejsGetPropertyByName(ejs, arg, EN("scheme")); scheme = ejsIs(ejs, schemeObj, String) ? ejsToMulti(ejs, schemeObj) : 0; hostObj = ejsGetPropertyByName(ejs, arg, EN("host")); host = ejsIs(ejs, hostObj, String) ? ejsToMulti(ejs, hostObj) : 0; port = 0; if ((portObj = ejsGetPropertyByName(ejs, arg, EN("port"))) != 0) { if (ejsIs(ejs, portObj, Number)) { port = ejsGetInt(ejs, portObj); } else if (ejsIs(ejs, portObj, String)) { port = (int) stoi(ejsToMulti(ejs, portObj)); } } pathObj = ejsGetPropertyByName(ejs, arg, EN("path")); path = ejsIs(ejs, pathObj, String) ? ejsToMulti(ejs, pathObj) : 0; referenceObj = ejsGetPropertyByName(ejs, arg, EN("reference")); reference = ejsIs(ejs, referenceObj, String) ? ejsToMulti(ejs, referenceObj) : 0; queryObj = ejsGetPropertyByName(ejs, arg, EN("query")); query = ejsIs(ejs, queryObj, String) ? ejsToMulti(ejs, queryObj) : 0; return httpCreateUriFromParts(scheme, host, port, path, reference, query, flags); }